From bf3885e0c635455e9ac9898525a556a4d5ab2ed3 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 11 Mar 2025 09:02:44 -0700 Subject: [PATCH 1/2] Add tests for cargo vendor error cases resolving --- tests/testsuite/vendor.rs | 86 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tests/testsuite/vendor.rs b/tests/testsuite/vendor.rs index dbf956eedf4..2a7f3dfa499 100644 --- a/tests/testsuite/vendor.rs +++ b/tests/testsuite/vendor.rs @@ -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 pkg lockfile + +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 + +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(); +} From 2158fe135777e6d58c1f960878de42be6a7c2587 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 11 Mar 2025 09:37:02 -0700 Subject: [PATCH 2/2] Add context for which workspace failed when resolving `cargo vendor` --- src/cargo/ops/vendor.rs | 12 ++++++------ tests/testsuite/vendor.rs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cargo/ops/vendor.rs b/src/cargo/ops/vendor.rs index 381e2528a43..b3fa4a132df 100644 --- a/src/cargo/ops/vendor.rs +++ b/src/cargo/ops/vendor.rs @@ -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 { @@ -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 diff --git a/tests/testsuite/vendor.rs b/tests/testsuite/vendor.rs index 2a7f3dfa499..db194026b64 100644 --- a/tests/testsuite/vendor.rs +++ b/tests/testsuite/vendor.rs @@ -2016,7 +2016,7 @@ fn error_loading_which_lock() { [ERROR] failed to sync Caused by: - failed to load pkg lockfile + failed to load lockfile for [ROOT]/foo/b Caused by: could not execute process `does-not-exist -vV` (never executed) @@ -2056,7 +2056,7 @@ fn error_downloading() { [ERROR] failed to sync Caused by: - failed to download packages + failed to download packages for [ROOT]/foo Caused by: failed to download from `[ROOTURL]/dl/bar/1.0.0/download`