]> git.lizzy.rs Git - rust.git/commitdiff
Merge #11391
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Tue, 1 Feb 2022 12:35:52 +0000 (12:35 +0000)
committerGitHub <noreply@github.com>
Tue, 1 Feb 2022 12:35:52 +0000 (12:35 +0000)
11391: minor: Add some debug traces for cfg fetching r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
crates/project_model/src/cfg_flag.rs
crates/project_model/src/rustc_cfg.rs

index bfdfd458fbfc4fc4f0980b79858bf151feb65b7e..f3dd8f51333be447b350e4baf402c0272c7e15b7 100644 (file)
@@ -1,7 +1,7 @@
 //! Parsing of CfgFlags as command line arguments, as in
 //!
 //! rustc main.rs --cfg foo --cfg 'feature="bar"'
-use std::str::FromStr;
+use std::{fmt, str::FromStr};
 
 use cfg::CfgOptions;
 
@@ -48,3 +48,16 @@ fn extend<T: IntoIterator<Item = CfgFlag>>(&mut self, iter: T) {
         }
     }
 }
+
+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)
+            }
+        }
+    }
+}
index 669aea0cd13aa4d54624f2fe10e108f22eab34e9..17e244d0649eddf073a9bf65e39fade35226ae4f 100644 (file)
@@ -19,38 +19,42 @@ pub(crate) fn get(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Ve
     }
 
     match get_rust_cfgs(cargo_toml, target) {
-        Ok(rustc_cfgs) => res.extend(rustc_cfgs.lines().map(|it| it.parse().unwrap())),
-        Err(e) => tracing::error!("failed to get rustc cfgs: {:#}", e),
+        Ok(rustc_cfgs) => {
+            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
 }
 
 fn get_rust_cfgs(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Result<String> {
-    let cargo_rust_cfgs = match cargo_toml {
-        Some(cargo_toml) => {
-            let mut cargo_config = Command::new(toolchain::cargo());
-            cargo_config
-                .current_dir(cargo_toml.parent())
-                .args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
-                .env("RUSTC_BOOTSTRAP", "1");
-            if let Some(target) = target {
-                cargo_config.args(&["--target", target]);
-            }
-            utf8_stdout(cargo_config).ok()
+    if let Some(cargo_toml) = cargo_toml {
+        let mut cargo_config = Command::new(toolchain::cargo());
+        cargo_config
+            .current_dir(cargo_toml.parent())
+            .args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
+            .env("RUSTC_BOOTSTRAP", "1");
+        if let Some(target) = target {
+            cargo_config.args(&["--target", target]);
         }
-        None => None,
-    };
-    match cargo_rust_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)
+        match utf8_stdout(cargo_config) {
+            Ok(it) => return Ok(it),
+            Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
         }
     }
+    // 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)
 }