Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: default to all targets when using --edition and --edition-idioms in cargo fix #15192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Rustin170506
Copy link
Member

@Rustin170506 Rustin170506 commented Feb 16, 2025

What does this PR try to resolve?

Close #13527

As we discussed, cargo fix should use the default target selection with cargo check.

In this PR, I modified cargo fix to no longer use all targets by default. For cargo fix --edition and cargo fix --edition-idioms, it will retain the old behavior and select all targets.

How should we test and review this PR?

Unit tests

Additional information

@rustbot
Copy link
Collaborator

rustbot commented Feb 16, 2025

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. A-cli Area: Command-line interface, option parsing, etc. Command-fix labels Feb 16, 2025
@Rustin170506 Rustin170506 force-pushed the rustin-patch-fix-targets branch from c4933a5 to d1c5bf8 Compare February 20, 2025 14:58
@Rustin170506 Rustin170506 force-pushed the rustin-patch-fix-targets branch from 60a57f7 to e9ee015 Compare March 5, 2025 14:43
@Rustin170506 Rustin170506 changed the title test: add fix_tests to validate cargo fix functionality fix: default to all targets when using --edition flags in cargo fix Mar 5, 2025
@Rustin170506 Rustin170506 changed the title fix: default to all targets when using --edition flags in cargo fix fix: default to all targets when using --edition and --edition-idioms flags in cargo fix Mar 5, 2025
@Rustin170506 Rustin170506 changed the title fix: default to all targets when using --edition and --edition-idioms flags in cargo fix fix: default to all targets when using --edition and --edition-idioms in cargo fix Mar 5, 2025
@Rustin170506 Rustin170506 force-pushed the rustin-patch-fix-targets branch from e9ee015 to f0cd503 Compare March 5, 2025 14:53
@Rustin170506 Rustin170506 marked this pull request as ready for review March 5, 2025 14:56
@Rustin170506 Rustin170506 force-pushed the rustin-patch-fix-targets branch from f0cd503 to 5a058c3 Compare March 6, 2025 13:43
@Rustin170506 Rustin170506 force-pushed the rustin-patch-fix-targets branch from 5a058c3 to a9f92ed Compare March 6, 2025 14:03
@Rustin170506 Rustin170506 force-pushed the rustin-patch-fix-targets branch from a9f92ed to 99745f0 Compare March 6, 2025 14:19
@@ -1424,7 +1424,7 @@ fn fix_to_broken_code() {
p.cargo("build").cwd("foo").run();

// Attempt to fix code, but our shim will always fail the second compile
p.cargo("fix --allow-no-vcs --broken-code")
p.cargo("fix --all-targets --allow-no-vcs --broken-code")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you said, this is suspicious and we should understand why in case there is a bad interaction happening

Copy link
Member Author

@Rustin170506 Rustin170506 Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I’m still looking into it. It seems that if we don’t select all targets, the foo rustc is called only twice. However, if we select all targets, it will be called three times, I believe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With all targets: p.cargo("fix --all-targets --allow-no-vcs --broken-code")

[COMPILING] bar v0.1.0 ([ROOT]/foo/bar)
[WARNING] failed to automatically apply fixes suggested by rustc to crate `bar`
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:

thread 'main' panicked at src/main.rs:23:29:
explicit panic
[NOTE] run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original diagnostics will follow.

[WARNING] variable does not need to be mutable
 --> src/lib.rs:1:20
  |
1 | pub fn foo() { let mut x = 3; let _ = x; }
  |                    ----^
  |                    |
  |                    [HELP] remove this `mut`
  |
  = [NOTE] `#[warn(unused_mut)]` on by default

[WARNING] `bar` (lib) generated 1 warning (run `cargo fix --lib -p bar` to apply 1 suggestion)

thread 'main' panicked at src/main.rs:23:29:
explicit panic
[NOTE] run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[ERROR] could not compile `bar` (lib test)

Caused by:
  process didn't exit successfully: `/Volumes/t7/code/cargo/target/debug/cargo [ROOT]/foo/foo/target/debug/foo --crate-name bar --edition=2015 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --emit=dep-info,metadata -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --test --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=022767cbb3148d36 -C extra-filename=-99a4e0453221de9d --out-dir [ROOT]/foo/bar/target/debug/deps -L dependency=[ROOT]/foo/bar/target/debug/deps` ([EXIT_STATUS]: 101)

Without all targets: p.cargo("fix --allow-no-vcs --broken-code")

[COMPILING] bar v0.1.0 ([ROOT]/foo/bar)
[WARNING] failed to automatically apply fixes suggested by rustc to crate `bar`
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:

thread 'main' panicked at src/main.rs:23:29:
explicit panic
[NOTE] run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original diagnostics will follow.

[WARNING] variable does not need to be mutable
 --> src/lib.rs:1:20
  |
1 | pub fn foo() { let mut x = 3; let _ = x; }
  |                    ----^
  |                    |
  |                    [HELP] remove this `mut`
  |
  = [NOTE] `#[warn(unused_mut)]` on by default

[WARNING] `bar` (lib) generated 1 warning (run `cargo fix --lib -p bar` to apply 1 suggestion)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if you have two targets, it also works: p.cargo("fix --lib --tests --allow-no-vcs --broken-code")

[COMPILING] bar v0.1.0 ([ROOT]/foo/bar)
[WARNING] failed to automatically apply fixes suggested by rustc to crate `bar`
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:

thread 'main' panicked at src/main.rs:23:29:
explicit panic
[NOTE] run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original diagnostics will follow.

[WARNING] variable does not need to be mutable
 --> src/lib.rs:1:20
  |
1 | pub fn foo() { let mut x = 3; let _ = x; }
  |                    ----^
  |                    |
  |                    [HELP] remove this `mut`
  |
  = [NOTE] `#[warn(unused_mut)]` on by default

[WARNING] `bar` (lib test) generated 1 warning (run `cargo fix --lib -p bar --tests` to apply 1 suggestion)

thread 'main' panicked at src/main.rs:23:29:
explicit panic
[NOTE] run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[ERROR] could not compile `bar` (lib)

Caused by:
  process didn't exit successfully: `/Volumes/t7/code/cargo/target/debug/cargo [ROOT]/foo/foo/target/debug/foo --crate-name bar --edition=2015 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=4b0938be085d245b -C extra-filename=-d245623a02104f1e --out-dir [ROOT]/foo/bar/target/debug/deps -L dependency=[ROOT]/foo/bar/target/debug/deps` ([EXIT_STATUS]: 101)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it’s related to how many times foo rustc is called in this command. If it’s called only twice for one target, the panic error is caught as a warning. However, if it’s called again, it turns into a compile error and causes a bailout. Then the status becomes 101.
I’ll dig into it further. I’m not sure how we handle the compile error here, but I’ll take a look tomorrow.

Copy link
Member Author

@Rustin170506 Rustin170506 Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change is reasonable. The test returns a 101 status because rustc was called three times. The first time, it attempts to fix build.rs with no errors. The second time, it tries to fix src/lib.rs, where there is one error, but we have a mechanism to output the errors as warnings when --broekn-code was specified.

cargo/src/cargo/ops/fix.rs

Lines 720 to 723 in 99745f0

// Exit with whatever exit code we initially started with. `cargo fix`
// treats this as a warning, and shouldn't return a failure code
// unless the code didn't compile in the first place.
exit_with(fixes.first_output.status);

The third time, it attempts to fix src/lib.rs again, but since no fixes are needed, the panic error is displayed directly, resulting in the 101 status.

if fixes.files.is_empty() {

Copy link
Member Author

@Rustin170506 Rustin170506 Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove --all-target. The status 0 is expected here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ehuss any idea what this test should be doing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli Area: Command-line interface, option parsing, etc. Command-fix S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cargo clippy and cargo clippy --fix --allow-dirty have different reports
3 participants