Skip to content

Commit 7509cc8

Browse files
committed
feat(config): warn user if auto-install is enabled
1 parent 283f4c9 commit 7509cc8

3 files changed

Lines changed: 75 additions & 2 deletions

File tree

src/config.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ impl<T> EnsureInstalled<T> {
141141
}
142142
}
143143

144+
impl<T: Display> EnsureInstalled<T> {
145+
fn warn_auto_install(&self, process: &Process) {
146+
// If we're already in a recursion, or we haven't just installed the active toolchain, then
147+
// don't print the warning.
148+
let recursions = process.var("RUST_RECURSION_COUNT");
149+
if recursions.is_ok_and(|it| it != "0") || !matches!(self.status, UpdateStatus::Installed) {
150+
return;
151+
}
152+
153+
warn!(
154+
"the missing active toolchain `{}` has been auto-installed",
155+
self.inner,
156+
);
157+
warn!("this might cause rustup commands to take longer time to finish than expected");
158+
info!("you may opt out with `RUSTUP_AUTO_INSTALL=0` or `rustup set auto-install disable`");
159+
}
160+
}
161+
144162
impl<T> Deref for EnsureInstalled<T> {
145163
type Target = T;
146164

@@ -544,6 +562,7 @@ impl<'a> Cfg<'a> {
544562
match self.ensure_active_toolchain(true, false).await {
545563
Ok(r) => {
546564
let (tc, source) = r;
565+
tc.warn_auto_install(self.process);
547566
Ok(Some((tc.inner, source)))
548567
}
549568
Err(e) => match e.downcast_ref::<RustupError>() {
@@ -747,8 +766,10 @@ impl<'a> Cfg<'a> {
747766
match name {
748767
Some((tc, source)) => {
749768
let install_if_missing = self.should_auto_install()?;
750-
let tc = Toolchain::from_local(tc, install_if_missing, self).await?;
751-
Ok((tc.inner, source))
769+
let EnsureInstalled { inner: tc, status } =
770+
Toolchain::from_local(tc, install_if_missing, self).await?;
771+
EnsureInstalled::new(tc.name(), status).warn_auto_install(self.process);
772+
Ok((tc, source))
752773
}
753774
None => {
754775
let (tc, source) = self

tests/suite/cli_misc.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,3 +1749,49 @@ info: falling back to "[EXTERN_PATH]"
17491749
"#]])
17501750
.is_ok();
17511751
}
1752+
1753+
#[tokio::test]
1754+
async fn warn_auto_install_on_proxy() {
1755+
let cx = CliTestContext::new(Scenario::SimpleV2).await;
1756+
cx.config
1757+
.expect_with_env(
1758+
["rustc", "--version"],
1759+
[("RUSTUP_TOOLCHAIN", "stable"), ("RUSTUP_AUTO_INSTALL", "1")],
1760+
)
1761+
.await
1762+
.with_stdout(snapbox::str![[r#"
1763+
1.1.0 (hash-stable-1.1.0)
1764+
1765+
"#]])
1766+
.with_stderr(snapbox::str![[r#"
1767+
...
1768+
warn: the missing active toolchain `stable-[HOST_TUPLE]` has been auto-installed
1769+
warn: this might cause rustup commands to take longer time to finish than expected
1770+
info: you may opt out with `RUSTUP_AUTO_INSTALL=0` or `rustup set auto-install disable`
1771+
...
1772+
"#]])
1773+
.is_ok();
1774+
}
1775+
1776+
#[tokio::test]
1777+
async fn warn_auto_install_on_rustup() {
1778+
let cx = CliTestContext::new(Scenario::SimpleV2).await;
1779+
cx.config
1780+
.expect_with_env(
1781+
["rustup", "doc", "--path"],
1782+
[("RUSTUP_TOOLCHAIN", "stable"), ("RUSTUP_AUTO_INSTALL", "1")],
1783+
)
1784+
.await
1785+
.with_stdout(snapbox::str![[r#"
1786+
[..]/toolchains/stable-[HOST_TUPLE]/share/doc/rust/html/index.html
1787+
1788+
"#]])
1789+
.with_stderr(snapbox::str![[r#"
1790+
...
1791+
warn: the missing active toolchain `stable-[HOST_TUPLE]` has been auto-installed
1792+
warn: this might cause rustup commands to take longer time to finish than expected
1793+
info: you may opt out with `RUSTUP_AUTO_INSTALL=0` or `rustup set auto-install disable`
1794+
...
1795+
"#]])
1796+
.is_ok();
1797+
}

tests/suite/cli_v2.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,9 @@ async fn remove_override_toolchain_err_handling() {
488488
info: syncing channel updates for beta-[HOST_TUPLE]
489489
info: latest update on 2015-01-02 for version 1.2.0 (hash-beta-1.2.0)
490490
info: downloading 4 components
491+
warn: the missing active toolchain `beta-[HOST_TUPLE]` has been auto-installed
492+
warn: this might cause rustup commands to take longer time to finish than expected
493+
info: you may opt out with `RUSTUP_AUTO_INSTALL=0` or `rustup set auto-install disable`
491494
492495
"#]])
493496
.is_ok();
@@ -521,6 +524,9 @@ async fn file_override_toolchain_err_handling() {
521524
info: syncing channel updates for beta-[HOST_TUPLE]
522525
info: latest update on 2015-01-02 for version 1.2.0 (hash-beta-1.2.0)
523526
info: downloading 4 components
527+
warn: the missing active toolchain `beta-[HOST_TUPLE]` has been auto-installed
528+
warn: this might cause rustup commands to take longer time to finish than expected
529+
info: you may opt out with `RUSTUP_AUTO_INSTALL=0` or `rustup set auto-install disable`
524530
525531
"#]])
526532
.is_ok();

0 commit comments

Comments
 (0)