Skip to content

Commit 869f74d

Browse files
committed
rustup which return err if asking an uninstalled toolchain
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent 0e163d2 commit 869f74d

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/cli/rustup_mode.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,7 @@ fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
10461046
cfg.which_binary_by_toolchain(toolchain, binary)?
10471047
} else {
10481048
cfg.which_binary(&utils::current_dir()?, binary)?
1049-
}
1050-
.expect("binary not found");
1049+
};
10511050

10521051
utils::assert_is_file(&binary_path)?;
10531052

src/config.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,20 +478,20 @@ impl Cfg {
478478

479479
pub(crate) fn which_binary_by_toolchain(
480480
&self,
481-
toolchain: &str,
481+
toolchain_name: &str,
482482
binary: &str,
483-
) -> Result<Option<PathBuf>> {
484-
let toolchain = self.get_toolchain(toolchain, false)?;
483+
) -> Result<PathBuf> {
484+
let toolchain = self.get_toolchain(toolchain_name, false)?;
485485
if toolchain.exists() {
486-
Ok(Some(toolchain.binary_file(binary)))
486+
Ok(toolchain.binary_file(binary))
487487
} else {
488-
Ok(None)
488+
Err(RustupError::ToolchainNotInstalled(toolchain_name.to_string()).into())
489489
}
490490
}
491491

492-
pub(crate) fn which_binary(&self, path: &Path, binary: &str) -> Result<Option<PathBuf>> {
492+
pub(crate) fn which_binary(&self, path: &Path, binary: &str) -> Result<PathBuf> {
493493
let (toolchain, _) = self.find_or_install_override_toolchain_or_default(path)?;
494-
Ok(Some(toolchain.binary_file(binary)))
494+
Ok(toolchain.binary_file(binary))
495495
}
496496

497497
pub(crate) fn upgrade_data(&self) -> Result<()> {

tests/cli-misc.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,36 @@ fn which() {
860860
});
861861
}
862862

863+
#[test]
864+
fn which_asking_uninstalled_toolchain() {
865+
setup(&|config| {
866+
let path_1 = config.customdir.join("custom-1");
867+
let path_1 = path_1.to_string_lossy();
868+
expect_ok(
869+
config,
870+
&["rustup", "toolchain", "link", "custom-1", &path_1],
871+
);
872+
expect_ok(config, &["rustup", "default", "custom-1"]);
873+
#[cfg(windows)]
874+
expect_stdout_ok(
875+
config,
876+
&["rustup", "which", "rustc"],
877+
"\\toolchains\\custom-1\\bin\\rustc",
878+
);
879+
#[cfg(not(windows))]
880+
expect_stdout_ok(
881+
config,
882+
&["rustup", "which", "rustc"],
883+
"/toolchains/custom-1/bin/rustc",
884+
);
885+
expect_err(
886+
config,
887+
&["rustup", "which", "--toolchain=nightly", "rustc"],
888+
"toolchain 'nightly' is not installed",
889+
);
890+
});
891+
}
892+
863893
#[test]
864894
fn override_by_toolchain_on_the_command_line() {
865895
setup(&|config| {

0 commit comments

Comments
 (0)