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

add a note that some warnings (and/or errors) can be auto-fixed #10989

Merged
merged 1 commit into from
Oct 28, 2022

Conversation

Muscraft
Copy link
Member

@Muscraft Muscraft commented Aug 15, 2022

This adds a note that some warnings can be auto-fixed by running cargo --fix. This only supports cargo check as we can't show the same note for cargo clippy since it is an external subcommand.

The message for the note was taken from #10976 (comment).

@rust-highfive
Copy link

r? @ehuss

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 15, 2022
@ehuss
Copy link
Contributor

ehuss commented Aug 15, 2022

I'm reluctant to add something like this for a few reasons:

  • I never run cargo fix. Something like this will just be constant noise for me. I think it is good to be very cautious about being too verbose or too prodding.
  • cargo fix often won't work correctly if there are any errors in the code.
  • cargo fix won't work with changes in the user's repo. They have to run with --allow-dirty and/or --allow-staged, which can be awkward. Also, there is a moderate risk that cargo fix will damage or otherwise mess up the user's code with no way to revert (the reason --allow-dirty is there). If there is no git repo, then it is doubly more dangerous. Or if the files haven't been added yet, cargo fix may overwrite them without confirmation.
  • Even filtering on machine-fixable errors, it is moderately common that the fix fails for one reason or another. I'm not particularly confident in pushing large numbers of people to use it more often. (Minor concern.)
  • This doesn't really make it clear to the user which issues will be fixed.

I appreciate it can be difficult to educate people on the existence of tools or options for performing certain tasks. I'm wondering, though, does rust-analyzer not show a quick-fix for fixable suggestions? Perhaps that would be a better route to make it easier for people to apply suggestions, or otherwise have easy access to it?

@Muscraft
Copy link
Member Author

I'm wondering, though, does rust-analyzer not show a quick-fix for fixable suggestions? Perhaps that would be a better route to make it easier for people to apply suggestions, or otherwise have easy access to it?

I haven't used rust-analyzer (in a while) but I know Clion (with the rust extension) gives quick fixes so I would assume RA does as well

I'm reluctant to add something like this for a few reasons:

Given the number of reasons, and the number of problems with cargo fix (which appear to be major blockers), it might be a good idea to close this and continue the discussion in #10976. I could see waiting on this until cargo fix has some issues resolved, or there is a new error/warning variant within rustc that would always be "fixable".

@epage
Copy link
Contributor

epage commented Aug 15, 2022

Let's leave this open but move this to the Issue

@epage epage added S-blocked and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 13, 2022
@Muscraft Muscraft force-pushed the add-auto-fix-note branch 2 times, most recently from 7bc8b95 to 63d32ec Compare September 27, 2022 19:24
@Muscraft
Copy link
Member Author

I've updated this with feedback from #10976.
Changes:

  • The note is added to the summary line as suggested here
  • The command to run now includes the exact command to run i.e.
    warning: `foo` (example "ex1") generated 1 warning (run `cargo fix --example "ex1"` to apply suggestions)
    warning: `foo` (bench "bench") generated 1 warning (run `cargo fix --bench "bench" --tests` to apply suggestions)
    warning: `foo` (bin "foo" test) generated 2 warnings (run `cargo fix --bin "foo" --tests` to apply suggestions)
    

Concerns:

  • Will adding this suggestion cause there to be too many issues filed and lots of work to fix things?
  • Is the output message too verbose/specific
  • Should the message only be shown once?

@weihanglo
Copy link
Member

  • Is the output message too verbose/specific

Looks fine to me, since Cargo already emits such note.

I am uncertain whether we need to make sure the suggested command is always runnable. A bad suggestion could be worse than no suggestion.

  • Should the message only be shown once?

I don't get it. Could you expand a bit?

@Muscraft
Copy link
Member Author

I am uncertain whether we need to make sure the suggested command is always runnable. A bad suggestion could be worse than no suggestion.

