formatters: `cargo fmt` is formatting whole workspace instead of edited file
Description
Newly added cargo fmt Rust formatter is invoked with cargo fmt -- $FILE but in practice is formatting the whole Cargo workspace.
cargo fmt actually does not support formatting single files
This change was introduced in version 1.1.2 by this commit: https://github.com/anomalyco/opencode/commit/cdd6ea514b94d8ce71770bb02f33ab979e9ee0f6
Plugins
none
OpenCode version
1.1.16
Steps to reproduce
- Create a Cargo project with 2 Rust files
- Introduce a change that would be formatted in file
A.rs - Ask to edit file
B.rsin opencode - both files
A.rsandB.rswould be formatted
Screenshot and/or share link
https://opncd.ai/share/elWQVtdO
Operating System
Omarchy
Terminal
Ghostty
This issue might be a duplicate of existing issues. Please check:
- #8163: rust代码, 做一个很简单的修改,就对所有文件fmt处理 (Same cargo fmt workspace formatting issue in Chinese)
Feel free to ignore if none of these address your specific case.
@thdxr It's a change you've introduced in https://github.com/anomalyco/opencode/commit/cdd6ea514b94d8ce71770bb02f33ab979e9ee0f6 and its breaking my workflow on a big monorepo I think the proper fix is reverting this commit, wdyt?
What about using cargo fmt -- $FILE --config skip_children=true?
What about using
cargo fmt -- $FILE --config skip_children=true?
It's not good enough:
$ cargo fmt --verbose -- --config skip_children=true src/some_file2.rs
[bin (2024)] "/home/goniz/Work/hello_world/src/main.rs"
rustfmt --edition 2024 --config skip_children=true src/some_file2.rs /home/goniz/Work/hello_world/src/main.rs
It's actually running a single rustfmt on the "entrypoint" module "main.rs"
So using skip_children does contain the scope of the operation, but does not exclude the main.rs that will always be touched as well
@thdxr @cravenceiling whats the use-case for using cargo fmt?
It's actually running a single
rustfmton the "entrypoint" module "main.rs" So usingskip_childrendoes contain the scope of the operation, but does not exclude themain.rsthat will always be touched as well
You're right... And I think Dax added support for cargo fmt just to be able to use it out of the box in opencode, but you can disable it with:
{
"$schema": "https://opencode.ai/config.json",
"formatter": {
"rustfmt": {
"disabled": true,
},
"cargofmt": {
"disabled": true
}
}
}
Or maybe is cargofmt the key.
Yes I know I can disable it. It's just not behaving like the rest of the default formatters. I think it should be reverted or at least be opt-in
@rekram1-node wdyt?