Skip to content

Commit 43dccc7

Browse files
committed
apply suggestions
1 parent 6b65d06 commit 43dccc7

File tree

3 files changed

+133
-4
lines changed

3 files changed

+133
-4
lines changed

src/cargo/core/resolver/features.rs

Lines changed: 4 additions & 1 deletion
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

Lines changed: 5 additions & 2 deletions
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
@@ -821,7 +824,7 @@ impl<'a> SummariesCache<'a> {
821824
.get(..4)
822825
.ok_or_else(|| anyhow::anyhow!("cache expected 4 bytes for index schema version"))?;
823826
let index_v = u32::from_le_bytes(index_v_bytes.try_into().unwrap());
824-
if index_v != INDEX_V_MAX && index_v != 3 {
827+
if index_v != INDEX_V_MAX {
825828
bail!(
826829
"index schema version {index_v} doesn't match the version I know ({INDEX_V_MAX})",
827830
);

tests/testsuite/artifact_dep.rs

Lines changed: 124 additions & 1 deletion
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,127 @@ 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+
// FIXME: `download_accessible` should work properly for artifact dependencies
1560+
#[cargo_test]
1561+
#[ignore = "broken, needs download_accessible fix"]
1562+
fn proc_macro_in_artifact_dep() {
1563+
// Forcing FeatureResolver to check a proc-macro for a dependency behind a
1564+
// target dependency.
1565+
if cross_compile::disabled() {
1566+
return;
1567+
}
1568+
Package::new("pm", "1.0.0")
1569+
.file("src/lib.rs", "")
1570+
.file(
1571+
"Cargo.toml",
1572+
r#"
1573+
[package]
1574+
name = "pm"
1575+
version = "1.0.0"
1576+
1577+
[lib]
1578+
proc-macro = true
1579+
1580+
"#,
1581+
)
1582+
.publish();
1583+
let alternate = cross_compile::alternate();
1584+
Package::new("bin-uses-pm", "1.0.0")
1585+
.target_dep("pm", "1.0", alternate)
1586+
.file("src/main.rs", "fn main() {}")
1587+
.publish();
1588+
// Simulate a network error downloading the proc-macro.
1589+
std::fs::remove_file(cargo_test_support::paths::root().join("dl/pm/1.0.0/download")).unwrap();
1590+
let p = project()
1591+
.file(
1592+
"Cargo.toml",
1593+
&format!(
1594+
r#"
1595+
[package]
1596+
name = "foo"
1597+
version = "0.1.0"
1598+
edition = "2021"
1599+
1600+
[dependencies]
1601+
bin-uses-pm = {{ version = "1.0", artifact = "bin", target = "{alternate}"}}
1602+
"#
1603+
),
1604+
)
1605+
.file("src/lib.rs", "")
1606+
.build();
1607+
1608+
p.cargo("check -Z bindeps")
1609+
.masquerade_as_nightly_cargo(&["bindeps"])
1610+
.with_stderr("")
1611+
.run();
1612+
}
1613+
14921614
#[cargo_test]
14931615
fn allow_dep_renames_with_multiple_versions() {
14941616
Package::new("bar", "1.0.0")
@@ -2896,7 +3018,8 @@ fn check_transitive_artifact_dependency_with_different_target() {
28963018
p.cargo("check -Z bindeps")
28973019
.masquerade_as_nightly_cargo(&["bindeps"])
28983020
.with_stderr_contains(
2899-
"error: failed to run `rustc` to learn about target-specific information",
3021+
"error: failed to determine target information for target `custom-target`.\n \
3022+
Artifact dependency `baz` in package `bar v0.0.0 [..]` requires building for `custom-target`",
29003023
)
29013024
.with_status(101)
29023025
.run();

0 commit comments

Comments
 (0)