Skip to content

Commit 23f19a2

Browse files
committed
Auto merge of rust-lang#17119 - Veykril:linked-rust-files, r=Veykril
internal: Extract common fields out of `ProjectWorkspace` variants
2 parents 3c0f2db + 0a29e6f commit 23f19a2

File tree

16 files changed

+280
-348
lines changed

16 files changed

+280
-348
lines changed

Diff for: src/tools/rust-analyzer/crates/flycheck/src/lib.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ impl FlycheckHandle {
125125
config: FlycheckConfig,
126126
sysroot_root: Option<AbsPathBuf>,
127127
workspace_root: AbsPathBuf,
128+
manifest_path: Option<AbsPathBuf>,
128129
) -> FlycheckHandle {
129-
let actor = FlycheckActor::new(id, sender, config, sysroot_root, workspace_root);
130+
let actor =
131+
FlycheckActor::new(id, sender, config, sysroot_root, workspace_root, manifest_path);
130132
let (sender, receiver) = unbounded::<StateChange>();
131133
let thread = stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker)
132134
.name("Flycheck".to_owned())
@@ -205,6 +207,7 @@ struct FlycheckActor {
205207
id: usize,
206208
sender: Box<dyn Fn(Message) + Send>,
207209
config: FlycheckConfig,
210+
manifest_path: Option<AbsPathBuf>,
208211
/// Either the workspace root of the workspace we are flychecking,
209212
/// or the project root of the project.
210213
root: AbsPathBuf,
@@ -233,6 +236,7 @@ impl FlycheckActor {
233236
config: FlycheckConfig,
234237
sysroot_root: Option<AbsPathBuf>,
235238
workspace_root: AbsPathBuf,
239+
manifest_path: Option<AbsPathBuf>,
236240
) -> FlycheckActor {
237241
tracing::info!(%id, ?workspace_root, "Spawning flycheck");
238242
FlycheckActor {
@@ -241,6 +245,7 @@ impl FlycheckActor {
241245
config,
242246
sysroot_root,
243247
root: workspace_root,
248+
manifest_path,
244249
command_handle: None,
245250
command_receiver: None,
246251
}
@@ -388,8 +393,13 @@ impl FlycheckActor {
388393
"--message-format=json"
389394
});
390395

391-
cmd.arg("--manifest-path");
392-
cmd.arg(self.root.join("Cargo.toml"));
396+
if let Some(manifest_path) = &self.manifest_path {
397+
cmd.arg("--manifest-path");
398+
cmd.arg(manifest_path);
399+
if manifest_path.extension().map_or(false, |ext| ext == "rs") {
400+
cmd.arg("-Zscript");
401+
}
402+
}
393403

394404
options.apply_on_command(&mut cmd);
395405
(cmd, options.extra_args.clone())

Diff for: src/tools/rust-analyzer/crates/load-cargo/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,7 @@ fn load_crate_graph(
333333
vfs: &mut vfs::Vfs,
334334
receiver: &Receiver<vfs::loader::Message>,
335335
) -> RootDatabase {
336-
let (ProjectWorkspace::Cargo { toolchain, target_layout, .. }
337-
| ProjectWorkspace::Json { toolchain, target_layout, .. }
338-
| ProjectWorkspace::DetachedFile { toolchain, target_layout, .. }) = ws;
336+
let ProjectWorkspace { toolchain, target_layout, .. } = ws;
339337

340338
let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
341339
let mut db = RootDatabase::new(lru_cap);

Diff for: src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl WorkspaceBuildScripts {
116116
}
117117
}
118118

119-
if manifest_path.extension().map_or(false, |ext| ext == "rs") {
119+
if manifest_path.is_rust_manifest() {
120120
cmd.arg("-Zscript");
121121
}
122122

Diff for: src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ impl CargoWorkspace {
307307
);
308308
}
309309
// The manifest is a rust file, so this means its a script manifest
310-
if cargo_toml.extension().is_some_and(|ext| ext == "rs") {
310+
if cargo_toml.is_rust_manifest() {
311311
// Deliberately don't set up RUSTC_BOOTSTRAP or a nightly override here, the user should
312312
// opt into it themselves.
313313
other_options.push("-Zscript".to_owned());

Diff for: src/tools/rust-analyzer/crates/project-model/src/env.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,19 @@ pub(crate) fn inject_rustc_tool_env(env: &mut Env, cargo_name: &str, kind: Targe
6060
}
6161

6262
pub(crate) fn cargo_config_env(
63-
cargo_toml: &ManifestPath,
63+
manifest: &ManifestPath,
6464
extra_env: &FxHashMap<String, String>,
6565
sysroot: Option<&Sysroot>,
6666
) -> FxHashMap<String, String> {
6767
let mut cargo_config = Sysroot::tool(sysroot, Tool::Cargo);
6868
cargo_config.envs(extra_env);
6969
cargo_config
70-
.current_dir(cargo_toml.parent())
70+
.current_dir(manifest.parent())
7171
.args(["-Z", "unstable-options", "config", "get", "env"])
7272
.env("RUSTC_BOOTSTRAP", "1");
73+
if manifest.is_rust_manifest() {
74+
cargo_config.arg("-Zscript");
75+
}
7376
// if successful we receive `env.key.value = "value" per entry
7477
tracing::debug!("Discovering cargo config env by {:?}", cargo_config);
7578
utf8_stdout(cargo_config).map(parse_output_cargo_config_env).unwrap_or_default()

Diff for: src/tools/rust-analyzer/crates/project-model/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub use crate::{
5252
manifest_path::ManifestPath,
5353
project_json::{ProjectJson, ProjectJsonData},
5454
sysroot::Sysroot,
55-
workspace::{FileLoader, PackageRoot, ProjectWorkspace},
55+
workspace::{FileLoader, PackageRoot, ProjectWorkspace, ProjectWorkspaceKind},
5656
};
5757
pub use cargo_metadata::Metadata;
5858

Diff for: src/tools/rust-analyzer/crates/project-model/src/manifest_path.rs

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ impl ManifestPath {
3838
pub fn canonicalize(&self) -> ! {
3939
(**self).canonicalize()
4040
}
41+
42+
pub fn is_rust_manifest(&self) -> bool {
43+
self.file.extension().map_or(false, |ext| ext == "rs")
44+
}
4145
}
4246

4347
impl fmt::Display for ManifestPath {

Diff for: src/tools/rust-analyzer/crates/project-model/src/tests.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use serde::de::DeserializeOwned;
1010
use triomphe::Arc;
1111

1212
use crate::{
13-
CargoWorkspace, CfgOverrides, ManifestPath, ProjectJson, ProjectJsonData, ProjectWorkspace,
14-
Sysroot, WorkspaceBuildScripts,
13+
workspace::ProjectWorkspaceKind, CargoWorkspace, CfgOverrides, ManifestPath, ProjectJson,
14+
ProjectJsonData, ProjectWorkspace, Sysroot, WorkspaceBuildScripts,
1515
};
1616

1717
fn load_cargo(file: &str) -> (CrateGraph, ProcMacroPaths) {
@@ -26,16 +26,18 @@ fn load_cargo_with_overrides(
2626
let manifest_path =
2727
ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap();
2828
let cargo_workspace = CargoWorkspace::new(meta, manifest_path);
29-
let project_workspace = ProjectWorkspace::Cargo {
30-
cargo: cargo_workspace,
31-
build_scripts: WorkspaceBuildScripts::default(),
29+
let project_workspace = ProjectWorkspace {
30+
kind: ProjectWorkspaceKind::Cargo {
31+
cargo: cargo_workspace,
32+
build_scripts: WorkspaceBuildScripts::default(),
33+
rustc: Err(None),
34+
cargo_config_extra_env: Default::default(),
35+
},
36+
cfg_overrides,
3237
sysroot: Err(None),
33-
rustc: Err(None),
3438
rustc_cfg: Vec::new(),
35-
cfg_overrides,
3639
toolchain: None,
3740
target_layout: Err("target_data_layout not loaded".into()),
38-
cargo_config_extra_env: Default::default(),
3941
};
4042
to_crate_graph(project_workspace)
4143
}
@@ -48,16 +50,18 @@ fn load_cargo_with_fake_sysroot(
4850
let manifest_path =
4951
ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap();
5052
let cargo_workspace = CargoWorkspace::new(meta, manifest_path);
51-
let project_workspace = ProjectWorkspace::Cargo {
52-
cargo: cargo_workspace,
53-
build_scripts: WorkspaceBuildScripts::default(),
53+
let project_workspace = ProjectWorkspace {
54+
kind: ProjectWorkspaceKind::Cargo {
55+
cargo: cargo_workspace,
56+
build_scripts: WorkspaceBuildScripts::default(),
57+
rustc: Err(None),
58+
cargo_config_extra_env: Default::default(),
59+
},
5460
sysroot: Ok(get_fake_sysroot()),
55-
rustc: Err(None),
5661
rustc_cfg: Vec::new(),
5762
cfg_overrides: Default::default(),
5863
toolchain: None,
5964
target_layout: Err("target_data_layout not loaded".into()),
60-
cargo_config_extra_env: Default::default(),
6165
};
6266
project_workspace.to_crate_graph(
6367
&mut {
@@ -74,8 +78,8 @@ fn load_rust_project(file: &str) -> (CrateGraph, ProcMacroPaths) {
7478
let data = get_test_json_file(file);
7579
let project = rooted_project_json(data);
7680
let sysroot = Ok(get_fake_sysroot());
77-
let project_workspace = ProjectWorkspace::Json {
78-
project,
81+
let project_workspace = ProjectWorkspace {
82+
kind: ProjectWorkspaceKind::Json(project),
7983
sysroot,
8084
rustc_cfg: Vec::new(),
8185
toolchain: None,
@@ -284,16 +288,18 @@ fn smoke_test_real_sysroot_cargo() {
284288
)
285289
.unwrap());
286290

287-
let project_workspace = ProjectWorkspace::Cargo {
288-
cargo: cargo_workspace,
289-
build_scripts: WorkspaceBuildScripts::default(),
291+
let project_workspace = ProjectWorkspace {
292+
kind: ProjectWorkspaceKind::Cargo {
293+
cargo: cargo_workspace,
294+
build_scripts: WorkspaceBuildScripts::default(),
295+
rustc: Err(None),
296+
cargo_config_extra_env: Default::default(),
297+
},
290298
sysroot,
291-
rustc: Err(None),
292299
rustc_cfg: Vec::new(),
293300
cfg_overrides: Default::default(),
294301
toolchain: None,
295302
target_layout: Err("target_data_layout not loaded".into()),
296-
cargo_config_extra_env: Default::default(),
297303
};
298304
project_workspace.to_crate_graph(
299305
&mut {

0 commit comments

Comments
 (0)