rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.29k stars 2.32k forks source link

`cargo fix`: Warnings from inapplicable lint fixes suppress actual errors (`deny`) from subsequent `cargo check` runs #13217

Open jinohkang-theori opened 6 months ago

jinohkang-theori commented 6 months ago

Problem

After cargo fix emits warnings, subsequent cargo check commands with the same arguments will not emit an error despite existence of -D flag, until the build cache is cleared.

Steps

user@31c2faa23618:/unfixable$ cargo init && cargo update
     Created binary (application) package
user@31c2faa23618:/unfixable$ cat > src/main.rs
fn main() {
    'unused: loop {}
}
user@31c2faa23618:/unfixable$ git add -A && git commit -m initial
[master (root-commit) 339f77e] initial
 4 files changed, 54 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Cargo.lock
 create mode 100644 Cargo.toml
 create mode 100644 src/main.rs
user@31c2faa23618:/unfixable$ RUSTFLAGS="-D unused_labels" cargo check
    Checking unfixable v0.1.0 (/unfixable)
error: unused label
 --> src/main.rs:2:5
  |
2 |     'unused: loop {}
  |     ^^^^^^^
  |
  = note: requested on the command line with `-D unused-labels`

error: could not compile `unfixable` (bin "unfixable") due to 1 previous error
user@31c2faa23618:/unfixable$ RUSTFLAGS="-D unused_labels" cargo fix
    Checking unfixable v0.1.0 (/unfixable)
warning: unused label
 --> src/main.rs:2:5
  |
2 |     'unused: loop {}
  |     ^^^^^^^
  |
  = note: requested on the command line with `-D unused-labels`

warning: `unfixable` (bin "unfixable" test) generated 1 warning
warning: `unfixable` (bin "unfixable") generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
user@31c2faa23618:/unfixable$ RUSTFLAGS="-D unused_labels" cargo check
warning: unused label
 --> src/main.rs:2:5
  |
2 |     'unused: loop {}
  |     ^^^^^^^
  |
  = note: requested on the command line with `-D unused-labels`

warning: `unfixable` (bin "unfixable") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s

Possible Solution(s)

No response

Notes

This looks like it's caused by forced demotion of errors into warnings in fix mode, which pollutes the result cache for cargo check.

Version

cargo 1.77.0-nightly (ac6bbb332 2023-12-26)
release: 1.77.0-nightly
commit-hash: ac6bbb33293d8d424c17ecdb42af3aac25fb7295
commit-date: 2023-12-26
host: x86_64-unknown-linux-gnu
libgit2: 1.7.1 (sys:0.18.1 vendored)
libcurl: 8.5.0-DEV (sys:0.4.70+curl-8.5.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Debian 11.0.0 [64-bit]
weihanglo commented 5 months ago

Thanks for the report.

I guess this is a matter of fact that cargo fix runs cargo check under the hood to get the JSON diagnostics from rustc. Hence, the result of cargo check is cached.

It might be tricky to fix this, as Cargo might need to distinguish caches between fix and check. Doing so would need to wipe out the entire caches, which is pretty undesired. For now I might recommend scoping cargo clean with --package or --target or other flags, so we can clean part of the cache without rebuild from the beginning.