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 panic when running cargo tree on a package with a cross compiled bindep #13207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rukai
Copy link
Contributor

@rukai rukai commented Dec 26, 2023

What does this PR try to resolve?

This PR fixes the cargo tree panic described in #12358 and #10593

How should we test and review this PR?

The new integration test is sufficient.

Additional information

There was discussion of holding off on this until a full design of how to present bindeps in cargo tree is arrived at.
However I think it makes sense to land this PR first as:

  • It introduces a test to catch the simple case.
  • The fix itself is small and (afaik) correct.
  • Provides genuine value to users by allowing usage of cargo tree in many more bindeps use cases.

So this PR is a good first step towards full support in cargo tree.

The changes to the activated_features methods are not related to the fix but just the improved error reporting I needed to fully diagnose the issue.
I moved activated_features_int into activated_features and activated_features_unverified as I was concerned of the performance cost of generating the full error when its not a fatal error and may occur many times.
I think this change will bring value in the future but I am happy to remove it if it is undesired.

I actually wrote up the test + fix independently before discovering #10593 (comment) , once I discovered it I copied in some specific parts to improve comments + correctness

I did however leave out the None if features_for != FeaturesFor::default() => features_for, because that seems wrong to me. proc macro and build dependencies of a bindep need to be compiled for the host, regardless of the bindeps target, otherwise the host cant run them to build the bindep.

@rustbot
Copy link
Collaborator

rustbot commented Dec 26, 2023

r? @epage

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-dependency-resolution Area: dependency resolution and the resolver A-features2 Area: issues specifically related to the v2 feature resolver Command-tree S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 26, 2023
@rukai
Copy link
Contributor Author

rukai commented Dec 26, 2023

probably makes sense for
r? weihanglo

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.

Sorry for the way too long delay of the review 😞.

@@ -319,8 +319,32 @@ impl ResolvedFeatures {
pkg_id: PackageId,
features_for: FeaturesFor,
) -> Vec<InternedString> {
self.activated_features_int(pkg_id, features_for)
.expect("activated_features for invalid package")
let fk = features_for.apply_opts(&self.opts);
Copy link
Member

Choose a reason for hiding this comment

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

Would like to see this refactor being in its own commit, to following atomic commit principle.

.and_then(|target| target.to_resolved_compile_target(requested_kind))
{
// Dependency has a `{ …, target = <triple> }`
Some(target) => FeaturesFor::ArtifactDep(target),
Copy link
Member

Choose a reason for hiding this comment

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

I might have a rotted memory of artifact dependencies. However, even when target is present the dependency might still be depended as a normal lib (with lib = true). The fix doesn't look 100% correct to me for this reason.

Copy link
Member

Choose a reason for hiding this comment

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

Okay this looks like an adaption of my own patch... Need to refresh my memory 😅.

@@ -1445,6 +1445,55 @@ foo v0.0.0 ([CWD])
)
.run();
}

#[cargo_test]
fn artifact_dep_target_specified() {
Copy link
Member

Choose a reason for hiding this comment

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

This test passes even without the patch in ops::tree::graph. We should probably first create a test verifying the current panic behavior in a commit, followed by the other commit fixing both the behavior and the test.

Copy link
Member

Choose a reason for hiding this comment

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

I did however leave out the None if features_for != FeaturesFor::default() => features_for, because that seems wrong to me. proc macro and build dependencies of a bindep need to be compiled for the host, regardless of the bindeps target, otherwise the host cant run them to build the bindep.

See https://rust-lang.github.io/rfcs/3028-cargo-binary-dependencies.html#reference-level-explanation, so arguably some build deps could be built for a target platform.

By default, build-dependencies are built for the host, while dependencies and dev-dependencies are built for the target. You can specify the target attribute to build for a specific target, such as target = "wasm32-wasi"; a literal target = "target" will build for the target even if specifying a build dependency. (If the target is not available, this will result in an error at build time, just as if building the specified crate with a --target option for an unavailable target.)

@weihanglo weihanglo added S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dependency-resolution Area: dependency resolution and the resolver A-features2 Area: issues specifically related to the v2 feature resolver Command-tree S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants