Skip to content

Commit 611b7c4

Browse files
authored
cargo vendor: Add context which workspace failed to resolve (#15297)
This adds some context to the `cargo vendor` command when it fails to resolve or load a workspace. This can be helpful when syncing a large number of workspaces, and it isn't clear which one is causing the problem.
2 parents 11df198 + 2158fe1 commit 611b7c4

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

src/cargo/ops/vendor.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ fn sync(
144144
// attempt. If anything fails here we basically just move on to the next
145145
// crate to work with.
146146
for ws in workspaces {
147-
let (packages, resolve) =
148-
ops::resolve_ws(ws, dry_run).context("failed to load pkg lockfile")?;
147+
let (packages, resolve) = ops::resolve_ws(ws, dry_run)
148+
.with_context(|| format!("failed to load lockfile for {}", ws.root().display()))?;
149149

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

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

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

197197
for pkg in resolve.iter() {
198198
// No need to vendor path crates since they're already in the

tests/testsuite/vendor.rs

+86
Original file line numberDiff line numberDiff line change
@@ -1981,3 +1981,89 @@ directory = "new-vendor-dir"
19811981
"#]])
19821982
.run();
19831983
}
1984+
1985+
#[cargo_test]
1986+
fn error_loading_which_lock() {
1987+
// Tests an error message to make sure it is clear which
1988+
// manifest/workspace caused the problem. In this particular case, it was
1989+
// because the 2024 edition wants to know which rust version is in use.
1990+
let p = project()
1991+
.file(
1992+
"Cargo.toml",
1993+
r#"
1994+
[package]
1995+
name = "a"
1996+
version = "0.1.0"
1997+
"#,
1998+
)
1999+
.file("src/lib.rs", "")
2000+
.file(
2001+
"b/Cargo.toml",
2002+
r#"
2003+
[package]
2004+
name = "b"
2005+
version = "0.1.0"
2006+
edition = "2024"
2007+
"#,
2008+
)
2009+
.file("b/src/lib.rs", "")
2010+
.build();
2011+
2012+
p.cargo("vendor --respect-source-config -s b/Cargo.toml")
2013+
.env("RUSTC", "does-not-exist")
2014+
.with_status(101)
2015+
.with_stderr_data(str![[r#"
2016+
[ERROR] failed to sync
2017+
2018+
Caused by:
2019+
failed to load lockfile for [ROOT]/foo/b
2020+
2021+
Caused by:
2022+
could not execute process `does-not-exist -vV` (never executed)
2023+
2024+
Caused by:
2025+
[NOT_FOUND]
2026+
2027+
"#]])
2028+
.run();
2029+
}
2030+
2031+
#[cargo_test]
2032+
fn error_downloading() {
2033+
// Tests the error message when downloading packages.
2034+
let p = project()
2035+
.file(
2036+
"Cargo.toml",
2037+
r#"
2038+
[package]
2039+
name = "foo"
2040+
version = "0.1.0"
2041+
2042+
[dependencies]
2043+
bar = "1.0"
2044+
"#,
2045+
)
2046+
.file("src/lib.rs", "")
2047+
.build();
2048+
2049+
Package::new("bar", "1.0.0").publish();
2050+
p.cargo("generate-lockfile").run();
2051+
std::fs::remove_file(cargo_test_support::paths::root().join("dl/bar/1.0.0/download")).unwrap();
2052+
p.cargo("vendor --respect-source-config")
2053+
.with_status(101)
2054+
.with_stderr_data(str![[r#"
2055+
[DOWNLOADING] crates ...
2056+
[ERROR] failed to sync
2057+
2058+
Caused by:
2059+
failed to download packages for [ROOT]/foo
2060+
2061+
Caused by:
2062+
failed to download from `[ROOTURL]/dl/bar/1.0.0/download`
2063+
2064+
Caused by:
2065+
[37] Could[..]t read a file:// file (Couldn't open file [ROOT]/dl/bar/1.0.0/download)
2066+
2067+
"#]])
2068+
.run();
2069+
}

0 commit comments

Comments
 (0)