From cc6eee09292e362d836377bbe2e36f2fd2158806 Mon Sep 17 00:00:00 2001 From: jam1garner Date: Sun, 12 Apr 2020 13:51:49 -0400 Subject: [PATCH 1/7] Allow fallback on value package.rust-src if XARGO_RUST_SRC doesn't exist --- src/lib.rs | 8 +++++++- src/rustc.rs | 6 ++++++ src/xargo.rs | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 883bb67..09d1fa2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,12 +134,18 @@ fn run(cargo_mode: XargoMode) -> Result> { // We can't build sysroot with stable or beta due to unstable features let sysroot = rustc::sysroot(verbose)?; let src = match meta.channel { - Channel::Dev => rustc::Src::from_env().ok_or( + Channel::Dev => if let Some(env) = rustc::Src::from_env() { + Some(env) + } else { + xargo::toml_src(&root)? + }.ok_or( "The XARGO_RUST_SRC env variable must be set and point to the \ Rust source directory when working with the 'dev' channel", )?, Channel::Nightly => if let Some(src) = rustc::Src::from_env() { src + } else if let Some(src) = xargo::toml_src(&root)? { + src } else { sysroot.src()? }, diff --git a/src/rustc.rs b/src/rustc.rs index 3e86816..890f9fc 100644 --- a/src/rustc.rs +++ b/src/rustc.rs @@ -108,6 +108,12 @@ impl Sysroot { } } +impl From for Src { + fn from(path: PathBuf) -> Self { + Self { path } + } +} + #[derive(Debug)] pub enum Target { Builtin { triple: String }, diff --git a/src/xargo.rs b/src/xargo.rs index c56d203..9f47a30 100644 --- a/src/xargo.rs +++ b/src/xargo.rs @@ -13,6 +13,7 @@ use errors::*; use extensions::CommandExt; use flock::{FileLock, Filesystem}; use {cargo, util}; +use rustc::Src; pub fn run( args: &Args, @@ -118,6 +119,11 @@ impl Toml { pub fn patch(&self) -> Option<&Value> { self.table.lookup("patch") } + + /// Returns the `rust-src` part of Xargo.toml + pub fn package(&self) -> Option<&Value> { + self.table.lookup("package") + } } /// Returns the closest directory containing a 'Xargo.toml' and the parsed @@ -130,3 +136,18 @@ pub fn toml(root: &Root) -> Result<(Option<&Path>, Option)> { Ok((None, None)) } } + +/// Returns the closest directory containing a 'Xargo.toml' and the parsed +/// content of this 'Xargo.toml' +pub fn toml_src(root: &Root) -> Result> { + Ok(toml(root)?.1.map(|toml|{ + toml.package().map(|package|{ + if let Value::Table(table) = package { + let src = table.get("rust-src")?.as_str()?; + Some(Src::from(PathBuf::from(src))) + } else { + None + } + }) + }).flatten().flatten()) +} From 29168ef0226f42742f8323d4fe4d1558e23292dc Mon Sep 17 00:00:00 2001 From: jam1garner Date: Sun, 12 Apr 2020 19:47:33 -0400 Subject: [PATCH 2/7] Fix failure to canonicalize path --- src/xargo.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/xargo.rs b/src/xargo.rs index 9f47a30..dd2fcf8 100644 --- a/src/xargo.rs +++ b/src/xargo.rs @@ -140,14 +140,22 @@ pub fn toml(root: &Root) -> Result<(Option<&Path>, Option)> { /// Returns the closest directory containing a 'Xargo.toml' and the parsed /// content of this 'Xargo.toml' pub fn toml_src(root: &Root) -> Result> { - Ok(toml(root)?.1.map(|toml|{ - toml.package().map(|package|{ - if let Value::Table(table) = package { - let src = table.get("rust-src")?.as_str()?; - Some(Src::from(PathBuf::from(src))) + Ok(if let Some(toml) = toml(root)?.1 { + if let Some(Value::Table(table)) = toml.package() { + if let Some(src) = table.get("rust-src").map(Value::as_str).flatten() { + if let Some(path) = PathBuf::from(src).canonicalize().ok() { + Some(Src::from(path)) + } else { + eprintln!("Warning: package.rust-src key exists but directory does not exist "); + None + } } else { None } - }) - }).flatten().flatten()) + } else { + None + } + } else { + None + }) } From 1ce938f5933a3283987aff47ed6340881e697bce Mon Sep 17 00:00:00 2001 From: jam1garner Date: Sun, 12 Apr 2020 20:28:39 -0400 Subject: [PATCH 3/7] Attempt to fix windows paths --- src/xargo.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/xargo.rs b/src/xargo.rs index dd2fcf8..176e7ba 100644 --- a/src/xargo.rs +++ b/src/xargo.rs @@ -143,6 +143,7 @@ pub fn toml_src(root: &Root) -> Result> { Ok(if let Some(toml) = toml(root)?.1 { if let Some(Value::Table(table)) = toml.package() { if let Some(src) = table.get("rust-src").map(Value::as_str).flatten() { + let src = src.split("/").collect::>().join(&std::path::MAIN_SEPARATOR.to_string()); if let Some(path) = PathBuf::from(src).canonicalize().ok() { Some(Src::from(path)) } else { From f9b8e7f64695498d89225975080cb1bd7e2fe7f2 Mon Sep 17 00:00:00 2001 From: jam1garner Date: Sun, 12 Apr 2020 20:32:36 -0400 Subject: [PATCH 4/7] Print out path --- src/xargo.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/xargo.rs b/src/xargo.rs index 176e7ba..1878ad1 100644 --- a/src/xargo.rs +++ b/src/xargo.rs @@ -145,6 +145,7 @@ pub fn toml_src(root: &Root) -> Result> { if let Some(src) = table.get("rust-src").map(Value::as_str).flatten() { let src = src.split("/").collect::>().join(&std::path::MAIN_SEPARATOR.to_string()); if let Some(path) = PathBuf::from(src).canonicalize().ok() { + dbg!(&path); Some(Src::from(path)) } else { eprintln!("Warning: package.rust-src key exists but directory does not exist "); From b379147e11dc5eab3e07e07b81b54ba64d2d007e Mon Sep 17 00:00:00 2001 From: jam1garner Date: Sun, 12 Apr 2020 20:42:00 -0400 Subject: [PATCH 5/7] Fix windows absolute path handling --- src/xargo.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/xargo.rs b/src/xargo.rs index 1878ad1..4bb4196 100644 --- a/src/xargo.rs +++ b/src/xargo.rs @@ -143,14 +143,7 @@ pub fn toml_src(root: &Root) -> Result> { Ok(if let Some(toml) = toml(root)?.1 { if let Some(Value::Table(table)) = toml.package() { if let Some(src) = table.get("rust-src").map(Value::as_str).flatten() { - let src = src.split("/").collect::>().join(&std::path::MAIN_SEPARATOR.to_string()); - if let Some(path) = PathBuf::from(src).canonicalize().ok() { - dbg!(&path); - Some(Src::from(path)) - } else { - eprintln!("Warning: package.rust-src key exists but directory does not exist "); - None - } + env::current_dir().map(|cur_dir| Src::from(cur_dir.join(src))).ok() } else { None } From 1a26e564eb6c958637b9f4399e4c7861c01e81f0 Mon Sep 17 00:00:00 2001 From: jam1garner Date: Mon, 13 Apr 2020 12:01:02 -0400 Subject: [PATCH 6/7] Switch Src::from into Src::new --- src/flock.rs | 2 +- src/rustc.rs | 10 ++++------ src/xargo.rs | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/flock.rs b/src/flock.rs index 09b98ba..8ab96a5 100644 --- a/src/flock.rs +++ b/src/flock.rs @@ -53,7 +53,7 @@ pub struct Filesystem { impl Filesystem { pub fn new(path: PathBuf) -> Filesystem { - Filesystem { path: path } + Filesystem { path } } pub fn join(&self, other: T) -> Filesystem diff --git a/src/rustc.rs b/src/rustc.rs index 890f9fc..4f10b5e 100644 --- a/src/rustc.rs +++ b/src/rustc.rs @@ -57,6 +57,10 @@ impl Src { pub fn path(&self) -> &Path { &self.path } + + pub fn new(path: PathBuf) -> Self { + Self { path } + } } /// Path to `rustc`'s sysroot @@ -108,12 +112,6 @@ impl Sysroot { } } -impl From for Src { - fn from(path: PathBuf) -> Self { - Self { path } - } -} - #[derive(Debug)] pub enum Target { Builtin { triple: String }, diff --git a/src/xargo.rs b/src/xargo.rs index 4bb4196..f119f86 100644 --- a/src/xargo.rs +++ b/src/xargo.rs @@ -143,7 +143,7 @@ pub fn toml_src(root: &Root) -> Result> { Ok(if let Some(toml) = toml(root)?.1 { if let Some(Value::Table(table)) = toml.package() { if let Some(src) = table.get("rust-src").map(Value::as_str).flatten() { - env::current_dir().map(|cur_dir| Src::from(cur_dir.join(src))).ok() + env::current_dir().map(|cur_dir| Src::new(cur_dir.join(src))).ok() } else { None } From 663993e57a6086d100ccd8c20b2ba0e3ca84d5d9 Mon Sep 17 00:00:00 2001 From: jam1garner <8260240+jam1garner@users.noreply.github.com> Date: Tue, 9 Jun 2020 00:09:00 -0400 Subject: [PATCH 7/7] Try and fix bug with paths with spaces in them --- src/cargo.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cargo.rs b/src/cargo.rs index d7fbdcf..999b353 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -46,7 +46,7 @@ impl Rustflags { pub fn for_xargo(&self, home: &Home) -> String { let mut flags = self.flags.clone(); flags.push("--sysroot".to_owned()); - flags.push(home.display().to_string()); + flags.push(format!("\"{}\"", home.display())); flags.join(" ") } } @@ -69,7 +69,7 @@ impl Rustdocflags { /// Stringifies these flags for Xargo consumption pub fn for_xargo(mut self, home: &Home) -> String { self.flags.push("--sysroot".to_owned()); - self.flags.push(home.display().to_string()); + self.flags.push(format!("\"{}\"", home.display())); self.flags.join(" ") } }