Skip to content

Commit b47800c

Browse files
authored
Merge pull request #1605 from nrc/self-update
self update after updating a specific toolchain
2 parents 1dd60c4 + eaab76e commit b47800c

File tree

8 files changed

+301
-96
lines changed

8 files changed

+301
-96
lines changed

src/rustup-cli/common.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,30 +200,42 @@ fn show_channel_updates(
200200
Ok(())
201201
}
202202

203-
pub fn update_all_channels(cfg: &Cfg, self_update: bool, force_update: bool) -> Result<()> {
203+
pub fn update_all_channels(cfg: &Cfg, do_self_update: bool, force_update: bool) -> Result<()> {
204204
let toolchains = cfg.update_all_channels(force_update)?;
205205

206206
if toolchains.is_empty() {
207207
info!("no updatable toolchains installed");
208208
}
209209

210-
let setup_path = if self_update {
211-
self_update::prepare_update()?
212-
} else {
213-
None
214-
};
210+
let show_channel_updates = || {
211+
if !toolchains.is_empty() {
212+
println!("");
215213

216-
if !toolchains.is_empty() {
217-
println!("");
214+
show_channel_updates(cfg, toolchains)?;
215+
}
216+
Ok(())
217+
};
218218

219-
show_channel_updates(cfg, toolchains)?;
219+
if do_self_update {
220+
self_update(show_channel_updates)
221+
} else {
222+
show_channel_updates()
220223
}
224+
}
225+
226+
pub fn self_update<F>(before_restart: F) -> Result<()>
227+
where
228+
F: FnOnce() -> Result<()>,
229+
{
230+
let setup_path = self_update::prepare_update()?;
231+
232+
before_restart()?;
221233

222234
if let Some(ref setup_path) = setup_path {
223235
self_update::run_update(setup_path)?;
224236

225237
unreachable!(); // update exits on success
226-
} else if self_update {
238+
} else {
227239
// Try again in case we emitted "tool `{}` is already installed" last time.
228240
self_update::install_proxies()?;
229241
}

src/rustup-cli/rustup_mode.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ pub fn cli() -> App<'static, 'static> {
132132
.help(TOOLCHAIN_ARG_HELP)
133133
.required(true)
134134
.multiple(true),
135+
)
136+
.arg(
137+
Arg::with_name("no-self-update")
138+
.help("Don't perform self update when running the `rustup` command")
139+
.long("no-self-update")
140+
.takes_value(false)
141+
.hidden(true),
142+
)
143+
.arg(
144+
Arg::with_name("force")
145+
.help("Force an update, even if some components are missing")
146+
.long("force")
147+
.takes_value(false),
135148
),
136149
)
137150
.subcommand(
@@ -196,6 +209,13 @@ pub fn cli() -> App<'static, 'static> {
196209
.help(TOOLCHAIN_ARG_HELP)
197210
.required(true)
198211
.multiple(true),
212+
)
213+
.arg(
214+
Arg::with_name("no-self-update")
215+
.help("Don't perform self update when running the `rustup` command")
216+
.long("no-self-update")
217+
.takes_value(false)
218+
.hidden(true),
199219
),
200220
)
201221
.subcommand(
@@ -577,6 +597,7 @@ fn default_(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
577597
}
578598

579599
fn update(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
600+
let self_update = !m.is_present("no-self-update") && !self_update::NEVER_SELF_UPDATE;
580601
if let Some(names) = m.values_of("toolchain") {
581602
for name in names {
582603
update_bare_triple_check(cfg, name)?;
@@ -595,12 +616,11 @@ fn update(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
595616
common::show_channel_update(cfg, toolchain.name(), Ok(status))?;
596617
}
597618
}
619+
if self_update {
620+
common::self_update(|| Ok(()))?;
621+
}
598622
} else {
599-
common::update_all_channels(
600-
cfg,
601-
!m.is_present("no-self-update") && !self_update::NEVER_SELF_UPDATE,
602-
m.is_present("force"),
603-
)?;
623+
common::update_all_channels(cfg, self_update, m.is_present("force"))?;
604624
}
605625

606626
Ok(())

tests/cli-exact.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn update() {
2525
setup(&|config| {
2626
expect_ok_ex(
2727
config,
28-
&["rustup", "update", "nightly"],
28+
&["rustup", "update", "nightly", "--no-self-update"],
2929
for_host!(
3030
r"
3131
nightly-{0} installed - 1.3.0 (hash-n-2)
@@ -52,10 +52,10 @@ info: installing component 'rust-docs'
5252
#[test]
5353
fn update_again() {
5454
setup(&|config| {
55-
expect_ok(config, &["rustup", "update", "nightly"]);
55+
expect_ok(config, &["rustup", "update", "nightly", "--no-self-update"]);
5656
expect_ok_ex(
5757
config,
58-
&["rustup", "update", "nightly"],
58+
&["rustup", "update", "nightly", "--no-self-update"],
5959
for_host!(
6060
r"
6161
nightly-{0} unchanged - 1.3.0 (hash-n-2)

tests/cli-misc.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn upgrade_v2_metadata_to_v12() {
133133
&["rustc", "--version"],
134134
for_host!("toolchain 'nightly-{0}' is not installed"),
135135
);
136-
expect_ok(config, &["rustup", "update", "nightly"]);
136+
expect_ok(config, &["rustup", "update", "nightly", "--no-self-update"]);
137137
expect_stdout_ok(config, &["rustc", "--version"], "hash-n-2");
138138
});
139139
}
@@ -185,7 +185,7 @@ fn update_all_no_update_whitespace() {
185185
setup(&|config| {
186186
expect_stdout_ok(
187187
config,
188-
&["rustup", "update", "nightly"],
188+
&["rustup", "update", "nightly", "--no-self-update"],
189189
for_host!(
190190
r"
191191
nightly-{} installed - 1.3.0 (hash-n-2)
@@ -200,7 +200,7 @@ fn update_all_no_update_whitespace() {
200200
#[test]
201201
fn update_works_without_term() {
202202
setup(&|config| {
203-
let mut cmd = clitools::cmd(config, "rustup", &["update", "nightly"]);
203+
let mut cmd = clitools::cmd(config, "rustup", &["update", "nightly", "--no-self-update"]);
204204
clitools::env(config, &mut cmd);
205205
cmd.env_remove("TERM");
206206

@@ -295,13 +295,13 @@ fn custom_toolchain_cargo_fallback_proxy() {
295295
);
296296
expect_ok(config, &["rustup", "default", "mytoolchain"]);
297297

298-
expect_ok(config, &["rustup", "update", "stable"]);
298+
expect_ok(config, &["rustup", "update", "stable", "--no-self-update"]);
299299
expect_stdout_ok(config, &["cargo", "--version"], "hash-s-2");
300300

301-
expect_ok(config, &["rustup", "update", "beta"]);
301+
expect_ok(config, &["rustup", "update", "beta", "--no-self-update"]);
302302
expect_stdout_ok(config, &["cargo", "--version"], "hash-b-2");
303303

304-
expect_ok(config, &["rustup", "update", "nightly"]);
304+
expect_ok(config, &["rustup", "update", "nightly", "--no-self-update"]);
305305
expect_stdout_ok(config, &["cargo", "--version"], "hash-n-2");
306306
});
307307
}
@@ -323,21 +323,21 @@ fn custom_toolchain_cargo_fallback_run() {
323323
);
324324
expect_ok(config, &["rustup", "default", "mytoolchain"]);
325325

326-
expect_ok(config, &["rustup", "update", "stable"]);
326+
expect_ok(config, &["rustup", "update", "stable", "--no-self-update"]);
327327
expect_stdout_ok(
328328
config,
329329
&["rustup", "run", "mytoolchain", "cargo", "--version"],
330330
"hash-s-2",
331331
);
332332

333-
expect_ok(config, &["rustup", "update", "beta"]);
333+
expect_ok(config, &["rustup", "update", "beta", "--no-self-update"]);
334334
expect_stdout_ok(
335335
config,
336336
&["rustup", "run", "mytoolchain", "cargo", "--version"],
337337
"hash-b-2",
338338
);
339339

340-
expect_ok(config, &["rustup", "update", "nightly"]);
340+
expect_ok(config, &["rustup", "update", "nightly", "--no-self-update"]);
341341
expect_stdout_ok(
342342
config,
343343
&["rustup", "run", "mytoolchain", "cargo", "--version"],
@@ -395,7 +395,7 @@ fn rustup_failed_path_search() {
395395
#[test]
396396
fn rustup_run_not_installed() {
397397
setup(&|config| {
398-
expect_ok(config, &["rustup", "install", "stable"]);
398+
expect_ok(config, &["rustup", "install", "stable", "--no-self-update"]);
399399
expect_err(
400400
config,
401401
&["rustup", "run", "nightly", "rustc", "--version"],
@@ -407,7 +407,7 @@ fn rustup_run_not_installed() {
407407
#[test]
408408
fn rustup_run_install() {
409409
setup(&|config| {
410-
expect_ok(config, &["rustup", "install", "stable"]);
410+
expect_ok(config, &["rustup", "install", "stable", "--no-self-update"]);
411411
expect_stderr_ok(
412412
config,
413413
&[
@@ -426,7 +426,7 @@ fn rustup_run_install() {
426426
#[test]
427427
fn multirust_env_compat() {
428428
setup(&|config| {
429-
let mut cmd = clitools::cmd(config, "rustup", &["update", "nightly"]);
429+
let mut cmd = clitools::cmd(config, "rustup", &["update", "nightly", "--no-self-update"]);
430430
clitools::env(config, &mut cmd);
431431
cmd.env_remove("RUSTUP_HOME");
432432
cmd.env("MULTIRUST_HOME", &config.rustupdir);

0 commit comments

Comments
 (0)