Skip to content

Commit 0e3dfb5

Browse files
committed
refactor(rustup-mode): extract warn_if_host_is_incompatible()
1 parent 42636d5 commit 0e3dfb5

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

src/cli/common.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ use git_testament::{git_testament, render_testament};
1515
use tracing::{debug, error, info, trace, warn};
1616

1717
use super::self_update;
18-
use crate::cli::download_tracker::DownloadTracker;
19-
use crate::dist::{
20-
manifest::ComponentStatus, notifications as dist_notifications, TargetTriple, ToolchainDesc,
18+
use crate::{
19+
cli::download_tracker::DownloadTracker,
20+
config::Cfg,
21+
dist::{
22+
manifest::ComponentStatus, notifications as dist_notifications, PartialToolchainDesc,
23+
TargetTriple, ToolchainDesc,
24+
},
25+
install::UpdateStatus,
26+
notifications::Notification,
27+
process::{terminalsource, Process},
28+
toolchain::{DistributableToolchain, LocalToolchainName, Toolchain, ToolchainName},
29+
utils::{notifications as util_notifications, notify::NotificationLevel, utils},
2130
};
22-
use crate::install::UpdateStatus;
23-
use crate::process::{terminalsource, Process};
24-
use crate::toolchain::{DistributableToolchain, LocalToolchainName, Toolchain, ToolchainName};
25-
use crate::utils::notifications as util_notifications;
26-
use crate::utils::notify::NotificationLevel;
27-
use crate::utils::utils;
28-
use crate::{config::Cfg, notifications::Notification};
2931

3032
pub(crate) const WARN_COMPLETE_PROFILE: &str = "downloading with complete profile isn't recommended unless you are a developer of the rust language";
3133

@@ -633,6 +635,25 @@ pub(crate) fn ignorable_error(
633635
}
634636
}
635637

638+
/// Warns if rustup is trying to install a toolchain that might not be
639+
/// able to run on the host system.
640+
pub(crate) fn warn_if_host_is_incompatible(
641+
process: &Process,
642+
name: &PartialToolchainDesc,
643+
force_non_host: bool,
644+
) -> Result<()> {
645+
if name.has_triple() {
646+
let host_arch = TargetTriple::from_host_or_build(process);
647+
let target_triple = name.clone().resolve(&host_arch)?.target;
648+
if !force_non_host && !host_arch.can_run(&target_triple)? {
649+
error!("DEPRECATED: future versions of rustup will require --force-non-host to install a non-host toolchain.");
650+
warn!("toolchain '{name}' may not be able to run on this system.");
651+
warn!("If you meant to build software to target that platform, perhaps try `rustup target add {target_triple}` instead?");
652+
}
653+
}
654+
Ok(())
655+
}
656+
636657
/// Warns if rustup is running under emulation, such as macOS Rosetta
637658
pub(crate) fn warn_if_host_is_emulated(process: &Process) {
638659
if TargetTriple::is_host_emulated() {

src/cli/rustup_mode.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use anyhow::{anyhow, Error, Result};
99
use clap::{builder::PossibleValue, Args, CommandFactory, Parser, Subcommand, ValueEnum};
1010
use clap_complete::Shell;
1111
use itertools::Itertools;
12-
use tracing::{error, info, trace, warn};
12+
use tracing::{info, trace, warn};
1313

1414
use crate::{
1515
cli::{
@@ -815,19 +815,7 @@ async fn update(cfg: &mut Cfg<'_>, opts: UpdateOpts) -> Result<utils::ExitCode>
815815
if !names.is_empty() {
816816
for name in names {
817817
// This needs another pass to fix it all up
818-
if name.has_triple() {
819-
let host_arch = TargetTriple::from_host_or_build(cfg.process);
820-
821-
let target_triple = name.clone().resolve(&host_arch)?.target;
822-
if !forced && !host_arch.can_run(&target_triple)? {
823-
error!("DEPRECATED: future versions of rustup will require --force-non-host to install a non-host toolchain.");
824-
warn!("toolchain '{name}' may not be able to run on this system.");
825-
warn!(
826-
"If you meant to build software to target that platform, perhaps try `rustup target add {}` instead?",
827-
target_triple.to_string()
828-
);
829-
}
830-
}
818+
common::warn_if_host_is_incompatible(cfg.process, &name, forced)?;
831819
let desc = name.resolve(&cfg.get_default_host_triple()?)?;
832820

833821
let components = opts.component.iter().map(|s| &**s).collect::<Vec<_>>();

0 commit comments

Comments
 (0)