Skip to content

Commit d8d131d

Browse files
committed
Move create_fallback_command to DistributableToolchain
1 parent 77d366d commit d8d131d

File tree

2 files changed

+55
-54
lines changed

2 files changed

+55
-54
lines changed

src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,8 @@ impl Cfg {
658658
for fallback in &["nightly", "beta", "stable"] {
659659
let fallback = self.get_toolchain(fallback, false)?;
660660
if fallback.exists() {
661-
let cmd = fallback.create_fallback_command("cargo", toolchain)?;
661+
let distributable = DistributableToolchain::new(&fallback)?;
662+
let cmd = distributable.create_fallback_command("cargo", toolchain)?;
662663
return Ok(Some(cmd));
663664
}
664665
}

src/toolchain.rs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -263,59 +263,6 @@ impl<'a> Toolchain<'a> {
263263
Ok(cmd)
264264
}
265265

266-
// Create a command as a fallback for another toolchain. This is used
267-
// to give custom toolchains access to cargo
268-
// Custom only. Installed only.
269-
pub fn create_fallback_command<T: AsRef<OsStr>>(
270-
&self,
271-
binary: T,
272-
primary_toolchain: &Toolchain<'_>,
273-
) -> Result<Command> {
274-
// With the hacks below this only works for cargo atm
275-
assert!(binary.as_ref() == "cargo" || binary.as_ref() == "cargo.exe");
276-
277-
if !self.exists() {
278-
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
279-
}
280-
if !primary_toolchain.exists() {
281-
return Err(ErrorKind::ToolchainNotInstalled(primary_toolchain.name.to_owned()).into());
282-
}
283-
284-
let src_file = self.path.join("bin").join(format!("cargo{}", EXE_SUFFIX));
285-
286-
// MAJOR HACKS: Copy cargo.exe to its own directory on windows before
287-
// running it. This is so that the fallback cargo, when it in turn runs
288-
// rustc.exe, will run the rustc.exe out of the PATH environment
289-
// variable, _not_ the rustc.exe sitting in the same directory as the
290-
// fallback. See the `fallback_cargo_calls_correct_rustc` test case and
291-
// PR 812.
292-
//
293-
// On Windows, spawning a process will search the running application's
294-
// directory for the exe to spawn before searching PATH, and we don't want
295-
// it to do that, because cargo's directory contains the _wrong_ rustc. See
296-
// the documentation for the lpCommandLine argument of CreateProcess.
297-
let exe_path = if cfg!(windows) {
298-
use std::fs;
299-
let fallback_dir = self.cfg.rustup_dir.join("fallback");
300-
fs::create_dir_all(&fallback_dir)
301-
.chain_err(|| "unable to create dir to hold fallback exe")?;
302-
let fallback_file = fallback_dir.join("cargo.exe");
303-
if fallback_file.exists() {
304-
fs::remove_file(&fallback_file)
305-
.chain_err(|| "unable to unlink old fallback exe")?;
306-
}
307-
fs::hard_link(&src_file, &fallback_file)
308-
.chain_err(|| "unable to hard link fallback exe")?;
309-
fallback_file
310-
} else {
311-
src_file
312-
};
313-
let mut cmd = Command::new(exe_path);
314-
primary_toolchain.set_env(&mut cmd); // set up the environment to match rustc, not cargo
315-
cmd.env("RUSTUP_TOOLCHAIN", &primary_toolchain.name);
316-
Ok(cmd)
317-
}
318-
319266
// Custom and Distributable. Installed only.
320267
fn set_env(&self, cmd: &mut Command) {
321268
self.set_ldpath(cmd);
@@ -596,6 +543,59 @@ impl<'a> DistributableToolchain<'a> {
596543
}
597544
}
598545

546+
// Create a command as a fallback for another toolchain. This is used
547+
// to give custom toolchains access to cargo
548+
// Installed only.
549+
pub fn create_fallback_command<T: AsRef<OsStr>>(
550+
&self,
551+
binary: T,
552+
primary_toolchain: &Toolchain<'_>,
553+
) -> Result<Command> {
554+
// With the hacks below this only works for cargo atm
555+
assert!(binary.as_ref() == "cargo" || binary.as_ref() == "cargo.exe");
556+
557+
if !self.0.exists() {
558+
return Err(ErrorKind::ToolchainNotInstalled(self.0.name.to_owned()).into());
559+
}
560+
if !primary_toolchain.exists() {
561+
return Err(ErrorKind::ToolchainNotInstalled(primary_toolchain.name.to_owned()).into());
562+
}
563+
564+
let src_file = self.0.path.join("bin").join(format!("cargo{}", EXE_SUFFIX));
565+
566+
// MAJOR HACKS: Copy cargo.exe to its own directory on windows before
567+
// running it. This is so that the fallback cargo, when it in turn runs
568+
// rustc.exe, will run the rustc.exe out of the PATH environment
569+
// variable, _not_ the rustc.exe sitting in the same directory as the
570+
// fallback. See the `fallback_cargo_calls_correct_rustc` test case and
571+
// PR 812.
572+
//
573+
// On Windows, spawning a process will search the running application's
574+
// directory for the exe to spawn before searching PATH, and we don't want
575+
// it to do that, because cargo's directory contains the _wrong_ rustc. See
576+
// the documentation for the lpCommandLine argument of CreateProcess.
577+
let exe_path = if cfg!(windows) {
578+
use std::fs;
579+
let fallback_dir = self.0.cfg.rustup_dir.join("fallback");
580+
fs::create_dir_all(&fallback_dir)
581+
.chain_err(|| "unable to create dir to hold fallback exe")?;
582+
let fallback_file = fallback_dir.join("cargo.exe");
583+
if fallback_file.exists() {
584+
fs::remove_file(&fallback_file)
585+
.chain_err(|| "unable to unlink old fallback exe")?;
586+
}
587+
fs::hard_link(&src_file, &fallback_file)
588+
.chain_err(|| "unable to hard link fallback exe")?;
589+
fallback_file
590+
} else {
591+
src_file
592+
};
593+
let mut cmd = Command::new(exe_path);
594+
primary_toolchain.set_env(&mut cmd); // set up the environment to match rustc, not cargo
595+
cmd.env("RUSTUP_TOOLCHAIN", &primary_toolchain.name);
596+
Ok(cmd)
597+
}
598+
599599
// Installed and not-installed?
600600
pub fn desc(&self) -> Result<ToolchainDesc> {
601601
Ok(ToolchainDesc::from_str(&self.0.name)?)

0 commit comments

Comments
 (0)