Skip to content

Commit 2da31be

Browse files
committed
Better error message for missing binary
1 parent 104c45e commit 2da31be

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

src/rustup/errors.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ error_chain! {
2626
description("override toolchain is not installed")
2727
display("override toolchain '{}' is not installed", t)
2828
}
29-
BinaryNotFound(t: String, bin: String) {
29+
BinaryNotFound(bin: String, t: String, is_default: bool) {
3030
description("toolchain does not contain binary")
31-
display("'{}' is not installed for the toolchain '{}'{}", bin, t, install_msg(bin))
31+
display("'{}' is not installed for the toolchain '{}'{}", bin, t, install_msg(bin, t, *is_default))
3232
}
3333
NeedMetadataUpgrade {
3434
description("rustup's metadata is out of date. run `rustup self upgrade-data`")
@@ -73,9 +73,15 @@ error_chain! {
7373
}
7474
}
7575

76-
fn install_msg(bin: &str) -> String {
76+
fn install_msg(bin: &str, toolchain: &str, is_default: bool) -> String {
7777
match component_for_bin(bin) {
78-
Some(c) => format!("\nTo install, run `rustup component add {}`", c),
78+
Some(c) => format!("\nTo install, run `rustup component add {}{}`", c, {
79+
if is_default {
80+
String::new()
81+
} else {
82+
format!(" --toolchain {}", toolchain)
83+
}
84+
}),
7985
None => String::new(),
8086
}
8187
}

src/rustup/toolchain.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,9 @@ impl<'a> Toolchain<'a> {
344344
.unwrap_or(0);
345345
if recursion_count > env_var::RUST_RECURSION_COUNT_MAX - 1 {
346346
return Err(ErrorKind::BinaryNotFound(
347-
self.name.clone(),
348347
binary.to_string_lossy().into(),
348+
self.name.clone(),
349+
Some(&self.name) == self.cfg.get_default().ok().as_ref(),
349350
)
350351
.into());
351352
}

tests/cli-misc.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ fn rustup_failed_path_search() {
380380
&config.customdir.join("custom-1").to_string_lossy(),
381381
],
382382
);
383+
384+
expect_ok(config, &["rustup", "default", "custom"]);
385+
383386
let broken = &["rustup", "run", "custom", "fake_proxy"];
384387
expect_err(
385388
config,
@@ -394,6 +397,54 @@ fn rustup_failed_path_search() {
394397
});
395398
}
396399

400+
#[test]
401+
fn rustup_failed_path_search_toolchain() {
402+
setup(&|config| {
403+
use std::env::consts::EXE_SUFFIX;
404+
405+
let ref rustup_path = config.exedir.join(&format!("rustup{}", EXE_SUFFIX));
406+
let ref tool_path = config.exedir.join(&format!("cargo-miri{}", EXE_SUFFIX));
407+
utils::hardlink_file(rustup_path, tool_path)
408+
.expect("Failed to create fake cargo-miri for test");
409+
410+
expect_ok(
411+
config,
412+
&[
413+
"rustup",
414+
"toolchain",
415+
"link",
416+
"custom-1",
417+
&config.customdir.join("custom-1").to_string_lossy(),
418+
],
419+
);
420+
421+
expect_ok(
422+
config,
423+
&[
424+
"rustup",
425+
"toolchain",
426+
"link",
427+
"custom-2",
428+
&config.customdir.join("custom-2").to_string_lossy(),
429+
],
430+
);
431+
432+
expect_ok(config, &["rustup", "default", "custom-2"]);
433+
434+
let broken = &["rustup", "run", "custom-1", "cargo-miri"];
435+
expect_err(
436+
config,
437+
broken,
438+
"rustup component add miri --toolchain custom-1",
439+
);
440+
441+
let broken = &["rustup", "run", "custom-2", "cargo-miri"];
442+
expect_err(config, broken, "rustup component add miri");
443+
444+
// Hardlink will be automatically cleaned up by test setup code
445+
});
446+
}
447+
397448
#[test]
398449
fn rustup_run_not_installed() {
399450
setup(&|config| {

0 commit comments

Comments
 (0)