Skip to content

Commit d105253

Browse files
committed
Force a current dir to be supplied for rustc info fetching
1 parent a4a6a43 commit d105253

File tree

7 files changed

+37
-25
lines changed

7 files changed

+37
-25
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/layout/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod closure;
1818

1919
fn current_machine_data_layout() -> String {
2020
project_model::toolchain_info::target_data_layout::get(
21-
QueryConfig::Rustc(&Sysroot::empty()),
21+
QueryConfig::Rustc(&Sysroot::empty(), &std::env::current_dir().unwrap()),
2222
None,
2323
&FxHashMap::default(),
2424
)

src/tools/rust-analyzer/crates/project-model/src/toolchain_info.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ pub mod rustc_cfg;
22
pub mod target_data_layout;
33
pub mod target_triple;
44

5+
use std::path::Path;
6+
57
use crate::{ManifestPath, Sysroot};
68

9+
#[derive(Copy, Clone)]
710
pub enum QueryConfig<'a> {
811
/// Directly invoke `rustc` to query the desired information.
9-
Rustc(&'a Sysroot),
12+
Rustc(&'a Sysroot, &'a Path),
1013
/// Attempt to use cargo to query the desired information, honoring cargo configurations.
1114
/// If this fails, falls back to invoking `rustc` directly.
1215
Cargo(&'a Sysroot, &'a ManifestPath),

src/tools/rust-analyzer/crates/project-model/src/toolchain_info/rustc_cfg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn rustc_print_cfg(
4343
config: QueryConfig<'_>,
4444
) -> anyhow::Result<String> {
4545
const RUSTC_ARGS: [&str; 3] = ["--print", "cfg", "-O"];
46-
let sysroot = match config {
46+
let (sysroot, current_dir) = match config {
4747
QueryConfig::Cargo(sysroot, cargo_toml) => {
4848
let mut cmd = sysroot.tool(Tool::Cargo, cargo_toml.parent());
4949
cmd.envs(extra_env);
@@ -60,14 +60,14 @@ fn rustc_print_cfg(
6060
%e,
6161
"failed to run `{cmd:?}`, falling back to invoking rustc directly"
6262
);
63-
sysroot
63+
(sysroot, cargo_toml.parent().as_ref())
6464
}
6565
}
6666
}
67-
QueryConfig::Rustc(sysroot) => sysroot,
67+
QueryConfig::Rustc(sysroot, current_dir) => (sysroot, current_dir),
6868
};
6969

70-
let mut cmd = sysroot.tool(Tool::Rustc, &std::env::current_dir()?);
70+
let mut cmd = sysroot.tool(Tool::Rustc, current_dir);
7171
cmd.envs(extra_env);
7272
cmd.args(RUSTC_ARGS);
7373
if let Some(target) = target {

src/tools/rust-analyzer/crates/project-model/src/toolchain_info/target_data_layout.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn get(
1919
anyhow::format_err!("could not parse target-spec-json from command output")
2020
})
2121
};
22-
let sysroot = match config {
22+
let (sysroot, current_dir) = match config {
2323
QueryConfig::Cargo(sysroot, cargo_toml) => {
2424
let mut cmd = sysroot.tool(Tool::Cargo, cargo_toml.parent());
2525
cmd.envs(extra_env);
@@ -36,14 +36,14 @@ pub fn get(
3636
Ok(output) => return process(output),
3737
Err(e) => {
3838
tracing::warn!(%e, "failed to run `{cmd:?}`, falling back to invoking rustc directly");
39-
sysroot
39+
(sysroot, cargo_toml.parent().as_ref())
4040
}
4141
}
4242
}
43-
QueryConfig::Rustc(sysroot) => sysroot,
43+
QueryConfig::Rustc(sysroot, current_dir) => (sysroot, current_dir),
4444
};
4545

46-
let mut cmd = Sysroot::tool(sysroot, Tool::Rustc, &std::env::current_dir()?);
46+
let mut cmd = Sysroot::tool(sysroot, Tool::Rustc, current_dir);
4747
cmd.envs(extra_env)
4848
.env("RUSTC_BOOTSTRAP", "1")
4949
.args(["-Z", "unstable-options"])

src/tools/rust-analyzer/crates/project-model/src/toolchain_info/target_triple.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::Path;
2+
13
use anyhow::Context;
24
use rustc_hash::FxHashMap;
35
use toolchain::Tool;
@@ -16,23 +18,24 @@ pub fn get(
1618
return Ok(vec![target.to_owned()]);
1719
}
1820

19-
let sysroot = match config {
21+
let (sysroot, current_dir) = match config {
2022
QueryConfig::Cargo(sysroot, cargo_toml) => {
2123
match cargo_config_build_target(cargo_toml, extra_env, sysroot) {
2224
Some(it) => return Ok(it),
23-
None => sysroot,
25+
None => (sysroot, cargo_toml.parent().as_ref()),
2426
}
2527
}
26-
QueryConfig::Rustc(sysroot) => sysroot,
28+
QueryConfig::Rustc(sysroot, current_dir) => (sysroot, current_dir),
2729
};
28-
rustc_discover_host_triple(extra_env, sysroot).map(|it| vec![it])
30+
rustc_discover_host_triple(extra_env, sysroot, current_dir).map(|it| vec![it])
2931
}
3032

3133
fn rustc_discover_host_triple(
3234
extra_env: &FxHashMap<String, String>,
3335
sysroot: &Sysroot,
36+
current_dir: &Path,
3437
) -> anyhow::Result<String> {
35-
let mut cmd = sysroot.tool(Tool::Rustc, &std::env::current_dir()?);
38+
let mut cmd = sysroot.tool(Tool::Rustc, current_dir);
3639
cmd.envs(extra_env);
3740
cmd.arg("-vV");
3841
let stdout = utf8_stdout(&mut cmd)

src/tools/rust-analyzer/crates/project-model/src/workspace.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ impl ProjectWorkspace {
373373
project_json.sysroot_src.clone(),
374374
&config.sysroot_query_metadata,
375375
);
376-
let cfg_config = QueryConfig::Rustc(&sysroot);
377-
let data_layout_config = QueryConfig::Rustc(&sysroot);
376+
let query_config = QueryConfig::Rustc(&sysroot, project_json.path().as_ref());
378377
let toolchain = match get_toolchain_version(
379378
project_json.path(),
380379
&sysroot,
@@ -390,8 +389,8 @@ impl ProjectWorkspace {
390389
};
391390

392391
let target = config.target.as_deref();
393-
let rustc_cfg = rustc_cfg::get(cfg_config, target, &config.extra_env);
394-
let data_layout = target_data_layout::get(data_layout_config, target, &config.extra_env);
392+
let rustc_cfg = rustc_cfg::get(query_config, target, &config.extra_env);
393+
let data_layout = target_data_layout::get(query_config, target, &config.extra_env);
395394
ProjectWorkspace {
396395
kind: ProjectWorkspaceKind::Json(project_json),
397396
sysroot,
@@ -432,9 +431,9 @@ impl ProjectWorkspace {
432431
&config.extra_env,
433432
)
434433
.unwrap_or_default();
435-
let rustc_cfg = rustc_cfg::get(QueryConfig::Rustc(&sysroot), None, &config.extra_env);
436-
let data_layout =
437-
target_data_layout::get(QueryConfig::Rustc(&sysroot), None, &config.extra_env);
434+
let query_config = QueryConfig::Rustc(&sysroot, dir.as_ref());
435+
let rustc_cfg = rustc_cfg::get(query_config, None, &config.extra_env);
436+
let data_layout = target_data_layout::get(query_config, None, &config.extra_env);
438437

439438
let cargo_script = CargoWorkspace::fetch_metadata(
440439
detached_file,
@@ -946,7 +945,11 @@ fn project_json_to_crate_graph(
946945

947946
let target_cfgs = match target.as_deref() {
948947
Some(target) => cfg_cache.entry(target).or_insert_with(|| {
949-
rustc_cfg::get(QueryConfig::Rustc(sysroot), Some(target), extra_env)
948+
rustc_cfg::get(
949+
QueryConfig::Rustc(sysroot, project.project_root().as_ref()),
950+
Some(target),
951+
extra_env,
952+
)
950953
}),
951954
None => &rustc_cfg,
952955
};

src/tools/rust-analyzer/crates/rust-analyzer/src/cli/rustc_tests.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ impl Tester {
7979
&cargo_config.extra_env,
8080
&SysrootQueryMetadata::CargoMetadata(Default::default()),
8181
);
82-
let data_layout =
83-
target_data_layout::get(QueryConfig::Rustc(&sysroot), None, &cargo_config.extra_env);
82+
let data_layout = target_data_layout::get(
83+
QueryConfig::Rustc(&sysroot, tmp_file.parent().unwrap().as_ref()),
84+
None,
85+
&cargo_config.extra_env,
86+
);
8487

8588
let workspace = ProjectWorkspace {
8689
kind: ProjectWorkspaceKind::DetachedFile {

0 commit comments

Comments
 (0)