I agree that a bad suggestion is much worse than no suggestion and it's something that I tried to make sure wouldn't happen. That being said, I think having a specific command to run is much better.
Pros:

  • It ensures that cargo fix is only being run on exactly what is giving the warning
  • It makes sure that cargo fix is not being run on anything with an error
  • Reduces unintended code churn
  • Can clearly see what target/package the command is for
  • Fixes applied target by target

Cons:

  • The lines are very long
  • Lots of new output
  • Chance to give the wrong command
  • Each command would need to be run separately (lots of cargo fix runs)
  • Doesn't suggest --allow-dirty so the command would probably always be "wrong"
  • Should the message only be shown once?

I don't get it. Could you expand a bit?

There is a chance that the long lines on potentially every summary line could be overwhelming. Having it output once per package(compared to target) would reduce this and make the command less specific. Going further and having it only be suggested once could make it so there is one place command is suggested and not affect every summary line. Having it be on its own line comes with its own problems but might make it easier to suggest a hyper-specific command (check version control status).

@Muscraft Muscraft force-pushed the add-auto-fix-note branch 2 times, most recently from dd69ce1 to 2a8a53c Compare October 18, 2022 19:06
Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

Looks fine! Thank you for the pull request.

src/cargo/core/compiler/job_queue.rs Outdated Show resolved Hide resolved
src/cargo/core/compiler/mod.rs Show resolved Hide resolved
src/cargo/core/compiler/job_queue.rs Outdated Show resolved Hide resolved
tests/testsuite/check.rs Outdated Show resolved Hide resolved
tests/testsuite/check.rs Outdated Show resolved Hide resolved
tests/testsuite/check.rs Outdated Show resolved Hide resolved
src/cargo/core/compiler/job_queue.rs Outdated Show resolved Hide resolved
src/cargo/core/compiler/job_queue.rs Show resolved Hide resolved
src/cargo/core/compiler/job_queue.rs Outdated Show resolved Hide resolved
src/cargo/core/compiler/mod.rs Show resolved Hide resolved
src/cargo/core/compiler/job_queue.rs Outdated Show resolved Hide resolved
Comment on lines +1424 to +1427
children: Vec<Diagnostic>,
}

Copy link
Member

Choose a reason for hiding this comment

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

Should we hand-pick field to deserialize? I see other places doing so.

Suggested change
children: Vec<Diagnostic>,
}
children: Vec<Diagnostic>,
}
#[derive(serde::Deserialize)]
struct Diagnostic {
spans: Vec<Span>,
// `children: Vec<Diagnostic>` if we need to check them recursively
}
#[derive(serde::Deserialize)]
struct Span {
suggestion_applicability: Option<Applicability>,
}

BTW, I feel like every existing ad-hoc deserialization should have a comment telling which struct it refers to.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we hand-pick field to deserialize? I see other places doing so.

I'm not sure what you mean by this

Copy link
Member

Choose a reason for hiding this comment

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

I mean deserializing only those fields we need instead of the whole Diagnostic struct

Copy link
Member Author

Choose a reason for hiding this comment

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

We could do that but I was trying to use the available types from rustfix. I figured that since it had the ability to tell what things were it would be kept updated, instead of updating multiple places.

Copy link
Member

Choose a reason for hiding this comment

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

If there is an update of types in rustfix crate, either reuse or not we still need to update to some extent.

I was asking because other places deserialize fields-of-interest. Besides, there might be a performance hit if a package start emitting thousands of warnings with full struct fields, though I guess it is rare.

Anyway, I am good with reuse types from rustfix. Let's move on :)

src/cargo/core/compiler/job_queue.rs Outdated Show resolved Hide resolved
Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

This looks good to me.

According to the conclusion of the recent Cargo meeting, we decide to enable rustfix suggestions on nightly to collect feedback. I am going to merge it soon to proceed.

