Skip to content

Commit 768f9b8

Browse files
committed
Auto merge of #1250 - SimonSapin:auto-install-overrides, r=alexcrichton
Automatically install override toolchain when missing. A typical scenario is: * I work on a repository that uses `rust-toolchain` to pin to a specific Nightly version * I run `git pull`, `rust-toolchain` has been changed to update to a new Rust version * I run `cargo build` Result before this PR (typically): rustup fails with an error like: ``` error: override toolchain 'nightly-2017-08-31' is not installed info: caused by: the toolchain file at '/home/simon/projects/servo/rust-toolchain' specifies an uninstalled toolchain ``` A better result would be to automatically install toolchains as needed. Closes #1218
2 parents c0ee150 + eae3131 commit 768f9b8

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

src/rustup/config.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,20 @@ impl Cfg {
259259
}
260260
};
261261

262-
match self.verify_toolchain(&name) {
263-
Ok(t) => {
264-
Ok(Some((t, reason)))
262+
match self.get_toolchain(&name, false) {
263+
Ok(toolchain) => {
264+
if toolchain.exists() {
265+
Ok(Some((toolchain, reason)))
266+
} else if toolchain.is_custom() {
267+
// Strip the confusing NotADirectory error and only mention that the override
268+
// toolchain is not installed.
269+
Err(Error::from(reason_err))
270+
.chain_err(|| ErrorKind::OverrideToolchainNotInstalled(name.to_string()))
271+
} else {
272+
try!(toolchain.install_from_dist());
273+
Ok(Some((toolchain, reason)))
274+
}
265275
}
266-
Err(Error(ErrorKind::Utils(::rustup_utils::ErrorKind::NotADirectory { .. }), _)) => {
267-
// Strip the confusing NotADirectory error and only mention that the override
268-
// toolchain is not installed.
269-
Err(Error::from(reason_err))
270-
.chain_err(|| ErrorKind::OverrideToolchainNotInstalled(name.to_string()))
271-
},
272276
Err(e) => {
273277
Err(e)
274278
.chain_err(|| Error::from(reason_err))

tests/cli-rustup.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ extern crate rustup_dist;
44
extern crate rustup_utils;
55
extern crate rustup_mock;
66
extern crate tempdir;
7-
extern crate regex;
87

98
use std::fs;
109
use std::env::consts::EXE_SUFFIX;
@@ -16,7 +15,6 @@ use rustup_mock::clitools::{self, Config, Scenario,
1615
expect_err,
1716
set_current_dist_date,
1817
this_host_triple};
19-
use regex::Regex;
2018

2119
macro_rules! for_host { ($s: expr) => (&format!($s, this_host_triple())) }
2220

@@ -621,15 +619,15 @@ fn show_toolchain_override_not_installed() {
621619
setup(&|config| {
622620
expect_ok(config, &["rustup", "override", "add", "nightly"]);
623621
expect_ok(config, &["rustup", "toolchain", "remove", "nightly"]);
624-
// I'm not sure this should really be erroring when the toolchain
625-
// is not installed; just capturing the behavior.
626622
let mut cmd = clitools::cmd(config, "rustup", &["show"]);
627623
clitools::env(config, &mut cmd);
628624
let out = cmd.output().unwrap();
629625
assert!(out.status.success());
630626
let stdout = String::from_utf8(out.stdout).unwrap();
627+
let stderr = String::from_utf8(out.stderr).unwrap();
631628
assert!(!stdout.contains("not a directory"));
632-
assert!(Regex::new(r"error: override toolchain 'nightly.*' is not installed, the directory override for '.*' specifies an uninstalled toolchain").unwrap().is_match(&stdout))
629+
assert!(!stdout.contains("is not installed"));
630+
assert!(stderr.contains("info: installing component 'rustc'"));
633631
});
634632
}
635633

@@ -658,11 +656,11 @@ fn show_toolchain_env_not_installed() {
658656
clitools::env(config, &mut cmd);
659657
cmd.env("RUSTUP_TOOLCHAIN", "nightly");
660658
let out = cmd.output().unwrap();
661-
// I'm not sure this should really be erroring when the toolchain
662-
// is not installed; just capturing the behavior.
663659
assert!(out.status.success());
664660
let stdout = String::from_utf8(out.stdout).unwrap();
665-
assert!(stdout.contains("override toolchain 'nightly' is not installed, the RUSTUP_TOOLCHAIN environment variable specifies an uninstalled toolchain"));
661+
let stderr = String::from_utf8(out.stderr).unwrap();
662+
assert!(!stdout.contains("is not installed"));
663+
assert!(stderr.contains("info: installing component 'rustc'"));
666664
});
667665
}
668666

tests/cli-v1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ fn remove_override_toolchain_err_handling() {
139139
expect_ok(config, &["rustup", "default", "nightly"]);
140140
expect_ok(config, &["rustup", "override", "add", "beta"]);
141141
expect_ok(config, &["rustup", "toolchain", "remove", "beta"]);
142-
expect_err(config, &["rustc"],
143-
for_host!("toolchain 'beta-{0}' is not installed"));
142+
expect_stderr_ok(config, &["rustc", "--version"], "info: installing component");
144143
});
145144
});
146145
}

tests/cli-v2.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,29 @@ fn remove_override_toolchain_err_handling() {
182182
expect_ok(config, &["rustup", "default", "nightly"]);
183183
expect_ok(config, &["rustup", "override", "add", "beta"]);
184184
expect_ok(config, &["rustup", "toolchain", "remove", "beta"]);
185-
expect_err(config, &["rustc"],
186-
for_host!("toolchain 'beta-{0}' is not installed"));
185+
expect_stderr_ok(config, &["rustc", "--version"], "info: installing component");
187186
});
188187
});
189188
}
190189

190+
#[test]
191+
fn file_override_toolchain_err_handling() {
192+
setup(&|config| {
193+
let cwd = config.current_dir();
194+
let toolchain_file = cwd.join("rust-toolchain");
195+
rustup_utils::raw::write_file(&toolchain_file, "beta").unwrap();
196+
expect_stderr_ok(config, &["rustc", "--version"], "info: installing component");
197+
});
198+
}
199+
200+
#[test]
201+
fn plus_override_toolchain_err_handling() {
202+
setup(&|config| {
203+
expect_err(config, &["rustc", "+beta"],
204+
for_host!("toolchain 'beta-{0}' is not installed"));
205+
});
206+
}
207+
191208
#[test]
192209
fn bad_sha_on_manifest() {
193210
setup(&|config| {

0 commit comments

Comments
 (0)