Skip to content

Simplify toolchain construction #3880

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

Merged
merged 16 commits into from
Jun 17, 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
10 changes: 4 additions & 6 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ use tracing::{debug, error, info, trace, warn};
use super::self_update;
use crate::cli::download_tracker::DownloadTracker;
use crate::currentprocess::{terminalsource, Process};
use crate::dist::{manifest::ComponentStatus, TargetTriple, ToolchainDesc};
use crate::dist::{
manifest::ComponentStatus, notifications as dist_notifications, TargetTriple, ToolchainDesc,
};
use crate::install::UpdateStatus;
use crate::toolchain::names::{LocalToolchainName, ToolchainName};
use crate::toolchain::toolchain::Toolchain;
use crate::toolchain::{DistributableToolchain, LocalToolchainName, Toolchain, ToolchainName};
use crate::utils::notifications as util_notifications;
use crate::utils::notify::NotificationLevel;
use crate::utils::utils;
use crate::{config::Cfg, notifications::Notification};
use crate::{
dist::notifications as dist_notifications, toolchain::distributable::DistributableToolchain,
};

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

Expand Down
15 changes: 2 additions & 13 deletions src/cli/proxy_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use std::{path::PathBuf, process::ExitStatus};

use anyhow::Result;

use crate::toolchain::toolchain::Toolchain;
use crate::{
cli::{common::set_globals, job, self_update},
command::run_command_for_dir,
currentprocess::Process,
toolchain::names::ResolvableLocalToolchainName,
toolchain::ResolvableLocalToolchainName,
};

#[cfg_attr(feature = "otel", tracing::instrument)]
Expand All @@ -33,16 +32,6 @@ pub async fn main(arg0: &str, current_dir: PathBuf, process: &Process) -> Result
.collect();

let cfg = set_globals(current_dir, false, true, process)?;
cfg.check_metadata_version()?;
let toolchain = toolchain
.map(|t| t.resolve(&cfg.get_default_host_triple()?))
.transpose()?;

let toolchain = match toolchain {
None => cfg.find_or_install_active_toolchain().await?.0,
Some(tc) => Toolchain::from_local(&tc, false, &cfg).await?,
};

let cmd = toolchain.command(arg0)?;
let cmd = cfg.local_toolchain(toolchain).await?.command(arg0)?;
run_command_for_dir(cmd, arg0, &cmd_args)
}
58 changes: 13 additions & 45 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ use crate::{
errors::RustupError,
install::{InstallMethod, UpdateStatus},
toolchain::{
distributable::DistributableToolchain,
names::{
CustomToolchainName, LocalToolchainName, MaybeResolvableToolchainName,
ResolvableLocalToolchainName, ResolvableToolchainName, ToolchainName,
},
toolchain::Toolchain,
CustomToolchainName, DistributableToolchain, LocalToolchainName,
MaybeResolvableToolchainName, ResolvableLocalToolchainName, ResolvableToolchainName,
Toolchain, ToolchainName,
},
utils::utils::{self, ExitCode},
};
Expand Down Expand Up @@ -540,25 +537,8 @@ pub async fn main(current_dir: PathBuf, process: &Process) -> Result<utils::Exit
Err(err) if err.kind() == DisplayVersion => {
write!(process.stdout().lock(), "{err}")?;
info!("This is the version for the rustup toolchain manager, not the rustc compiler.");

#[cfg_attr(feature = "otel", tracing::instrument)]
async fn rustc_version(
current_dir: PathBuf,
process: &Process,
) -> std::result::Result<String, Box<dyn std::error::Error>> {
let cfg = &mut common::set_globals(current_dir, false, true, process)?;

if let Some(t) = process.args().find(|x| x.starts_with('+')) {
trace!("Fetching rustc version from toolchain `{}`", t);
cfg.set_toolchain_override(&ResolvableToolchainName::try_from(&t[1..])?);
}

let toolchain = cfg.find_or_install_active_toolchain().await?.0;

Ok(toolchain.rustc_version())
}

match rustc_version(current_dir, process).await {
let mut cfg = common::set_globals(current_dir, false, true, process)?;
match cfg.active_rustc_version().await {
Ok(version) => info!("The currently active `rustc` version is `{}`", version),
Err(err) => trace!("Wanted to tell you the current rustc version, too, but ran into this error: {}", err),
}
Expand All @@ -583,14 +563,13 @@ pub async fn main(current_dir: PathBuf, process: &Process) -> Result<utils::Exit
Err(err)
}
}?;

let cfg = &mut common::set_globals(current_dir, matches.verbose, matches.quiet, process)?;

if let Some(t) = &matches.plus_toolchain {
cfg.set_toolchain_override(t);
}

cfg.check_metadata_version()?;

let Some(subcmd) = matches.subcmd else {
eprintln!("{}", Rustup::command().render_long_help());
return Ok(utils::ExitCode(1));
Expand Down Expand Up @@ -845,16 +824,13 @@ async fn update(cfg: &mut Cfg<'_>, opts: UpdateOpts) -> Result<utils::ExitCode>
let force = opts.force;
let allow_downgrade = opts.allow_downgrade;
let profile = cfg.get_profile()?;
let status = match crate::toolchain::distributable::DistributableToolchain::new(
cfg,
desc.clone(),
) {
let status = match DistributableToolchain::new(cfg, desc.clone()) {
Ok(mut d) => {
d.update_extra(&components, &targets, profile, force, allow_downgrade)
.await?
}
Err(RustupError::ToolchainNotInstalled(_)) => {
crate::toolchain::distributable::DistributableToolchain::install(
DistributableToolchain::install(
cfg,
&desc,
&components,
Expand Down Expand Up @@ -907,7 +883,7 @@ async fn run(
install: bool,
) -> Result<ExitStatus> {
let toolchain = toolchain.resolve(&cfg.get_default_host_triple()?)?;
let toolchain = Toolchain::from_local(&toolchain, install, cfg).await?;
let toolchain = Toolchain::from_local(toolchain, install, cfg).await?;
let cmd = toolchain.command(&command[0])?;
command::run_command_for_dir(cmd, &command[0], &command[1..])
}
Expand All @@ -917,13 +893,7 @@ async fn which(
binary: &str,
toolchain: Option<ResolvableToolchainName>,
) -> Result<utils::ExitCode> {
let binary_path = if let Some(toolchain) = toolchain {
let desc = toolchain.resolve(&cfg.get_default_host_triple()?)?;
Toolchain::new(cfg, desc.into())?.binary_file(binary)
} else {
let (toolchain, _) = cfg.find_or_install_active_toolchain().await?;
toolchain.binary_file(binary)
};
let binary_path = cfg.resolve_toolchain(toolchain).await?.binary_file(binary);

utils::assert_is_file(&binary_path)?;

Expand Down Expand Up @@ -1446,7 +1416,7 @@ async fn doc(
mut topic: Option<&str>,
doc_page: &DocPage,
) -> Result<utils::ExitCode> {
let toolchain = Toolchain::from_partial(toolchain, cfg).await?;
let toolchain = cfg.toolchain_from_partial(toolchain).await?;

if let Ok(distributable) = DistributableToolchain::try_from(&toolchain) {
if let [_] = distributable
Expand Down Expand Up @@ -1507,10 +1477,8 @@ async fn man(
command: &str,
toolchain: Option<PartialToolchainDesc>,
) -> Result<utils::ExitCode> {
let toolchain = Toolchain::from_partial(toolchain, cfg).await?;
let mut path = toolchain.path().to_path_buf();
path.push("share");
path.push("man");
let toolchain = cfg.toolchain_from_partial(toolchain).await?;
let path = toolchain.man_path();
utils::assert_is_directory(&path)?;

let mut manpaths = std::ffi::OsString::from(path);
Expand Down
5 changes: 2 additions & 3 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ use crate::{
errors::RustupError,
install::UpdateStatus,
toolchain::{
distributable::DistributableToolchain,
names::{MaybeOfficialToolchainName, ResolvableToolchainName, ToolchainName},
toolchain::Toolchain,
DistributableToolchain, MaybeOfficialToolchainName, ResolvableToolchainName, Toolchain,
ToolchainName,
},
utils::{utils, Notification},
DUP_TOOLS, TOOLS,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/setup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
currentprocess::Process,
dist::Profile,
toolchain::names::MaybeOfficialToolchainName,
toolchain::MaybeOfficialToolchainName,
utils::utils,
};

Expand Down
Loading