Skip to content

Commit f4f5117

Browse files
bors[bot]tweksteen
andauthored
Merge #4860
4860: Accept relative paths in rust-project.json r=matklad a=tweksteen If a relative path is found as part of Crate.root_module or Root.path, interpret it as relative to the location of the rust-project.json file. Fixes: #4816 Co-authored-by: Thiébaud Weksteen <tweek@google.com>
2 parents 5b013e5 + 44f28f6 commit f4f5117

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

crates/ra_project_model/src/lib.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ pub enum ProjectWorkspace {
2929
/// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`.
3030
Cargo { cargo: CargoWorkspace, sysroot: Sysroot },
3131
/// Project workspace was manually specified using a `rust-project.json` file.
32-
Json { project: JsonProject },
33-
}
34-
35-
impl From<JsonProject> for ProjectWorkspace {
36-
fn from(project: JsonProject) -> ProjectWorkspace {
37-
ProjectWorkspace::Json { project }
38-
}
32+
Json { project: JsonProject, project_location: PathBuf },
3933
}
4034

4135
/// `PackageRoot` describes a package root folder.
@@ -164,10 +158,15 @@ impl ProjectWorkspace {
164158
format!("Failed to open json file {}", project_json.display())
165159
})?;
166160
let reader = BufReader::new(file);
161+
let project_location = match project_json.parent() {
162+
Some(parent) => PathBuf::from(parent),
163+
None => PathBuf::new(),
164+
};
167165
ProjectWorkspace::Json {
168166
project: from_reader(reader).with_context(|| {
169167
format!("Failed to deserialize json file {}", project_json.display())
170168
})?,
169+
project_location: project_location,
171170
}
172171
}
173172
ProjectManifest::CargoToml(cargo_toml) => {
@@ -200,9 +199,11 @@ impl ProjectWorkspace {
200199
/// the root is a member of the current workspace
201200
pub fn to_roots(&self) -> Vec<PackageRoot> {
202201
match self {
203-
ProjectWorkspace::Json { project } => {
204-
project.roots.iter().map(|r| PackageRoot::new_member(r.path.clone())).collect()
205-
}
202+
ProjectWorkspace::Json { project, project_location } => project
203+
.roots
204+
.iter()
205+
.map(|r| PackageRoot::new_member(project_location.join(&r.path)))
206+
.collect(),
206207
ProjectWorkspace::Cargo { cargo, sysroot } => cargo
207208
.packages()
208209
.map(|pkg| PackageRoot {
@@ -219,7 +220,7 @@ impl ProjectWorkspace {
219220

220221
pub fn proc_macro_dylib_paths(&self) -> Vec<PathBuf> {
221222
match self {
222-
ProjectWorkspace::Json { project } => project
223+
ProjectWorkspace::Json { project, .. } => project
223224
.crates
224225
.iter()
225226
.filter_map(|krate| krate.proc_macro_dylib_path.as_ref())
@@ -235,7 +236,7 @@ impl ProjectWorkspace {
235236

236237
pub fn n_packages(&self) -> usize {
237238
match self {
238-
ProjectWorkspace::Json { project } => project.crates.len(),
239+
ProjectWorkspace::Json { project, .. } => project.crates.len(),
239240
ProjectWorkspace::Cargo { cargo, sysroot } => {
240241
cargo.packages().len() + sysroot.crates().len()
241242
}
@@ -251,13 +252,14 @@ impl ProjectWorkspace {
251252
) -> CrateGraph {
252253
let mut crate_graph = CrateGraph::default();
253254
match self {
254-
ProjectWorkspace::Json { project } => {
255+
ProjectWorkspace::Json { project, project_location } => {
255256
let crates: FxHashMap<_, _> = project
256257
.crates
257258
.iter()
258259
.enumerate()
259260
.filter_map(|(seq_index, krate)| {
260-
let file_id = load(&krate.root_module)?;
261+
let file_path = project_location.join(&krate.root_module);
262+
let file_id = load(&file_path)?;
261263
let edition = match krate.edition {
262264
json_project::Edition::Edition2015 => Edition::Edition2015,
263265
json_project::Edition::Edition2018 => Edition::Edition2018,
@@ -540,7 +542,7 @@ impl ProjectWorkspace {
540542
ProjectWorkspace::Cargo { cargo, .. } => {
541543
Some(cargo.workspace_root()).filter(|root| path.starts_with(root))
542544
}
543-
ProjectWorkspace::Json { project: JsonProject { roots, .. } } => roots
545+
ProjectWorkspace::Json { project: JsonProject { roots, .. }, .. } => roots
544546
.iter()
545547
.find(|root| path.starts_with(&root.path))
546548
.map(|root| root.path.as_ref()),

crates/rust-analyzer/src/bin/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ fn run_server() -> Result<()> {
108108
config.update(value);
109109
}
110110
config.update_caps(&initialize_params.capabilities);
111+
let cwd = std::env::current_dir()?;
112+
config.root_path =
113+
initialize_params.root_uri.and_then(|it| it.to_file_path().ok()).unwrap_or(cwd);
111114

112115
if config.linked_projects.is_empty() {
113-
let cwd = std::env::current_dir()?;
114-
let root =
115-
initialize_params.root_uri.and_then(|it| it.to_file_path().ok()).unwrap_or(cwd);
116116
let workspace_roots = initialize_params
117117
.workspace_folders
118118
.map(|workspaces| {
@@ -122,7 +122,7 @@ fn run_server() -> Result<()> {
122122
.collect::<Vec<_>>()
123123
})
124124
.filter(|workspaces| !workspaces.is_empty())
125-
.unwrap_or_else(|| vec![root]);
125+
.unwrap_or_else(|| vec![config.root_path.clone()]);
126126

127127
config.linked_projects = ProjectManifest::discover_all(&workspace_roots)
128128
.into_iter()

crates/rust-analyzer/src/config.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ pub struct Config {
3838

3939
pub with_sysroot: bool,
4040
pub linked_projects: Vec<LinkedProject>,
41+
pub root_path: PathBuf,
4142
}
4243

4344
#[derive(Debug, Clone)]
4445
pub enum LinkedProject {
4546
ProjectManifest(ProjectManifest),
46-
JsonProject(JsonProject),
47+
InlineJsonProject(JsonProject),
4748
}
4849

4950
impl From<ProjectManifest> for LinkedProject {
@@ -54,7 +55,7 @@ impl From<ProjectManifest> for LinkedProject {
5455

5556
impl From<JsonProject> for LinkedProject {
5657
fn from(v: JsonProject) -> Self {
57-
LinkedProject::JsonProject(v)
58+
LinkedProject::InlineJsonProject(v)
5859
}
5960
}
6061

@@ -167,6 +168,7 @@ impl Default for Config {
167168
lens: LensConfig::default(),
168169
hover: HoverConfig::default(),
169170
linked_projects: Vec::new(),
171+
root_path: PathBuf::new(),
170172
}
171173
}
172174
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
121121
})
122122
.ok()
123123
}
124-
LinkedProject::JsonProject(it) => Some(it.clone().into()),
124+
LinkedProject::InlineJsonProject(it) => {
125+
Some(ra_project_model::ProjectWorkspace::Json {
126+
project: it.clone(),
127+
project_location: config.root_path.clone(),
128+
})
129+
}
125130
})
126131
.collect::<Vec<_>>()
127132
};

0 commit comments

Comments
 (0)