mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-22 19:34:16 +00:00
minor: Add some debug traces for cfg fetching
This commit is contained in:
parent
fcdced115e
commit
dbd5a70ea3
2 changed files with 43 additions and 26 deletions
|
@ -1,7 +1,7 @@
|
||||||
//! Parsing of CfgFlags as command line arguments, as in
|
//! Parsing of CfgFlags as command line arguments, as in
|
||||||
//!
|
//!
|
||||||
//! rustc main.rs --cfg foo --cfg 'feature="bar"'
|
//! rustc main.rs --cfg foo --cfg 'feature="bar"'
|
||||||
use std::str::FromStr;
|
use std::{fmt, str::FromStr};
|
||||||
|
|
||||||
use cfg::CfgOptions;
|
use cfg::CfgOptions;
|
||||||
|
|
||||||
|
@ -48,3 +48,16 @@ impl Extend<CfgFlag> for CfgOptions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for CfgFlag {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
CfgFlag::Atom(atom) => f.write_str(atom),
|
||||||
|
CfgFlag::KeyValue { key, value } => {
|
||||||
|
f.write_str(key)?;
|
||||||
|
f.write_str("=")?;
|
||||||
|
f.write_str(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -19,38 +19,42 @@ pub(crate) fn get(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Ve
|
||||||
}
|
}
|
||||||
|
|
||||||
match get_rust_cfgs(cargo_toml, target) {
|
match get_rust_cfgs(cargo_toml, target) {
|
||||||
Ok(rustc_cfgs) => res.extend(rustc_cfgs.lines().map(|it| it.parse().unwrap())),
|
Ok(rustc_cfgs) => {
|
||||||
Err(e) => tracing::error!("failed to get rustc cfgs: {:#}", e),
|
tracing::debug!(
|
||||||
|
"rustc cfgs found: {:?}",
|
||||||
|
rustc_cfgs
|
||||||
|
.lines()
|
||||||
|
.map(|it| it.parse::<CfgFlag>().map(|it| it.to_string()))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
);
|
||||||
|
res.extend(rustc_cfgs.lines().filter_map(|it| it.parse().ok()));
|
||||||
|
}
|
||||||
|
Err(e) => tracing::error!("failed to get rustc cfgs: {e:?}"),
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_rust_cfgs(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Result<String> {
|
fn get_rust_cfgs(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Result<String> {
|
||||||
let cargo_rust_cfgs = match cargo_toml {
|
if let Some(cargo_toml) = cargo_toml {
|
||||||
Some(cargo_toml) => {
|
let mut cargo_config = Command::new(toolchain::cargo());
|
||||||
let mut cargo_config = Command::new(toolchain::cargo());
|
cargo_config
|
||||||
cargo_config
|
.current_dir(cargo_toml.parent())
|
||||||
.current_dir(cargo_toml.parent())
|
.args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
|
||||||
.args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
|
.env("RUSTC_BOOTSTRAP", "1");
|
||||||
.env("RUSTC_BOOTSTRAP", "1");
|
if let Some(target) = target {
|
||||||
if let Some(target) = target {
|
cargo_config.args(&["--target", target]);
|
||||||
cargo_config.args(&["--target", target]);
|
|
||||||
}
|
|
||||||
utf8_stdout(cargo_config).ok()
|
|
||||||
}
|
}
|
||||||
None => None,
|
match utf8_stdout(cargo_config) {
|
||||||
};
|
Ok(it) => return Ok(it),
|
||||||
match cargo_rust_cfgs {
|
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
|
||||||
Some(stdout) => Ok(stdout),
|
|
||||||
None => {
|
|
||||||
// using unstable cargo features failed, fall back to using plain rustc
|
|
||||||
let mut cmd = Command::new(toolchain::rustc());
|
|
||||||
cmd.args(&["--print", "cfg", "-O"]);
|
|
||||||
if let Some(target) = target {
|
|
||||||
cmd.args(&["--target", target]);
|
|
||||||
}
|
|
||||||
utf8_stdout(cmd)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// using unstable cargo features failed, fall back to using plain rustc
|
||||||
|
let mut cmd = Command::new(toolchain::rustc());
|
||||||
|
cmd.args(&["--print", "cfg", "-O"]);
|
||||||
|
if let Some(target) = target {
|
||||||
|
cmd.args(&["--target", target]);
|
||||||
|
}
|
||||||
|
utf8_stdout(cmd)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue