Skip to content

Commit dcbb433

Browse files
committed
Make the rust-version error recommend cargo update -p crate@ver --precise ...
People encountering a dependency with a newer `rust-version` requirement may not know about `cargo update --precise`, or may consider alternate approaches that may be harmful (such as pinning with a `=` dependency). Provide specific guidance in the error message. If the user is using `cargo install`, suggest `cargo install --locked` instead.
1 parent e6827ee commit dcbb433

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/cargo/ops/cargo_compile.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,12 +661,32 @@ pub fn create_bcx<'a, 'cfg>(
661661
continue;
662662
}
663663

664+
let guidance = if ws.is_ephemeral() {
665+
if ws.ignore_lock() {
666+
"Try re-running cargo install with `--locked`".to_string()
667+
} else {
668+
String::new()
669+
}
670+
} else {
671+
format!(
672+
"Either upgrade to rustc {} or newer, or use\n\
673+
cargo update -p {}@{} --precise ver\n\
674+
where `ver` is the latest version of `{}` supporting rustc {}",
675+
version,
676+
unit.pkg.name(),
677+
unit.pkg.version(),
678+
unit.pkg.name(),
679+
current_version,
680+
)
681+
};
682+
664683
anyhow::bail!(
665684
"package `{}` cannot be built because it requires rustc {} or newer, \
666-
while the currently active rustc version is {}",
685+
while the currently active rustc version is {}\n{}",
667686
unit.pkg,
668687
version,
669688
current_version,
689+
guidance,
670690
);
671691
}
672692
}

tests/testsuite/rust_version.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ fn rust_version_too_high() {
124124
.with_status(101)
125125
.with_stderr(
126126
"error: package `foo v0.0.1 ([..])` cannot be built because it requires \
127-
rustc 1.9876.0 or newer, while the currently active rustc version is [..]",
127+
rustc 1.9876.0 or newer, while the currently active rustc version is [..]\n\
128+
Either upgrade to rustc 1.9876.0 or newer, or use\n\
129+
cargo update -p foo@0.0.1 --precise ver\n\
130+
where `ver` is the latest version of `foo` supporting rustc [..]",
128131
)
129132
.run();
130133
p.cargo("build --ignore-rust-version").run();
@@ -159,7 +162,10 @@ fn rust_version_dependency_fails() {
159162
Downloading crates ...\n \
160163
Downloaded bar v0.0.1 (registry `[..]`)\n\
161164
error: package `bar v0.0.1` cannot be built because it requires \
162-
rustc 1.2345.0 or newer, while the currently active rustc version is [..]",
165+
rustc 1.2345.0 or newer, while the currently active rustc version is [..]\n\
166+
Either upgrade to rustc 1.2345.0 or newer, or use\n\
167+
cargo update -p bar@0.0.1 --precise ver\n\
168+
where `ver` is the latest version of `bar` supporting rustc [..]",
163169
)
164170
.run();
165171
p.cargo("build --ignore-rust-version").run();

0 commit comments

Comments
 (0)