aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-06-15 16:03:31 +0100
committerGitHub <[email protected]>2020-06-15 16:03:31 +0100
commitf4f51171ca6d99b693df2ef6fb71f0347999aa9f (patch)
treeb7dcce2438c520dc30cd90b9290e3419054ef55b /crates/ra_project_model
parent5b013e5665a34fd757fd6c48dc912606c0915b2c (diff)
parent44f28f65af15371cfe35e3599571e7217d6a9db1 (diff)
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 <[email protected]>
Diffstat (limited to 'crates/ra_project_model')
-rw-r--r--crates/ra_project_model/src/lib.rs32
1 files changed, 17 insertions, 15 deletions
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index cb0e27dce..9541362f5 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -29,13 +29,7 @@ pub enum ProjectWorkspace {
29 /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`. 29 /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`.
30 Cargo { cargo: CargoWorkspace, sysroot: Sysroot }, 30 Cargo { cargo: CargoWorkspace, sysroot: Sysroot },
31 /// Project workspace was manually specified using a `rust-project.json` file. 31 /// Project workspace was manually specified using a `rust-project.json` file.
32 Json { project: JsonProject }, 32 Json { project: JsonProject, project_location: PathBuf },
33}
34
35impl From<JsonProject> for ProjectWorkspace {
36 fn from(project: JsonProject) -> ProjectWorkspace {
37 ProjectWorkspace::Json { project }
38 }
39} 33}
40 34
41/// `PackageRoot` describes a package root folder. 35/// `PackageRoot` describes a package root folder.
@@ -164,10 +158,15 @@ impl ProjectWorkspace {
164 format!("Failed to open json file {}", project_json.display()) 158 format!("Failed to open json file {}", project_json.display())
165 })?; 159 })?;
166 let reader = BufReader::new(file); 160 let reader = BufReader::new(file);
161 let project_location = match project_json.parent() {
162 Some(parent) => PathBuf::from(parent),
163 None => PathBuf::new(),
164 };
167 ProjectWorkspace::Json { 165 ProjectWorkspace::Json {
168 project: from_reader(reader).with_context(|| { 166 project: from_reader(reader).with_context(|| {
169 format!("Failed to deserialize json file {}", project_json.display()) 167 format!("Failed to deserialize json file {}", project_json.display())
170 })?, 168 })?,
169 project_location: project_location,
171 } 170 }
172 } 171 }
173 ProjectManifest::CargoToml(cargo_toml) => { 172 ProjectManifest::CargoToml(cargo_toml) => {
@@ -200,9 +199,11 @@ impl ProjectWorkspace {
200 /// the root is a member of the current workspace 199 /// the root is a member of the current workspace
201 pub fn to_roots(&self) -> Vec<PackageRoot> { 200 pub fn to_roots(&self) -> Vec<PackageRoot> {
202 match self { 201 match self {
203 ProjectWorkspace::Json { project } => { 202 ProjectWorkspace::Json { project, project_location } => project
204 project.roots.iter().map(|r| PackageRoot::new_member(r.path.clone())).collect() 203 .roots
205 } 204 .iter()
205 .map(|r| PackageRoot::new_member(project_location.join(&r.path)))
206 .collect(),
206 ProjectWorkspace::Cargo { cargo, sysroot } => cargo 207 ProjectWorkspace::Cargo { cargo, sysroot } => cargo
207 .packages() 208 .packages()
208 .map(|pkg| PackageRoot { 209 .map(|pkg| PackageRoot {
@@ -219,7 +220,7 @@ impl ProjectWorkspace {
219 220
220 pub fn proc_macro_dylib_paths(&self) -> Vec<PathBuf> { 221 pub fn proc_macro_dylib_paths(&self) -> Vec<PathBuf> {
221 match self { 222 match self {
222 ProjectWorkspace::Json { project } => project 223 ProjectWorkspace::Json { project, .. } => project
223 .crates 224 .crates
224 .iter() 225 .iter()
225 .filter_map(|krate| krate.proc_macro_dylib_path.as_ref()) 226 .filter_map(|krate| krate.proc_macro_dylib_path.as_ref())
@@ -235,7 +236,7 @@ impl ProjectWorkspace {
235 236
236 pub fn n_packages(&self) -> usize { 237 pub fn n_packages(&self) -> usize {
237 match self { 238 match self {
238 ProjectWorkspace::Json { project } => project.crates.len(), 239 ProjectWorkspace::Json { project, .. } => project.crates.len(),
239 ProjectWorkspace::Cargo { cargo, sysroot } => { 240 ProjectWorkspace::Cargo { cargo, sysroot } => {
240 cargo.packages().len() + sysroot.crates().len() 241 cargo.packages().len() + sysroot.crates().len()
241 } 242 }
@@ -251,13 +252,14 @@ impl ProjectWorkspace {
251 ) -> CrateGraph { 252 ) -> CrateGraph {
252 let mut crate_graph = CrateGraph::default(); 253 let mut crate_graph = CrateGraph::default();
253 match self { 254 match self {
254 ProjectWorkspace::Json { project } => { 255 ProjectWorkspace::Json { project, project_location } => {
255 let crates: FxHashMap<_, _> = project 256 let crates: FxHashMap<_, _> = project
256 .crates 257 .crates
257 .iter() 258 .iter()
258 .enumerate() 259 .enumerate()
259 .filter_map(|(seq_index, krate)| { 260 .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)?;
261 let edition = match krate.edition { 263 let edition = match krate.edition {
262 json_project::Edition::Edition2015 => Edition::Edition2015, 264 json_project::Edition::Edition2015 => Edition::Edition2015,
263 json_project::Edition::Edition2018 => Edition::Edition2018, 265 json_project::Edition::Edition2018 => Edition::Edition2018,
@@ -540,7 +542,7 @@ impl ProjectWorkspace {
540 ProjectWorkspace::Cargo { cargo, .. } => { 542 ProjectWorkspace::Cargo { cargo, .. } => {
541 Some(cargo.workspace_root()).filter(|root| path.starts_with(root)) 543 Some(cargo.workspace_root()).filter(|root| path.starts_with(root))
542 } 544 }
543 ProjectWorkspace::Json { project: JsonProject { roots, .. } } => roots 545 ProjectWorkspace::Json { project: JsonProject { roots, .. }, .. } => roots
544 .iter() 546 .iter()
545 .find(|root| path.starts_with(&root.path)) 547 .find(|root| path.starts_with(&root.path))
546 .map(|root| root.path.as_ref()), 548 .map(|root| root.path.as_ref()),