diff --git a/Cargo.lock b/Cargo.lock index 1c3134d195..8296509f94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1988,6 +1988,7 @@ dependencies = [ "threadpool", "tokio", "tokio-retry", + "tokio-stream", "toml", "tracing", "tracing-opentelemetry", diff --git a/Cargo.toml b/Cargo.toml index ed2b7d6e0e..e18fde42f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,6 +85,7 @@ thiserror.workspace = true threadpool = "1" tokio-retry.workspace = true tokio.workspace = true +tokio-stream.workspace = true toml = "0.8" tracing-opentelemetry = { workspace = true, optional = true } tracing-subscriber = { workspace = true, optional = true, features = ["env-filter"] } diff --git a/src/bin/rustup-init.rs b/src/bin/rustup-init.rs index 9b624a48a7..b37a20125c 100644 --- a/src/bin/rustup-init.rs +++ b/src/bin/rustup-init.rs @@ -140,7 +140,7 @@ async fn run_rustup_inner() -> Result { } Some(n) => { is_proxyable_tools(n)?; - proxy_mode::main(n).map(ExitCode::from) + proxy_mode::main(n).await.map(ExitCode::from) } None => { // Weird case. No arg0, or it's unparsable. diff --git a/src/cli/common.rs b/src/cli/common.rs index 7591fe6f5e..be0c6237ae 100644 --- a/src/cli/common.rs +++ b/src/cli/common.rs @@ -290,7 +290,7 @@ pub(crate) async fn update_all_channels( do_self_update: bool, force_update: bool, ) -> Result { - let toolchains = cfg.update_all_channels(force_update)?; + let toolchains = cfg.update_all_channels(force_update).await?; if toolchains.is_empty() { info!("no updatable toolchains installed"); diff --git a/src/cli/proxy_mode.rs b/src/cli/proxy_mode.rs index f45919b454..331d73737c 100644 --- a/src/cli/proxy_mode.rs +++ b/src/cli/proxy_mode.rs @@ -13,7 +13,7 @@ use crate::{ }; #[cfg_attr(feature = "otel", tracing::instrument)] -pub fn main(arg0: &str) -> Result { +pub async fn main(arg0: &str) -> Result { self_update::cleanup_self_updater()?; let _setup = job::setup(); @@ -40,19 +40,22 @@ pub fn main(arg0: &str) -> Result { let toolchain = toolchain .map(|t| t.resolve(&cfg.get_default_host_triple()?)) .transpose()?; - direct_proxy(&cfg, arg0, toolchain, &cmd_args) + direct_proxy(&cfg, arg0, toolchain, &cmd_args).await } #[cfg_attr(feature = "otel", tracing::instrument(skip(cfg)))] -fn direct_proxy( +async fn direct_proxy( cfg: &Cfg, arg0: &str, toolchain: Option, args: &[OsString], ) -> Result { let cmd = match toolchain { - None => cfg.create_command_for_dir(&utils::current_dir()?, arg0)?, - Some(tc) => cfg.create_command_for_toolchain(&tc, false, arg0)?, + None => { + cfg.create_command_for_dir(&utils::current_dir()?, arg0) + .await? + } + Some(tc) => cfg.create_command_for_toolchain(&tc, false, arg0).await?, }; run_command_for_dir(cmd, arg0, args) } diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 524d2f0f0e..b24c39dd50 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -541,7 +541,7 @@ pub async fn main() -> Result { info!("This is the version for the rustup toolchain manager, not the rustc compiler."); #[cfg_attr(feature = "otel", tracing::instrument)] - fn rustc_version() -> std::result::Result> { + async fn rustc_version() -> std::result::Result> { let cfg = &mut common::set_globals(false, true)?; let cwd = std::env::current_dir()?; @@ -550,12 +550,12 @@ pub async fn main() -> Result { cfg.set_toolchain_override(&ResolvableToolchainName::try_from(&t[1..])?); } - let toolchain = cfg.find_or_install_active_toolchain(&cwd)?.0; + let toolchain = cfg.find_or_install_active_toolchain(&cwd).await?.0; Ok(toolchain.rustc_version()) } - match rustc_version() { + match rustc_version().await { Ok(version) => info!("The currently active `rustc` version is `{}`", version), Err(err) => debug!("Wanted to tell you the current rustc version, too, but ran into this error: {}", err), } @@ -640,7 +640,7 @@ pub async fn main() -> Result { TargetSubcmd::List { toolchain, installed, - } => handle_epipe(target_list(cfg, toolchain, installed)), + } => handle_epipe(target_list(cfg, toolchain, installed).await), TargetSubcmd::Add { target, toolchain } => target_add(cfg, target, toolchain).await, TargetSubcmd::Remove { target, toolchain } => { target_remove(cfg, target, toolchain).await @@ -650,7 +650,7 @@ pub async fn main() -> Result { ComponentSubcmd::List { toolchain, installed, - } => handle_epipe(component_list(cfg, toolchain, installed)), + } => handle_epipe(component_list(cfg, toolchain, installed).await), ComponentSubcmd::Add { component, toolchain, @@ -675,16 +675,18 @@ pub async fn main() -> Result { toolchain, command, install, - } => run(cfg, toolchain, command, install).map(ExitCode::from), - RustupSubcmd::Which { command, toolchain } => which(cfg, &command, toolchain), + } => run(cfg, toolchain, command, install) + .await + .map(ExitCode::from), + RustupSubcmd::Which { command, toolchain } => which(cfg, &command, toolchain).await, RustupSubcmd::Doc { path, toolchain, topic, page, - } => doc(cfg, path, toolchain, topic.as_deref(), &page), + } => doc(cfg, path, toolchain, topic.as_deref(), &page).await, #[cfg(not(windows))] - RustupSubcmd::Man { command, toolchain } => man(cfg, &command, toolchain), + RustupSubcmd::Man { command, toolchain } => man(cfg, &command, toolchain).await, RustupSubcmd::Self_ { subcmd } => match subcmd { SelfSubcmd::Update => self_update::update(cfg).await, SelfSubcmd::Uninstall { no_prompt } => self_update::uninstall(no_prompt), @@ -893,18 +895,20 @@ async fn update(cfg: &mut Cfg, opts: UpdateOpts) -> Result { Ok(utils::ExitCode(0)) } -fn run( +async fn run( cfg: &Cfg, toolchain: ResolvableLocalToolchainName, command: Vec, install: bool, ) -> Result { let toolchain = toolchain.resolve(&cfg.get_default_host_triple()?)?; - let cmd = cfg.create_command_for_toolchain(&toolchain, install, &command[0])?; + let cmd = cfg + .create_command_for_toolchain(&toolchain, install, &command[0]) + .await?; command::run_command_for_dir(cmd, &command[0], &command[1..]) } -fn which( +async fn which( cfg: &Cfg, binary: &str, toolchain: Option, @@ -913,7 +917,7 @@ fn which( let desc = toolchain.resolve(&cfg.get_default_host_triple()?)?; Toolchain::new(cfg, desc.into())?.binary_file(binary) } else { - cfg.which_binary(&utils::current_dir()?, binary)? + cfg.which_binary(&utils::current_dir()?, binary).await? }; utils::assert_is_file(&binary_path)?; @@ -1091,13 +1095,13 @@ fn show_rustup_home(cfg: &Cfg) -> Result { Ok(utils::ExitCode(0)) } -fn target_list( +async fn target_list( cfg: &Cfg, toolchain: Option, installed_only: bool, ) -> Result { // downcasting required because the toolchain files can name any toolchain - let distributable = DistributableToolchain::from_partial(toolchain, cfg)?; + let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?; common::list_items( distributable, |c| { @@ -1122,7 +1126,7 @@ async fn target_add( // isn't a feature yet. // list_components *and* add_component would both be inappropriate for // custom toolchains. - let distributable = DistributableToolchain::from_partial(toolchain, cfg)?; + let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?; let components = distributable.components()?; if targets.contains(&"all".to_string()) { @@ -1166,7 +1170,7 @@ async fn target_remove( targets: Vec, toolchain: Option, ) -> Result { - let distributable = DistributableToolchain::from_partial(toolchain, cfg)?; + let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?; for target in targets { let target = TargetTriple::new(target); @@ -1195,13 +1199,13 @@ async fn target_remove( Ok(utils::ExitCode(0)) } -fn component_list( +async fn component_list( cfg: &Cfg, toolchain: Option, installed_only: bool, ) -> Result { // downcasting required because the toolchain files can name any toolchain - let distributable = DistributableToolchain::from_partial(toolchain, cfg)?; + let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?; common::list_items(distributable, |c| Some(&c.name), installed_only)?; Ok(utils::ExitCode(0)) } @@ -1212,7 +1216,7 @@ async fn component_add( toolchain: Option, target: Option, ) -> Result { - let distributable = DistributableToolchain::from_partial(toolchain, cfg)?; + let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?; let target = get_target(target, &distributable); for component in &components { @@ -1238,7 +1242,7 @@ async fn component_remove( toolchain: Option, target: Option, ) -> Result { - let distributable = DistributableToolchain::from_partial(toolchain, cfg)?; + let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?; let target = get_target(target, &distributable); for component in &components { @@ -1418,14 +1422,14 @@ docs_data![ (embedded_book, "The Embedded Rust Book", "embedded-book/index.html"), ]; -fn doc( +async fn doc( cfg: &Cfg, path_only: bool, toolchain: Option, mut topic: Option<&str>, doc_page: &DocPage, ) -> Result { - let toolchain = Toolchain::from_partial(toolchain, cfg)?; + let toolchain = Toolchain::from_partial(toolchain, cfg).await?; if let Ok(distributable) = DistributableToolchain::try_from(&toolchain) { if let [_] = distributable @@ -1481,14 +1485,14 @@ fn doc( } #[cfg(not(windows))] -fn man( +async fn man( cfg: &Cfg, command: &str, toolchain: Option, ) -> Result { use crate::currentprocess::varsource::VarSource; - let toolchain = Toolchain::from_partial(toolchain, cfg)?; + let toolchain = Toolchain::from_partial(toolchain, cfg).await?; let mut path = toolchain.path().to_path_buf(); path.push("share"); path.push("man"); diff --git a/src/cli/self_update.rs b/src/cli/self_update.rs index d889ad14f9..5ae5559e70 100644 --- a/src/cli/self_update.rs +++ b/src/cli/self_update.rs @@ -458,7 +458,7 @@ pub(crate) async fn install( } } - let install_res: Result = (|| { + let install_res = || async { install_bins()?; #[cfg(unix)] @@ -478,12 +478,13 @@ pub(crate) async fn install( opts.targets, verbose, quiet, - )?; + ) + .await?; Ok(utils::ExitCode(0)) - })(); + }; - if let Err(e) = install_res { + if let Err(e) = install_res().await { report_error(&e); // On windows, where installation happens in a console @@ -839,7 +840,7 @@ pub(crate) fn install_proxies() -> Result<()> { Ok(()) } -fn maybe_install_rust( +async fn maybe_install_rust( toolchain: Option, profile_str: &str, default_host_triple: Option<&str>, @@ -869,16 +870,19 @@ fn maybe_install_rust( // - delete the partial install and start over // For now, we error. let mut toolchain = DistributableToolchain::new(&cfg, desc.clone())?; - utils::run_future(toolchain.update(components, targets, cfg.get_profile()?))? + toolchain + .update(components, targets, cfg.get_profile()?) + .await? } else { - utils::run_future(DistributableToolchain::install( + DistributableToolchain::install( &cfg, desc, components, targets, cfg.get_profile()?, true, - ))? + ) + .await? .0 }; diff --git a/src/config.rs b/src/config.rs index e92f43084f..6cc28c1437 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,7 @@ use std::sync::Arc; use anyhow::{anyhow, bail, Context, Result}; use serde::Deserialize; use thiserror::Error as ThisError; +use tokio_stream::StreamExt; use crate::{ cli::self_update::SelfUpdateMode, @@ -490,8 +491,8 @@ impl Cfg { Ok(self.update_hash_dir.join(toolchain.to_string())) } - pub(crate) fn which_binary(&self, path: &Path, binary: &str) -> Result { - let (toolchain, _) = self.find_or_install_active_toolchain(path)?; + pub(crate) async fn which_binary(&self, path: &Path, binary: &str) -> Result { + let (toolchain, _) = self.find_or_install_active_toolchain(path).await?; Ok(toolchain.binary_file(binary)) } @@ -726,16 +727,17 @@ impl Cfg { } } - pub(crate) fn find_or_install_active_toolchain( + pub(crate) async fn find_or_install_active_toolchain( &self, path: &Path, ) -> Result<(Toolchain<'_>, ActiveReason)> { - self.maybe_find_or_install_active_toolchain(path)? + self.maybe_find_or_install_active_toolchain(path) + .await? .ok_or(RustupError::ToolchainNotSelected.into()) } #[cfg_attr(feature = "otel", tracing::instrument(skip_all))] - pub(crate) fn maybe_find_or_install_active_toolchain( + pub(crate) async fn maybe_find_or_install_active_toolchain( &self, path: &Path, ) -> Result, ActiveReason)>> { @@ -756,8 +758,9 @@ impl Cfg { targets, profile, } => { - let toolchain = - self.ensure_installed(toolchain, components, targets, profile)?; + let toolchain = self + .ensure_installed(toolchain, components, targets, profile) + .await?; Ok(Some((toolchain, reason))) } }, @@ -770,7 +773,9 @@ impl Cfg { } Some(ToolchainName::Official(toolchain_desc)) => { let reason = ActiveReason::Default; - let toolchain = self.ensure_installed(toolchain_desc, vec![], vec![], None)?; + let toolchain = self + .ensure_installed(toolchain_desc, vec![], vec![], None) + .await?; Ok(Some((toolchain, reason))) } }, @@ -779,7 +784,7 @@ impl Cfg { // Returns a Toolchain matching the given ToolchainDesc, installing it and // the given components and targets if they aren't already installed. - fn ensure_installed( + async fn ensure_installed( &self, toolchain: ToolchainDesc, components: Vec, @@ -790,23 +795,22 @@ impl Cfg { let targets: Vec<_> = targets.iter().map(AsRef::as_ref).collect(); let toolchain = match DistributableToolchain::new(self, toolchain.clone()) { Err(RustupError::ToolchainNotInstalled(_)) => { - utils::run_future(DistributableToolchain::install( + DistributableToolchain::install( self, &toolchain, &components, &targets, profile.unwrap_or(Profile::Default), false, - ))? + ) + .await? .1 } Ok(mut distributable) => { if !distributable.components_exist(&components, &targets)? { - utils::run_future(distributable.update( - &components, - &targets, - profile.unwrap_or(Profile::Default), - ))?; + distributable + .update(&components, &targets, profile.unwrap_or(Profile::Default)) + .await?; } distributable } @@ -889,7 +893,7 @@ impl Cfg { }) } - pub(crate) fn update_all_channels( + pub(crate) async fn update_all_channels( &self, force_update: bool, ) -> Result)>> { @@ -898,22 +902,17 @@ impl Cfg { let profile = self.get_profile()?; // Update toolchains and collect the results - let channels = channels.map(|(desc, mut distributable)| { - let st = utils::run_future(distributable.update_extra( - &[], - &[], - profile, - force_update, - false, - )); - + let channels = tokio_stream::iter(channels).then(|(desc, mut distributable)| async move { + let st = distributable + .update_extra(&[], &[], profile, force_update, false) + .await; if let Err(ref e) = st { (self.notify_handler)(Notification::NonFatalError(e)); } (desc, st) }); - Ok(channels.collect()) + Ok(channels.collect().await) } #[cfg_attr(feature = "otel", tracing::instrument(skip_all))] @@ -932,12 +931,16 @@ impl Cfg { }) } - pub(crate) fn create_command_for_dir(&self, path: &Path, binary: &str) -> Result { - let (toolchain, _) = self.find_or_install_active_toolchain(path)?; + pub(crate) async fn create_command_for_dir( + &self, + path: &Path, + binary: &str, + ) -> Result { + let (toolchain, _) = self.find_or_install_active_toolchain(path).await?; self.create_command_for_toolchain_(toolchain, binary) } - pub(crate) fn create_command_for_toolchain( + pub(crate) async fn create_command_for_toolchain( &self, toolchain_name: &LocalToolchainName, install_if_missing: bool, @@ -948,14 +951,15 @@ impl Cfg { match DistributableToolchain::new(self, desc.clone()) { Err(RustupError::ToolchainNotInstalled(_)) => { if install_if_missing { - utils::run_future(DistributableToolchain::install( + DistributableToolchain::install( self, desc, &[], &[], self.get_profile()?, true, - ))?; + ) + .await?; } } o => { diff --git a/src/dist/manifestation/tests.rs b/src/dist/manifestation/tests.rs index 86aef3f0f2..d7f66d1a65 100644 --- a/src/dist/manifestation/tests.rs +++ b/src/dist/manifestation/tests.rs @@ -2,15 +2,16 @@ // server (mocked on the file system) #![allow(clippy::type_complexity)] -use std::cell::Cell; use std::collections::HashMap; use std::env; use std::fs; use std::path::Path; use std::str::FromStr; use std::sync::Arc; +use std::{cell::Cell, future::Future}; use anyhow::{anyhow, Result}; +use tokio::{runtime::Handle, task}; use url::Url; use rustup_macros::unit_test as test; @@ -32,6 +33,24 @@ use crate::{ const SHA256_HASH_LEN: usize = 64; +/// Temporary thunk to support asyncifying from underneath. +fn run_future(f: F) -> Result +where + F: Future>, + E: std::convert::From, +{ + match Handle::try_current() { + Ok(current) => { + // hide the asyncness for now. + task::block_in_place(|| current.block_on(f)) + } + Err(_) => { + // Make a runtime to hide the asyncness. + tokio::runtime::Runtime::new()?.block_on(f) + } + } +} + // Creates a mock dist server populated with some test data fn create_mock_dist_server( path: &Path, @@ -323,7 +342,7 @@ fn rename_component() { )]; change_channel_date(url, "nightly", "2016-02-01"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -337,7 +356,7 @@ fn rename_component() { assert!(utils::path_exists(prefix.path().join("bin/bonus"))); assert!(!utils::path_exists(prefix.path().join("bin/bobo"))); change_channel_date(url, "nightly", "2016-02-02"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -388,7 +407,7 @@ fn rename_component_new() { )]; // Install the basics from day 1 change_channel_date(url, "nightly", "2016-02-01"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -405,7 +424,7 @@ fn rename_component_new() { // Now we move to day 2, where bobo is part of the set of things we want // to have installed change_channel_date(url, "nightly", "2016-02-02"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -576,7 +595,7 @@ fn initial_install(comps: Compressions) { prefix, download_cfg, tmp_cx| { - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -615,7 +634,7 @@ fn test_uninstall() { prefix, download_cfg, tmp_cx| { - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -640,7 +659,7 @@ fn uninstall_removes_config_file() { prefix, download_cfg, tmp_cx| { - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -669,7 +688,7 @@ fn upgrade() { download_cfg, tmp_cx| { change_channel_date(url, "nightly", "2016-02-01"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -685,7 +704,7 @@ fn upgrade() { fs::read_to_string(prefix.path().join("bin/rustc")).unwrap() ); change_channel_date(url, "nightly", "2016-02-02"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -747,7 +766,7 @@ fn unavailable_component() { change_channel_date(url, "nightly", "2016-02-01"); // Update with bonus. - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -762,7 +781,7 @@ fn unavailable_component() { change_channel_date(url, "nightly", "2016-02-02"); // Update without bonus, should fail. - let err = utils::run_future(update_from_dist( + let err = run_future(update_from_dist( url, toolchain, prefix, @@ -817,7 +836,7 @@ fn unavailable_component_from_profile() { &|url, toolchain, prefix, download_cfg, tmp_cx| { change_channel_date(url, "nightly", "2016-02-01"); // Update with rustc. - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -832,7 +851,7 @@ fn unavailable_component_from_profile() { change_channel_date(url, "nightly", "2016-02-02"); // Update without rustc, should fail. - let err = utils::run_future(update_from_dist( + let err = run_future(update_from_dist( url, toolchain, prefix, @@ -859,7 +878,7 @@ fn unavailable_component_from_profile() { _ => panic!(), } - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -906,7 +925,7 @@ fn removed_component() { // Update with bonus. change_channel_date(url, "nightly", "2016-02-01"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -921,7 +940,7 @@ fn removed_component() { // Update without bonus, should fail with RequestedComponentsUnavailable change_channel_date(url, "nightly", "2016-02-02"); - let err = utils::run_future(update_from_dist( + let err = run_future(update_from_dist( url, toolchain, prefix, @@ -988,7 +1007,7 @@ fn unavailable_components_is_target() { // Update with rust-std change_channel_date(url, "nightly", "2016-02-01"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1009,7 +1028,7 @@ fn unavailable_components_is_target() { // Update without rust-std change_channel_date(url, "nightly", "2016-02-02"); - let err = utils::run_future(update_from_dist( + let err = run_future(update_from_dist( url, toolchain, prefix, @@ -1083,7 +1102,7 @@ fn unavailable_components_with_same_target() { &|url, toolchain, prefix, download_cfg, tmp_cx| { // Update with rust-std and rustc change_channel_date(url, "nightly", "2016-02-01"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1099,7 +1118,7 @@ fn unavailable_components_with_same_target() { // Update without rust-std and rustc change_channel_date(url, "nightly", "2016-02-02"); - let err = utils::run_future(update_from_dist( + let err = run_future(update_from_dist( url, toolchain, prefix, @@ -1156,7 +1175,7 @@ fn update_preserves_extensions() { ]; change_channel_date(url, "nightly", "2016-02-01"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1176,7 +1195,7 @@ fn update_preserves_extensions() { )); change_channel_date(url, "nightly", "2016-02-02"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1204,7 +1223,7 @@ fn update_makes_no_changes_for_identical_manifest() { prefix, download_cfg, tmp_cx| { - let status = utils::run_future(update_from_dist( + let status = run_future(update_from_dist( url, toolchain, prefix, @@ -1216,7 +1235,7 @@ fn update_makes_no_changes_for_identical_manifest() { )) .unwrap(); assert_eq!(status, UpdateStatus::Changed); - let status = utils::run_future(update_from_dist( + let status = run_future(update_from_dist( url, toolchain, prefix, @@ -1251,7 +1270,7 @@ fn add_extensions_for_initial_install() { ), ]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1278,7 +1297,7 @@ fn add_extensions_for_same_manifest() { prefix, download_cfg, tmp_cx| { - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1303,7 +1322,7 @@ fn add_extensions_for_same_manifest() { ), ]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1333,7 +1352,7 @@ fn add_extensions_for_upgrade() { tmp_cx| { change_channel_date(url, "nightly", "2016-02-01"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1360,7 +1379,7 @@ fn add_extensions_for_upgrade() { ), ]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1395,7 +1414,7 @@ fn add_extension_not_in_manifest() { true, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1423,7 +1442,7 @@ fn add_extension_that_is_required_component() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1452,7 +1471,7 @@ fn add_extensions_does_not_remove_other_components() { prefix, download_cfg, temp_cx| { - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1470,7 +1489,7 @@ fn add_extensions_does_not_remove_other_components() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1501,7 +1520,7 @@ fn remove_extensions_for_initial_install() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1535,7 +1554,7 @@ fn remove_extensions_for_same_manifest() { ), ]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1553,7 +1572,7 @@ fn remove_extensions_for_same_manifest() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1596,7 +1615,7 @@ fn remove_extensions_for_upgrade() { ), ]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1616,7 +1635,7 @@ fn remove_extensions_for_upgrade() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1647,7 +1666,7 @@ fn remove_extension_not_in_manifest() { tmp_cx| { change_channel_date(url, "nightly", "2016-02-01"); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1667,7 +1686,7 @@ fn remove_extension_not_in_manifest() { true, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1712,7 +1731,7 @@ fn remove_extension_not_in_manifest_but_is_already_installed() { Some(TargetTriple::new("x86_64-apple-darwin")), true, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1732,7 +1751,7 @@ fn remove_extension_not_in_manifest_but_is_already_installed() { Some(TargetTriple::new("x86_64-apple-darwin")), true, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1755,7 +1774,7 @@ fn remove_extension_that_is_required_component() { prefix, download_cfg, temp_cx| { - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1773,7 +1792,7 @@ fn remove_extension_that_is_required_component() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1795,7 +1814,7 @@ fn remove_extension_not_installed() { prefix, download_cfg, temp_cx| { - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1813,7 +1832,7 @@ fn remove_extension_not_installed() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1844,7 +1863,7 @@ fn remove_extensions_does_not_remove_other_components() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1862,7 +1881,7 @@ fn remove_extensions_does_not_remove_other_components() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1893,7 +1912,7 @@ fn add_and_remove_for_upgrade() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1919,7 +1938,7 @@ fn add_and_remove_for_upgrade() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1953,7 +1972,7 @@ fn add_and_remove() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -1977,7 +1996,7 @@ fn add_and_remove() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -2005,7 +2024,7 @@ fn add_and_remove_same_component() { prefix, download_cfg, temp_cx| { - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -2029,7 +2048,7 @@ fn add_and_remove_same_component() { false, )]; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -2054,7 +2073,7 @@ fn bad_component_hash() { let path = path.join("dist/2016-02-02/rustc-nightly-x86_64-apple-darwin.tar.gz"); utils_raw::write_file(&path, "bogus").unwrap(); - let err = utils::run_future(update_from_dist( + let err = run_future(update_from_dist( url, toolchain, prefix, @@ -2084,7 +2103,7 @@ fn unable_to_download_component() { let path = path.join("dist/2016-02-02/rustc-nightly-x86_64-apple-darwin.tar.gz"); fs::remove_file(path).unwrap(); - let err = utils::run_future(update_from_dist( + let err = run_future(update_from_dist( url, toolchain, prefix, @@ -2141,7 +2160,7 @@ fn reuse_downloaded_file() { }, }; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -2156,7 +2175,7 @@ fn reuse_downloaded_file() { allow_installation(prefix); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -2208,7 +2227,7 @@ fn checks_files_hashes_before_reuse() { }, }; - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, @@ -2255,7 +2274,7 @@ fn handle_corrupt_partial_downloads() { ) .unwrap(); - utils::run_future(update_from_dist( + run_future(update_from_dist( url, toolchain, prefix, diff --git a/src/toolchain/distributable.rs b/src/toolchain/distributable.rs index faa58736fe..63b9d34fd9 100644 --- a/src/toolchain/distributable.rs +++ b/src/toolchain/distributable.rs @@ -33,11 +33,13 @@ pub(crate) struct DistributableToolchain<'a> { } impl<'a> DistributableToolchain<'a> { - pub(crate) fn from_partial( + pub(crate) async fn from_partial( toolchain: Option, cfg: &'a Cfg, ) -> anyhow::Result { - Ok(Self::try_from(&Toolchain::from_partial(toolchain, cfg)?)?) + Ok(Self::try_from( + &Toolchain::from_partial(toolchain, cfg).await?, + )?) } pub(crate) fn new(cfg: &'a Cfg, desc: ToolchainDesc) -> Result { diff --git a/src/toolchain/toolchain.rs b/src/toolchain/toolchain.rs index a5257e65dc..6458f6100f 100644 --- a/src/toolchain/toolchain.rs +++ b/src/toolchain/toolchain.rs @@ -37,7 +37,7 @@ pub(crate) struct Toolchain<'a> { } impl<'a> Toolchain<'a> { - pub(crate) fn from_partial( + pub(crate) async fn from_partial( toolchain: Option, cfg: &'a Cfg, ) -> anyhow::Result { @@ -48,7 +48,7 @@ impl<'a> Toolchain<'a> { } None => { let cwd = utils::current_dir()?; - let (toolchain, _) = cfg.find_or_install_active_toolchain(&cwd)?; + let (toolchain, _) = cfg.find_or_install_active_toolchain(&cwd).await?; Ok(toolchain) } diff --git a/src/utils/utils.rs b/src/utils/utils.rs index ae04c0c8b2..6037442706 100644 --- a/src/utils/utils.rs +++ b/src/utils/utils.rs @@ -1,6 +1,5 @@ use std::env; use std::fs::{self, File}; -use std::future::Future; use std::io::{self, BufReader, Write}; use std::path::{Path, PathBuf}; use std::process::ExitStatus; @@ -10,8 +9,6 @@ use home::env as home; use retry::delay::{jitter, Fibonacci}; use retry::{retry, OperationResult}; use sha2::Sha256; -use tokio::runtime::Handle; -use tokio::task; use url::Url; use crate::currentprocess::{ @@ -268,24 +265,6 @@ async fn download_file_( res } -/// Temporary thunk to support asyncifying from underneath. -pub(crate) fn run_future(f: F) -> Result -where - F: Future>, - E: std::convert::From, -{ - match Handle::try_current() { - Ok(current) => { - // hide the asyncness for now. - task::block_in_place(|| current.block_on(f)) - } - Err(_) => { - // Make a runtime to hide the asyncness. - tokio::runtime::Runtime::new()?.block_on(f) - } - } -} - pub(crate) fn parse_url(url: &str) -> Result { Url::parse(url).with_context(|| format!("failed to parse url: {url}")) }