Skip to content

Commit 6494655

Browse files
rami3ldjc
authored andcommitted
refactor(config): make ensure_installed async
1 parent 73c6b19 commit 6494655

File tree

6 files changed

+58
-44
lines changed

6 files changed

+58
-44
lines changed

src/bin/rustup-init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async fn run_rustup_inner() -> Result<utils::ExitCode> {
140140
}
141141
Some(n) => {
142142
is_proxyable_tools(n)?;
143-
proxy_mode::main(n).map(ExitCode::from)
143+
proxy_mode::main(n).await.map(ExitCode::from)
144144
}
145145
None => {
146146
// Weird case. No arg0, or it's unparsable.

src/cli/proxy_mode.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
};
1414

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

1919
let _setup = job::setup();
@@ -40,18 +40,21 @@ pub fn main(arg0: &str) -> Result<ExitStatus> {
4040
let toolchain = toolchain
4141
.map(|t| t.resolve(&cfg.get_default_host_triple()?))
4242
.transpose()?;
43-
direct_proxy(&cfg, arg0, toolchain, &cmd_args)
43+
direct_proxy(&cfg, arg0, toolchain, &cmd_args).await
4444
}
4545

4646
#[cfg_attr(feature = "otel", tracing::instrument(skip(cfg)))]
47-
fn direct_proxy(
47+
async fn direct_proxy(
4848
cfg: &Cfg,
4949
arg0: &str,
5050
toolchain: Option<LocalToolchainName>,
5151
args: &[OsString],
5252
) -> Result<ExitStatus> {
5353
let cmd = match toolchain {
54-
None => cfg.create_command_for_dir(&utils::current_dir()?, arg0)?,
54+
None => {
55+
cfg.create_command_for_dir(&utils::current_dir()?, arg0)
56+
.await?
57+
}
5558
Some(tc) => cfg.create_command_for_toolchain(&tc, false, arg0)?,
5659
};
5760
run_command_for_dir(cmd, arg0, args)

src/cli/rustup_mode.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ pub async fn main() -> Result<utils::ExitCode> {
541541
info!("This is the version for the rustup toolchain manager, not the rustc compiler.");
542542

543543
#[cfg_attr(feature = "otel", tracing::instrument)]
544-
fn rustc_version() -> std::result::Result<String, Box<dyn std::error::Error>> {
544+
async fn rustc_version() -> std::result::Result<String, Box<dyn std::error::Error>> {
545545
let cfg = &mut common::set_globals(false, true)?;
546546
let cwd = std::env::current_dir()?;
547547

@@ -550,12 +550,12 @@ pub async fn main() -> Result<utils::ExitCode> {
550550
cfg.set_toolchain_override(&ResolvableToolchainName::try_from(&t[1..])?);
551551
}
552552

553-
let toolchain = cfg.find_or_install_active_toolchain(&cwd)?.0;
553+
let toolchain = cfg.find_or_install_active_toolchain(&cwd).await?.0;
554554

555555
Ok(toolchain.rustc_version())
556556
}
557557

558-
match rustc_version() {
558+
match rustc_version().await {
559559
Ok(version) => info!("The currently active `rustc` version is `{}`", version),
560560
Err(err) => debug!("Wanted to tell you the current rustc version, too, but ran into this error: {}", err),
561561
}
@@ -640,7 +640,7 @@ pub async fn main() -> Result<utils::ExitCode> {
640640
TargetSubcmd::List {
641641
toolchain,
642642
installed,
643-
} => handle_epipe(target_list(cfg, toolchain, installed)),
643+
} => handle_epipe(target_list(cfg, toolchain, installed).await),
644644
TargetSubcmd::Add { target, toolchain } => target_add(cfg, target, toolchain).await,
645645
TargetSubcmd::Remove { target, toolchain } => {
646646
target_remove(cfg, target, toolchain).await
@@ -650,7 +650,7 @@ pub async fn main() -> Result<utils::ExitCode> {
650650
ComponentSubcmd::List {
651651
toolchain,
652652
installed,
653-
} => handle_epipe(component_list(cfg, toolchain, installed)),
653+
} => handle_epipe(component_list(cfg, toolchain, installed).await),
654654
ComponentSubcmd::Add {
655655
component,
656656
toolchain,
@@ -676,15 +676,15 @@ pub async fn main() -> Result<utils::ExitCode> {
676676
command,
677677
install,
678678
} => run(cfg, toolchain, command, install).map(ExitCode::from),
679-
RustupSubcmd::Which { command, toolchain } => which(cfg, &command, toolchain),
679+
RustupSubcmd::Which { command, toolchain } => which(cfg, &command, toolchain).await,
680680
RustupSubcmd::Doc {
681681
path,
682682
toolchain,
683683
topic,
684684
page,
685-
} => doc(cfg, path, toolchain, topic.as_deref(), &page),
685+
} => doc(cfg, path, toolchain, topic.as_deref(), &page).await,
686686
#[cfg(not(windows))]
687-
RustupSubcmd::Man { command, toolchain } => man(cfg, &command, toolchain),
687+
RustupSubcmd::Man { command, toolchain } => man(cfg, &command, toolchain).await,
688688
RustupSubcmd::Self_ { subcmd } => match subcmd {
689689
SelfSubcmd::Update => self_update::update(cfg).await,
690690
SelfSubcmd::Uninstall { no_prompt } => self_update::uninstall(no_prompt),
@@ -904,7 +904,7 @@ fn run(
904904
command::run_command_for_dir(cmd, &command[0], &command[1..])
905905
}
906906

907-
fn which(
907+
async fn which(
908908
cfg: &Cfg,
909909
binary: &str,
910910
toolchain: Option<ResolvableToolchainName>,
@@ -913,7 +913,7 @@ fn which(
913913
let desc = toolchain.resolve(&cfg.get_default_host_triple()?)?;
914914
Toolchain::new(cfg, desc.into())?.binary_file(binary)
915915
} else {
916-
cfg.which_binary(&utils::current_dir()?, binary)?
916+
cfg.which_binary(&utils::current_dir()?, binary).await?
917917
};
918918

919919
utils::assert_is_file(&binary_path)?;
@@ -1091,13 +1091,13 @@ fn show_rustup_home(cfg: &Cfg) -> Result<utils::ExitCode> {
10911091
Ok(utils::ExitCode(0))
10921092
}
10931093

1094-
fn target_list(
1094+
async fn target_list(
10951095
cfg: &Cfg,
10961096
toolchain: Option<PartialToolchainDesc>,
10971097
installed_only: bool,
10981098
) -> Result<utils::ExitCode> {
10991099
// downcasting required because the toolchain files can name any toolchain
1100-
let distributable = DistributableToolchain::from_partial(toolchain, cfg)?;
1100+
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
11011101
common::list_items(
11021102
distributable,
11031103
|c| {
@@ -1122,7 +1122,7 @@ async fn target_add(
11221122
// isn't a feature yet.
11231123
// list_components *and* add_component would both be inappropriate for
11241124
// custom toolchains.
1125-
let distributable = DistributableToolchain::from_partial(toolchain, cfg)?;
1125+
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
11261126
let components = distributable.components()?;
11271127

11281128
if targets.contains(&"all".to_string()) {
@@ -1166,7 +1166,7 @@ async fn target_remove(
11661166
targets: Vec<String>,
11671167
toolchain: Option<PartialToolchainDesc>,
11681168
) -> Result<utils::ExitCode> {
1169-
let distributable = DistributableToolchain::from_partial(toolchain, cfg)?;
1169+
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
11701170

11711171
for target in targets {
11721172
let target = TargetTriple::new(target);
@@ -1195,13 +1195,13 @@ async fn target_remove(
11951195
Ok(utils::ExitCode(0))
11961196
}
11971197

1198-
fn component_list(
1198+
async fn component_list(
11991199
cfg: &Cfg,
12001200
toolchain: Option<PartialToolchainDesc>,
12011201
installed_only: bool,
12021202
) -> Result<utils::ExitCode> {
12031203
// downcasting required because the toolchain files can name any toolchain
1204-
let distributable = DistributableToolchain::from_partial(toolchain, cfg)?;
1204+
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
12051205
common::list_items(distributable, |c| Some(&c.name), installed_only)?;
12061206
Ok(utils::ExitCode(0))
12071207
}
@@ -1212,7 +1212,7 @@ async fn component_add(
12121212
toolchain: Option<PartialToolchainDesc>,
12131213
target: Option<String>,
12141214
) -> Result<utils::ExitCode> {
1215-
let distributable = DistributableToolchain::from_partial(toolchain, cfg)?;
1215+
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
12161216
let target = get_target(target, &distributable);
12171217

12181218
for component in &components {
@@ -1238,7 +1238,7 @@ async fn component_remove(
12381238
toolchain: Option<PartialToolchainDesc>,
12391239
target: Option<String>,
12401240
) -> Result<utils::ExitCode> {
1241-
let distributable = DistributableToolchain::from_partial(toolchain, cfg)?;
1241+
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
12421242
let target = get_target(target, &distributable);
12431243

12441244
for component in &components {
@@ -1418,14 +1418,14 @@ docs_data![
14181418
(embedded_book, "The Embedded Rust Book", "embedded-book/index.html"),
14191419
];
14201420

1421-
fn doc(
1421+
async fn doc(
14221422
cfg: &Cfg,
14231423
path_only: bool,
14241424
toolchain: Option<PartialToolchainDesc>,
14251425
mut topic: Option<&str>,
14261426
doc_page: &DocPage,
14271427
) -> Result<utils::ExitCode> {
1428-
let toolchain = Toolchain::from_partial(toolchain, cfg)?;
1428+
let toolchain = Toolchain::from_partial(toolchain, cfg).await?;
14291429

14301430
if let Ok(distributable) = DistributableToolchain::try_from(&toolchain) {
14311431
if let [_] = distributable
@@ -1481,14 +1481,14 @@ fn doc(
14811481
}
14821482

14831483
#[cfg(not(windows))]
1484-
fn man(
1484+
async fn man(
14851485
cfg: &Cfg,
14861486
command: &str,
14871487
toolchain: Option<PartialToolchainDesc>,
14881488
) -> Result<utils::ExitCode> {
14891489
use crate::currentprocess::varsource::VarSource;
14901490

1491-
let toolchain = Toolchain::from_partial(toolchain, cfg)?;
1491+
let toolchain = Toolchain::from_partial(toolchain, cfg).await?;
14921492
let mut path = toolchain.path().to_path_buf();
14931493
path.push("share");
14941494
path.push("man");

src/config.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,8 @@ impl Cfg {
490490
Ok(self.update_hash_dir.join(toolchain.to_string()))
491491
}
492492

493-
pub(crate) fn which_binary(&self, path: &Path, binary: &str) -> Result<PathBuf> {
494-
let (toolchain, _) = self.find_or_install_active_toolchain(path)?;
493+
pub(crate) async fn which_binary(&self, path: &Path, binary: &str) -> Result<PathBuf> {
494+
let (toolchain, _) = self.find_or_install_active_toolchain(path).await?;
495495
Ok(toolchain.binary_file(binary))
496496
}
497497

@@ -726,16 +726,17 @@ impl Cfg {
726726
}
727727
}
728728

729-
pub(crate) fn find_or_install_active_toolchain(
729+
pub(crate) async fn find_or_install_active_toolchain(
730730
&self,
731731
path: &Path,
732732
) -> Result<(Toolchain<'_>, ActiveReason)> {
733-
self.maybe_find_or_install_active_toolchain(path)?
733+
self.maybe_find_or_install_active_toolchain(path)
734+
.await?
734735
.ok_or(RustupError::ToolchainNotSelected.into())
735736
}
736737

737738
#[cfg_attr(feature = "otel", tracing::instrument(skip_all))]
738-
pub(crate) fn maybe_find_or_install_active_toolchain(
739+
pub(crate) async fn maybe_find_or_install_active_toolchain(
739740
&self,
740741
path: &Path,
741742
) -> Result<Option<(Toolchain<'_>, ActiveReason)>> {
@@ -756,8 +757,9 @@ impl Cfg {
756757
targets,
757758
profile,
758759
} => {
759-
let toolchain =
760-
self.ensure_installed(toolchain, components, targets, profile)?;
760+
let toolchain = self
761+
.ensure_installed(toolchain, components, targets, profile)
762+
.await?;
761763
Ok(Some((toolchain, reason)))
762764
}
763765
},
@@ -770,7 +772,9 @@ impl Cfg {
770772
}
771773
Some(ToolchainName::Official(toolchain_desc)) => {
772774
let reason = ActiveReason::Default;
773-
let toolchain = self.ensure_installed(toolchain_desc, vec![], vec![], None)?;
775+
let toolchain = self
776+
.ensure_installed(toolchain_desc, vec![], vec![], None)
777+
.await?;
774778
Ok(Some((toolchain, reason)))
775779
}
776780
},
@@ -779,7 +783,7 @@ impl Cfg {
779783

780784
// Returns a Toolchain matching the given ToolchainDesc, installing it and
781785
// the given components and targets if they aren't already installed.
782-
fn ensure_installed(
786+
async fn ensure_installed(
783787
&self,
784788
toolchain: ToolchainDesc,
785789
components: Vec<String>,
@@ -790,14 +794,15 @@ impl Cfg {
790794
let targets: Vec<_> = targets.iter().map(AsRef::as_ref).collect();
791795
let toolchain = match DistributableToolchain::new(self, toolchain.clone()) {
792796
Err(RustupError::ToolchainNotInstalled(_)) => {
793-
utils::run_future(DistributableToolchain::install(
797+
DistributableToolchain::install(
794798
self,
795799
&toolchain,
796800
&components,
797801
&targets,
798802
profile.unwrap_or(Profile::Default),
799803
false,
800-
))?
804+
)
805+
.await?
801806
.1
802807
}
803808
Ok(mut distributable) => {
@@ -932,8 +937,12 @@ impl Cfg {
932937
})
933938
}
934939

935-
pub(crate) fn create_command_for_dir(&self, path: &Path, binary: &str) -> Result<Command> {
936-
let (toolchain, _) = self.find_or_install_active_toolchain(path)?;
940+
pub(crate) async fn create_command_for_dir(
941+
&self,
942+
path: &Path,
943+
binary: &str,
944+
) -> Result<Command> {
945+
let (toolchain, _) = self.find_or_install_active_toolchain(path).await?;
937946
self.create_command_for_toolchain_(toolchain, binary)
938947
}
939948

src/toolchain/distributable.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ pub(crate) struct DistributableToolchain<'a> {
3333
}
3434

3535
impl<'a> DistributableToolchain<'a> {
36-
pub(crate) fn from_partial(
36+
pub(crate) async fn from_partial(
3737
toolchain: Option<PartialToolchainDesc>,
3838
cfg: &'a Cfg,
3939
) -> anyhow::Result<Self> {
40-
Ok(Self::try_from(&Toolchain::from_partial(toolchain, cfg)?)?)
40+
Ok(Self::try_from(
41+
&Toolchain::from_partial(toolchain, cfg).await?,
42+
)?)
4143
}
4244

4345
pub(crate) fn new(cfg: &'a Cfg, desc: ToolchainDesc) -> Result<Self, RustupError> {

src/toolchain/toolchain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(crate) struct Toolchain<'a> {
3737
}
3838

3939
impl<'a> Toolchain<'a> {
40-
pub(crate) fn from_partial(
40+
pub(crate) async fn from_partial(
4141
toolchain: Option<PartialToolchainDesc>,
4242
cfg: &'a Cfg,
4343
) -> anyhow::Result<Self> {
@@ -48,7 +48,7 @@ impl<'a> Toolchain<'a> {
4848
}
4949
None => {
5050
let cwd = utils::current_dir()?;
51-
let (toolchain, _) = cfg.find_or_install_active_toolchain(&cwd)?;
51+
let (toolchain, _) = cfg.find_or_install_active_toolchain(&cwd).await?;
5252

5353
Ok(toolchain)
5454
}

0 commit comments

Comments
 (0)