-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
@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 I do think #14028 should be merged first so we make sure we don't have regressions. |
(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) |
☔ The latest upstream changes (presumably #14028) made this pull request unmergeable. Please resolve the merge conflicts. |
d64eeee
to
03b05e1
Compare
☔ The latest upstream changes (presumably #14106) made this pull request unmergeable. Please resolve the merge conflicts. |
3b4617d
to
ccced4b
Compare
ccced4b
to
9d94382
Compare
Try to minimize code changes and performance impact, which will return a custom Error |
20f9989
to
44fd447
Compare
44fd447
to
9208a57
Compare
9208a57
to
d153619
Compare
The suggestions above have been considered. Then in the subsequent commit, add these alternation by the way.
|
There was a problem hiding this 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:
or
Credit to @Muscraft doing this experiment. See Muscraft@c0b44e0 for the code that generate the snapshots above.
src/cargo/util/toml/mod.rs
Outdated
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
d153619
to
b28eef9
Compare
I have added the These tips are more user-friendly. |
Thanks! That looks awesome. Will go ahead and merge this. @bors r+ |
☀️ Test successful - checks-actions |
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)
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)
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
tests/testsuite/lints/unused_optional_dependencies.rs
to snapshotAnd rename
MissingField
toMissingFieldError