Skip to content

Commit 08e00b7

Browse files
tjkirchrami3l
authored andcommitted
rustup check: adopt no-self-update logic
The 'self update' subcommand checks whether self updates have been disabled, but 'check' did not, meaning the check output could include updates you're powerless to accept. This adds the logic from self update to check, no longer showing updates that can't be applied by rustup, along with its --no-self-update flag, in case you *are* able to update rustup but don't want to check it for updates at the moment.
1 parent ac249c8 commit 08e00b7

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/cli/rustup_mode.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ enum RustupSubcmd {
177177
},
178178

179179
/// Check for updates to Rust toolchains and rustup
180-
Check,
180+
Check {
181+
#[command(flatten)]
182+
opts: CheckOpts,
183+
},
181184

182185
/// Modify a toolchain's supported targets
183186
Target {
@@ -338,6 +341,13 @@ enum ToolchainSubcmd {
338341
},
339342
}
340343

344+
#[derive(Debug, Default, Args)]
345+
struct CheckOpts {
346+
/// Don't check for self update when running the `rustup check` command
347+
#[arg(long)]
348+
no_self_update: bool,
349+
}
350+
341351
#[derive(Debug, Default, Args)]
342352
struct UpdateOpts {
343353
#[arg(
@@ -650,7 +660,7 @@ pub async fn main(
650660
}
651661
ToolchainSubcmd::Uninstall { opts } => toolchain_remove(cfg, opts).await,
652662
},
653-
RustupSubcmd::Check => check_updates(cfg).await,
663+
RustupSubcmd::Check { opts } => check_updates(cfg, opts).await,
654664
RustupSubcmd::Default {
655665
toolchain,
656666
force_non_host,
@@ -779,7 +789,7 @@ async fn default_(
779789
Ok(utils::ExitCode(0))
780790
}
781791

782-
async fn check_updates(cfg: &Cfg<'_>) -> Result<utils::ExitCode> {
792+
async fn check_updates(cfg: &Cfg<'_>, opts: CheckOpts) -> Result<utils::ExitCode> {
783793
let mut t = cfg.process.stdout().terminal(cfg.process);
784794
let channels = cfg.list_channels()?;
785795

@@ -815,7 +825,18 @@ async fn check_updates(cfg: &Cfg<'_>) -> Result<utils::ExitCode> {
815825
}
816826
}
817827

818-
check_rustup_update(cfg.process).await?;
828+
let self_update_mode = cfg.get_self_update_mode()?;
829+
// Priority: no-self-update feature > self_update_mode > no-self-update args.
830+
// Check for update only if rustup does **not** have the no-self-update feature,
831+
// and auto-self-update is configured to **enable**
832+
// and has **no** no-self-update parameter.
833+
let self_update = !self_update::NEVER_SELF_UPDATE
834+
&& self_update_mode == SelfUpdateMode::Enable
835+
&& !opts.no_self_update;
836+
837+
if self_update {
838+
check_rustup_update(cfg.process).await?;
839+
}
819840

820841
Ok(utils::ExitCode(0))
821842
}

tests/suite/cli-ui/rustup/rustup_check_cmd_help_flag_stdout.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ stdout = """
44
...
55
Check for updates to Rust toolchains and rustup
66
7-
Usage: rustup[EXE] check
7+
Usage: rustup[EXE] check [OPTIONS]
88
99
Options:
10-
-h, --help Print help
10+
--no-self-update Don't check for self update when running the `rustup check` command
11+
-h, --help Print help
1112
"""
1213
stderr = ""

tests/suite/cli_exact.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ async fn check_updates_self() {
208208
let _dist_guard = cx.with_update_server(test_version);
209209
let current_version = env!("CARGO_PKG_VERSION");
210210

211+
// We are checking an update to rustup itself in this test.
212+
cx.config
213+
.run("rustup", ["set", "auto-self-update", "enable"], &[])
214+
.await;
215+
211216
cx.config
212217
.expect_stdout_ok(
213218
&["rustup", "check"],
@@ -224,6 +229,12 @@ async fn check_updates_self_no_change() {
224229
let current_version = env!("CARGO_PKG_VERSION");
225230
let mut cx = CliTestContext::new(Scenario::SimpleV2).await;
226231
let _dist_guard = cx.with_update_server(current_version);
232+
233+
// We are checking an update to rustup itself in this test.
234+
cx.config
235+
.run("rustup", ["set", "auto-self-update", "enable"], &[])
236+
.await;
237+
227238
cx.config
228239
.expect_stdout_ok(
229240
&["rustup", "check"],

0 commit comments

Comments
 (0)