Skip to content

Commit

Permalink
refactor(cli/common)!: deny installing a host-incompatible toolchain …
Browse files Browse the repository at this point in the history
…w/o `--force-non-host`
  • Loading branch information
rami3l committed Sep 22, 2024
1 parent d28e306 commit 9cf01dd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
18 changes: 11 additions & 7 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
dist::{
manifest::ComponentStatus, notifications as dist_notifications, TargetTriple, ToolchainDesc,
},
errors::RustupError,
install::UpdateStatus,
notifications::Notification,
process::{terminalsource, Process},
Expand Down Expand Up @@ -626,9 +627,11 @@ pub(crate) fn ignorable_error(
}
}

/// Warns if rustup is trying to install a toolchain that might not be
/// able to run on the host system.
pub(crate) fn warn_if_host_is_incompatible(
/// Returns an error for a toolchain if both conditions are met:
/// - The toolchain has an incompatible target triple,
/// i.e. it might not be able to run on the host system.
/// - The `force_non_host` flag is set to `false`.
pub(crate) fn check_non_host_toolchain(
toolchain: String,
host_arch: &TargetTriple,
target_triple: &TargetTriple,
Expand All @@ -637,10 +640,11 @@ pub(crate) fn warn_if_host_is_incompatible(
if force_non_host || host_arch.can_run(target_triple)? {
return Ok(());
}
error!("DEPRECATED: future versions of rustup will require --force-non-host to install a non-host toolchain.");
warn!("toolchain '{toolchain}' may not be able to run on this system.");
warn!("If you meant to build software to target that platform, perhaps try `rustup target add {target_triple}` instead?");
Ok(())
Err(RustupError::ToolchainIncompatible {
toolchain,
target_triple: target_triple.clone(),
}
.into())
}

/// Warns if rustup is running under emulation, such as macOS Rosetta
Expand Down
2 changes: 1 addition & 1 deletion src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ async fn update(
if name.has_triple() {
let host_arch = TargetTriple::from_host_or_build(cfg.process);
let target_triple = name.clone().resolve(&host_arch)?.target;
common::warn_if_host_is_incompatible(
common::check_non_host_toolchain(
name.to_string(),
&host_arch,
&target_triple,
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ impl<'a> Cfg<'a> {
force_non_host: bool,
verbose: bool,
) -> Result<(UpdateStatus, Toolchain<'_>)> {
common::warn_if_host_is_incompatible(
common::check_non_host_toolchain(
toolchain.to_string(),
&TargetTriple::from_host_or_build(self.process),
&toolchain.target,
Expand Down
9 changes: 9 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ pub enum RustupError {
},
#[error("command failed: '{}'", PathBuf::from(.name).display())]
RunningCommand { name: OsString },
#[error(
"toolchain '{toolchain}' may not be able to run on this system\n\
note: to build software for that platform, try `rustup target add {target_triple}` instead\n\
note: add the `--force-non-host` flag to install the toolchain anyway"
)]
ToolchainIncompatible {
toolchain: String,
target_triple: TargetTriple,
},
#[error("toolchain '{0}' is not installable")]
ToolchainNotInstallable(String),
#[error(
Expand Down
2 changes: 1 addition & 1 deletion tests/suite/cli_misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ async fn multi_host_smoke_test() {
let mut cx = CliTestContext::new(Scenario::MultiHost).await;
let toolchain = format!("nightly-{}", clitools::MULTI_ARCH1);
cx.config
.expect_ok(&["rustup", "default", &toolchain])
.expect_ok(&["rustup", "default", &toolchain, "--force-non-host"])
.await;
cx.config
.expect_stdout_ok(&["rustc", "--version"], "xxxx-nightly-2")
Expand Down
39 changes: 23 additions & 16 deletions tests/suite/cli_rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ async fn show_multiple_targets() {
"rustup",
"default",
&format!("nightly-{}", clitools::MULTI_ARCH1),
"--force-non-host",
])
.await;
cx.config
Expand Down Expand Up @@ -801,6 +802,7 @@ async fn show_multiple_toolchains_and_targets() {
"rustup",
"default",
&format!("nightly-{}", clitools::MULTI_ARCH1),
"--force-non-host",
])
.await;
cx.config
Expand All @@ -810,6 +812,7 @@ async fn show_multiple_toolchains_and_targets() {
.expect_ok(&[
"rustup",
"update",
"--force-non-host",
&format!("stable-{}", clitools::MULTI_ARCH1),
])
.await;
Expand Down Expand Up @@ -2731,29 +2734,33 @@ async fn check_unix_settings_fallback() {
}

#[tokio::test]
async fn warn_on_unmatch_build() {
async fn deny_incompatible_toolchain_install() {
let cx = CliTestContext::new(Scenario::MultiHost).await;
let arch = clitools::MULTI_ARCH1;
cx.config.expect_stderr_ok(
&["rustup", "toolchain", "install", &format!("nightly-{arch}")],
&format!(
r"warn: toolchain 'nightly-{arch}' may not be able to run on this system.
warn: If you meant to build software to target that platform, perhaps try `rustup target add {arch}` instead?",
),
).await;
cx.config
.expect_err(
&["rustup", "toolchain", "install", &format!("nightly-{arch}")],
&format!(
"error: toolchain 'nightly-{arch}' may not be able to run on this system
note: to build software for that platform, try `rustup target add {arch}` instead",
),
)
.await;
}

#[tokio::test]
async fn warn_on_unmatch_build_default() {
async fn deny_incompatible_toolchain_default() {
let cx = CliTestContext::new(Scenario::MultiHost).await;
let arch = clitools::MULTI_ARCH1;
cx.config.expect_stderr_ok(
&["rustup", "default", &format!("nightly-{arch}")],
&format!(
r"warn: toolchain 'nightly-{arch}' may not be able to run on this system.
warn: If you meant to build software to target that platform, perhaps try `rustup target add {arch}` instead?",
),
).await;
cx.config
.expect_err(
&["rustup", "default", &format!("nightly-{arch}")],
&format!(
"error: toolchain 'nightly-{arch}' may not be able to run on this system
note: to build software for that platform, try `rustup target add {arch}` instead",
),
)
.await;
}

#[tokio::test]
Expand Down

0 comments on commit 9cf01dd

Please sign in to comment.