Skip to content

Commit 9d095e2

Browse files
committed
Add rustup run --install, fix #1293
1 parent ab75525 commit 9d095e2

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/rustup-cli/proxy_mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn main() -> Result<()> {
4949
fn direct_proxy(cfg: &Cfg, arg0: &str, toolchain: Option<&str>, args: &[OsString]) -> Result<()> {
5050
let cmd = match toolchain {
5151
None => try!(cfg.create_command_for_dir(&try!(utils::current_dir()), arg0)),
52-
Some(tc) => try!(cfg.create_command_for_toolchain(tc, arg0)),
52+
Some(tc) => try!(cfg.create_command_for_toolchain(tc, false, arg0)),
5353
};
5454
Ok(try!(run_command_for_dir(cmd, arg0, args, &cfg)))
5555
}

src/rustup-cli/rustup_mode.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ pub fn cli() -> App<'static, 'static> {
277277
.about("Run a command with an environment configured for a given toolchain")
278278
.after_help(RUN_HELP)
279279
.setting(AppSettings::TrailingVarArg)
280+
.arg(Arg::with_name("install")
281+
.help("Install the requested toolchain if needed")
282+
.long("install"))
280283
.arg(Arg::with_name("toolchain")
281284
.help(TOOLCHAIN_ARG_HELP)
282285
.required(true))
@@ -482,7 +485,7 @@ fn run(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
482485
let ref toolchain = m.value_of("toolchain").expect("");
483486
let args = m.values_of("command").unwrap();
484487
let args: Vec<_> = args.collect();
485-
let cmd = try!(cfg.create_command_for_toolchain(toolchain, args[0]));
488+
let cmd = try!(cfg.create_command_for_toolchain(toolchain, m.is_present("install"), args[0]));
486489

487490
Ok(try!(command::run_command_for_dir(cmd, args[0], &args[1..], &cfg)))
488491
}

src/rustup/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,12 @@ impl Cfg {
410410
}
411411
}
412412

413-
pub fn create_command_for_toolchain(&self, toolchain: &str, binary: &str) -> Result<Command> {
413+
pub fn create_command_for_toolchain(&self, toolchain: &str, install_if_missing: bool,
414+
binary: &str) -> Result<Command> {
414415
let ref toolchain = try!(self.get_toolchain(toolchain, false));
416+
if install_if_missing && !toolchain.exists() {
417+
try!(toolchain.install_from_dist());
418+
}
415419

416420
if let Some(cmd) = try!(self.maybe_do_cargo_fallback(toolchain, binary)) {
417421
Ok(cmd)

tests/cli-misc.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,24 @@ fn rustup_failed_path_search() {
320320
});
321321
}
322322

323+
#[test]
324+
fn rustup_run_not_installed() {
325+
setup(&|config| {
326+
expect_ok(config, &["rustup", "install", "stable"]);
327+
expect_err(config, &["rustup", "run", "nightly", "rustc", "--version"],
328+
for_host!("toolchain 'nightly-{0}' is not installed"));
329+
});
330+
}
331+
332+
#[test]
333+
fn rustup_run_install() {
334+
setup(&|config| {
335+
expect_ok(config, &["rustup", "install", "stable"]);
336+
expect_stderr_ok(config, &["rustup", "run", "--install", "nightly", "cargo", "--version"],
337+
"info: installing component 'rustc'");
338+
});
339+
}
340+
323341
#[test]
324342
fn multirust_env_compat() {
325343
setup(&|config| {

0 commit comments

Comments
 (0)