Skip to content

refactor: restrict run_future() usages to tests #3856

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 5 commits into from
Jun 3, 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
1 change: 1 addition & 0 deletions 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 @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion src/bin/rustup-init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async fn run_rustup_inner() -> Result<utils::ExitCode> {
}
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.
Expand Down
2 changes: 1 addition & 1 deletion src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub(crate) async fn update_all_channels(
do_self_update: bool,
force_update: bool,
) -> Result<utils::ExitCode> {
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");
Expand Down
13 changes: 8 additions & 5 deletions src/cli/proxy_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
};

#[cfg_attr(feature = "otel", tracing::instrument)]
pub fn main(arg0: &str) -> Result<ExitStatus> {
pub async fn main(arg0: &str) -> Result<ExitStatus> {
self_update::cleanup_self_updater()?;

let _setup = job::setup();
Expand All @@ -40,19 +40,22 @@ pub fn main(arg0: &str) -> Result<ExitStatus> {
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<LocalToolchainName>,
args: &[OsString],
) -> Result<ExitStatus> {
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)
}
54 changes: 29 additions & 25 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ pub async fn main() -> Result<utils::ExitCode> {
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<String, Box<dyn std::error::Error>> {
async fn rustc_version() -> std::result::Result<String, Box<dyn std::error::Error>> {
let cfg = &mut common::set_globals(false, true)?;
let cwd = std::env::current_dir()?;

Expand All @@ -550,12 +550,12 @@ pub async fn main() -> Result<utils::ExitCode> {
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),
}
Expand Down Expand Up @@ -640,7 +640,7 @@ pub async fn main() -> Result<utils::ExitCode> {
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
Expand All @@ -650,7 +650,7 @@ pub async fn main() -> Result<utils::ExitCode> {
ComponentSubcmd::List {
toolchain,
installed,
} => handle_epipe(component_list(cfg, toolchain, installed)),
} => handle_epipe(component_list(cfg, toolchain, installed).await),
ComponentSubcmd::Add {
component,
toolchain,
Expand All @@ -675,16 +675,18 @@ pub async fn main() -> Result<utils::ExitCode> {
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),
Expand Down Expand Up @@ -893,18 +895,20 @@ async fn update(cfg: &mut Cfg, opts: UpdateOpts) -> Result<utils::ExitCode> {
Ok(utils::ExitCode(0))
}

fn run(
async fn run(
cfg: &Cfg,
toolchain: ResolvableLocalToolchainName,
command: Vec<String>,
install: bool,
) -> Result<ExitStatus> {
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<ResolvableToolchainName>,
Expand All @@ -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)?;
Expand Down Expand Up @@ -1091,13 +1095,13 @@ fn show_rustup_home(cfg: &Cfg) -> Result<utils::ExitCode> {
Ok(utils::ExitCode(0))
}

fn target_list(
async fn target_list(
cfg: &Cfg,
toolchain: Option<PartialToolchainDesc>,
installed_only: bool,
) -> Result<utils::ExitCode> {
// 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| {
Expand All @@ -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()) {
Expand Down Expand Up @@ -1166,7 +1170,7 @@ async fn target_remove(
targets: Vec<String>,
toolchain: Option<PartialToolchainDesc>,
) -> Result<utils::ExitCode> {
let distributable = DistributableToolchain::from_partial(toolchain, cfg)?;
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;

for target in targets {
let target = TargetTriple::new(target);
Expand Down Expand Up @@ -1195,13 +1199,13 @@ async fn target_remove(
Ok(utils::ExitCode(0))
}

fn component_list(
async fn component_list(
cfg: &Cfg,
toolchain: Option<PartialToolchainDesc>,
installed_only: bool,
) -> Result<utils::ExitCode> {
// 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))
}
Expand All @@ -1212,7 +1216,7 @@ async fn component_add(
toolchain: Option<PartialToolchainDesc>,
target: Option<String>,
) -> Result<utils::ExitCode> {
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 {
Expand All @@ -1238,7 +1242,7 @@ async fn component_remove(
toolchain: Option<PartialToolchainDesc>,
target: Option<String>,
) -> Result<utils::ExitCode> {
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 {
Expand Down Expand Up @@ -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<PartialToolchainDesc>,
mut topic: Option<&str>,
doc_page: &DocPage,
) -> Result<utils::ExitCode> {
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
Expand Down Expand Up @@ -1481,14 +1485,14 @@ fn doc(
}

#[cfg(not(windows))]
fn man(
async fn man(
cfg: &Cfg,
command: &str,
toolchain: Option<PartialToolchainDesc>,
) -> Result<utils::ExitCode> {
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");
Expand Down
20 changes: 12 additions & 8 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ pub(crate) async fn install(
}
}

let install_res: Result<utils::ExitCode> = (|| {
let install_res = || async {
install_bins()?;

#[cfg(unix)]
Expand All @@ -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
Expand Down Expand Up @@ -839,7 +840,7 @@ pub(crate) fn install_proxies() -> Result<()> {
Ok(())
}

fn maybe_install_rust(
async fn maybe_install_rust(
toolchain: Option<MaybeOfficialToolchainName>,
profile_str: &str,
default_host_triple: Option<&str>,
Expand Down Expand Up @@ -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
};

Expand Down
Loading
Loading