Skip to content

cargo vendor: Add context which workspace failed to resolve #15297

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

Merged
merged 2 commits into from
Mar 11, 2025
Merged
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
12 changes: 6 additions & 6 deletions src/cargo/ops/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ fn sync(
// attempt. If anything fails here we basically just move on to the next
// crate to work with.
for ws in workspaces {
let (packages, resolve) =
ops::resolve_ws(ws, dry_run).context("failed to load pkg lockfile")?;
let (packages, resolve) = ops::resolve_ws(ws, dry_run)
.with_context(|| format!("failed to load lockfile for {}", ws.root().display()))?;

packages
.get_many(resolve.iter())
.context("failed to download packages")?;
.with_context(|| format!("failed to download packages for {}", ws.root().display()))?;

for pkg in resolve.iter() {
let sid = if opts.respect_source_config {
Expand Down Expand Up @@ -187,12 +187,12 @@ fn sync(
// Next up let's actually download all crates and start storing internal
// tables about them.
for ws in workspaces {
let (packages, resolve) =
ops::resolve_ws(ws, dry_run).context("failed to load pkg lockfile")?;
let (packages, resolve) = ops::resolve_ws(ws, dry_run)
.with_context(|| format!("failed to load lockfile for {}", ws.root().display()))?;

packages
.get_many(resolve.iter())
.context("failed to download packages")?;
.with_context(|| format!("failed to download packages for {}", ws.root().display()))?;

for pkg in resolve.iter() {
// No need to vendor path crates since they're already in the
Expand Down
86 changes: 86 additions & 0 deletions tests/testsuite/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1981,3 +1981,89 @@ directory = "new-vendor-dir"
"#]])
.run();
}

#[cargo_test]
fn error_loading_which_lock() {
// Tests an error message to make sure it is clear which
// manifest/workspace caused the problem. In this particular case, it was
// because the 2024 edition wants to know which rust version is in use.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
"#,
)
.file("src/lib.rs", "")
.file(
"b/Cargo.toml",
r#"
[package]
name = "b"
version = "0.1.0"
edition = "2024"
"#,
)
.file("b/src/lib.rs", "")
.build();

p.cargo("vendor --respect-source-config -s b/Cargo.toml")
.env("RUSTC", "does-not-exist")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to sync

Caused by:
failed to load lockfile for [ROOT]/foo/b

Caused by:
could not execute process `does-not-exist -vV` (never executed)

Caused by:
[NOT_FOUND]

"#]])
.run();
}

#[cargo_test]
fn error_downloading() {
// Tests the error message when downloading packages.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"

[dependencies]
bar = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();

Package::new("bar", "1.0.0").publish();
p.cargo("generate-lockfile").run();
std::fs::remove_file(cargo_test_support::paths::root().join("dl/bar/1.0.0/download")).unwrap();
p.cargo("vendor --respect-source-config")
.with_status(101)
.with_stderr_data(str![[r#"
[DOWNLOADING] crates ...
[ERROR] failed to sync

Caused by:
failed to download packages for [ROOT]/foo

Caused by:
failed to download from `[ROOTURL]/dl/bar/1.0.0/download`

Caused by:
[37] Could[..]t read a file:// file (Couldn't open file [ROOT]/dl/bar/1.0.0/download)

"#]])
.run();
}