opencode icon indicating copy to clipboard operation
opencode copied to clipboard

formatters: `cargo fmt` is formatting whole workspace instead of edited file

Open goniz opened this issue 1 week ago • 7 comments

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

  1. Create a Cargo project with 2 Rust files
  2. Introduce a change that would be formatted in file A.rs
  3. Ask to edit file B.rs in opencode
  4. both files A.rs and B.rs would be formatted

Screenshot and/or share link

https://opncd.ai/share/elWQVtdO

Operating System

Omarchy

Terminal

Ghostty

goniz avatar Jan 13 '26 09:01 goniz

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.

github-actions[bot] avatar Jan 13 '26 09:01 github-actions[bot]

@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?

goniz avatar Jan 13 '26 09:01 goniz

What about using cargo fmt -- $FILE --config skip_children=true?

cravenceiling avatar Jan 13 '26 11:01 cravenceiling

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

goniz avatar Jan 13 '26 11:01 goniz

@thdxr @cravenceiling whats the use-case for using cargo fmt?

goniz avatar Jan 13 '26 11:01 goniz

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

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.

cravenceiling avatar Jan 13 '26 12:01 cravenceiling

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

goniz avatar Jan 13 '26 12:01 goniz

@rekram1-node wdyt?

goniz avatar Jan 14 '26 07:01 goniz