Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fallback on value package.rust-src if XARGO_RUST_SRC doesn't exist #288

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Copy link
Collaborator

@RalfJung RalfJung Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this RUSTFLAGS? The quotes won't work there. There currently just is no way to pass arguments with spaces via RUSTFLAGS. That is a cargo bug: rust-lang/cargo#6139

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I'm sorry I forgot I never closed this PR, I was trying to fix some random person's issue and must've pushed to master, my bad! Thank you for the additional info!

flags.join(" ")
}
}
Expand All @@ -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(" ")
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct Filesystem {

impl Filesystem {
pub fn new(path: PathBuf) -> Filesystem {
Filesystem { path: path }
Filesystem { path }
}

pub fn join<T>(&self, other: T) -> Filesystem
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ fn run(cargo_mode: XargoMode) -> Result<Option<ExitStatus>> {
// 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()?
},
Expand Down
4 changes: 4 additions & 0 deletions src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/xargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use errors::*;
use extensions::CommandExt;
use flock::{FileLock, Filesystem};
use {cargo, util};
use rustc::Src;

pub fn run(
args: &Args,
Expand Down Expand Up @@ -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
Expand All @@ -130,3 +136,21 @@ pub fn toml(root: &Root) -> Result<(Option<&Path>, Option<Toml>)> {
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<Option<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() {
env::current_dir().map(|cur_dir| Src::new(cur_dir.join(src))).ok()
} else {
None
}
} else {
None
}
} else {
None
})
}