Skip to content

Commit 7701ec1

Browse files
authored
Merge pull request #1619 from In-line/better-error-message
Better error message for missing binary
2 parents f309967 + 2da31be commit 7701ec1

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
@@ -27,9 +27,9 @@ error_chain! {
2727
description("override toolchain is not installed")
2828
display("override toolchain '{}' is not installed", t)
2929
}
30-
BinaryNotFound(t: String, bin: String) {
30+
BinaryNotFound(bin: String, t: String, is_default: bool) {
3131
description("toolchain does not contain binary")
32-
display("'{}' is not installed for the toolchain '{}'{}", bin, t, install_msg(bin))
32+
display("'{}' is not installed for the toolchain '{}'{}", bin, t, install_msg(bin, t, *is_default))
3333
}
3434
NeedMetadataUpgrade {
3535
description("rustup's metadata is out of date. run `rustup self upgrade-data`")
@@ -74,9 +74,15 @@ error_chain! {
7474
}
7575
}
7676

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

src/rustup/toolchain.rs

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

tests/cli-misc.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ fn rustup_failed_path_search() {
374374
&config.customdir.join("custom-1").to_string_lossy(),
375375
],
376376
);
377+
378+
expect_ok(config, &["rustup", "default", "custom"]);
379+
377380
let broken = &["rustup", "run", "custom", "fake_proxy"];
378381
expect_err(
379382
config,
@@ -388,6 +391,54 @@ fn rustup_failed_path_search() {
388391
});
389392
}
390393

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

0 commit comments

Comments
 (0)