Skip to content

Commit 8f464a5

Browse files
committed
Implement 11036 cargo changes
1 parent 6c4fa43 commit 8f464a5

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 78 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,12 +14,14 @@ 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;
2021
use itertools::Itertools;
2122
use semver::VersionReq;
2223
use tempfile::Builder as TempFileBuilder;
24+
use tracing::debug;
2325

2426
struct Transaction {
2527
bins: Vec<PathBuf>,
@@ -39,6 +41,42 @@ impl Drop for Transaction {
3941
}
4042
}
4143

44+
enum RustupToolchainSource {
45+
Default,
46+
Environment,
47+
CommandLine,
48+
OverrideDB,
49+
ToolchainFile,
50+
Other(String),
51+
}
52+
53+
#[allow(dead_code)]
54+
impl RustupToolchainSource {
55+
fn is_implicit_override(&self) -> Option<bool> {
56+
match self {
57+
Self::Default => Some(false),
58+
Self::Environment => Some(true),
59+
Self::CommandLine => Some(false),
60+
Self::OverrideDB => Some(true),
61+
Self::ToolchainFile => Some(true),
62+
Self::Other(_) => None,
63+
}
64+
}
65+
}
66+
67+
impl fmt::Display for RustupToolchainSource {
68+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69+
f.write_str(match self {
70+
Self::Default => "default",
71+
Self::Environment => "environment variable",
72+
Self::CommandLine => "command line",
73+
Self::OverrideDB => "rustup directory override",
74+
Self::ToolchainFile => "rustup toolchain file",
75+
Self::Other(other) => other,
76+
})
77+
}
78+
}
79+
4280
struct InstallablePackage<'gctx> {
4381
gctx: &'gctx GlobalContext,
4482
opts: ops::CompileOptions,
@@ -316,6 +354,31 @@ impl<'gctx> InstallablePackage<'gctx> {
316354
fn install_one(mut self, dry_run: bool) -> CargoResult<bool> {
317355
self.gctx.shell().status("Installing", &self.pkg)?;
318356

357+
if let Some(source) = get_rustup_toolchain_source()
358+
&& source.is_implicit_override().unwrap_or_else(|| {
359+
debug!("ignoring unrecognized rustup toolchain source `{source}`");
360+
false
361+
})
362+
{
363+
#[expect(clippy::disallowed_methods, reason = "consistency with rustup")]
364+
let maybe_toolchain = env::var("RUSTUP_TOOLCHAIN")
365+
.ok()
366+
.map(|toolchain| format!(" with `{toolchain}`"))
367+
.unwrap_or_default();
368+
let report = &[Level::WARNING
369+
.secondary_title(format!(
370+
"default toolchain implicitly overridden{maybe_toolchain} by {source}"
371+
))
372+
.element(Level::HELP.message(format!(
373+
"use `cargo +stable install` if you meant to use the stable toolchain"
374+
)))
375+
.element(Level::NOTE.message(format!(
376+
"rustup selects the toolchain based on the parent environment and not the \
377+
environment of the package being installed"
378+
)))];
379+
self.gctx.shell().print_report(report, false)?;
380+
}
381+
319382
// Normalize to absolute path for consistency throughout.
320383
// See: https://github.com/rust-lang/cargo/issues/16023
321384
let dst = self.root.join("bin").into_path_unlocked();
@@ -611,6 +674,20 @@ impl<'gctx> InstallablePackage<'gctx> {
611674
}
612675
}
613676

677+
fn get_rustup_toolchain_source() -> Option<RustupToolchainSource> {
678+
#[expect(clippy::disallowed_methods, reason = "consistency with rustup")]
679+
let source = std::env::var("RUSTUP_TOOLCHAIN_SOURCE").ok()?;
680+
let source = match source.as_str() {
681+
"default" => RustupToolchainSource::Default,
682+
"env" => RustupToolchainSource::Environment,
683+
"cli" => RustupToolchainSource::CommandLine,
684+
"path-override" => RustupToolchainSource::OverrideDB,
685+
"toolchain-file" => RustupToolchainSource::ToolchainFile,
686+
other => RustupToolchainSource::Other(other.to_owned()),
687+
};
688+
Some(source)
689+
}
690+
614691
fn make_warning_about_missing_features(binaries: &[&Target]) -> String {
615692
let max_targets_listed = 7;
616693
let target_features_message = binaries

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,10 @@ 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] default toolchain implicitly overridden with `test-toolchain` by environment variable
402+
|
403+
= [HELP] use `cargo +stable install` if you meant to use the stable toolchain
404+
= [NOTE] rustup selects the toolchain based on the parent environment and not the environment of the package being installed
393405
[COMPILING] foo v0.0.1
394406
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
395407
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]
@@ -410,6 +422,10 @@ fn cargo_install_with_toolchain_source_path_override() {
410422
[DOWNLOADING] crates ...
411423
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
412424
[INSTALLING] foo v0.0.1
425+
[WARNING] default toolchain implicitly overridden with `test-toolchain` by rustup directory override
426+
|
427+
= [HELP] use `cargo +stable install` if you meant to use the stable toolchain
428+
= [NOTE] rustup selects the toolchain based on the parent environment and not the environment of the package being installed
413429
[COMPILING] foo v0.0.1
414430
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
415431
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]
@@ -430,6 +446,10 @@ fn cargo_install_with_toolchain_source_toolchain_file() {
430446
[DOWNLOADING] crates ...
431447
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
432448
[INSTALLING] foo v0.0.1
449+
[WARNING] default toolchain implicitly overridden with `test-toolchain` by rustup toolchain file
450+
|
451+
= [HELP] use `cargo +stable install` if you meant to use the stable toolchain
452+
= [NOTE] rustup selects the toolchain based on the parent environment and not the environment of the package being installed
433453
[COMPILING] foo v0.0.1
434454
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
435455
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]

0 commit comments

Comments
 (0)