Skip to content

Commit bd62811

Browse files
committed
Implement 11036 cargo changes
1 parent e63e787 commit bd62811

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
22
use std::path::{Path, PathBuf};
33
use std::sync::Arc;
4-
use std::{env, fs};
4+
use std::{env, fmt, fs};
55

66
use crate::core::compiler::{CompileKind, DefaultExecutor, Executor, UnitOutput};
77
use crate::core::{Dependency, Edition, Package, PackageId, SourceId, Target, Workspace};
@@ -14,6 +14,7 @@ use crate::util::errors::CargoResult;
1414
use crate::util::{Filesystem, GlobalContext, Rustc};
1515
use crate::{drop_println, ops};
1616

17+
use annotate_snippets::Level;
1718
use anyhow::{Context as _, bail};
1819
use cargo_util::paths;
1920
use cargo_util_schemas::core::PartialVersion;
@@ -39,6 +40,42 @@ impl Drop for Transaction {
3940
}
4041
}
4142

43+
enum RustupToolchainSource {
44+
Default,
45+
Environment,
46+
CommandLine,
47+
OverrideDB,
48+
ToolchainFile,
49+
Other(String),
50+
}
51+
52+
#[allow(dead_code)]
53+
impl RustupToolchainSource {
54+
fn is_default_or_cli_override(&self) -> Option<bool> {
55+
match self {
56+
Self::Default => Some(true),
57+
Self::Environment => Some(false),
58+
Self::CommandLine => Some(true),
59+
Self::OverrideDB => Some(false),
60+
Self::ToolchainFile => Some(false),
61+
Self::Other(_) => None,
62+
}
63+
}
64+
}
65+
66+
impl fmt::Display for RustupToolchainSource {
67+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68+
f.write_str(match self {
69+
Self::Default => "default",
70+
Self::Environment => "env",
71+
Self::CommandLine => "cli",
72+
Self::OverrideDB => "path-override",
73+
Self::ToolchainFile => "toolchain-file",
74+
Self::Other(other) => other,
75+
})
76+
}
77+
}
78+
4279
struct InstallablePackage<'gctx> {
4380
gctx: &'gctx GlobalContext,
4481
opts: ops::CompileOptions,
@@ -316,6 +353,27 @@ impl<'gctx> InstallablePackage<'gctx> {
316353
fn install_one(mut self, dry_run: bool) -> CargoResult<bool> {
317354
self.gctx.shell().status("Installing", &self.pkg)?;
318355

356+
if let Some(source) = self.get_rustup_toolchain_source()
357+
&& source.is_default_or_cli_override() != Some(true)
358+
{
359+
#[expect(
360+
clippy::disallowed_methods,
361+
reason = "config should not set `RUSTUP` environment variables"
362+
)]
363+
let maybe_toolchain = env::var("RUSTUP_TOOLCHAIN")
364+
.ok()
365+
.map(|toolchain| format!(" `{toolchain}`"))
366+
.unwrap_or_default();
367+
let report = &[Level::WARNING
368+
.secondary_title(format!(
369+
"using non-default toolchain{maybe_toolchain} overridden by {source}"
370+
))
371+
.element(Level::HELP.message(format!(
372+
"use `cargo +stable install` if you meant to use the stable toolchain."
373+
)))];
374+
self.gctx.shell().print_report(report, false)?;
375+
}
376+
319377
// Normalize to absolute path for consistency throughout.
320378
// See: https://github.com/rust-lang/cargo/issues/16023
321379
let dst = self.root.join("bin").into_path_unlocked();
@@ -593,6 +651,23 @@ impl<'gctx> InstallablePackage<'gctx> {
593651
}
594652
}
595653

