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: improve message for inactive weak optional feature with edition2024 through unused dep collection #14026

Merged
merged 5 commits into from
Jul 3, 2024

Conversation

linyihai
Copy link
Contributor

@linyihai linyihai commented Jun 7, 2024

What does this PR try to resolve?

Collect the unused dependencies to check whether a weak optional dependency had set. Then we can improve the message when weak optional dependency inactive.

Fixes #14015

How should we test and review this PR?

One commit test added, one commit fixed and updated

Additional information

Part of #14039

  • migrate tests/testsuite/lints/unused_optional_dependencies.rs to snapshot

And rename MissingField to MissingFieldError

@rustbot
Copy link
Collaborator

rustbot commented Jun 7, 2024

r? @ehuss

rustbot has assigned @ehuss.
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 A-dependency-resolution Area: dependency resolution and the resolver A-manifest Area: Cargo.toml issues A-registries Area: registries S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 7, 2024
@linyihai
Copy link
Contributor Author

linyihai commented Jun 7, 2024

r? @epage

I'm sorry split the fix into two. If #14019 more reasonable and finally merge, this PR can be closed.

@epage
Copy link
Contributor

epage commented Jun 7, 2024

@Muscraft would be interested in your thoughts on this approach to solving #14015.

#14019 is an attempt but it lacks the information for a clear message, so this tries to track the additional information to make that happen.

At least one potential ramification of this is we could simplified unused optional dependency lint to just checking the unused_dependencies tables

I do think #14028 should be merged first so we make sure we don't have regressions.

@epage
Copy link
Contributor

epage commented Jun 7, 2024

