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

Combine rustfmt's stdin based mode of operation with cargo-fmt's ability to infer the rust edition #5602

Closed
joebb97 opened this issue Nov 15, 2022 · 2 comments

Comments

@joebb97
Copy link

joebb97 commented Nov 15, 2022

This is probably asking too much but it'd be nice if cargo-fmt could support reading a file from stdin and putting formatted output on stdout like rustfmt does. With this there'd be no real need to specify rustfmt --edition <edition>.

The use case is a linting tool like ale which runs rustfmt as
/bin/sh -c rustfmt < ${path_to_file_in_tmpdir} . It then takes this output and copies into the vim buffer. As far as I know ale doesn't support linters that modify files in place.

This currently doesn't work for files that use syntax from editions greater than 2015 (the default for rustfmt). It'd be nice not to have to hardcode a rust edition in my vim configuration and instead be able to point the config to cargo-fmt running in a "stdin mode", i.e:

/bin/sh -c cargo-fmt --stdin < ${path_to_file_in_tmpdir} . That way I can always adhere the project's edition specified in Cargo.toml as well as other configuration options specified in rustfmt.toml (assuming cargo-fmt adheres to the project's rustfmt.toml, I could be wrong about that).

I'd love to try to implement something like this if you all think it'd be valuable, but I understand if not. I can always see what I can do on the ale side of things to get this working.

@calebcartwright
Copy link
Member

Thanks for reaching out, making your case, and offering to assist, but I think it's probably best to close this.

Will elaborate a bit more but in short:

  1. tactically the best bet is to just incorporate/recommend a rustfmt.toml file in the project repo/directory with the edition value set to whatever it needs to be
  2. though it's stalled/a bit blocked at the moment, there's work underway in in Allow cargo-fmt to handle formatting individual files. #5071 to make it more feasible to use cargo fmt against a single target which (IIRC) should cover this too. Would rather track discussion and efforts there instead of bifurcating

Different editors/IDEs have taken different approaches to formatting behavior, both in how they run rustfmt and if/how they attempt to provide edition information. At the end of the day though, rustfmt will always pick up the rustfmt.toml value (provided the file is in a supported location) so while some will argue it's duplicative to have to specify the edition in Cargo manifests and the rustfmt config file, it's the path of least resistance.

assuming cargo-fmt adheres to the project's rustfmt.toml, I could be wrong about that

Yup it will. cargo fmt is actually just a very, very lightweight rustfmt command builder. It simply uses some cargo utilities to discover information about the project/workspace, and then it runs the corresponding rustfmt ... commands with that discovered info being passed along as args to rustfmt

@calebcartwright calebcartwright closed this as not planned Won't fix, can't repro, duplicate, stale Nov 16, 2022
@joebb97
Copy link
Author

joebb97 commented Nov 16, 2022

Thanks for the detailed answer and your help! Looks like having rustfmt.toml in the repo and having the CWD be the root of the repo is good enough for now.

Looking forward to when #5071 lands 👍

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