From 9802c0ccd0cf408260961eb61581b61363c4b47b Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Wed, 16 Dec 2020 17:38:21 -0800 Subject: [PATCH 1/3] Default to host platform for cargo metadata This modifies the logic for calling cargo metadata so that it will use the host platform if no explicit target platform is given. This is needed since cargo metadata defaults to outputting information for _all_ targets. Fixes #6908. --- crates/project_model/src/cargo_workspace.rs | 32 +++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs index 894c5c952aa..91fb645da99 100644 --- a/crates/project_model/src/cargo_workspace.rs +++ b/crates/project_model/src/cargo_workspace.rs @@ -16,6 +16,7 @@ use rustc_hash::FxHashMap; use crate::cfg_flag::CfgFlag; +use crate::utf8_stdout; /// `CargoWorkspace` represents the logical structure of, well, a Cargo /// workspace. It pretty closely mirrors `cargo metadata` output. @@ -166,8 +167,35 @@ pub fn from_cargo_metadata( if let Some(parent) = cargo_toml.parent() { meta.current_dir(parent.to_path_buf()); } - if let Some(target) = config.target.as_ref() { - meta.other_options(vec![String::from("--filter-platform"), target.clone()]); + let target = if let Some(target) = config.target.as_ref() { + Some(target.clone()) + } else { + // cargo metadata defaults to giving information for _all_ targets. + // In the absence of a preference from the user, we use the host platform. + let mut rustc = Command::new(toolchain::rustc()); + rustc.current_dir(cargo_toml.parent().unwrap()).arg("-vV"); + log::debug!("Discovering host platform by {:?}", rustc); + match utf8_stdout(rustc) { + Ok(stdout) => { + let field = "host: "; + let target = + stdout.lines().find(|l| l.starts_with(field)).map(|l| &l[field.len()..]); + if let Some(target) = target { + Some(target.to_string()) + } else { + // If we fail to resolve the host platform, it's not the end of the world. + log::info!("rustc -vV did not report host platform, got:\n{}", stdout); + None + } + } + Err(e) => { + log::warn!("Failed to discover host platform: {}", e); + None + } + } + }; + if let Some(target) = target { + meta.other_options(vec![String::from("--filter-platform"), target]); } let mut meta = meta.exec().with_context(|| { format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display()) From 71478e067be8ce1a7cf9f5ec3ed654f8c204017f Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Thu, 17 Dec 2020 13:18:23 -0500 Subject: [PATCH 2/3] Update crates/project_model/src/cargo_workspace.rs Co-authored-by: Aleksey Kladov --- crates/project_model/src/cargo_workspace.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs index 91fb645da99..4d339d9c02f 100644 --- a/crates/project_model/src/cargo_workspace.rs +++ b/crates/project_model/src/cargo_workspace.rs @@ -179,7 +179,7 @@ pub fn from_cargo_metadata( Ok(stdout) => { let field = "host: "; let target = - stdout.lines().find(|l| l.starts_with(field)).map(|l| &l[field.len()..]); + stdout.lines().find_map(|l| l.strip_prefix(field)); if let Some(target) = target { Some(target.to_string()) } else { From faed47b3d31d2bf533aac489046ed9efc30f239d Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Thu, 17 Dec 2020 10:24:58 -0800 Subject: [PATCH 3/3] Fun times with rustfmt --- crates/project_model/src/cargo_workspace.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs index 4d339d9c02f..bb3b6f2efbb 100644 --- a/crates/project_model/src/cargo_workspace.rs +++ b/crates/project_model/src/cargo_workspace.rs @@ -178,8 +178,7 @@ pub fn from_cargo_metadata( match utf8_stdout(rustc) { Ok(stdout) => { let field = "host: "; - let target = - stdout.lines().find_map(|l| l.strip_prefix(field)); + let target = stdout.lines().find_map(|l| l.strip_prefix(field)); if let Some(target) = target { Some(target.to_string()) } else {