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

Platform-specific-ness is carried via deps #9982

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;

use anyhow::Context as _;
use cargo_platform::Platform;
use log::debug;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::rc::Rc;
Expand All @@ -37,7 +38,7 @@ pub struct RegistryQueryer<'a> {
registry_cache: HashMap<Dependency, Rc<Vec<Summary>>>,
/// a cache of `Dependency`s that are required for a `Summary`
summary_cache: HashMap<
(Option<PackageId>, Summary, ResolveOpts),
(Option<(PackageId, Option<Platform>)>, Summary, ResolveOpts),
Rc<(HashSet<InternedString>, Rc<Vec<DepInfo>>)>,
>,
/// all the cases we ended up using a supplied replacement
Expand Down Expand Up @@ -189,36 +190,47 @@ impl<'a> RegistryQueryer<'a> {
pub fn build_deps(
&mut self,
cx: &Context,
parent: Option<PackageId>,
parent: Option<(PackageId, Option<Platform>)>,
candidate: &Summary,
opts: &ResolveOpts,
) -> ActivateResult<Rc<(HashSet<InternedString>, Rc<Vec<DepInfo>>)>> {
// if we have calculated a result before, then we can just return it,
// as it is a "pure" query of its arguments.
if let Some(out) = self
.summary_cache
.get(&(parent, candidate.clone(), opts.clone()))
.get(&(parent.clone(), candidate.clone(), opts.clone()))
.cloned()
{
return Ok(out);
}

let (parent_package_id, parent_platform) = match parent.clone() {
Some((package_id, platform)) => (Some(package_id), platform),
None => (None, None),
};

// First, figure out our set of dependencies based on the requested set
// of features. This also calculates what features we're going to enable
// for our own dependencies.
let (used_features, deps) = resolve_features(parent, candidate, opts)?;
let (used_features, deps) = resolve_features(parent_package_id, candidate, opts)?;

// Next, transform all dependencies into a list of possible candidates
// which can satisfy that dependency.
let mut deps = deps
.into_iter()
.map(|(dep, features)| {
.map(|(mut dep, features)| {
let candidates = self.query(&dep).with_context(|| {
format!(
"failed to get `{}` as a dependency of {}",
dep.package_name(),
describe_path_in_context(cx, &candidate.package_id()),
)
})?;
if dep.platform().is_none() && parent_platform.is_some() {
// If the parent has a specific target, the child will also only be used in that target.
// Tracking this allows for analysing platform-specificness of transitive dependencies in `cargo metadata`.
dep.set_platform(parent_platform.clone());
}
Ok((dep, candidates, features))
})
.collect::<CargoResult<Vec<DepInfo>>>()?;
Expand Down
8 changes: 6 additions & 2 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,12 @@ fn activate(
};

let now = Instant::now();
let (used_features, deps) =
&*registry.build_deps(cx, parent.map(|p| p.0.package_id()), &candidate, opts)?;
let (used_features, deps) = &*registry.build_deps(
cx,
parent.map(|(summary, dep)| (summary.package_id(), dep.platform().map(|p| p.to_owned()))),
&candidate,
opts,
)?;

// Record what list of features is active for this package.
if !used_features.is_empty() {
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/features2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,6 @@ fn doc_optional() {
"\
[UPDATING] [..]
[DOWNLOADING] crates ...
[DOWNLOADED] spin v1.0.0 [..]
[DOWNLOADED] bar v1.0.0 [..]
[DOCUMENTING] bar v1.0.0
[CHECKING] bar v1.0.0
Expand Down
281 changes: 280 additions & 1 deletion tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use cargo_test_support::install::cargo_home;
use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package;
use cargo_test_support::registry::{Dependency, Package};
use cargo_test_support::{basic_bin_manifest, basic_lib_manifest, main_file, project, rustc_host};
use serde_json::json;

Expand Down Expand Up @@ -3070,6 +3070,285 @@ fn dep_kinds_workspace() {
.run();
}

#[cargo_test]
fn target_specific_feature() {
Package::new("optdep", "1.0.0").publish();
Package::new("common", "1.0.0")
.add_dep(Dependency::new("optdep", "1.0").optional(true))
.publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"

[dependencies]
common = "1.0"

[target.x86_64-unknown-linux-gnu.dependencies]
common = { version = "1.0.0" }

[target.x86_64-apple-darwin.dependencies]
common = { version = "1.0.0", features = ["optdep"] }
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("metadata")
.with_json(
r#"{
"metadata": null,
"packages": [
{
"authors": [],
"categories": [],
"default_run": null,
"dependencies": [
{
"features": [],
"kind": null,
"name": "optdep",
"optional": false,
"registry": null,
"rename": null,
"req": "^1.0",
"source": "registry+https://github.com/rust-lang/crates.io-index",
"target": null,
"uses_default_features": true
}
],
"description": null,
"documentation": null,
"edition": "2015",
"features": {},
"homepage": null,
"id": "common 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keywords": [],
"license": null,
"license_file": null,
"links": null,
"manifest_path": "[..]/common-1.0.0/Cargo.toml",
"metadata": null,
"name": "common",
"publish": null,
"readme": null,
"repository": null,
"rust_version": null,
"source": "registry+https://github.com/rust-lang/crates.io-index",
"targets": [
{
"crate_types": [
"lib"
],
"doc": true,
"doctest": true,
"edition": "2015",
"kind": [
"lib"
],
"name": "common",
"src_path": "[..]/common-1.0.0/src/lib.rs",
"test": true
}
],
"version": "1.0.0"
},
{
"authors": [],
"categories": [],
"default_run": null,
"dependencies": [
{
"features": [],
"kind": null,
"name": "common",
"optional": false,
"registry": null,
"rename": null,
"req": "^1.0",
"source": "registry+https://github.com/rust-lang/crates.io-index",
"target": null,
"uses_default_features": true
},
{
"features": [
"optdep"
],
"kind": null,
"name": "common",
"optional": false,
"registry": null,
"rename": null,
"req": "^1.0.0",
"source": "registry+https://github.com/rust-lang/crates.io-index",
"target": "x86_64-apple-darwin",
"uses_default_features": true
},
{
"features": [],
"kind": null,
"name": "common",
"optional": false,
"registry": null,
"rename": null,
"req": "^1.0.0",
"source": "registry+https://github.com/rust-lang/crates.io-index",
"target": "x86_64-unknown-linux-gnu",
"uses_default_features": true
}
],
"description": null,
"documentation": null,
"edition": "2015",
"features": {},
"homepage": null,
"id": "foo 1.0.0 (path+file://[..]/foo)",
"keywords": [],
"license": null,
"license_file": null,
"links": null,
"manifest_path": "[..]/foo/Cargo.toml",
"metadata": null,
"name": "foo",
"publish": null,
"readme": null,
"repository": null,
"rust_version": null,
"source": null,
"targets": [
{
"crate_types": [
"lib"
],
"doc": true,
"doctest": true,
"edition": "2015",
"kind": [
"lib"
],
"name": "foo",
"src_path": "[..]/foo/src/lib.rs",
"test": true
}
],
"version": "1.0.0"
},
{
"authors": [],
"categories": [],
"default_run": null,
"dependencies": [],
"description": null,
"documentation": null,
"edition": "2015",
"features": {},
"homepage": null,
"id": "optdep 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"keywords": [],
"license": null,
"license_file": null,
"links": null,
"manifest_path": "[..]/optdep-1.0.0/Cargo.toml",
"metadata": null,
"name": "optdep",
"publish": null,
"readme": null,
"repository": null,
"rust_version": null,
"source": "registry+https://github.com/rust-lang/crates.io-index",
"targets": [
{
"crate_types": [
"lib"
],
"doc": true,
"doctest": true,
"edition": "2015",
"kind": [
"lib"
],
"name": "optdep",
"src_path": "[..]/optdep-1.0.0/src/lib.rs",
"test": true
}
],
"version": "1.0.0"
}
],
"resolve": {
"nodes": [
{
"dependencies": [
"optdep 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)"
],
"deps": [
{
"dep_kinds": [
{
"kind": null,
"target": "x86_64-apple-darwin"
}
],
"name": "optdep",
"pkg": "optdep 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)"
}
],
"features": [
"optdep"
],
"id": "common 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)"
},
{
"dependencies": [
"common 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)"
],
"deps": [
{
"dep_kinds": [
{
"kind": null,
"target": null
},
{
"kind": null,
"target": "x86_64-apple-darwin"
},
{
"kind": null,
"target": "x86_64-unknown-linux-gnu"
}
],
"name": "common",
"pkg": "common 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)"
}
],
"features": [],
"id": "foo 1.0.0 (path+file://[..]/foo)"
},
{
"dependencies": [],
"deps": [],
"features": [],
"id": "optdep 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)"
}
],
"root": "foo 1.0.0 (path+file://[..]/foo)"
},
"target_directory": "[..]/foo/target",
"version": 1,
"workspace_members": [
"foo 1.0.0 (path+file://[..]/foo)"
],
"workspace_root": "[..]/foo"
}"#,
)
.run();
}

// Creating non-utf8 path is an OS-specific pain, so let's run this only on
// linux, where arbitrary bytes work.
#[cfg(target_os = "linux")]
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,6 @@ fn itarget_opt_dep() {
"\
foo v1.0.0 ([..]/foo)
└── common v1.0.0
└── optdep v1.0.0
",
)
.run();
Expand Down