Summary
When rustfmt runs with --check and encounters a module resolution error, it prefixes the diagnostic with:
This wording is misleading in check mode because --check does not write formatted output back to the source files. It determines whether formatting would produce changes.
The module resolution diagnostic itself is correct. The confusing part is specifically the top-level Error writing files prefix.
This is related to #5167, which was fixed by #5243. That change corrected the module resolution diagnostic when both module.rs and module/mod.rs exist. This report concerns the top-level prefix used while rustfmt is running in check mode.
I tried to format this code:
with both of the following module files present:
src/responses.rs
src/responses/mod.rs
Expected behavior
I expected rustfmt to report the module resolution error without implying that it attempted to write source files.
For example, a mode-independent prefix could be used:
Error formatting files: failed to resolve mod `responses`: file for module found at both ".../src/responses.rs" and ".../src/responses/mod.rs"
A check-specific prefix could also be used:
Error checking files: failed to resolve mod `responses`: file for module found at both ".../src/responses.rs" and ".../src/responses/mod.rs"
The command should continue to exit with a non-zero status.
Actual behavior
Instead, rustfmt reports:
Error writing files: failed to resolve mod `responses`: file for module found at both "/tmp/rustfmt-check-error-repro/src/responses.rs" and "/tmp/rustfmt-check-error-
repro/src/responses/mod.rs"
The command exits with status 1.
I compared the hashes, sizes, and modification times of all three source files before and after running the command. None of the files changed.
The same behavior is present in stable rustfmt and in rustfmt built from the current main branch.
Configuration
rustfmt cli options used:
$ cargo fmt --all -- --check
rustfmt configuration file:
No rustfmt.toml or .rustfmt.toml file was present.
Reproduction Steps
-
Create a new library crate:
cargo new rustfmt-check-error-repro --lib
cd rustfmt-check-error-repro
-
Declare an external module:
printf 'mod responses;\n' > src/lib.rs
-
Create both valid candidate paths for the module:
touch src/responses.rs
mkdir -p src/responses
touch src/responses/mod.rs
-
Run rustfmt in check mode:
cargo fmt --all -- --check
-
Observe the Error writing files prefix and exit status 1.
Meta
The behavior was reproduced with the stable version:
rustfmt --version:
rustfmt 1.9.0-stable (8bab26f4f6 2026-07-14)
It was also reproduced after cloning and building the current main branch of rust-lang/rustfmt:
repository commit: cd670efa2051459d8a4b0cab741c486746cda95a
rustfmt 1.10.0-nightly (cd670efa20 2026-07-21)
rustc 1.99.0-nightly (eff8269f7 2026-07-18)
cargo 1.99.0-nightly (3efb1f477 2026-07-17)
Environment:
Operating system: Linux, x86_64
Distribution: Ubuntu 24.04.4 LTS
Summary
When
rustfmtruns with--checkand encounters a module resolution error, it prefixes the diagnostic with:This wording is misleading in check mode because
--checkdoes not write formatted output back to the source files. It determines whether formatting would produce changes.The module resolution diagnostic itself is correct. The confusing part is specifically the top-level
Error writing filesprefix.This is related to #5167, which was fixed by #5243. That change corrected the module resolution diagnostic when both
module.rsandmodule/mod.rsexist. This report concerns the top-level prefix used while rustfmt is running in check mode.I tried to format this code:
with both of the following module files present:
Expected behavior
I expected rustfmt to report the module resolution error without implying that it attempted to write source files.
For example, a mode-independent prefix could be used:
A check-specific prefix could also be used:
The command should continue to exit with a non-zero status.
Actual behavior
Instead, rustfmt reports:
The command exits with status
1.I compared the hashes, sizes, and modification times of all three source files before and after running the command. None of the files changed.
The same behavior is present in stable rustfmt and in rustfmt built from the current
mainbranch.Configuration
rustfmtcli options used:rustfmtconfiguration file:Reproduction Steps
Create a new library crate:
cargo new rustfmt-check-error-repro --lib cd rustfmt-check-error-reproDeclare an external module:
Create both valid candidate paths for the module:
Run rustfmt in check mode:
Observe the
Error writing filesprefix and exit status1.Meta
The behavior was reproduced with the stable version:
rustfmt --version:It was also reproduced after cloning and building the current
mainbranch ofrust-lang/rustfmt:Environment: