Skip to content

Commit f0f222f

Browse files
committed
Implement 11036 cargo changes
1 parent 126bf84 commit f0f222f

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 58 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,26 @@ impl Drop for Transaction {
3940
}
4041
}
4142

43+
enum RustupToolchainSource {
44+
Default,
45+
Environment,
46+
CommandLine,
47+
OverrideDB,
48+
ToolchainFile,
49+
}
50+
51+
impl fmt::Display for RustupToolchainSource {
52+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53+
f.write_str(match self {
54+
Self::Default => "default",
55+
Self::Environment => "env",
56+
Self::CommandLine => "cli",
57+
Self::OverrideDB => "path-override",
58+
Self::ToolchainFile => "toolchain-file",
59+
})
60+
}
61+
}
62+
4263
struct InstallablePackage<'gctx> {
4364
gctx: &'gctx GlobalContext,
4465
opts: ops::CompileOptions,
@@ -316,6 +337,28 @@ impl<'gctx> InstallablePackage<'gctx> {
316337
fn install_one(mut self, dry_run: bool) -> CargoResult<bool> {
317338
self.gctx.shell().status("Installing", &self.pkg)?;
318339

340+
if let Some(source) = self.get_rustup_toolchain_source()
341+
&& !matches!(
342+
source,
343+
RustupToolchainSource::Default | RustupToolchainSource::CommandLine
344+
)
345+
{
346+
let maybe_toolchain = self
347+
.gctx
348+
.get_env("RUSTUP_TOOLCHAIN")
349+
.ok()
350+
.map(|toolchain| format!(" `{toolchain}`"))
351+
.unwrap_or_default();
352+
let report = &[Level::WARNING
353+
.secondary_title(format!(
354+
"using non-default toolchain{maybe_toolchain} overridden by {source}"
355+
))
356+
.element(Level::HELP.message(format!(
357+
"use `cargo +stable install` if you meant to use the stable toolchain."
358+
)))];
359+
self.gctx.shell().print_report(report, false)?;
360+
}
361+
319362
// Normalize to absolute path for consistency throughout.
320363
// See: https://github.com/rust-lang/cargo/issues/16023
321364
let dst = self.root.join("bin").into_path_unlocked();
@@ -593,6 +636,20 @@ impl<'gctx> InstallablePackage<'gctx> {
593636
}
594637
}
595638

639+
fn get_rustup_toolchain_source(&self) -> Option<RustupToolchainSource> {
640+
self.gctx
641+
.get_env("RUSTUP_TOOLCHAIN_SOURCE")
642+
.ok()
643+
.and_then(|val| match val {
644+
"default" => Some(RustupToolchainSource::Default),
645+
"env" => Some(RustupToolchainSource::Environment),
646+
"cli" => Some(RustupToolchainSource::CommandLine),
647+
"path-override" => Some(RustupToolchainSource::OverrideDB),
648+
"toolchain-file" => Some(RustupToolchainSource::ToolchainFile),
649+
_ => None,
650+
})
651+
}
652+
596653
fn check_yanked_install(&self) -> CargoResult<()> {
597654
if self.ws.ignore_lock() || !self.ws.root().join("Cargo.lock").exists() {
598655
return Ok(());

tests/testsuite/rustup.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ fn cargo_install_with_non_default_toolchain() {
328328
[DOWNLOADING] crates ...
329329
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
330330
[INSTALLING] foo v0.0.1
331+
[WARNING] using non-default toolchain `test-toolchain` overridden by env
332+
|
333+
= [HELP] use `cargo +stable install` if you meant to use the stable toolchain.
331334
[COMPILING] foo v0.0.1
332335
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
333336
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]

0 commit comments

Comments
 (0)