Skip to content

Commit edfaf09

Browse files
committed
feat(cli): warn when removing the last/host target for a toolchain
1 parent 8231e9a commit edfaf09

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

Cargo.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ flate2 = "1"
5757
fs_at.workspace = true
5858
git-testament = "0.2"
5959
home = "0.5.4"
60+
itertools = "0.12"
6061
libc = "0.2"
6162
once_cell.workspace = true
6263
opener = "0.6.0"

src/cli/rustup_mode.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use clap::{
1010
Arg, ArgAction, ArgGroup, ArgMatches, Command, ValueEnum,
1111
};
1212
use clap_complete::Shell;
13+
use itertools::Itertools;
1314

1415
use crate::{
1516
cli::{
@@ -1334,11 +1335,23 @@ fn target_remove(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
13341335
let distributable = DistributableToolchain::try_from(&toolchain)?;
13351336

13361337
for target in m.get_many::<String>("target").unwrap() {
1337-
let new_component = Component::new(
1338-
"rust-std".to_string(),
1339-
Some(TargetTriple::new(target)),
1340-
false,
1341-
);
1338+
let target = TargetTriple::new(target);
1339+
let default_target = cfg.get_default_host_triple()?;
1340+
if target == default_target {
1341+
warn!("after removing the default host target, proc-macros and build scripts might no longer build");
1342+
}
1343+
let has_at_most_one_target = {
1344+
let components = distributable.components()?;
1345+
// Every component target that is not `None` (wildcard).
1346+
let targets = components
1347+
.iter()
1348+
.filter_map(|c| c.installed.then(|| c.component.target.clone()).flatten());
1349+
targets.unique().at_most_one().is_ok()
1350+
};
1351+
if has_at_most_one_target {
1352+
warn!("after removing the last target, no build targets will be available");
1353+
}
1354+
let new_component = Component::new("rust-std".to_string(), Some(target), false);
13421355
distributable.remove_component(new_component)?;
13431356
}
13441357

tests/suite/cli_v2.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,9 +952,31 @@ fn remove_target_again() {
952952
#[test]
953953
fn remove_target_host() {
954954
setup(&|config| {
955-
let trip = this_host_triple();
955+
let host = this_host_triple();
956956
config.expect_ok(&["rustup", "default", "nightly"]);
957-
config.expect_ok(&["rustup", "target", "remove", &trip]);
957+
config.expect_ok(&["rustup", "target", "add", clitools::CROSS_ARCH1]);
958+
config.expect_stderr_ok(
959+
&["rustup", "target", "remove", &host],
960+
"after removing the default host target, proc-macros and build scripts might no longer build",
961+
);
962+
let path = format!("toolchains/nightly-{host}/lib/rustlib/{host}/lib/libstd.rlib");
963+
assert!(!config.rustupdir.has(path));
964+
let path = format!("toolchains/nightly-{host}/lib/rustlib/{host}/lib");
965+
assert!(!config.rustupdir.has(path));
966+
let path = format!("toolchains/nightly-{host}/lib/rustlib/{host}");
967+
assert!(!config.rustupdir.has(path));
968+
});
969+
}
970+
971+
#[test]
972+
fn remove_target_last() {
973+
setup(&|config| {
974+
let host = this_host_triple();
975+
config.expect_ok(&["rustup", "default", "nightly"]);
976+
config.expect_stderr_ok(
977+
&["rustup", "target", "remove", &host],
978+
"after removing the last target, no build targets will be available",
979+
);
958980
});
959981
}
960982

0 commit comments

Comments
 (0)