Skip to content

Commit

Permalink
Auto merge of rust-lang#16545 - Veykril:fix-target-layout-fetch, r=Ve…
Browse files Browse the repository at this point in the history
…ykril

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,
  • Loading branch information
bors committed Feb 12, 2024
2 parents cf87333 + a7641a8 commit 3770f73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
32 changes: 19 additions & 13 deletions crates/project-model/src/rustc_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn get_rust_cfgs(
extra_env: &FxHashMap<String, String>,
config: RustcCfgConfig<'_>,
) -> anyhow::Result<String> {
match config {
let sysroot = match config {
RustcCfgConfig::Cargo(sysroot, cargo_oml) => {
let cargo = Sysroot::discover_tool(sysroot, toolchain::Tool::Cargo)?;
let mut cmd = Command::new(cargo);
Expand All @@ -79,19 +79,25 @@ fn get_rust_cfgs(
cmd.args(["--target", target]);
}

utf8_stdout(cmd).context("Unable to run `cargo rustc`")
}
RustcCfgConfig::Rustc(sysroot) => {
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
tracing::debug!(?rustc, "using explicit rustc from sysroot");
let mut cmd = Command::new(rustc);
cmd.envs(extra_env);
cmd.args(["--print", "cfg", "-O"]);
if let Some(target) = target {
cmd.args(["--target", target]);
match utf8_stdout(cmd) {
Ok(it) => return Ok(it),
Err(e) => {
tracing::warn!("failed to run `cargo rustc --print cfg`, falling back to invoking rustc directly: {e}");
sysroot
}
}

utf8_stdout(cmd).context("Unable to run `rustc`")
}
RustcCfgConfig::Rustc(sysroot) => sysroot,
};

let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
tracing::debug!(?rustc, "using explicit rustc from sysroot");
let mut cmd = Command::new(rustc);
cmd.envs(extra_env);
cmd.args(["--print", "cfg", "-O"]);
if let Some(target) = target {
cmd.args(["--target", target]);
}

utf8_stdout(cmd).context("unable to fetch cfgs via `rustc --print cfg -O`")
}
42 changes: 26 additions & 16 deletions crates/project-model/src/target_data_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,41 @@ pub fn get(
target: Option<&str>,
extra_env: &FxHashMap<String, String>,
) -> anyhow::Result<String> {
let output = match config {
let process = |output: String| {
(|| Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned()))()
.ok_or_else(|| {
anyhow::format_err!("could not fetch target-spec-json from command output")
})
};
let sysroot = match config {
RustcDataLayoutConfig::Cargo(sysroot, cargo_toml) => {
let cargo = Sysroot::discover_tool(sysroot, toolchain::Tool::Cargo)?;
let mut cmd = Command::new(cargo);
cmd.envs(extra_env);
cmd.current_dir(cargo_toml.parent())
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
.args(["rustc", "--", "-Z", "unstable-options", "--print", "target-spec-json"])
.env("RUSTC_BOOTSTRAP", "1");
if let Some(target) = target {
cmd.args(["--target", target]);
}
utf8_stdout(cmd)
}
RustcDataLayoutConfig::Rustc(sysroot) => {
let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
let mut cmd = Command::new(rustc);
cmd.envs(extra_env)
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
.env("RUSTC_BOOTSTRAP", "1");
if let Some(target) = target {
cmd.args(["--target", target]);
match utf8_stdout(cmd) {
Ok(output) => return process(output),
Err(e) => {
tracing::warn!("failed to run `cargo rustc --print target-spec-json`, falling back to invoking rustc directly: {e}");
sysroot
}
}
utf8_stdout(cmd)
}
}?;
(|| Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned()))()
.ok_or_else(|| anyhow::format_err!("could not fetch target-spec-json from command output"))
RustcDataLayoutConfig::Rustc(sysroot) => sysroot,
};

let rustc = Sysroot::discover_tool(sysroot, toolchain::Tool::Rustc)?;
let mut cmd = Command::new(rustc);
cmd.envs(extra_env)
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
.env("RUSTC_BOOTSTRAP", "1");
if let Some(target) = target {
cmd.args(["--target", target]);
}
process(utf8_stdout(cmd)?)
}

0 comments on commit 3770f73

Please sign in to comment.