From dcbb4333408e8ca4505215de7700edad728baf31 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Fri, 22 Jul 2022 23:52:49 -0700 Subject: [PATCH 1/2] 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. --- src/cargo/ops/cargo_compile.rs | 22 +++++++++++++++++++++- tests/testsuite/rust_version.rs | 10 ++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 5d273295ae3..2ac85cad48a 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -661,12 +661,32 @@ pub fn create_bcx<'a, 'cfg>( continue; } + let guidance = if ws.is_ephemeral() { + if ws.ignore_lock() { + "Try re-running cargo install with `--locked`".to_string() + } else { + String::new() + } + } else { + format!( + "Either upgrade to rustc {} or newer, or use\n\ + cargo update -p {}@{} --precise ver\n\ + where `ver` is the latest version of `{}` supporting rustc {}", + version, + unit.pkg.name(), + unit.pkg.version(), + unit.pkg.name(), + current_version, + ) + }; + anyhow::bail!( "package `{}` cannot be built because it requires rustc {} or newer, \ - while the currently active rustc version is {}", + while the currently active rustc version is {}\n{}", unit.pkg, version, current_version, + guidance, ); } } diff --git a/tests/testsuite/rust_version.rs b/tests/testsuite/rust_version.rs index 6f849141ff6..1823cdad98a 100644 --- a/tests/testsuite/rust_version.rs +++ b/tests/testsuite/rust_version.rs @@ -124,7 +124,10 @@ fn rust_version_too_high() { .with_status(101) .with_stderr( "error: package `foo v0.0.1 ([..])` cannot be built because it requires \ - rustc 1.9876.0 or newer, while the currently active rustc version is [..]", + rustc 1.9876.0 or newer, while the currently active rustc version is [..]\n\ + Either upgrade to rustc 1.9876.0 or newer, or use\n\ + cargo update -p foo@0.0.1 --precise ver\n\ + where `ver` is the latest version of `foo` supporting rustc [..]", ) .run(); p.cargo("build --ignore-rust-version").run(); @@ -159,7 +162,10 @@ fn rust_version_dependency_fails() { Downloading crates ...\n \ Downloaded bar v0.0.1 (registry `[..]`)\n\ error: package `bar v0.0.1` cannot be built because it requires \ - rustc 1.2345.0 or newer, while the currently active rustc version is [..]", + rustc 1.2345.0 or newer, while the currently active rustc version is [..]\n\ + Either upgrade to rustc 1.2345.0 or newer, or use\n\ + cargo update -p bar@0.0.1 --precise ver\n\ + where `ver` is the latest version of `bar` supporting rustc [..]", ) .run(); p.cargo("build --ignore-rust-version").run(); From 934e79072ab40891e6e7dacadad0505f6029515c Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Tue, 2 Aug 2022 10:50:46 -0700 Subject: [PATCH 2/2] Only show advice to use `cargo update --precise` for non-local packages Packages in the local workspace can't get updated this way; the user just needs to point to a different source, or otherwise update the package themselves. --- src/cargo/ops/cargo_compile.rs | 4 +++- tests/testsuite/rust_version.rs | 5 +---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 2ac85cad48a..ced55770ef8 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -667,7 +667,7 @@ pub fn create_bcx<'a, 'cfg>( } else { String::new() } - } else { + } else if !unit.is_local() { format!( "Either upgrade to rustc {} or newer, or use\n\ cargo update -p {}@{} --precise ver\n\ @@ -678,6 +678,8 @@ pub fn create_bcx<'a, 'cfg>( unit.pkg.name(), current_version, ) + } else { + String::new() }; anyhow::bail!( diff --git a/tests/testsuite/rust_version.rs b/tests/testsuite/rust_version.rs index 1823cdad98a..68d54190cb0 100644 --- a/tests/testsuite/rust_version.rs +++ b/tests/testsuite/rust_version.rs @@ -124,10 +124,7 @@ fn rust_version_too_high() { .with_status(101) .with_stderr( "error: package `foo v0.0.1 ([..])` cannot be built because it requires \ - rustc 1.9876.0 or newer, while the currently active rustc version is [..]\n\ - Either upgrade to rustc 1.9876.0 or newer, or use\n\ - cargo update -p foo@0.0.1 --precise ver\n\ - where `ver` is the latest version of `foo` supporting rustc [..]", + rustc 1.9876.0 or newer, while the currently active rustc version is [..]\n\n", ) .run(); p.cargo("build --ignore-rust-version").run();