Skip to content

Commit 63b814b

Browse files
committed
Support --toolchain in front of rustup which
1 parent a2f8fce commit 63b814b

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/cli/rustup_mode.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub fn main() -> Result<()> {
3232
let matches = cli().get_matches();
3333
let verbose = matches.is_present("verbose");
3434
let quiet = matches.is_present("quiet");
35+
let toolchain = matches.value_of("toolchain");
3536
let cfg = &common::set_globals(verbose, quiet)?;
3637

3738
if maybe_upgrade_data(cfg, &matches)? {
@@ -79,7 +80,7 @@ pub fn main() -> Result<()> {
7980
(_, _) => unreachable!(),
8081
},
8182
("run", Some(m)) => run(cfg, m)?,
82-
("which", Some(m)) => which(cfg, m)?,
83+
("which", Some(m)) => which(cfg, m, toolchain)?,
8384
("doc", Some(m)) => doc(cfg, m)?,
8485
("man", Some(m)) => man(cfg, m)?,
8586
("self", Some(c)) => match c.subcommand() {
@@ -817,16 +818,20 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
817818
process::exit(c)
818819
}
819820

820-
fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
821+
fn which(cfg: &Cfg, m: &ArgMatches<'_>, toolchain_by_root_cmd: Option<&str>) -> Result<()> {
821822
let binary = m.value_of("command").expect("");
822-
let toolchain_provided = m.is_present("toolchain");
823-
let binary_path = if toolchain_provided {
824-
let toolchain = m.value_of("toolchain").expect("");
825-
cfg.which_binary_by_toolchain(toolchain, binary)?
823+
let binary_path = if let Some(toolchain_by_root_cmd) = toolchain_by_root_cmd {
824+
cfg.which_binary_by_toolchain(toolchain_by_root_cmd, binary)?
826825
.expect("binary not found")
827826
} else {
828-
cfg.which_binary(&utils::current_dir()?, binary)?
829-
.expect("binary not found")
827+
if m.is_present("toolchain") {
828+
let toolchain = m.value_of("toolchain").expect("");
829+
cfg.which_binary_by_toolchain(toolchain, binary)?
830+
.expect("binary not found")
831+
} else {
832+
cfg.which_binary(&utils::current_dir()?, binary)?
833+
.expect("binary not found")
834+
}
830835
};
831836

832837
utils::assert_is_file(&binary_path)?;

tests/cli-misc.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,5 +903,17 @@ fn which() {
903903
&["rustup", "which", "--toolchain=custom-2", "rustc"],
904904
"/toolchains/custom-2/bin/rustc",
905905
);
906+
#[cfg(windows)]
907+
expect_stdout_ok(
908+
config,
909+
&["rustup", "--toolchain=custom-2", "which", "rustc"],
910+
"\\toolchains\\custom-2\\bin\\rustc",
911+
);
912+
#[cfg(not(windows))]
913+
expect_stdout_ok(
914+
config,
915+
&["rustup", "--toolchain=custom-2", "which", "rustc"],
916+
"/toolchains/custom-2/bin/rustc",
917+
);
906918
});
907919
}

0 commit comments

Comments
 (0)