Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Aug 19, 2023
1 parent f55f4c5 commit 17b106b
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::core::resolver::{Resolve, ResolveBehavior};
use crate::core::{FeatureValue, PackageId, PackageIdSpec, PackageSet, Workspace};
use crate::util::interning::InternedString;
use crate::util::CargoResult;
use anyhow::bail;
use anyhow::{bail, Context};
use itertools::Itertools;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::rc::Rc;
Expand Down Expand Up @@ -871,8 +871,11 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
// not all targets may be queried before resolution since artifact dependencies
// and per-pkg-targets are not immediately known.
let mut activate_target = |target| {
let name = dep.name_in_toml();
self.target_data
.merge_compile_kind(CompileKind::Target(target))
.with_context(|| format!("failed to determine target information for target `{target}`.\n \
Artifact dependency `{name}` in package `{pkg_id}` requires building for `{target}`", target = target.rustc_target()))
};
CargoResult::Ok((
artifact.is_lib(),
Expand Down
7 changes: 5 additions & 2 deletions src/cargo/sources/registry/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ struct Summaries {
/// A lazily parsed [`IndexSummary`].
enum MaybeIndexSummary {
/// A summary which has not been parsed, The `start` and `end` are pointers
/// into [`Summaries::raw_data`] which this isRegistryDependency an entry of.
/// into [`Summaries::raw_data`] which this is an entry of.
Unparsed { start: usize, end: usize },

/// An actually parsed summary.
Expand Down Expand Up @@ -313,6 +313,9 @@ pub struct IndexPackage<'a> {
///
/// Version `2` schema adds the `features2` field.
///
/// Version `3` schema adds `artifact`, `bindep_targes`, and `lib` for
/// artifact dependencies support.
///
/// This provides a method to safely introduce changes to index entries
/// and allow older versions of cargo to ignore newer entries it doesn't
/// understand. This is honored as of 1.51, so unfortunately older
Expand Down Expand Up @@ -815,7 +818,7 @@ impl<'a> SummariesCache<'a> {
.get(..4)
.ok_or_else(|| anyhow::anyhow!("cache expected 4 bytes for index schema version"))?;
let index_v = u32::from_le_bytes(index_v_bytes.try_into().unwrap());
if index_v != INDEX_V_MAX && index_v != 3 {
if index_v != INDEX_V_MAX {
bail!(
"index schema version {index_v} doesn't match the version I know ({INDEX_V_MAX})",
);
Expand Down
125 changes: 124 additions & 1 deletion tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,7 @@ fn targets_are_picked_up_from_non_workspace_artifact_deps() {

let mut dep = registry::Dependency::new("artifact", "1.0.0");
Package::new("uses-artifact", "1.0.0")
.schema_version(3)
.file(
"src/lib.rs",
r#"pub fn uses_artifact() { let _b = include_bytes!(env!("CARGO_BIN_FILE_ARTIFACT")); }"#,
Expand Down Expand Up @@ -1489,6 +1490,127 @@ fn targets_are_picked_up_from_non_workspace_artifact_deps() {
.run();
}

#[cargo_test]
fn index_version_filtering() {
if cross_compile::disabled() {
return;
}
let target = cross_compile::alternate();

Package::new("artifact", "1.0.0")
.file("src/main.rs", r#"fn main() {}"#)
.file("src/lib.rs", r#"pub fn lib() {}"#)
.publish();

let mut dep = registry::Dependency::new("artifact", "1.0.0");

Package::new("bar", "1.0.0").publish();
Package::new("bar", "1.0.1")
.schema_version(3)
.add_dep(dep.artifact("bin", Some(target.to_string())))
.publish();

// Verify that without `-Zbindeps` that it does not use 1.0.1.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("tree")
.with_stdout("foo v0.1.0 [..]\n└── bar v1.0.0")
.run();

// And with -Zbindeps it can use 1.0.1.
p.cargo("update -Zbindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr(
"\
[UPDATING] [..]
[ADDING] artifact v1.0.0
[UPDATING] bar v1.0.0 -> v1.0.1",
)
.run();

// And without -Zbindeps, now that 1.0.1 is in Cargo.lock, it should fail.
p.cargo("check")
.with_status(101)
.with_stderr(
"\
[UPDATING] [..]
error: failed to select a version for the requirement `bar = \"^1.0\"` (locked to 1.0.1)
candidate versions found which didn't match: 1.0.0
location searched: [..]
required by package `foo v0.1.0 [..]`
perhaps a crate was updated and forgotten to be re-vendored?",
)
.run();
}

// FIXME: `download_accessible` should work properly for artifact dependencies
#[cargo_test]
#[ignore = "broken, needs download_accessible fix"]
fn proc_macro_in_artifact_dep() {
// Forcing FeatureResolver to check a proc-macro for a dependency behind a
// target dependency.
if cross_compile::disabled() {
return;
}
Package::new("pm", "1.0.0")
.file("src/lib.rs", "")
.file(
"Cargo.toml",
r#"
[package]
name = "pm"
version = "1.0.0"
[lib]
proc-macro = true
"#,
)
.publish();
let alternate = cross_compile::alternate();
Package::new("bin-uses-pm", "1.0.0")
.target_dep("pm", "1.0", alternate)
.file("src/main.rs", "fn main() {}")
.publish();
// Simulate a network error downloading the proc-macro.
std::fs::remove_file(cargo_test_support::paths::root().join("dl/pm/1.0.0/download")).unwrap();
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]
bin-uses-pm = {{ version = "1.0", artifact = "bin", target = "{alternate}"}}
"#
),
)
.file("src/lib.rs", "")
.build();

p.cargo("check -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr("")
.run();
}

#[cargo_test]
fn allow_dep_renames_with_multiple_versions() {
Package::new("bar", "1.0.0")
Expand Down Expand Up @@ -2896,7 +3018,8 @@ fn check_transitive_artifact_dependency_with_different_target() {
p.cargo("check -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr_contains(
"error: failed to run `rustc` to learn about target-specific information",
"error: failed to determine target information for target `custom-target`.\n \
Artifact dependency `baz` in package `bar v0.0.0 [..]` requires building for `custom-target`",
)
.with_status(101)
.run();
Expand Down

0 comments on commit 17b106b

Please sign in to comment.