Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo fmt cannot be used for formatting a single rust buffer #95

Closed
jenanwise opened this issue Aug 2, 2020 · 6 comments
Closed

cargo fmt cannot be used for formatting a single rust buffer #95

jenanwise opened this issue Aug 2, 2020 · 6 comments

Comments

@jenanwise
Copy link

In #87 / #88, the rustfmt formatter was switched from calling rustfmt directly to using cargo fmt so that it would respect Cargo.toml. Unfortunately, cargo fmt cannot format a single file. It will actually format all the files in the crate. When combined with --emit=stdout (as in current master), this will spit out all the crate's src into the current buffer on save.

See discussion in rust-lang/rustfmt#4074

There doesn't appear to be a way to respect Cargo.toml while formatting a single buffer.

@lassik
Copy link
Owner

lassik commented Aug 4, 2020

Thanks for reporting the bug. I don't use Rust myself, so I'll follow your recommendations on what to do.

Skimming that rustfmt thread, I got the impression that the problem is there's no simple way to run rustfmt so that it parses the same config file cargo fmt parses. And when we run cargo fmt we are almost guaranteed to format extra files in addition to the one we want.

@lassik
Copy link
Owner

lassik commented Aug 4, 2020

Does this imply that Rust formatting currently cannot be made work properly for projects using non-standard formatter settings? In other words, we need to wait for the rustfmt and/or cargo fmt maintainers to add a command line flag for editor plugins.

@jenanwise
Copy link
Author

Skimming that rustfmt thread, I got the impression that the problem is there's no simple way to run rustfmt so that it parses the same config file cargo fmt parses. And when we run cargo fmt we are almost guaranteed to format extra files in addition to the one we want.

That's correct, at least for now.

Does this imply that Rust formatting currently cannot be made work properly for projects using non-standard formatter settings?

Not quite. rustfmt respects settings in the project's rustfmt.toml. As far as I am aware, the only real rustfmt config option that matters in Cargo.toml is edition. By default, rustfmt uses the "2015" edition, which does not allow the async keyword. Cargo.toml specifies the Rust edition for the project, so using cargo fmt gets you automatic edition config for free, but other configuration options still go in rustfmt.toml (even when using cargo fmt).

It's worth noting that the next major release of rustfmt will default to the "2018" edition.

My recommendation would be to revert #88 and advise users to create a rustfmt.toml if they need any configuration. That seems to be the recommendation for editor integrations from the rustlang team for now.

@lassik
Copy link
Owner

lassik commented Aug 4, 2020

And you can set the edition in rustfmt.toml just as well as in Cargo.toml, so people using 2018 can solve their problem simply by creating the former file. That sounds very reasonable indeed. Let's do it.

lassik added a commit that referenced this issue Aug 4, 2020
"cargo fmt" currently doesn't work sensibly for stdin->stdout
formatting -- if the project has multiple Rust source files, it
formats all of them instead of formatting only the one we want. There
is currently no way to fix this in "cargo fmt", so revert to
"rustfmt".

Thanks to Sondre Nilsen for solving this problem.

See also issue #88 (Use cargo fmt instead of rustfmt).
@lassik
Copy link
Owner

lassik commented Aug 4, 2020

OK, pushed a fix to master. Please test and close this issue if it solves the problem. Thanks for an excellent bug report!

@jenanwise
Copy link
Author

It works, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants