Skip to content

Commit dbd5a70

Browse files
committed
minor: Add some debug traces for cfg fetching
1 parent fcdced1 commit dbd5a70

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

crates/project_model/src/cfg_flag.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Parsing of CfgFlags as command line arguments, as in
22
//!
33
//! rustc main.rs --cfg foo --cfg 'feature="bar"'
4-
use std::str::FromStr;
4+
use std::{fmt, str::FromStr};
55

66
use cfg::CfgOptions;
77

@@ -48,3 +48,16 @@ impl Extend<CfgFlag> for CfgOptions {
4848
}
4949
}
5050
}
51+
52+
impl fmt::Display for CfgFlag {
53+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
match self {
55+
CfgFlag::Atom(atom) => f.write_str(atom),
56+
CfgFlag::KeyValue { key, value } => {
57+
f.write_str(key)?;
58+
f.write_str("=")?;
59+
f.write_str(value)
60+
}
61+
}
62+
}
63+
}

crates/project_model/src/rustc_cfg.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,42 @@ pub(crate) fn get(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Ve
1919
}
2020

2121
match get_rust_cfgs(cargo_toml, target) {
22-
Ok(rustc_cfgs) => res.extend(rustc_cfgs.lines().map(|it| it.parse().unwrap())),
23-
Err(e) => tracing::error!("failed to get rustc cfgs: {:#}", e),
22+
Ok(rustc_cfgs) => {
23+
tracing::debug!(
24+
"rustc cfgs found: {:?}",
25+
rustc_cfgs
26+
.lines()
27+
.map(|it| it.parse::<CfgFlag>().map(|it| it.to_string()))
28+
.collect::<Vec<_>>()
29+
);
30+
res.extend(rustc_cfgs.lines().filter_map(|it| it.parse().ok()));
31+
}
32+
Err(e) => tracing::error!("failed to get rustc cfgs: {e:?}"),
2433
}
2534

2635
res
2736
}
2837

2938
fn get_rust_cfgs(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Result<String> {
30-
let cargo_rust_cfgs = match cargo_toml {
31-
Some(cargo_toml) => {
32-
let mut cargo_config = Command::new(toolchain::cargo());
33-
cargo_config
34-
.current_dir(cargo_toml.parent())
35-
.args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
36-
.env("RUSTC_BOOTSTRAP", "1");
37-
if let Some(target) = target {
38-
cargo_config.args(&["--target", target]);
39-
}
40-
utf8_stdout(cargo_config).ok()
39+
if let Some(cargo_toml) = cargo_toml {
40+
let mut cargo_config = Command::new(toolchain::cargo());
41+
cargo_config
42+
.current_dir(cargo_toml.parent())
43+
.args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
44+
.env("RUSTC_BOOTSTRAP", "1");
45+
if let Some(target) = target {
46+
cargo_config.args(&["--target", target]);
4147
}
42-
None => None,
43-
};
44-
match cargo_rust_cfgs {
45-
Some(stdout) => Ok(stdout),
46-
None => {
47-
// using unstable cargo features failed, fall back to using plain rustc
48-
let mut cmd = Command::new(toolchain::rustc());
49-
cmd.args(&["--print", "cfg", "-O"]);
50-
if let Some(target) = target {
51-
cmd.args(&["--target", target]);
52-
}
53-
utf8_stdout(cmd)
48+
match utf8_stdout(cargo_config) {
49+
Ok(it) => return Ok(it),
50+
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
5451
}
5552
}
53+
// using unstable cargo features failed, fall back to using plain rustc
54+
let mut cmd = Command::new(toolchain::rustc());
55+
cmd.args(&["--print", "cfg", "-O"]);
56+
if let Some(target) = target {
57+
cmd.args(&["--target", target]);
58+
}
59+
utf8_stdout(cmd)
5660
}

0 commit comments

Comments
 (0)