rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

`--check` can overwrite files when `--config emit_mode=files` is used

Open ikripaka opened this issue 4 months ago • 3 comments

Summary

--check can overwrite a source file when emit_mode=files is supplied through --config.

rustfmt --check --emit files <file> is correctly rejected as incompatible. However, the equivalent --config emit_mode=files bypasses that validation and causes rustfmt to format the file in place.

I tried to format this code:

fn main(){println!("hello");}

Expected behavior

I expected --check to be non-mutating regardless of the supplied configuration:

  • Leave file.rs unchanged.
  • Print a diff showing the required formatting.
  • Exit with status code 1.

Alternatively, rustfmt could reject the conflicting --check --config emit_mode=files combination, as it already rejects --check --emit files.

The help message from the cli:


Options:
        --check         Run in 'check' mode. Exits with 0 if input is
                        formatted correctly. Exits with 1 and prints a diff if
                        formatting is required.

Actual behaviour

Instead, rustfmt rewrote file.rs in place:

fn main() {
    println!("hello");
}

It printed nothing to stdout or stderr and exited with status code 0.

Configuration

rustfmt cli options used:

$ rustfmt --check --config emit_mode=files file.rs

rustfmt configuration file:

No rustfmt.toml or .rustfmt.toml file was present.

Reproduction Steps

  1. Create file.rs with this unformatted source:

    fn main(){println!("hello");}
    
  2. Optionally record its checksum:

    shasum -a 256 file.rs
    
  3. Run rustfmt in check mode with emit_mode=files supplied through inline configuration:

    rustfmt --check --config emit_mode=files file.rs
    
  4. Observe that rustfmt exits with status code 0, emits no diff, and rewrites file.rs.

  5. Compare its checksum or contents with the original file.

For comparison, this direct flag combination is rejected:

$ rustfmt --check --emit files file.rs
Invalid to use `--emit` and `--check`

Meta

rustfmt --version:

rustfmt 1.9.0-stable (ac68faa20c 2026-05-25)

I have not yet tested beta or nightly.

ikripaka avatar Jul 30 '26 13:07 ikripaka

Probably just need to add a new check around here to make sure that emit_mode isn't listed in the inline_config

ytmimi avatar Jul 30 '26 19:07 ytmimi

I was thinking of taking this one. The inline-config path could reject emit_mode=files when --check is active, matching the existing --emit files validation, then add a regression test that confirms the source file and exit behavior stay unchanged. Does that fit the direction you had in mind, and is there a preferred test location for this CLI path?

tomatotomata avatar Jul 31 '26 00:07 tomatotomata

I'd like to take this. The cause is that --check sets emit_mode to Diff before the inline --config overrides are applied, so --config emit_mode=files clobbers it and rustfmt writes the file in place. Moving the --check enforcement to after the overrides keeps it non-mutating. PR incoming.

DoTuanAnh2k1 avatar Aug 01 '26 02:08 DoTuanAnh2k1