(also I should note that I've only looked at the high level details and not all of the nitty gritty of the implementation yet)

@epage epage changed the title fix: collect unused dependencies to improve the message when weak optional dependency inactive fix: improve message for inactive weak optional feature with edition2024 through unused dep collection Jun 7, 2024
@bors
Copy link
Collaborator

bors commented Jun 7, 2024

☔ The latest upstream changes (presumably #14028) made this pull request unmergeable. Please resolve the merge conflicts.

@linyihai linyihai force-pushed the weak-optional-inactive branch 3 times, most recently from d64eeee to 03b05e1 Compare June 10, 2024 08:53
@bors
Copy link
Collaborator

bors commented Jun 20, 2024

☔ The latest upstream changes (presumably #14106) made this pull request unmergeable. Please resolve the merge conflicts.

@linyihai linyihai force-pushed the weak-optional-inactive branch 4 times, most recently from 3b4617d to ccced4b Compare June 25, 2024 03:21
@linyihai
Copy link
Contributor Author

Try to minimize code changes and performance impact, which will return a custom Error MissingDependency when Summary::new? found a dependency missing. And then handle the MissingDependency in to_real_manifest.

@linyihai linyihai force-pushed the weak-optional-inactive branch 2 times, most recently from 20f9989 to 44fd447 Compare June 28, 2024 02:07
tests/testsuite/lints/unused_optional_dependencies.rs Outdated Show resolved Hide resolved
tests/testsuite/lints/unused_optional_dependencies.rs Outdated Show resolved Hide resolved
src/cargo/core/summary.rs Outdated Show resolved Hide resolved
src/cargo/core/summary.rs Outdated Show resolved Hide resolved
src/cargo/core/summary.rs Outdated Show resolved Hide resolved
src/cargo/core/summary.rs Outdated Show resolved Hide resolved
src/cargo/util/toml/mod.rs Outdated Show resolved Hide resolved
@rustbot rustbot added the A-configuration Area: cargo config files and env vars label Jul 2, 2024
@linyihai
Copy link
Contributor Author

linyihai commented Jul 2, 2024

The suggestions above have been considered.

Then in the subsequent commit, add these alternation by the way.

  • Rename MissingField to MissingFieldError
  • Migrate unused_optional_dependencies to snapshot
    The changes are small and easy to review :)

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.

Have talked to @Muscraft and we think the current mechanism of this is fine!

In the future, we might want to make a dedicated Error type (probably an enum) for Summary::new() through thiserror crate, so we don't need to downcast them anymore. The Error type could additionally be handled in cargo::util::toml with original manifest TOML to get an enhanced error with richer information.

The future version would be something like this:

fn error_with_original_toml(err: &SummaryError, original_toml: &TomlManifest) -> CargoResult<()> {
    match err {
        SmmmaryError::Foo => return Err(...),
        SmmmaryError::BarError => return Err(...),
        SummaryError::MissingDependency(missing_dep) => {
		    let message = "...";
            if let Err(err) = gctx.shell().print_message(message) {
            	return Err(err.into());
            }
            return Err(AlreadyPrintedError::new(anyhow!("").into()).into());
        }
    }
}

which might be rendered to something like:

image

or

image

Credit to @Muscraft doing this experiment. See Muscraft@c0b44e0 for the code that generate the snapshots above.

src/cargo/core/summary.rs Outdated Show resolved Hide resolved
Comment on lines 1461 to 1473
if let Some(missing_dep) = err.downcast_mut::<MissingDependencyError>() {
if missing_dep.weak_optional() {
// dev-dependencies are not allowed to be optional
let mut orig_deps = vec![
original_toml.dependencies.as_ref(),
original_toml.build_dependencies.as_ref(),
];
for (_, platform) in original_toml.target.iter().flatten() {
orig_deps.extend(vec![
platform.dependencies.as_ref(),
platform.build_dependencies.as_ref(),
]);
}
for deps in orig_deps {
if let Some(deps) = deps {
if deps.keys().any(|p| *p.as_str() == missing_dep.dep_name()) {
missing_dep.set_unused_dependency(true);
}
}
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

To me, it is a bit odd to mutate an error. Could we extract this login to a function, which accepts the original_toml and the err as arguments and returns a refined error?

By doing that, MissingDependencyError doesn't need the field unused_dependency. We could do the logic complete outside core::summary.Summary is used by both index file parsing and manifest parsing, but the missing dep feature error doesn't make sense for index parsing at this moment. Therefore, MissingDependencyError will emit only one kind of error message.

Also, we could make all fields pub because the error is not intended to be modified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your suggestions.

All have been rebased into fix: improve message for inactive weak optional feature with edition2024

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And the last commit has added the missing_dep_diagnostic, but I shamless copy the code, may be it can be improved in latter PR.

@linyihai
Copy link
Contributor Author

linyihai commented Jul 3, 2024

Credit to @Muscraft doing this experiment. See Muscraft@c0b44e0 for the code that generate the snapshots above.

I have added the missing_dep_diagnostic into the last commit with tiny twist, thanks @Muscraft.

These tips are more user-friendly.

@weihanglo
Copy link
Member

Thanks! That looks awesome. Will go ahead and merge this.

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 3, 2024

📌 Commit b28eef9 has been approved by weihanglo

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 3, 2024
@bors
Copy link
Collaborator

bors commented Jul 3, 2024

⌛ Testing commit b28eef9 with merge 838d81d...

@bors
Copy link
Collaborator

bors commented Jul 3, 2024

☀️ Test successful - checks-actions
Approved by: weihanglo
Pushing 838d81d to master...

@bors bors merged commit 838d81d into rust-lang:master Jul 3, 2024
22 checks passed
@linyihai linyihai deleted the weak-optional-inactive branch July 4, 2024 01:14
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 5, 2024
Update cargo

13 commits in a515d463427b3912ec0365d106791f88c1c14e1b..f86ce56396168edf5d0e1c412ddda0b2edba7d46
2024-07-02 20:53:36 +0000 to 2024-07-05 17:52:05 +0000
- test: Migrate jobserver to snapbox (rust-lang/cargo#14191)
- chore(deps): update msrv (3 versions) to v1.77 (rust-lang/cargo#14186)
- test: migrate build_plan and build_script to snapbox (rust-lang/cargo#14193)
- test: migrate cfg and check to snapbox (rust-lang/cargo#14185)
- test: migrate install* and inheritable_workspace_fields to snapbox (rust-lang/cargo#14170)
- Pass rustflags to artifacts built with implicit targets when using target-applies-to-host (rust-lang/cargo#13900)
- test: Migrate network tests to snapbox (rust-lang/cargo#14187)
- test: migrate some files to snapbox (rust-lang/cargo#14113)
- test: Auto-redact `... after last build at ...`; Migrate `freshness` to Snapbox (rust-lang/cargo#14161)
- chore: fix some typos (rust-lang/cargo#14182)
- fix: improve message for inactive weak optional feature with edition2024 through unused dep collection (rust-lang/cargo#14026)
- test:migrate `doc/directory/docscrape` to snapbox (rust-lang/cargo#14171)
- test: Migrate git_auth to snapbox (rust-lang/cargo#14172)
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 7, 2024
Update cargo

20 commits in a515d463427b3912ec0365d106791f88c1c14e1b..154fdac39ae9629954e19e9986fd2cf2cdd8d964
2024-07-02 20:53:36 +0000 to 2024-07-07 01:28:23 +0000
- test: relax redactions for rust-lang/rust (rust-lang/cargo#14203)
- use "bootstrap" instead of "rustbuild" (rust-lang/cargo#14207)
- test: migrate serveral files to snapbox (rust-lang/cargo#14180)
- Add rustdocflags to Unit's Debug impl (rust-lang/cargo#14201)
- Allow enabling `config-include` feature in config (rust-lang/cargo#14196)
- fix(test): Restore `does_not_contain` for check (rust-lang/cargo#14198)
- test: migrate patch, pkgid, proc_macro and progress to snapbox (rust-lang/cargo#14181)
- test: Migrate jobserver to snapbox (rust-lang/cargo#14191)
- chore(deps): update msrv (3 versions) to v1.77 (rust-lang/cargo#14186)
- test: migrate build_plan and build_script to snapbox (rust-lang/cargo#14193)
- test: migrate cfg and check to snapbox (rust-lang/cargo#14185)
- test: migrate install* and inheritable_workspace_fields to snapbox (rust-lang/cargo#14170)
- Pass rustflags to artifacts built with implicit targets when using target-applies-to-host (rust-lang/cargo#13900)
- test: Migrate network tests to snapbox (rust-lang/cargo#14187)
- test: migrate some files to snapbox (rust-lang/cargo#14113)
- test: Auto-redact `... after last build at ...`; Migrate `freshness` to Snapbox (rust-lang/cargo#14161)
- chore: fix some typos (rust-lang/cargo#14182)
- fix: improve message for inactive weak optional feature with edition2024 through unused dep collection (rust-lang/cargo#14026)
- test:migrate `doc/directory/docscrape` to snapbox (rust-lang/cargo#14171)
- test: Migrate git_auth to snapbox (rust-lang/cargo#14172)
@rustbot rustbot added this to the 1.81.0 milestone Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-configuration Area: cargo config files and env vars A-dependency-resolution Area: dependency resolution and the resolver A-manifest Area: Cargo.toml issues A-registries Area: registries 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.

Bad error message when a weak dep features's depedency is an unused optional dependency
6 participants