Skip to content

Commit 3770f73

Browse files
committed
Auto merge of rust-lang#16545 - Veykril:fix-target-layout-fetch, r=Veykril
fix: Fix target layout fetching rust-lang/rust-analyzer#16537 broke this, as `cargo rustc` cannot run against a virtual workspace, so it will always fail in such projects (like rust-analyzer itself). This brings back the plain rustc fallback,
2 parents cf87333 + a7641a8 commit 3770f73

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

crates/project-model/src/rustc_cfg.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn get_rust_cfgs(
6767
extra_env: &FxHashMap<String, String>,
6868
config: RustcCfgConfig<'_>,
6969
) -> anyhow::Result<String> {
70-
match config {
70+
let sysroot = match config {
7171
RustcCfgConfig::Cargo(sysroot, cargo_oml) => {
7272
let cargo = Sysroot::discover_tool(sysroot, toolchain::Tool::Cargo)?;
7373
let mut cmd = Command::new(cargo);
@@ -79,19 +79,25 @@ fn get_rust_cfgs(
7979
cmd.args(["--target", target]);
8080
}
8181

82-
utf8_stdout(cmd).context("Unable to run `cargo rustc`")
83-
}
84-
RustcCfgConfig::Rustc(sysroot) => {
85-
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
86-
tracing::debug!(?rustc, "using explicit rustc from sysroot");
87-
let mut cmd = Command::new(rustc);
88-
cmd.envs(extra_env);
89-
cmd.args(["--print", "cfg", "-O"]);
90-
if let Some(target) = target {
91-
cmd.args(["--target", target]);
82+
match utf8_stdout(cmd) {
83+
Ok(it) => return Ok(it),
84+
Err(e) => {
85+
tracing::warn!("failed to run `cargo rustc --print cfg`, falling back to invoking rustc directly: {e}");
86+
sysroot
87+
}
9288
}
93-
94-
utf8_stdout(cmd).context("Unable to run `rustc`")
9589
}
90+
RustcCfgConfig::Rustc(sysroot) => sysroot,
91+
};
92+
93+
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
94+
tracing::debug!(?rustc, "using explicit rustc from sysroot");
95+
let mut cmd = Command::new(rustc);
96+
cmd.envs(extra_env);
97+
cmd.args(["--print", "cfg", "-O"]);
98+
if let Some(target) = target {
99+
cmd.args(["--target", target]);
96100
}
101+
102+
utf8_stdout(cmd).context("unable to fetch cfgs via `rustc --print cfg -O`")
97103
}

crates/project-model/src/target_data_layout.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,41 @@ pub fn get(
2020
target: Option<&str>,
2121
extra_env: &FxHashMap<String, String>,
2222
) -> anyhow::Result<String> {
23-
let output = match config {
23+
let process = |output: String| {
24+
(|| Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned()))()
25+
.ok_or_else(|| {
26+
anyhow::format_err!("could not fetch target-spec-json from command output")
27+
})
28+
};
29+
let sysroot = match config {
2430
RustcDataLayoutConfig::Cargo(sysroot, cargo_toml) => {
2531
let cargo = Sysroot::discover_tool(sysroot, toolchain::Tool::Cargo)?;
2632
let mut cmd = Command::new(cargo);
2733
cmd.envs(extra_env);
2834
cmd.current_dir(cargo_toml.parent())
29-
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
35+
.args(["rustc", "--", "-Z", "unstable-options", "--print", "target-spec-json"])
3036
.env("RUSTC_BOOTSTRAP", "1");
3137
if let Some(target) = target {
3238
cmd.args(["--target", target]);
3339
}
34-
utf8_stdout(cmd)
35-
}
36-
RustcDataLayoutConfig::Rustc(sysroot) => {
37-
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
38-
let mut cmd = Command::new(rustc);
39-
cmd.envs(extra_env)
40-
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
41-
.env("RUSTC_BOOTSTRAP", "1");
42-
if let Some(target) = target {
43-
cmd.args(["--target", target]);
40+
match utf8_stdout(cmd) {
41+
Ok(output) => return process(output),
42+
Err(e) => {
43+
tracing::warn!("failed to run `cargo rustc --print target-spec-json`, falling back to invoking rustc directly: {e}");
44+
sysroot
45+
}
4446
}
45-
utf8_stdout(cmd)
4647
}
47-
}?;
48-
(|| Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned()))()
49-
.ok_or_else(|| anyhow::format_err!("could not fetch target-spec-json from command output"))
48+
RustcDataLayoutConfig::Rustc(sysroot) => sysroot,
49+
};
50+
51+
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
52+
let mut cmd = Command::new(rustc);
53+
cmd.envs(extra_env)
54+
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
55+
.env("RUSTC_BOOTSTRAP", "1");
56+
if let Some(target) = target {
57+
cmd.args(["--target", target]);
58+
}
59+
process(utf8_stdout(cmd)?)
5060
}

0 commit comments

Comments
 (0)