mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +00:00
fix: Fix target-data-layout fetching incorrectly passing 'rustc' to rustc
This commit is contained in:
parent
c9d33cddc9
commit
384fa4b84a
8 changed files with 144 additions and 63 deletions
|
@ -1,6 +1,7 @@
|
|||
//! Runs `rustc --print target-spec-json` to get the target_data_layout.
|
||||
use std::process::Command;
|
||||
|
||||
use anyhow::Result;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{utf8_stdout, ManifestPath};
|
||||
|
@ -9,7 +10,7 @@ pub(super) fn get(
|
|||
cargo_toml: Option<&ManifestPath>,
|
||||
target: Option<&str>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
) -> Option<String> {
|
||||
) -> Result<String> {
|
||||
let output = (|| {
|
||||
if let Some(cargo_toml) = cargo_toml {
|
||||
let mut cmd = Command::new(toolchain::rustc());
|
||||
|
@ -28,13 +29,13 @@ pub(super) fn get(
|
|||
// using unstable cargo features failed, fall back to using plain rustc
|
||||
let mut cmd = Command::new(toolchain::rustc());
|
||||
cmd.envs(extra_env)
|
||||
.args(["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
|
||||
.args(["-Z", "unstable-options", "--print", "target-spec-json"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
if let Some(target) = target {
|
||||
cmd.args(["--target", target]);
|
||||
}
|
||||
utf8_stdout(cmd)
|
||||
})()
|
||||
.ok()?;
|
||||
Some(output.split_once(r#""data-layout": ""#)?.1.split_once('"')?.0.to_owned())
|
||||
})()?;
|
||||
(|| 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"))
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ fn load_cargo_with_overrides(file: &str, cfg_overrides: CfgOverrides) -> CrateGr
|
|||
rustc_cfg: Vec::new(),
|
||||
cfg_overrides,
|
||||
toolchain: None,
|
||||
target_layout: None,
|
||||
target_layout: Err("target_data_layout not loaded".into()),
|
||||
};
|
||||
to_crate_graph(project_workspace)
|
||||
}
|
||||
|
@ -151,7 +151,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"debug_assertions",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -221,7 +223,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"debug_assertions",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -300,7 +304,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"debug_assertions",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -379,7 +385,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"debug_assertions",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -467,7 +475,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
|
|||
"feature=use_std",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -553,7 +563,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -625,7 +637,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -706,7 +720,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -787,7 +803,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -875,7 +893,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
|
|||
"feature=use_std",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -952,7 +972,9 @@ fn cargo_hello_world_project_model() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1024,7 +1046,9 @@ fn cargo_hello_world_project_model() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1105,7 +1129,9 @@ fn cargo_hello_world_project_model() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1186,7 +1212,9 @@ fn cargo_hello_world_project_model() {
|
|||
"test",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1274,7 +1302,9 @@ fn cargo_hello_world_project_model() {
|
|||
"feature=use_std",
|
||||
],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"target_data_layout not loaded",
|
||||
),
|
||||
env: Env {
|
||||
entries: {
|
||||
"CARGO_PKG_LICENSE": "",
|
||||
|
@ -1343,7 +1373,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1388,7 +1420,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1423,7 +1457,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1458,7 +1494,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1493,7 +1531,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1538,7 +1578,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1573,7 +1615,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1681,7 +1725,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1716,7 +1762,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1751,7 +1799,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
@ -1786,7 +1836,9 @@ fn rust_project_hello_world_project_model() {
|
|||
potential_cfg_options: CfgOptions(
|
||||
[],
|
||||
),
|
||||
target_layout: None,
|
||||
target_layout: Err(
|
||||
"rust-project.json projects have no target layout set",
|
||||
),
|
||||
env: Env {
|
||||
entries: {},
|
||||
},
|
||||
|
|
|
@ -7,7 +7,7 @@ use std::{collections::VecDeque, fmt, fs, process::Command, sync::Arc};
|
|||
use anyhow::{format_err, Context, Result};
|
||||
use base_db::{
|
||||
CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Edition, Env,
|
||||
FileId, LangCrateOrigin, ProcMacroLoadResult,
|
||||
FileId, LangCrateOrigin, ProcMacroLoadResult, TargetLayoutLoadResult,
|
||||
};
|
||||
use cfg::{CfgDiff, CfgOptions};
|
||||
use paths::{AbsPath, AbsPathBuf};
|
||||
|
@ -79,7 +79,7 @@ pub enum ProjectWorkspace {
|
|||
rustc_cfg: Vec<CfgFlag>,
|
||||
cfg_overrides: CfgOverrides,
|
||||
toolchain: Option<Version>,
|
||||
target_layout: Option<String>,
|
||||
target_layout: Result<String, String>,
|
||||
},
|
||||
/// Project workspace was manually specified using a `rust-project.json` file.
|
||||
Json { project: ProjectJson, sysroot: Option<Sysroot>, rustc_cfg: Vec<CfgFlag> },
|
||||
|
@ -249,6 +249,9 @@ impl ProjectWorkspace {
|
|||
config.target.as_deref(),
|
||||
&config.extra_env,
|
||||
);
|
||||
if let Err(e) = &data_layout {
|
||||
tracing::error!(%e, "failed fetching data layout for {cargo_toml:?} workspace");
|
||||
}
|
||||
ProjectWorkspace::Cargo {
|
||||
cargo,
|
||||
build_scripts: WorkspaceBuildScripts::default(),
|
||||
|
@ -257,7 +260,7 @@ impl ProjectWorkspace {
|
|||
rustc_cfg,
|
||||
cfg_overrides,
|
||||
toolchain,
|
||||
target_layout: data_layout,
|
||||
target_layout: data_layout.map_err(|it| it.to_string()),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -540,7 +543,7 @@ impl ProjectWorkspace {
|
|||
project,
|
||||
sysroot,
|
||||
extra_env,
|
||||
None,
|
||||
Err("rust-project.json projects have no target layout set".into()),
|
||||
),
|
||||
ProjectWorkspace::Cargo {
|
||||
cargo,
|
||||
|
@ -560,10 +563,19 @@ impl ProjectWorkspace {
|
|||
rustc_cfg.clone(),
|
||||
cfg_overrides,
|
||||
build_scripts,
|
||||
target_layout.as_deref().map(Arc::from),
|
||||
match target_layout.as_ref() {
|
||||
Ok(it) => Ok(Arc::from(it.as_str())),
|
||||
Err(it) => Err(Arc::from(it.as_str())),
|
||||
},
|
||||
),
|
||||
ProjectWorkspace::DetachedFiles { files, sysroot, rustc_cfg } => {
|
||||
detached_files_to_crate_graph(rustc_cfg.clone(), load, files, sysroot, None)
|
||||
detached_files_to_crate_graph(
|
||||
rustc_cfg.clone(),
|
||||
load,
|
||||
files,
|
||||
sysroot,
|
||||
Err("detached file projects have no target layout set".into()),
|
||||
)
|
||||
}
|
||||
};
|
||||
if crate_graph.patch_cfg_if() {
|
||||
|
@ -582,7 +594,7 @@ fn project_json_to_crate_graph(
|
|||
project: &ProjectJson,
|
||||
sysroot: &Option<Sysroot>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
target_layout: Option<Arc<str>>,
|
||||
target_layout: TargetLayoutLoadResult,
|
||||
) -> CrateGraph {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let sysroot_deps = sysroot.as_ref().map(|sysroot| {
|
||||
|
@ -686,7 +698,7 @@ fn cargo_to_crate_graph(
|
|||
rustc_cfg: Vec<CfgFlag>,
|
||||
override_cfg: &CfgOverrides,
|
||||
build_scripts: &WorkspaceBuildScripts,
|
||||
target_layout: Option<Arc<str>>,
|
||||
target_layout: TargetLayoutLoadResult,
|
||||
) -> CrateGraph {
|
||||
let _p = profile::span("cargo_to_crate_graph");
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
|
@ -852,7 +864,7 @@ fn detached_files_to_crate_graph(
|
|||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
detached_files: &[AbsPathBuf],
|
||||
sysroot: &Option<Sysroot>,
|
||||
target_layout: Option<Arc<str>>,
|
||||
target_layout: TargetLayoutLoadResult,
|
||||
) -> CrateGraph {
|
||||
let _p = profile::span("detached_files_to_crate_graph");
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
|
@ -917,7 +929,7 @@ fn handle_rustc_crates(
|
|||
cfg_options: &CfgOptions,
|
||||
override_cfg: &CfgOverrides,
|
||||
build_scripts: &WorkspaceBuildScripts,
|
||||
target_layout: Option<Arc<str>>,
|
||||
target_layout: TargetLayoutLoadResult,
|
||||
) {
|
||||
let mut rustc_pkg_crates = FxHashMap::default();
|
||||
// The root package of the rustc-dev component is rustc_driver, so we match that
|
||||
|
@ -1039,7 +1051,7 @@ fn add_target_crate_root(
|
|||
file_id: FileId,
|
||||
cargo_name: &str,
|
||||
is_proc_macro: bool,
|
||||
target_layout: Option<Arc<str>>,
|
||||
target_layout: TargetLayoutLoadResult,
|
||||
) -> CrateId {
|
||||
let edition = pkg.edition;
|
||||
let mut potential_cfg_options = cfg_options.clone();
|
||||
|
@ -1108,7 +1120,7 @@ fn sysroot_to_crate_graph(
|
|||
crate_graph: &mut CrateGraph,
|
||||
sysroot: &Sysroot,
|
||||
rustc_cfg: Vec<CfgFlag>,
|
||||
target_layout: Option<Arc<str>>,
|
||||
target_layout: TargetLayoutLoadResult,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
) -> (SysrootPublicDeps, Option<CrateId>) {
|
||||
let _p = profile::span("sysroot_to_crate_graph");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue