Skip to content

Commit df276af

Browse files
committed
apply suggestions
1 parent f55f4c5 commit df276af

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
lines changed

src/cargo/core/resolver/features.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::core::resolver::{Resolve, ResolveBehavior};
4545
use crate::core::{FeatureValue, PackageId, PackageIdSpec, PackageSet, Workspace};
4646
use crate::util::interning::InternedString;
4747
use crate::util::CargoResult;
48-
use anyhow::bail;
48+
use anyhow::{bail, Context};
4949
use itertools::Itertools;
5050
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
5151
use std::rc::Rc;
@@ -871,8 +871,11 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
871871
// not all targets may be queried before resolution since artifact dependencies
872872
// and per-pkg-targets are not immediately known.
873873
let mut activate_target = |target| {
874+
let name = dep.name_in_toml();
874875
self.target_data
875876
.merge_compile_kind(CompileKind::Target(target))
877+
.with_context(|| format!("failed to determine target information for target `{target}`.\n \
878+
Artifact dependency `{name}` in package `{pkg_id}` requires building for `{target}`", target = target.rustc_target()))
876879
};
877880
CargoResult::Ok((
878881
artifact.is_lib(),

src/cargo/sources/registry/index.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ struct Summaries {
178178
/// A lazily parsed [`IndexSummary`].
179179
enum MaybeIndexSummary {
180180
/// A summary which has not been parsed, The `start` and `end` are pointers
181-
/// into [`Summaries::raw_data`] which this isRegistryDependency an entry of.
181+
/// into [`Summaries::raw_data`] which this is an entry of.
182182
Unparsed { start: usize, end: usize },
183183

184184
/// An actually parsed summary.
@@ -313,6 +313,9 @@ pub struct IndexPackage<'a> {
313313
///
314314
/// Version `2` schema adds the `features2` field.
315315
///
316+
/// Version `3` schema adds `artifact`, `bindep_targes`, and `lib` for
317+
/// artifact dependencies support.
318+
///
316319
/// This provides a method to safely introduce changes to index entries
317320
/// and allow older versions of cargo to ignore newer entries it doesn't
318321
/// understand. This is honored as of 1.51, so unfortunately older
@@ -815,7 +818,7 @@ impl<'a> SummariesCache<'a> {
815818
.get(..4)
816819
.ok_or_else(|| anyhow::anyhow!("cache expected 4 bytes for index schema version"))?;
817820
let index_v = u32::from_le_bytes(index_v_bytes.try_into().unwrap());
818-
if index_v != INDEX_V_MAX && index_v != 3 {
821+
if index_v != INDEX_V_MAX {
819822
bail!(
820823
"index schema version {index_v} doesn't match the version I know ({INDEX_V_MAX})",
821824
);

tests/testsuite/artifact_dep.rs

+123-1
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,7 @@ fn targets_are_picked_up_from_non_workspace_artifact_deps() {
14581458

14591459
let mut dep = registry::Dependency::new("artifact", "1.0.0");
14601460
Package::new("uses-artifact", "1.0.0")
1461+
.schema_version(3)
14611462
.file(
14621463
"src/lib.rs",
14631464
r#"pub fn uses_artifact() { let _b = include_bytes!(env!("CARGO_BIN_FILE_ARTIFACT")); }"#,
@@ -1489,6 +1490,126 @@ fn targets_are_picked_up_from_non_workspace_artifact_deps() {
14891490
.run();
14901491
}
14911492

1493+
#[cargo_test]
1494+
fn index_version_filtering() {
1495+
if cross_compile::disabled() {
1496+
return;
1497+
}
1498+
let target = cross_compile::alternate();
1499+
1500+
Package::new("artifact", "1.0.0")
1501+
.file("src/main.rs", r#"fn main() {}"#)
1502+
.file("src/lib.rs", r#"pub fn lib() {}"#)
1503+
.publish();
1504+
1505+
let mut dep = registry::Dependency::new("artifact", "1.0.0");
1506+
1507+
Package::new("bar", "1.0.0").publish();
1508+
Package::new("bar", "1.0.1")
1509+
.schema_version(3)
1510+
.add_dep(dep.artifact("bin", Some(target.to_string())))
1511+
.publish();
1512+
1513+
// Verify that without `-Zbindeps` that it does not use 1.0.1.
1514+
let p = project()
1515+
.file(
1516+
"Cargo.toml",
1517+
r#"
1518+
[package]
1519+
name = "foo"
1520+
version = "0.1.0"
1521+
1522+
[dependencies]
1523+
bar = "1.0"
1524+
"#,
1525+
)
1526+
.file("src/lib.rs", "")
1527+
.build();
1528+
1529+
p.cargo("tree")
1530+
.with_stdout("foo v0.1.0 [..]\n└── bar v1.0.0")
1531+
.run();
1532+
1533+
// And with -Zbindeps it can use 1.0.1.
1534+
p.cargo("update -Zbindeps")
1535+
.masquerade_as_nightly_cargo(&["bindeps"])
1536+
.with_stderr(
1537+
"\
1538+
[UPDATING] [..]
1539+
[ADDING] artifact v1.0.0
1540+
[UPDATING] bar v1.0.0 -> v1.0.1",
1541+
)
1542+
.run();
1543+
1544+
// And without -Zbindeps, now that 1.0.1 is in Cargo.lock, it should fail.
1545+
p.cargo("check")
1546+
.with_status(101)
1547+
.with_stderr(
1548+
"\
1549+
[UPDATING] [..]
1550+
error: failed to select a version for the requirement `bar = \"^1.0\"` (locked to 1.0.1)
1551+
candidate versions found which didn't match: 1.0.0
1552+
location searched: [..]
1553+
required by package `foo v0.1.0 [..]`
1554+
perhaps a crate was updated and forgotten to be re-vendored?",
1555+
)
1556+
.run();
1557+
}
1558+
1559+
#[cargo_test]
1560+
#[ignore = "broken, needs download_accessible fix"]
1561+
fn proc_macro_in_artifact_dep() {
1562+
// Forcing FeatureResolver to check a proc-macro for a dependency behind a
1563+
// target dependency.
1564+
if cross_compile::disabled() {
1565+
return;
1566+
}
1567+
Package::new("pm", "1.0.0")
1568+
.file("src/lib.rs", "")
1569+
.file(
1570+
"Cargo.toml",
1571+
r#"
1572+
[package]
1573+
name = "pm"
1574+
version = "1.0.0"
1575+
1576+
[lib]
1577+
proc-macro = true
1578+
1579+
"#,
1580+
)
1581+
.publish();
1582+
let alternate = cross_compile::alternate();
1583+
Package::new("bin-uses-pm", "1.0.0")
1584+
.target_dep("pm", "1.0", alternate)
1585+
.file("src/main.rs", "fn main() {}")
1586+
.publish();
1587+
// Simulate a network error downloading the proc-macro.
1588+
std::fs::remove_file(cargo_test_support::paths::root().join("dl/pm/1.0.0/download")).unwrap();
1589+
let p = project()
1590+
.file(
1591+
"Cargo.toml",
1592+
&format!(
1593+
r#"
1594+
[package]
1595+
name = "foo"
1596+
version = "0.1.0"
1597+
edition = "2021"
1598+
1599+
[dependencies]
1600+
bin-uses-pm = {{ version = "1.0", artifact = "bin", target = "{alternate}"}}
1601+
"#
1602+
),
1603+
)
1604+
.file("src/lib.rs", "")
1605+
.build();
1606+
1607+
p.cargo("check -Z bindeps")
1608+
.masquerade_as_nightly_cargo(&["bindeps"])
1609+
.with_stderr("")
1610+
.run();
1611+
}
1612+
14921613
#[cargo_test]
14931614
fn allow_dep_renames_with_multiple_versions() {
14941615
Package::new("bar", "1.0.0")
@@ -2896,7 +3017,8 @@ fn check_transitive_artifact_dependency_with_different_target() {
28963017
p.cargo("check -Z bindeps")
28973018
.masquerade_as_nightly_cargo(&["bindeps"])
28983019
.with_stderr_contains(
2899-
"error: failed to run `rustc` to learn about target-specific information",
3020+
"error: failed to determine target information for target `custom-target`.\n \
3021+
Artifact dependency `baz` in package `bar v0.0.0 [..]` requires building for `custom-target`",
29003022
)
29013023
.with_status(101)
29023024
.run();

0 commit comments

Comments
 (0)