Skip to content

cargo fmt doesn't respect the passed files #30

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

Closed
zimbatm opened this issue Feb 1, 2021 · 6 comments
Closed

cargo fmt doesn't respect the passed files #30

zimbatm opened this issue Feb 1, 2021 · 6 comments
Labels
formatter Issue specific to a formatter

Comments

@zimbatm
Copy link
Member

zimbatm commented Feb 1, 2021

cargo fmt parses the Cargo.toml and then generates its own list of files to format.

$ strace -f -e execve -s 4096 cargo fmt -- src/customlog.rs 
execve("/nix/store/lw6s4749i3z2kpg26742ivjcq746jvv2-devshell-dir/bin/cargo", ["cargo", "fmt", "--", "src/customlog.rs"], 0x7ffd5c548050 /* 82 vars */) = 0
execve("/nix/store/lw6s4749i3z2kpg26742ivjcq746jvv2-devshell-dir/bin/cargo-fmt", ["/nix/store/lw6s4749i3z2kpg26742ivjcq746jvv2-devshell-dir/bin/cargo-fmt", "fmt", "--", "src/customlog.rs"], 0x55be3c2d64c0 /* 85 vars */) = 0
strace: Process 3095549 attached
[pid 3095549] execve("/nix/store/fk9vq870hfgwj8w92r4zysvj3a52mzgh-rust/bin/cargo", ["/nix/store/fk9vq870hfgwj8w92r4zysvj3a52mzgh-rust/bin/cargo", "metadata", "--format-version", "1", "--no-deps", "--offline"], 0x7ffecc7ef7e0 /* 85 vars */) = 0
[pid 3095549] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=3095549, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
strace: Process 3095550 attached
[pid 3095550] execve("/nix/store/lw6s4749i3z2kpg26742ivjcq746jvv2-devshell-dir/bin/rustfmt", ["rustfmt", "/home/zimbatm/go/src/github.com/numtide/prjfmt/src/lib.rs", "/home/zimbatm/go/src/github.com/numtide/prjfmt/src/main.rs", "--edition", "2018", "src/customlog.rs"], 0x7ffecc7ef7e0 /* 85 vars */) = 0
[pid 3095550] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=3095550, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++

You can see that 3095550 rustfmt is being invoked with the list of files of the project, plus the one passed as an argument.

@zimbatm zimbatm added the formatter Issue specific to a formatter label Feb 1, 2021
@basile-henry
Copy link
Contributor

basile-henry commented Feb 1, 2021

I think it makes more sense to use rustfmt directly for Rust. cargo fmt is basically prjfmt for Rust (it uses rustfmt under the hood) 😄

@zimbatm
Copy link
Member Author

zimbatm commented Feb 1, 2021

I think so too. The only missing bit is to keep the --edition flag in sync with the Cargo.toml file.

@zimbatm
Copy link
Member Author

zimbatm commented Feb 27, 2021

I documented this briefly in docs/formatters.md . Let's add all the formatters and their support status there.

@zimbatm zimbatm closed this as completed Feb 27, 2021
brianmcgee pushed a commit that referenced this issue May 13, 2024
@domenkozar
Copy link

Kind of, when you have a mix of packages that use different editions, rustfmt isn't aware which crate uses what edition. See https://git.snix.dev/snix/snix/issues/115

@jfly
Copy link
Collaborator

jfly commented Apr 29, 2025

Then it does sound like cargo fmt is useful to us, as it has more than just tree traversal logic in it.

I found this issue discussing this missing feature (the fact that you can't cargo fmt a single file) here: rust-lang/rustfmt#4485 (comment). Based on my reading of the conversation, it seems like they might be open to a change to cargo fmt that does respect the list of files given.

@domenkozar
Copy link

See rust-lang/rustfmt#5071

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

No branches or pull requests

4 participants