aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/project_model/src/lib.rs')
-rw-r--r--crates/project_model/src/lib.rs40
1 files changed, 29 insertions, 11 deletions
diff --git a/crates/project_model/src/lib.rs b/crates/project_model/src/lib.rs
index 2d91939ce..258f60e28 100644
--- a/crates/project_model/src/lib.rs
+++ b/crates/project_model/src/lib.rs
@@ -33,7 +33,7 @@ pub enum ProjectWorkspace {
33 /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`. 33 /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`.
34 Cargo { cargo: CargoWorkspace, sysroot: Sysroot }, 34 Cargo { cargo: CargoWorkspace, sysroot: Sysroot },
35 /// Project workspace was manually specified using a `rust-project.json` file. 35 /// Project workspace was manually specified using a `rust-project.json` file.
36 Json { project: ProjectJson }, 36 Json { project: ProjectJson, sysroot: Option<Sysroot> },
37} 37}
38 38
39impl fmt::Debug for ProjectWorkspace { 39impl fmt::Debug for ProjectWorkspace {
@@ -44,10 +44,10 @@ impl fmt::Debug for ProjectWorkspace {
44 .field("n_packages", &cargo.packages().len()) 44 .field("n_packages", &cargo.packages().len())
45 .field("n_sysroot_crates", &sysroot.crates().len()) 45 .field("n_sysroot_crates", &sysroot.crates().len())
46 .finish(), 46 .finish(),
47 ProjectWorkspace::Json { project } => { 47 ProjectWorkspace::Json { project, sysroot } => {
48 let mut debug_struct = f.debug_struct("Json"); 48 let mut debug_struct = f.debug_struct("Json");
49 debug_struct.field("n_crates", &project.n_crates()); 49 debug_struct.field("n_crates", &project.n_crates());
50 if let Some(sysroot) = &project.sysroot { 50 if let Some(sysroot) = sysroot {
51 debug_struct.field("n_sysroot_crates", &sysroot.crates().len()); 51 debug_struct.field("n_sysroot_crates", &sysroot.crates().len());
52 } 52 }
53 debug_struct.finish() 53 debug_struct.finish()
@@ -169,7 +169,11 @@ impl ProjectWorkspace {
169 })?; 169 })?;
170 let project_location = project_json.parent().unwrap().to_path_buf(); 170 let project_location = project_json.parent().unwrap().to_path_buf();
171 let project = ProjectJson::new(&project_location, data); 171 let project = ProjectJson::new(&project_location, data);
172 ProjectWorkspace::Json { project } 172 let sysroot = match &project.sysroot_src {
173 Some(path) => Some(Sysroot::load(path)?),
174 None => None,
175 };
176 ProjectWorkspace::Json { project, sysroot }
173 } 177 }
174 ProjectManifest::CargoToml(cargo_toml) => { 178 ProjectManifest::CargoToml(cargo_toml) => {
175 let cargo_version = utf8_stdout({ 179 let cargo_version = utf8_stdout({
@@ -203,12 +207,21 @@ impl ProjectWorkspace {
203 Ok(res) 207 Ok(res)
204 } 208 }
205 209
210 pub fn load_inline(project_json: ProjectJson) -> Result<ProjectWorkspace> {
211 let sysroot = match &project_json.sysroot_src {
212 Some(path) => Some(Sysroot::load(path)?),
213 None => None,
214 };
215
216 Ok(ProjectWorkspace::Json { project: project_json, sysroot })
217 }
218
206 /// Returns the roots for the current `ProjectWorkspace` 219 /// Returns the roots for the current `ProjectWorkspace`
207 /// The return type contains the path and whether or not 220 /// The return type contains the path and whether or not
208 /// the root is a member of the current workspace 221 /// the root is a member of the current workspace
209 pub fn to_roots(&self) -> Vec<PackageRoot> { 222 pub fn to_roots(&self) -> Vec<PackageRoot> {
210 match self { 223 match self {
211 ProjectWorkspace::Json { project } => project 224 ProjectWorkspace::Json { project, sysroot } => project
212 .crates() 225 .crates()
213 .map(|(_, krate)| PackageRoot { 226 .map(|(_, krate)| PackageRoot {
214 is_member: krate.is_workspace_member, 227 is_member: krate.is_workspace_member,
@@ -217,7 +230,7 @@ impl ProjectWorkspace {
217 }) 230 })
218 .collect::<FxHashSet<_>>() 231 .collect::<FxHashSet<_>>()
219 .into_iter() 232 .into_iter()
220 .chain(project.sysroot.as_ref().into_iter().flat_map(|sysroot| { 233 .chain(sysroot.as_ref().into_iter().flat_map(|sysroot| {
221 sysroot.crates().map(move |krate| PackageRoot { 234 sysroot.crates().map(move |krate| PackageRoot {
222 is_member: false, 235 is_member: false,
223 include: vec![sysroot[krate].root_dir().to_path_buf()], 236 include: vec![sysroot[krate].root_dir().to_path_buf()],
@@ -255,7 +268,7 @@ impl ProjectWorkspace {
255 268
256 pub fn proc_macro_dylib_paths(&self) -> Vec<AbsPathBuf> { 269 pub fn proc_macro_dylib_paths(&self) -> Vec<AbsPathBuf> {
257 match self { 270 match self {
258 ProjectWorkspace::Json { project } => project 271 ProjectWorkspace::Json { project, sysroot: _ } => project
259 .crates() 272 .crates()
260 .filter_map(|(_, krate)| krate.proc_macro_dylib_path.as_ref()) 273 .filter_map(|(_, krate)| krate.proc_macro_dylib_path.as_ref())
261 .cloned() 274 .cloned()
@@ -285,9 +298,8 @@ impl ProjectWorkspace {
285 ) -> CrateGraph { 298 ) -> CrateGraph {
286 let mut crate_graph = CrateGraph::default(); 299 let mut crate_graph = CrateGraph::default();
287 match self { 300 match self {
288 ProjectWorkspace::Json { project } => { 301 ProjectWorkspace::Json { project, sysroot } => {
289 let sysroot_dps = project 302 let sysroot_dps = sysroot
290 .sysroot
291 .as_ref() 303 .as_ref()
292 .map(|sysroot| sysroot_to_crate_graph(&mut crate_graph, sysroot, target, load)); 304 .map(|sysroot| sysroot_to_crate_graph(&mut crate_graph, sysroot, target, load));
293 305
@@ -296,7 +308,13 @@ impl ProjectWorkspace {
296 .crates() 308 .crates()
297 .filter_map(|(crate_id, krate)| { 309 .filter_map(|(crate_id, krate)| {
298 let file_path = &krate.root_module; 310 let file_path = &krate.root_module;
299 let file_id = load(&file_path)?; 311 let file_id = match load(&file_path) {
312 Some(id) => id,
313 None => {
314 log::error!("failed to load crate root {}", file_path.display());
315 return None;
316 }
317 };
300 318
301 let env = krate.env.clone().into_iter().collect(); 319 let env = krate.env.clone().into_iter().collect();
302 let proc_macro = krate 320 let proc_macro = krate