Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): warn when removing the last/host target for a toolchain #3637

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ flate2 = "1"
fs_at.workspace = true
git-testament = "0.2"
home = "0.5.4"
itertools = "0.12"
libc = "0.2"
once_cell.workspace = true
opener = "0.6.0"
Expand Down
7 changes: 1 addition & 6 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,7 @@ pub(crate) fn list_components(

pub(crate) fn list_installed_components(distributable: DistributableToolchain<'_>) -> Result<()> {
let t = process().stdout();
let manifestation = distributable.get_manifestation()?;
let config = manifestation.read_config()?.unwrap_or_default();
let manifest = distributable.get_manifest()?;
let components = manifest.query_components(distributable.desc(), &config)?;

for component in components {
for component in distributable.components()? {
if component.installed {
writeln!(t.lock(), "{}", component.name)?;
}
Expand Down
23 changes: 18 additions & 5 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use clap::{
Arg, ArgAction, ArgGroup, ArgMatches, Command, ValueEnum,
};
use clap_complete::Shell;
use itertools::Itertools;

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

for target in m.get_many::<String>("target").unwrap() {
let new_component = Component::new(
"rust-std".to_string(),
Some(TargetTriple::new(target)),
false,
);
let target = TargetTriple::new(target);
let default_target = cfg.get_default_host_triple()?;
if target == default_target {
warn!("after removing the default host target, proc-macros and build scripts might no longer build");
}
let has_at_most_one_target = {
let components = distributable.components()?;
// Every component target that is not `None` (wildcard).
let targets = components
.iter()
.filter_map(|c| c.installed.then(|| c.component.target.clone()).flatten());
targets.unique().at_most_one().is_ok()
};
rami3l marked this conversation as resolved.
Show resolved Hide resolved
if has_at_most_one_target {
warn!("after removing the last target, no build targets will be available");
}
let new_component = Component::new("rust-std".to_string(), Some(target), false);
distributable.remove_component(new_component)?;
}

Expand Down
7 changes: 7 additions & 0 deletions src/toolchain/distributable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ impl<'a> DistributableToolchain<'a> {
Ok(())
}

pub(crate) fn components(&self) -> anyhow::Result<Vec<crate::dist::manifest::ComponentStatus>> {
rami3l marked this conversation as resolved.
Show resolved Hide resolved
let manifestation = self.get_manifestation()?;
let config = manifestation.read_config()?.unwrap_or_default();
let manifest = self.get_manifest()?;
manifest.query_components(self.desc(), &config)
}

/// Are all the components installed in this distribution
pub(crate) fn components_exist(
&self,
Expand Down
26 changes: 24 additions & 2 deletions tests/suite/cli_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,31 @@ fn remove_target_again() {
#[test]
fn remove_target_host() {
setup(&|config| {
let trip = this_host_triple();
let host = this_host_triple();
config.expect_ok(&["rustup", "default", "nightly"]);
config.expect_ok(&["rustup", "target", "remove", &trip]);
config.expect_ok(&["rustup", "target", "add", clitools::CROSS_ARCH1]);
config.expect_stderr_ok(
&["rustup", "target", "remove", &host],
"after removing the default host target, proc-macros and build scripts might no longer build",
);
let path = format!("toolchains/nightly-{host}/lib/rustlib/{host}/lib/libstd.rlib");
assert!(!config.rustupdir.has(path));
let path = format!("toolchains/nightly-{host}/lib/rustlib/{host}/lib");
assert!(!config.rustupdir.has(path));
let path = format!("toolchains/nightly-{host}/lib/rustlib/{host}");
assert!(!config.rustupdir.has(path));
});
}

#[test]
fn remove_target_last() {
setup(&|config| {
let host = this_host_triple();
config.expect_ok(&["rustup", "default", "nightly"]);
config.expect_stderr_ok(
&["rustup", "target", "remove", &host],
"after removing the last target, no build targets will be available",
);
});
}

Expand Down