654+
fn get_rustup_toolchain_source(&self) -> Option<RustupToolchainSource> {
655+
#[expect(
656+
clippy::disallowed_methods,
657+
reason = "config should not set `RUSTUP` environment variables"
658+
)]
659+
let source = std::env::var("RUSTUP_TOOLCHAIN_SOURCE").ok()?;
660+
let source = match source.as_str() {
661+
"default" => RustupToolchainSource::Default,
662+
"env" => RustupToolchainSource::Environment,
663+
"cli" => RustupToolchainSource::CommandLine,
664+
"path-override" => RustupToolchainSource::OverrideDB,
665+
"toolchain-file" => RustupToolchainSource::ToolchainFile,
666+
other => RustupToolchainSource::Other(other.to_owned()),
667+
};
668+
Some(source)
669+
}
670+
596671
fn check_yanked_install(&self) -> CargoResult<()> {
597672
if self.ws.ignore_lock() || !self.ws.root().join("Cargo.lock").exists() {
598673
return Ok(());

tests/testsuite/rustup.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ fn real_rustc_wrapper(bin_dir: &Path, message: &str) -> PathBuf {
5959
// The toolchain rustc needs to call the real rustc. In order to do that,
6060
// it needs to restore or clear the RUSTUP environment variables so that
6161
// if rustup is installed, it will call the correct rustc.
62+
let rustup_toolchain_source_setup = match std::env::var_os("RUSTUP_TOOLCHAIN_SOURCE") {
63+
Some(t) => format!(
64+
".env(\"RUSTUP_TOOLCHAIN_SOURCE\", \"{}\")",
65+
t.into_string().unwrap()
66+
),
67+
None => String::new(),
68+
};
6269
let rustup_toolchain_setup = match std::env::var_os("RUSTUP_TOOLCHAIN") {
6370
Some(t) => format!(
6471
".env(\"RUSTUP_TOOLCHAIN\", \"{}\")",
@@ -82,6 +89,7 @@ fn real_rustc_wrapper(bin_dir: &Path, message: &str) -> PathBuf {
8289
eprintln!("{message}");
8390
let r = std::process::Command::new(env!("CARGO_RUSTUP_TEST_real_rustc"))
8491
.args(std::env::args_os().skip(1))
92+
{rustup_toolchain_source_setup}
8593
{rustup_toolchain_setup}
8694
{rustup_home_setup}
8795
.status();
@@ -390,6 +398,9 @@ fn cargo_install_with_toolchain_source_env() {
390398
[DOWNLOADING] crates ...
391399
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
392400
[INSTALLING] foo v0.0.1
401+
[WARNING] using non-default toolchain `test-toolchain` overridden by env
402+
|
403+
= [HELP] use `cargo +stable install` if you meant to use the stable toolchain.
393404
[COMPILING] foo v0.0.1
394405
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
395406
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]
@@ -410,6 +421,9 @@ fn cargo_install_with_toolchain_source_path_override() {
410421
[DOWNLOADING] crates ...
411422
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
412423
[INSTALLING] foo v0.0.1
424+
[WARNING] using non-default toolchain `test-toolchain` overridden by path-override
425+
|
426+
= [HELP] use `cargo +stable install` if you meant to use the stable toolchain.
413427
[COMPILING] foo v0.0.1
414428
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
415429
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]
@@ -430,6 +444,9 @@ fn cargo_install_with_toolchain_source_toolchain_file() {
430444
[DOWNLOADING] crates ...
431445
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
432446
[INSTALLING] foo v0.0.1
447+
[WARNING] using non-default toolchain `test-toolchain` overridden by toolchain-file
448+
|
449+
= [HELP] use `cargo +stable install` if you meant to use the stable toolchain.
433450
[COMPILING] foo v0.0.1
434451
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
435452
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]
@@ -450,6 +467,9 @@ fn cargo_install_with_toolchain_source_unrecognized() {
450467
[DOWNLOADING] crates ...
451468
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
452469
[INSTALLING] foo v0.0.1
470+
[WARNING] using non-default toolchain `test-toolchain` overridden by unrecognized
471+
|
472+
= [HELP] use `cargo +stable install` if you meant to use the stable toolchain.
453473
[COMPILING] foo v0.0.1
454474
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
455475
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]

0 commit comments

Comments
 (0)