Just note that, one little concern I have is that Cargo now does full deserialization on Diagnostic struct. Not sure how it impacts the real world performance but I assume it is rare.

@weihanglo
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Oct 28, 2022

📌 Commit 8ff8eaa has been approved by weihanglo

It is now in the queue for this repository.

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Oct 28, 2022
@bors
Copy link
Contributor

bors commented Oct 28, 2022

⌛ Testing commit 8ff8eaa with merge 2d04bcd...

@bors
Copy link
Contributor

bors commented Oct 28, 2022

☀️ Test successful - checks-actions
Approved by: weihanglo
Pushing 2d04bcd to master...

@bors bors merged commit 2d04bcd into rust-lang:master Oct 28, 2022
@Muscraft Muscraft deleted the add-auto-fix-note branch October 28, 2022 13:36
weihanglo added a commit to weihanglo/rust that referenced this pull request Nov 2, 2022
14 commits in 7e484fc1a766f56dbc95380f45719698e0c82749..810cbad9a123ad4ee0a55a96171b8f8478ff1c03
2022-10-27 15:20:57 +0000 to 2022-11-02 21:04:31 +0000
- Update curl-sys (rust-lang/cargo#11326)
- Mention fix on build script deadlock (rust-lang/cargo#11325)
- Make cargo forward pre-existing CARGO if set (rust-lang/cargo#11285)
- Clean up workspace dependencies after cargo remove (rust-lang/cargo#11242)
- Update the outdated link for rust-semverver (rust-lang/cargo#11322)
- Fix broken link to compilation entry point (rust-lang/cargo#11317)
- Only remove fingerprints and build script artifacts of the requested package (rust-lang/cargo#10621)
- Newer anyhow features are required (rust-lang/cargo#11316)
- Clean stale git temp files (rust-lang/cargo#11308)
- Report crate size on package and publish (rust-lang/cargo#11270)
- add a note that some warnings (and/or errors) can be auto-fixed (rust-lang/cargo#10989)
- Update libcurl (rust-lang/cargo#11307)
- artifact deps shoud works when target field specified coexists with `optional = true` (rust-lang/cargo#11183)
- Fix singular verb in tests page (rust-lang/cargo#11300)
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 3, 2022
Update cargo

14 commits in 7e484fc1a766f56dbc95380f45719698e0c82749..810cbad9a123ad4ee0a55a96171b8f8478ff1c03
2022-10-27 15:20:57 +0000 to 2022-11-02 21:04:31 +0000
- Update curl-sys (rust-lang/cargo#11326)
- Mention fix on build script deadlock (rust-lang/cargo#11325)
- Make cargo forward pre-existing CARGO if set (rust-lang/cargo#11285)
- Clean up workspace dependencies after cargo remove (rust-lang/cargo#11242)
- Update the outdated link for rust-semverver (rust-lang/cargo#11322)
- Fix broken link to compilation entry point (rust-lang/cargo#11317)
- Only remove fingerprints and build script artifacts of the requested package (rust-lang/cargo#10621)
- Newer anyhow features are required (rust-lang/cargo#11316)
- Clean stale git temp files (rust-lang/cargo#11308)
- Report crate size on package and publish (rust-lang/cargo#11270)
- add a note that some warnings (and/or errors) can be auto-fixed (rust-lang/cargo#10989)
- Update libcurl (rust-lang/cargo#11307)
- artifact deps shoud works when target field specified coexists with `optional = true` (rust-lang/cargo#11183)
- Fix singular verb in tests page (rust-lang/cargo#11300)

r? `@ghost`
bors pushed a commit to rust-lang-ci/rust that referenced this pull request Nov 5, 2022
20 commits in 7e484fc1a766f56dbc95380f45719698e0c82749..9286a1beba5b28b115bad67de2ae91fb1c61eb0b
2022-10-27 15:20:57 +0000 to 2022-11-04 06:41:49 +0000
- chore: Upgrade dependencies (rust-lang/cargo#11328)
- Clean more aggressively in CI (rust-lang/cargo#11335)
- Remove remove_dir_all (rust-lang/cargo#11333)
- test(publish): Cover more wait-for-publish cases (rust-lang/cargo#11327)
- Revert rust-lang/cargo#11183 (rust-lang/cargo#11331)
- fix(semver-check): adapt to a different error for variant not covered (rust-lang/cargo#11332)
- Update curl-sys (rust-lang/cargo#11326)
- Mention fix on build script deadlock (rust-lang/cargo#11325)
- Make cargo forward pre-existing CARGO if set (rust-lang/cargo#11285)
- Clean up workspace dependencies after cargo remove (rust-lang/cargo#11242)
- Update the outdated link for rust-semverver (rust-lang/cargo#11322)
- Fix broken link to compilation entry point (rust-lang/cargo#11317)
- Only remove fingerprints and build script artifacts of the requested package (rust-lang/cargo#10621)
- Newer anyhow features are required (rust-lang/cargo#11316)
- Clean stale git temp files (rust-lang/cargo#11308)
- Report crate size on package and publish (rust-lang/cargo#11270)
- add a note that some warnings (and/or errors) can be auto-fixed (rust-lang/cargo#10989)
- Update libcurl (rust-lang/cargo#11307)
- artifact deps shoud works when target field specified coexists with `optional = true` (rust-lang/cargo#11183)
- Fix singular verb in tests page (rust-lang/cargo#11300)
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 5, 2022
Update cargo

20 commits in 7e484fc1a766f56dbc95380f45719698e0c82749..9286a1beba5b28b115bad67de2ae91fb1c61eb0b 2022-10-27 15:20:57 +0000 to 2022-11-04 06:41:49 +0000
- chore: Upgrade dependencies (rust-lang/cargo#11328)
- Clean more aggressively in CI (rust-lang/cargo#11335)
- Remove remove_dir_all (rust-lang/cargo#11333)
- test(publish): Cover more wait-for-publish cases (rust-lang/cargo#11327)
- Revert rust-lang/cargo#11183 (rust-lang/cargo#11331)
- fix(semver-check): adapt to a different error for variant not covered (rust-lang/cargo#11332)
- Update curl-sys (rust-lang/cargo#11326)
- Mention fix on build script deadlock (rust-lang/cargo#11325)
- Make cargo forward pre-existing CARGO if set (rust-lang/cargo#11285)
- Clean up workspace dependencies after cargo remove (rust-lang/cargo#11242)
- Update the outdated link for rust-semverver (rust-lang/cargo#11322)
- Fix broken link to compilation entry point (rust-lang/cargo#11317)
- Only remove fingerprints and build script artifacts of the requested package (rust-lang/cargo#10621)
- Newer anyhow features are required (rust-lang/cargo#11316)
- Clean stale git temp files (rust-lang/cargo#11308)
- Report crate size on package and publish (rust-lang/cargo#11270)
- add a note that some warnings (and/or errors) can be auto-fixed (rust-lang/cargo#10989)
- Update libcurl (rust-lang/cargo#11307)
- artifact deps shoud works when target field specified coexists with `optional = true` (rust-lang/cargo#11183)
- Fix singular verb in tests page (rust-lang/cargo#11300)

r? `@ghost`
bors added a commit that referenced this pull request Jan 22, 2023
feat: stabilize auto fix note

A note that some warnings could be fixed by running a `cargo fix` command was added in #10989 and made to work with `clippy` in #11399. It has only been turned on for `nightly` builds so far; this PR would make it show on `stable`.

The original motivation for making this note `nightly` only, was to [allow for iteration](#10976 (comment)) on the message output. There has yet to be any feedback on the message format in the time that it has been on `nightly`. This was brought up in a recent cargo team meeting and it was thought that we should move forward with showing this on `stable`.

close #10976
@ehuss ehuss added this to the 1.67.0 milestone Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants