diff options
author | Aleksey Kladov <[email protected]> | 2020-06-24 12:34:24 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-06-24 13:39:34 +0100 |
commit | 154cb8243b4212fddf73b3b3c48b5f5e8a712876 (patch) | |
tree | f4ec79c17c8a9432b2d2cfc615ce8b7205d76ed7 /crates/ra_project_model | |
parent | dff62def2ea31e7eb0b010c35d60cd53318eab80 (diff) |
Be more explicit about absolute paths at various places
Diffstat (limited to 'crates/ra_project_model')
-rw-r--r-- | crates/ra_project_model/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_project_model/src/cargo_workspace.rs | 42 | ||||
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 51 | ||||
-rw-r--r-- | crates/ra_project_model/src/sysroot.rs | 23 |
4 files changed, 59 insertions, 58 deletions
diff --git a/crates/ra_project_model/Cargo.toml b/crates/ra_project_model/Cargo.toml index e4a60f4c0..818947014 100644 --- a/crates/ra_project_model/Cargo.toml +++ b/crates/ra_project_model/Cargo.toml | |||
@@ -18,6 +18,7 @@ ra_cfg = { path = "../ra_cfg" } | |||
18 | ra_db = { path = "../ra_db" } | 18 | ra_db = { path = "../ra_db" } |
19 | ra_toolchain = { path = "../ra_toolchain" } | 19 | ra_toolchain = { path = "../ra_toolchain" } |
20 | ra_proc_macro = { path = "../ra_proc_macro" } | 20 | ra_proc_macro = { path = "../ra_proc_macro" } |
21 | paths = { path = "../paths" } | ||
21 | 22 | ||
22 | serde = { version = "1.0.106", features = ["derive"] } | 23 | serde = { version = "1.0.106", features = ["derive"] } |
23 | serde_json = "1.0.48" | 24 | serde_json = "1.0.48" |
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 4b7444039..8ce63ab3c 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs | |||
@@ -1,14 +1,10 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use std::{ | 3 | use std::{ffi::OsStr, ops, path::Path, process::Command}; |
4 | ffi::OsStr, | ||
5 | ops, | ||
6 | path::{Path, PathBuf}, | ||
7 | process::Command, | ||
8 | }; | ||
9 | 4 | ||
10 | use anyhow::{Context, Result}; | 5 | use anyhow::{Context, Result}; |
11 | use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; | 6 | use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; |
7 | use paths::{AbsPath, AbsPathBuf}; | ||
12 | use ra_arena::{Arena, Idx}; | 8 | use ra_arena::{Arena, Idx}; |
13 | use ra_db::Edition; | 9 | use ra_db::Edition; |
14 | use rustc_hash::FxHashMap; | 10 | use rustc_hash::FxHashMap; |
@@ -20,11 +16,14 @@ use rustc_hash::FxHashMap; | |||
20 | /// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates, | 16 | /// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates, |
21 | /// while this knows about `Packages` & `Targets`: purely cargo-related | 17 | /// while this knows about `Packages` & `Targets`: purely cargo-related |
22 | /// concepts. | 18 | /// concepts. |
19 | /// | ||
20 | /// We use absolute paths here, `cargo metadata` guarantees to always produce | ||
21 | /// abs paths. | ||
23 | #[derive(Debug, Clone)] | 22 | #[derive(Debug, Clone)] |
24 | pub struct CargoWorkspace { | 23 | pub struct CargoWorkspace { |
25 | packages: Arena<PackageData>, | 24 | packages: Arena<PackageData>, |
26 | targets: Arena<TargetData>, | 25 | targets: Arena<TargetData>, |
27 | workspace_root: PathBuf, | 26 | workspace_root: AbsPathBuf, |
28 | } | 27 | } |
29 | 28 | ||
30 | impl ops::Index<Package> for CargoWorkspace { | 29 | impl ops::Index<Package> for CargoWorkspace { |
@@ -80,15 +79,15 @@ pub type Target = Idx<TargetData>; | |||
80 | pub struct PackageData { | 79 | pub struct PackageData { |
81 | pub version: String, | 80 | pub version: String, |
82 | pub name: String, | 81 | pub name: String, |
83 | pub manifest: PathBuf, | 82 | pub manifest: AbsPathBuf, |
84 | pub targets: Vec<Target>, | 83 | pub targets: Vec<Target>, |
85 | pub is_member: bool, | 84 | pub is_member: bool, |
86 | pub dependencies: Vec<PackageDependency>, | 85 | pub dependencies: Vec<PackageDependency>, |
87 | pub edition: Edition, | 86 | pub edition: Edition, |
88 | pub features: Vec<String>, | 87 | pub features: Vec<String>, |
89 | pub cfgs: Vec<String>, | 88 | pub cfgs: Vec<String>, |
90 | pub out_dir: Option<PathBuf>, | 89 | pub out_dir: Option<AbsPathBuf>, |
91 | pub proc_macro_dylib_path: Option<PathBuf>, | 90 | pub proc_macro_dylib_path: Option<AbsPathBuf>, |
92 | } | 91 | } |
93 | 92 | ||
94 | #[derive(Debug, Clone)] | 93 | #[derive(Debug, Clone)] |
@@ -101,7 +100,7 @@ pub struct PackageDependency { | |||
101 | pub struct TargetData { | 100 | pub struct TargetData { |
102 | pub package: Package, | 101 | pub package: Package, |
103 | pub name: String, | 102 | pub name: String, |
104 | pub root: PathBuf, | 103 | pub root: AbsPathBuf, |
105 | pub kind: TargetKind, | 104 | pub kind: TargetKind, |
106 | pub is_proc_macro: bool, | 105 | pub is_proc_macro: bool, |
107 | } | 106 | } |
@@ -135,7 +134,7 @@ impl TargetKind { | |||
135 | } | 134 | } |
136 | 135 | ||
137 | impl PackageData { | 136 | impl PackageData { |
138 | pub fn root(&self) -> &Path { | 137 | pub fn root(&self) -> &AbsPath { |
139 | self.manifest.parent().unwrap() | 138 | self.manifest.parent().unwrap() |
140 | } | 139 | } |
141 | } | 140 | } |
@@ -193,7 +192,7 @@ impl CargoWorkspace { | |||
193 | let pkg = packages.alloc(PackageData { | 192 | let pkg = packages.alloc(PackageData { |
194 | name, | 193 | name, |
195 | version: version.to_string(), | 194 | version: version.to_string(), |
196 | manifest: manifest_path, | 195 | manifest: AbsPathBuf::assert(manifest_path), |
197 | targets: Vec::new(), | 196 | targets: Vec::new(), |
198 | is_member, | 197 | is_member, |
199 | edition, | 198 | edition, |
@@ -210,7 +209,7 @@ impl CargoWorkspace { | |||
210 | let tgt = targets.alloc(TargetData { | 209 | let tgt = targets.alloc(TargetData { |
211 | package: pkg, | 210 | package: pkg, |
212 | name: meta_tgt.name, | 211 | name: meta_tgt.name, |
213 | root: meta_tgt.src_path.clone(), | 212 | root: AbsPathBuf::assert(meta_tgt.src_path.clone()), |
214 | kind: TargetKind::new(meta_tgt.kind.as_slice()), | 213 | kind: TargetKind::new(meta_tgt.kind.as_slice()), |
215 | is_proc_macro, | 214 | is_proc_macro, |
216 | }); | 215 | }); |
@@ -246,16 +245,17 @@ impl CargoWorkspace { | |||
246 | packages[source].features.extend(node.features); | 245 | packages[source].features.extend(node.features); |
247 | } | 246 | } |
248 | 247 | ||
249 | Ok(CargoWorkspace { packages, targets, workspace_root: meta.workspace_root }) | 248 | let workspace_root = AbsPathBuf::assert(meta.workspace_root); |
249 | Ok(CargoWorkspace { packages, targets, workspace_root: workspace_root }) | ||
250 | } | 250 | } |
251 | 251 | ||
252 | pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a { | 252 | pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a { |
253 | self.packages.iter().map(|(id, _pkg)| id) | 253 | self.packages.iter().map(|(id, _pkg)| id) |
254 | } | 254 | } |
255 | 255 | ||
256 | pub fn target_by_root(&self, root: &Path) -> Option<Target> { | 256 | pub fn target_by_root(&self, root: &AbsPath) -> Option<Target> { |
257 | self.packages() | 257 | self.packages() |
258 | .filter_map(|pkg| self[pkg].targets.iter().find(|&&it| self[it].root == root)) | 258 | .filter_map(|pkg| self[pkg].targets.iter().find(|&&it| &self[it].root == root)) |
259 | .next() | 259 | .next() |
260 | .copied() | 260 | .copied() |
261 | } | 261 | } |
@@ -279,8 +279,8 @@ impl CargoWorkspace { | |||
279 | 279 | ||
280 | #[derive(Debug, Clone, Default)] | 280 | #[derive(Debug, Clone, Default)] |
281 | pub struct ExternResources { | 281 | pub struct ExternResources { |
282 | out_dirs: FxHashMap<PackageId, PathBuf>, | 282 | out_dirs: FxHashMap<PackageId, AbsPathBuf>, |
283 | proc_dylib_paths: FxHashMap<PackageId, PathBuf>, | 283 | proc_dylib_paths: FxHashMap<PackageId, AbsPathBuf>, |
284 | cfgs: FxHashMap<PackageId, Vec<String>>, | 284 | cfgs: FxHashMap<PackageId, Vec<String>>, |
285 | } | 285 | } |
286 | 286 | ||
@@ -308,6 +308,7 @@ pub fn load_extern_resources( | |||
308 | if let Ok(message) = message { | 308 | if let Ok(message) = message { |
309 | match message { | 309 | match message { |
310 | Message::BuildScriptExecuted(BuildScript { package_id, out_dir, cfgs, .. }) => { | 310 | Message::BuildScriptExecuted(BuildScript { package_id, out_dir, cfgs, .. }) => { |
311 | let out_dir = AbsPathBuf::assert(out_dir); | ||
311 | res.out_dirs.insert(package_id.clone(), out_dir); | 312 | res.out_dirs.insert(package_id.clone(), out_dir); |
312 | res.cfgs.insert(package_id, cfgs); | 313 | res.cfgs.insert(package_id, cfgs); |
313 | } | 314 | } |
@@ -317,7 +318,8 @@ pub fn load_extern_resources( | |||
317 | // Skip rmeta file | 318 | // Skip rmeta file |
318 | if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name)) | 319 | if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name)) |
319 | { | 320 | { |
320 | res.proc_dylib_paths.insert(package_id, filename.clone()); | 321 | let filename = AbsPathBuf::assert(filename.clone()); |
322 | res.proc_dylib_paths.insert(package_id, filename); | ||
321 | } | 323 | } |
322 | } | 324 | } |
323 | } | 325 | } |
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index fe3e81689..ac88532f0 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs | |||
@@ -7,11 +7,12 @@ mod sysroot; | |||
7 | use std::{ | 7 | use std::{ |
8 | fs::{read_dir, File, ReadDir}, | 8 | fs::{read_dir, File, ReadDir}, |
9 | io::{self, BufReader}, | 9 | io::{self, BufReader}, |
10 | path::{Path, PathBuf}, | 10 | path::Path, |
11 | process::{Command, Output}, | 11 | process::{Command, Output}, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | use anyhow::{bail, Context, Result}; | 14 | use anyhow::{bail, Context, Result}; |
15 | use paths::{AbsPath, AbsPathBuf}; | ||
15 | use ra_cfg::CfgOptions; | 16 | use ra_cfg::CfgOptions; |
16 | use ra_db::{CrateGraph, CrateName, Edition, Env, FileId}; | 17 | use ra_db::{CrateGraph, CrateName, Edition, Env, FileId}; |
17 | use rustc_hash::{FxHashMap, FxHashSet}; | 18 | use rustc_hash::{FxHashMap, FxHashSet}; |
@@ -29,7 +30,7 @@ pub enum ProjectWorkspace { | |||
29 | /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`. | 30 | /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`. |
30 | Cargo { cargo: CargoWorkspace, sysroot: Sysroot }, | 31 | Cargo { cargo: CargoWorkspace, sysroot: Sysroot }, |
31 | /// Project workspace was manually specified using a `rust-project.json` file. | 32 | /// Project workspace was manually specified using a `rust-project.json` file. |
32 | Json { project: JsonProject, project_location: PathBuf }, | 33 | Json { project: JsonProject, project_location: AbsPathBuf }, |
33 | } | 34 | } |
34 | 35 | ||
35 | /// `PackageRoot` describes a package root folder. | 36 | /// `PackageRoot` describes a package root folder. |
@@ -38,22 +39,22 @@ pub enum ProjectWorkspace { | |||
38 | #[derive(Debug, Clone)] | 39 | #[derive(Debug, Clone)] |
39 | pub struct PackageRoot { | 40 | pub struct PackageRoot { |
40 | /// Path to the root folder | 41 | /// Path to the root folder |
41 | path: PathBuf, | 42 | path: AbsPathBuf, |
42 | /// Is a member of the current workspace | 43 | /// Is a member of the current workspace |
43 | is_member: bool, | 44 | is_member: bool, |
44 | out_dir: Option<PathBuf>, | 45 | out_dir: Option<AbsPathBuf>, |
45 | } | 46 | } |
46 | impl PackageRoot { | 47 | impl PackageRoot { |
47 | pub fn new_member(path: PathBuf) -> PackageRoot { | 48 | pub fn new_member(path: AbsPathBuf) -> PackageRoot { |
48 | Self { path, is_member: true, out_dir: None } | 49 | Self { path, is_member: true, out_dir: None } |
49 | } | 50 | } |
50 | pub fn new_non_member(path: PathBuf) -> PackageRoot { | 51 | pub fn new_non_member(path: AbsPathBuf) -> PackageRoot { |
51 | Self { path, is_member: false, out_dir: None } | 52 | Self { path, is_member: false, out_dir: None } |
52 | } | 53 | } |
53 | pub fn path(&self) -> &Path { | 54 | pub fn path(&self) -> &AbsPath { |
54 | &self.path | 55 | &self.path |
55 | } | 56 | } |
56 | pub fn out_dir(&self) -> Option<&Path> { | 57 | pub fn out_dir(&self) -> Option<&AbsPath> { |
57 | self.out_dir.as_deref() | 58 | self.out_dir.as_deref() |
58 | } | 59 | } |
59 | pub fn is_member(&self) -> bool { | 60 | pub fn is_member(&self) -> bool { |
@@ -63,12 +64,12 @@ impl PackageRoot { | |||
63 | 64 | ||
64 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] | 65 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] |
65 | pub enum ProjectManifest { | 66 | pub enum ProjectManifest { |
66 | ProjectJson(PathBuf), | 67 | ProjectJson(AbsPathBuf), |
67 | CargoToml(PathBuf), | 68 | CargoToml(AbsPathBuf), |
68 | } | 69 | } |
69 | 70 | ||
70 | impl ProjectManifest { | 71 | impl ProjectManifest { |
71 | pub fn from_manifest_file(path: PathBuf) -> Result<ProjectManifest> { | 72 | pub fn from_manifest_file(path: AbsPathBuf) -> Result<ProjectManifest> { |
72 | if path.ends_with("rust-project.json") { | 73 | if path.ends_with("rust-project.json") { |
73 | return Ok(ProjectManifest::ProjectJson(path)); | 74 | return Ok(ProjectManifest::ProjectJson(path)); |
74 | } | 75 | } |
@@ -78,7 +79,7 @@ impl ProjectManifest { | |||
78 | bail!("project root must point to Cargo.toml or rust-project.json: {}", path.display()) | 79 | bail!("project root must point to Cargo.toml or rust-project.json: {}", path.display()) |
79 | } | 80 | } |
80 | 81 | ||
81 | pub fn discover_single(path: &Path) -> Result<ProjectManifest> { | 82 | pub fn discover_single(path: &AbsPath) -> Result<ProjectManifest> { |
82 | let mut candidates = ProjectManifest::discover(path)?; | 83 | let mut candidates = ProjectManifest::discover(path)?; |
83 | let res = match candidates.pop() { | 84 | let res = match candidates.pop() { |
84 | None => bail!("no projects"), | 85 | None => bail!("no projects"), |
@@ -91,23 +92,23 @@ impl ProjectManifest { | |||
91 | Ok(res) | 92 | Ok(res) |
92 | } | 93 | } |
93 | 94 | ||
94 | pub fn discover(path: &Path) -> io::Result<Vec<ProjectManifest>> { | 95 | pub fn discover(path: &AbsPath) -> io::Result<Vec<ProjectManifest>> { |
95 | if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") { | 96 | if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") { |
96 | return Ok(vec![ProjectManifest::ProjectJson(project_json)]); | 97 | return Ok(vec![ProjectManifest::ProjectJson(project_json)]); |
97 | } | 98 | } |
98 | return find_cargo_toml(path) | 99 | return find_cargo_toml(path) |
99 | .map(|paths| paths.into_iter().map(ProjectManifest::CargoToml).collect()); | 100 | .map(|paths| paths.into_iter().map(ProjectManifest::CargoToml).collect()); |
100 | 101 | ||
101 | fn find_cargo_toml(path: &Path) -> io::Result<Vec<PathBuf>> { | 102 | fn find_cargo_toml(path: &AbsPath) -> io::Result<Vec<AbsPathBuf>> { |
102 | match find_in_parent_dirs(path, "Cargo.toml") { | 103 | match find_in_parent_dirs(path, "Cargo.toml") { |
103 | Some(it) => Ok(vec![it]), | 104 | Some(it) => Ok(vec![it]), |
104 | None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)), | 105 | None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)), |
105 | } | 106 | } |
106 | } | 107 | } |
107 | 108 | ||
108 | fn find_in_parent_dirs(path: &Path, target_file_name: &str) -> Option<PathBuf> { | 109 | fn find_in_parent_dirs(path: &AbsPath, target_file_name: &str) -> Option<AbsPathBuf> { |
109 | if path.ends_with(target_file_name) { | 110 | if path.ends_with(target_file_name) { |
110 | return Some(path.to_owned()); | 111 | return Some(path.to_path_buf()); |
111 | } | 112 | } |
112 | 113 | ||
113 | let mut curr = Some(path); | 114 | let mut curr = Some(path); |
@@ -123,17 +124,18 @@ impl ProjectManifest { | |||
123 | None | 124 | None |
124 | } | 125 | } |
125 | 126 | ||
126 | fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> { | 127 | fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<AbsPathBuf> { |
127 | // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects | 128 | // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects |
128 | entities | 129 | entities |
129 | .filter_map(Result::ok) | 130 | .filter_map(Result::ok) |
130 | .map(|it| it.path().join("Cargo.toml")) | 131 | .map(|it| it.path().join("Cargo.toml")) |
131 | .filter(|it| it.exists()) | 132 | .filter(|it| it.exists()) |
133 | .map(AbsPathBuf::assert) | ||
132 | .collect() | 134 | .collect() |
133 | } | 135 | } |
134 | } | 136 | } |
135 | 137 | ||
136 | pub fn discover_all(paths: &[impl AsRef<Path>]) -> Vec<ProjectManifest> { | 138 | pub fn discover_all(paths: &[impl AsRef<AbsPath>]) -> Vec<ProjectManifest> { |
137 | let mut res = paths | 139 | let mut res = paths |
138 | .iter() | 140 | .iter() |
139 | .filter_map(|it| ProjectManifest::discover(it.as_ref()).ok()) | 141 | .filter_map(|it| ProjectManifest::discover(it.as_ref()).ok()) |
@@ -158,15 +160,12 @@ impl ProjectWorkspace { | |||
158 | format!("Failed to open json file {}", project_json.display()) | 160 | format!("Failed to open json file {}", project_json.display()) |
159 | })?; | 161 | })?; |
160 | let reader = BufReader::new(file); | 162 | let reader = BufReader::new(file); |
161 | let project_location = match project_json.parent() { | 163 | let project_location = project_json.parent().unwrap().to_path_buf(); |
162 | Some(parent) => PathBuf::from(parent), | ||
163 | None => PathBuf::new(), | ||
164 | }; | ||
165 | ProjectWorkspace::Json { | 164 | ProjectWorkspace::Json { |
166 | project: from_reader(reader).with_context(|| { | 165 | project: from_reader(reader).with_context(|| { |
167 | format!("Failed to deserialize json file {}", project_json.display()) | 166 | format!("Failed to deserialize json file {}", project_json.display()) |
168 | })?, | 167 | })?, |
169 | project_location: project_location, | 168 | project_location, |
170 | } | 169 | } |
171 | } | 170 | } |
172 | ProjectManifest::CargoToml(cargo_toml) => { | 171 | ProjectManifest::CargoToml(cargo_toml) => { |
@@ -218,13 +217,13 @@ impl ProjectWorkspace { | |||
218 | } | 217 | } |
219 | } | 218 | } |
220 | 219 | ||
221 | pub fn proc_macro_dylib_paths(&self) -> Vec<PathBuf> { | 220 | pub fn proc_macro_dylib_paths(&self) -> Vec<AbsPathBuf> { |
222 | match self { | 221 | match self { |
223 | ProjectWorkspace::Json { project, .. } => project | 222 | ProjectWorkspace::Json { project, project_location } => project |
224 | .crates | 223 | .crates |
225 | .iter() | 224 | .iter() |
226 | .filter_map(|krate| krate.proc_macro_dylib_path.as_ref()) | 225 | .filter_map(|krate| krate.proc_macro_dylib_path.as_ref()) |
227 | .cloned() | 226 | .map(|it| project_location.join(it)) |
228 | .collect(), | 227 | .collect(), |
229 | ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => cargo | 228 | ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => cargo |
230 | .packages() | 229 | .packages() |
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index a8a196e64..943ff92df 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs | |||
@@ -1,15 +1,12 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use std::{ | 3 | use std::{convert::TryFrom, env, ops, path::Path, process::Command}; |
4 | env, ops, | ||
5 | path::{Path, PathBuf}, | ||
6 | process::Command, | ||
7 | }; | ||
8 | 4 | ||
9 | use anyhow::{bail, Result}; | 5 | use anyhow::{bail, format_err, Result}; |
10 | use ra_arena::{Arena, Idx}; | 6 | use ra_arena::{Arena, Idx}; |
11 | 7 | ||
12 | use crate::output; | 8 | use crate::output; |
9 | use paths::{AbsPath, AbsPathBuf}; | ||
13 | 10 | ||
14 | #[derive(Default, Debug, Clone)] | 11 | #[derive(Default, Debug, Clone)] |
15 | pub struct Sysroot { | 12 | pub struct Sysroot { |
@@ -21,7 +18,7 @@ pub type SysrootCrate = Idx<SysrootCrateData>; | |||
21 | #[derive(Debug, Clone)] | 18 | #[derive(Debug, Clone)] |
22 | pub struct SysrootCrateData { | 19 | pub struct SysrootCrateData { |
23 | pub name: String, | 20 | pub name: String, |
24 | pub root: PathBuf, | 21 | pub root: AbsPathBuf, |
25 | pub deps: Vec<SysrootCrate>, | 22 | pub deps: Vec<SysrootCrate>, |
26 | } | 23 | } |
27 | 24 | ||
@@ -53,7 +50,7 @@ impl Sysroot { | |||
53 | self.crates.iter().map(|(id, _data)| id) | 50 | self.crates.iter().map(|(id, _data)| id) |
54 | } | 51 | } |
55 | 52 | ||
56 | pub fn discover(cargo_toml: &Path) -> Result<Sysroot> { | 53 | pub fn discover(cargo_toml: &AbsPath) -> Result<Sysroot> { |
57 | let src = get_or_install_rust_src(cargo_toml)?; | 54 | let src = get_or_install_rust_src(cargo_toml)?; |
58 | let mut sysroot = Sysroot { crates: Arena::default() }; | 55 | let mut sysroot = Sysroot { crates: Arena::default() }; |
59 | for name in SYSROOT_CRATES.trim().lines() { | 56 | for name in SYSROOT_CRATES.trim().lines() { |
@@ -86,16 +83,18 @@ impl Sysroot { | |||
86 | } | 83 | } |
87 | } | 84 | } |
88 | 85 | ||
89 | fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> { | 86 | fn get_or_install_rust_src(cargo_toml: &AbsPath) -> Result<AbsPathBuf> { |
90 | if let Ok(path) = env::var("RUST_SRC_PATH") { | 87 | if let Ok(path) = env::var("RUST_SRC_PATH") { |
91 | return Ok(path.into()); | 88 | let path = AbsPathBuf::try_from(path.as_str()) |
89 | .map_err(|path| format_err!("RUST_SRC_PATH must be absolute: {}", path.display()))?; | ||
90 | return Ok(path); | ||
92 | } | 91 | } |
93 | let current_dir = cargo_toml.parent().unwrap(); | 92 | let current_dir = cargo_toml.parent().unwrap(); |
94 | let mut rustc = Command::new(ra_toolchain::rustc()); | 93 | let mut rustc = Command::new(ra_toolchain::rustc()); |
95 | rustc.current_dir(current_dir).args(&["--print", "sysroot"]); | 94 | rustc.current_dir(current_dir).args(&["--print", "sysroot"]); |
96 | let rustc_output = output(rustc)?; | 95 | let rustc_output = output(rustc)?; |
97 | let stdout = String::from_utf8(rustc_output.stdout)?; | 96 | let stdout = String::from_utf8(rustc_output.stdout)?; |
98 | let sysroot_path = Path::new(stdout.trim()); | 97 | let sysroot_path = AbsPath::assert(Path::new(stdout.trim())); |
99 | let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); | 98 | let src_path = sysroot_path.join("lib/rustlib/src/rust/src"); |
100 | 99 | ||
101 | if !src_path.exists() { | 100 | if !src_path.exists() { |
@@ -116,7 +115,7 @@ fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> { | |||
116 | } | 115 | } |
117 | 116 | ||
118 | impl SysrootCrateData { | 117 | impl SysrootCrateData { |
119 | pub fn root_dir(&self) -> &Path { | 118 | pub fn root_dir(&self) -> &AbsPath { |
120 | self.root.parent().unwrap() | 119 | self.root.parent().unwrap() |
121 | } | 120 | } |
122 | } | 121 | } |