From 0d82b6587a5fa75abc19412a6faaa63a27e9817a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 18 Nov 2016 11:41:05 -0800 Subject: [PATCH] wip --- src/rustup-cli/proxy_mode.rs | 11 +++++++--- src/rustup-mock/src/mock_bin_src.rs | 11 +++++++++- src/rustup/command.rs | 31 ++++++++++++++--------------- src/rustup/config.rs | 4 ++++ src/rustup/env_var.rs | 1 + src/rustup/toolchain.rs | 15 +++++++++++++- tests/cli-rustup.rs | 11 ++++++++-- 7 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/rustup-cli/proxy_mode.rs b/src/rustup-cli/proxy_mode.rs index b93acab78be..8f3d54e6c93 100644 --- a/src/rustup-cli/proxy_mode.rs +++ b/src/rustup-cli/proxy_mode.rs @@ -34,13 +34,18 @@ pub fn main() -> Result<()> { // Build command args now while we know whether or not to skip arg 1. let cmd_args: Vec<_> = if toolchain.is_none() { - env::args_os().collect() + env::args_os().skip(1).collect() } else { - env::args_os().take(1).chain(env::args_os().skip(2)).collect() + env::args_os().skip(2).collect() }; let cfg = try!(set_globals(false)); try!(cfg.check_metadata_version()); + println!("PPATH {}", ::std::env::var("PATH").unwrap_or("X".to_string())); + println!("PRUSTUP_TOOLCHAIN {}", ::std::env::var("RUSTUP_TOOLCHAIN").unwrap_or("X".to_string())); + println!("PCARGO_HOME {}", ::std::env::var("CARGO_HOME").unwrap_or("X".to_string())); + println!("arg0 {}", arg0); + println!("args[0] {}", cmd_args[0].to_string_lossy()); try!(direct_proxy(&cfg, arg0, toolchain, &cmd_args)); Ok(()) @@ -51,6 +56,6 @@ fn direct_proxy(cfg: &Cfg, arg0: &str, toolchain: Option<&str>, args: &[OsString None => try!(cfg.create_command_for_dir(&try!(utils::current_dir()), arg0)), Some(tc) => try!(cfg.create_command_for_toolchain(tc, arg0)), }; - Ok(try!(run_command_for_dir(cmd, &args, &cfg))) + Ok(try!(run_command_for_dir(cmd, arg0, args, &cfg))) } diff --git a/src/rustup-mock/src/mock_bin_src.rs b/src/rustup-mock/src/mock_bin_src.rs index 2475cf566c7..a292d7e79e5 100644 --- a/src/rustup-mock/src/mock_bin_src.rs +++ b/src/rustup-mock/src/mock_bin_src.rs @@ -1,7 +1,15 @@ use std::process::Command; use std::io::{self, BufWriter, Write}; +use std::env::consts::EXE_SUFFIX; fn main() { + let args: Vec<_> = ::std::env::args().collect(); + let env = ::std::env::var("PATH").unwrap(); + println!("mock-bin self-exe {}", ::std::env::current_exe().unwrap().display()); + println!("mock-bin cwd {}", ::std::env::current_dir().unwrap().display()); + println!("mock-bin arg0 {}", args.get(0).unwrap()); + println!("mock-bin arg1 {}", args.get(1).unwrap()); + println!("mock-bin PATH {}", env); let args: Vec<_> = ::std::env::args().collect(); if args.get(1) == Some(&"--version".to_string()) { println!("%EXAMPLE_VERSION% (%EXAMPLE_VERSION_HASH%)"); @@ -18,7 +26,8 @@ fn main() { // Used by the fallback_cargo_calls_correct_rustc test. Tests that // the environment has been set up right such that invoking rustc // will actually invoke the wrapper - Command::new("rustc").arg("--version").status().unwrap(); + let rustc = &format!("rustc{}", EXE_SUFFIX); + Command::new(rustc).arg("--version").status().unwrap(); } else { panic!("bad mock proxy commandline"); } diff --git a/src/rustup/command.rs b/src/rustup/command.rs index 52119a26874..eea51ea5e17 100644 --- a/src/rustup/command.rs +++ b/src/rustup/command.rs @@ -1,8 +1,6 @@ -use std::env; use std::ffi::OsStr; use std::fs::File; use std::io::{self, Write, BufRead, BufReader, Seek, SeekFrom}; -use std::path::PathBuf; use std::process::{self, Command, Stdio}; use std::time::Instant; use regex::Regex; @@ -16,21 +14,19 @@ use telemetry::{Telemetry, TelemetryEvent}; pub fn run_command_for_dir>(cmd: Command, + arg0: &S, args: &[S], cfg: &Cfg) -> Result<()> { - let arg0 = env::args().next().map(PathBuf::from); - let arg0 = arg0.as_ref() - .and_then(|a| a.file_name()) - .and_then(|a| a.to_str()); - let arg0 = try!(arg0.ok_or(ErrorKind::NoExeName)); - if (arg0 == "rustc" || arg0 == "rustc.exe") && try!(cfg.telemetry_enabled()) { - return telemetry_rustc(cmd, args, cfg); + if (arg0.as_ref() == "rustc" || arg0.as_ref() == "rustc.exe") && try!(cfg.telemetry_enabled()) { + return telemetry_rustc(cmd, arg0, args, cfg); } - run_command_for_dir_without_telemetry(cmd, args) + run_command_for_dir_without_telemetry(cmd, arg0, args) } -fn telemetry_rustc>(mut cmd: Command, args: &[S], cfg: &Cfg) -> Result<()> { +fn telemetry_rustc>(mut cmd: Command, + arg0: &S, + args: &[S], cfg: &Cfg) -> Result<()> { #[cfg(unix)] fn file_as_stdio(file: &File) -> Stdio { use std::os::unix::io::{AsRawFd, FromRawFd}; @@ -45,7 +41,7 @@ fn telemetry_rustc>(mut cmd: Command, args: &[S], cfg: &Cfg) -> let now = Instant::now(); - cmd.args(&args[1..]); + cmd.args(args); let has_color_args = args.iter().any(|e| { let e = e.as_ref().to_str().unwrap_or(""); @@ -130,19 +126,22 @@ fn telemetry_rustc>(mut cmd: Command, args: &[S], cfg: &Cfg) -> }); Err(e).chain_err(|| rustup_utils::ErrorKind::RunningCommand { - name: args[0].as_ref().to_owned(), + name: arg0.as_ref().to_owned(), }) }, } } -fn run_command_for_dir_without_telemetry>(mut cmd: Command, args: &[S]) -> Result<()> { - cmd.args(&args[1..]); +fn run_command_for_dir_without_telemetry>( + mut cmd: Command, arg0: &S, args: &[S]) -> Result<()> +{ + cmd.args(&args); // FIXME rust-lang/rust#32254. It's not clear to me // when and why this is needed. cmd.stdin(process::Stdio::inherit()); + println!("ARG0 {}", arg0.as_ref().to_string_lossy()); match cmd.status() { Ok(status) => { // Ensure correct exit code is returned @@ -151,7 +150,7 @@ fn run_command_for_dir_without_telemetry>(mut cmd: Command, args } Err(e) => { Err(e).chain_err(|| rustup_utils::ErrorKind::RunningCommand { - name: args[0].as_ref().to_owned(), + name: arg0.as_ref().to_owned(), }) } } diff --git a/src/rustup/config.rs b/src/rustup/config.rs index 15b95ec2dc9..ac1be8ce741 100644 --- a/src/rustup/config.rs +++ b/src/rustup/config.rs @@ -321,11 +321,15 @@ impl Cfg { } pub fn create_command_for_toolchain(&self, toolchain: &str, binary: &str) -> Result { + println!("AA"); let ref toolchain = try!(self.get_toolchain(toolchain, false)); + println!("BB"); if let Some(cmd) = try!(self.maybe_do_cargo_fallback(toolchain, binary)) { + println!("CC"); Ok(cmd) } else { + println!("DD"); toolchain.create_command(binary) } } diff --git a/src/rustup/env_var.rs b/src/rustup/env_var.rs index 8f51fbbed7b..c35dbcc2670 100644 --- a/src/rustup/env_var.rs +++ b/src/rustup/env_var.rs @@ -25,6 +25,7 @@ pub fn prepend_path(name: &str, value: &Path, cmd: &mut Command) { parts.extend(env::split_paths(v)); } let new_value = env::join_paths(parts).unwrap_or_else(|_| OsString::from(value)); + println!("PATH {}", new_value.to_string_lossy()); cmd.env(name, new_value); } diff --git a/src/rustup/toolchain.rs b/src/rustup/toolchain.rs index 3009048ec06..a38746d5e61 100644 --- a/src/rustup/toolchain.rs +++ b/src/rustup/toolchain.rs @@ -66,7 +66,9 @@ impl<'a> Toolchain<'a> { &self.path } pub fn exists(&self) -> bool { - utils::is_directory(&self.path) + println!("exists? {}", self.path.display()); + use std::fs; + utils::is_directory(&self.path) || fs::symlink_metadata(&self.path).unwrap().file_type().is_symlink() } pub fn verify(&self) -> Result<()> { Ok(try!(utils::assert_is_directory(&self.path))) @@ -277,6 +279,7 @@ impl<'a> Toolchain<'a> { } let bin_path = &self.path.join("bin").join(binary.as_ref()); + println!("BINPATH {}", bin_path.display()); let mut cmd = Command::new(bin_path); self.set_env(&mut cmd); Ok(cmd) @@ -286,6 +289,14 @@ impl<'a> Toolchain<'a> { // to give custom toolchains access to cargo pub fn create_fallback_command>(&self, binary: T, primary_toolchain: &Toolchain) -> Result { + if !self.exists() { + return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into()); + } + if !primary_toolchain.exists() { + return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into()); + } + println!("RUNNING {}", binary.as_ref().to_string_lossy()); + println!("TOOLCHAIN {}", primary_toolchain.name); let mut cmd = try!(self.create_command(binary)); cmd.env("RUSTUP_TOOLCHAIN", &primary_toolchain.name); Ok(cmd) @@ -300,12 +311,14 @@ impl<'a> Toolchain<'a> { // multirust agree. if let Ok(cargo_home) = utils::cargo_home() { cmd.env("CARGO_HOME", &cargo_home); + println!("CCC {}", cargo_home.display()); } env_var::inc("RUST_RECURSION_COUNT", cmd); cmd.env("RUSTUP_TOOLCHAIN", &self.name); cmd.env("RUSTUP_HOME", &self.cfg.multirust_dir); + println!("RUH {}", self.cfg.multirust_dir.display()); } pub fn set_ldpath(&self, cmd: &mut Command) { diff --git a/tests/cli-rustup.rs b/tests/cli-rustup.rs index eba1e8af999..7db93433431 100644 --- a/tests/cli-rustup.rs +++ b/tests/cli-rustup.rs @@ -283,7 +283,10 @@ fn fallback_cargo_calls_correct_rustc() { let ref cargo_bin_path = config.cargodir.join("bin"); fs::create_dir_all(cargo_bin_path).unwrap(); let ref rustc_path = cargo_bin_path.join(format!("rustc{}", EXE_SUFFIX)); - fs::hard_link(rustup_path, rustc_path).unwrap(); + //fs::hard_link(rustup_path, rustc_path).unwrap(); + let ref rustcc_path = cargo_bin_path.join(format!("rustcc{}", EXE_SUFFIX)); + fs::hard_link(rustup_path, rustcc_path).unwrap(); + fs::rename(rustcc_path, rustc_path).unwrap(); // Install a custom toolchain and a nightly toolchain for the cargo fallback let path = config.customdir.join("custom-1"); @@ -295,12 +298,16 @@ fn fallback_cargo_calls_correct_rustc() { expect_stdout_ok(config, &["rustc", "--version"], "hash-c-1"); + //assert!(rustc_path.exists()); + println!("SRCHAU {}", rustup_path.display()); + // Here --call-rustc tells the mock cargo bin to exec `rustc --version`. // We should be ultimately calling the custom rustc, according to the // RUSTUP_TOOLCHAIN variable set by the original "cargo" proxy, and // interpreted by the nested "rustc" proxy. expect_stdout_ok(config, &["cargo", "--call-rustc"], - "hash-c-1"); + "hash-n-2" /* "hash-c-1" */); + ::std::process::exit(-1); }); }