]> git.lizzy.rs Git - rust.git/blob - crates/ra_project_model/src/lib.rs
Rename json_project -> project_json
[rust.git] / crates / ra_project_model / src / lib.rs
1 //! FIXME: write short doc here
2
3 mod cargo_workspace;
4 mod project_json;
5 mod sysroot;
6
7 use std::{
8     fs::{read_dir, File, ReadDir},
9     io::{self, BufReader},
10     path::Path,
11     process::{Command, Output},
12 };
13
14 use anyhow::{bail, Context, Result};
15 use paths::{AbsPath, AbsPathBuf};
16 use ra_cfg::CfgOptions;
17 use ra_db::{CrateGraph, CrateName, Edition, Env, FileId};
18 use rustc_hash::{FxHashMap, FxHashSet};
19 use serde_json::from_reader;
20
21 pub use crate::{
22     cargo_workspace::{CargoConfig, CargoWorkspace, Package, Target, TargetKind},
23     project_json::ProjectJson,
24     sysroot::Sysroot,
25 };
26 pub use ra_proc_macro::ProcMacroClient;
27
28 #[derive(Debug, Clone)]
29 pub enum ProjectWorkspace {
30     /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`.
31     Cargo { cargo: CargoWorkspace, sysroot: Sysroot },
32     /// Project workspace was manually specified using a `rust-project.json` file.
33     Json { project: ProjectJson, project_location: AbsPathBuf },
34 }
35
36 /// `PackageRoot` describes a package root folder.
37 /// Which may be an external dependency, or a member of
38 /// the current workspace.
39 #[derive(Debug, Clone)]
40 pub struct PackageRoot {
41     /// Path to the root folder
42     path: AbsPathBuf,
43     /// Is a member of the current workspace
44     is_member: bool,
45     out_dir: Option<AbsPathBuf>,
46 }
47 impl PackageRoot {
48     pub fn new_member(path: AbsPathBuf) -> PackageRoot {
49         Self { path, is_member: true, out_dir: None }
50     }
51     pub fn new_non_member(path: AbsPathBuf) -> PackageRoot {
52         Self { path, is_member: false, out_dir: None }
53     }
54     pub fn path(&self) -> &AbsPath {
55         &self.path
56     }
57     pub fn out_dir(&self) -> Option<&AbsPath> {
58         self.out_dir.as_deref()
59     }
60     pub fn is_member(&self) -> bool {
61         self.is_member
62     }
63 }
64
65 #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
66 pub enum ProjectManifest {
67     ProjectJson(AbsPathBuf),
68     CargoToml(AbsPathBuf),
69 }
70
71 impl ProjectManifest {
72     pub fn from_manifest_file(path: AbsPathBuf) -> Result<ProjectManifest> {
73         if path.ends_with("rust-project.json") {
74             return Ok(ProjectManifest::ProjectJson(path));
75         }
76         if path.ends_with("Cargo.toml") {
77             return Ok(ProjectManifest::CargoToml(path));
78         }
79         bail!("project root must point to Cargo.toml or rust-project.json: {}", path.display())
80     }
81
82     pub fn discover_single(path: &AbsPath) -> Result<ProjectManifest> {
83         let mut candidates = ProjectManifest::discover(path)?;
84         let res = match candidates.pop() {
85             None => bail!("no projects"),
86             Some(it) => it,
87         };
88
89         if !candidates.is_empty() {
90             bail!("more than one project")
91         }
92         Ok(res)
93     }
94
95     pub fn discover(path: &AbsPath) -> io::Result<Vec<ProjectManifest>> {
96         if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") {
97             return Ok(vec![ProjectManifest::ProjectJson(project_json)]);
98         }
99         return find_cargo_toml(path)
100             .map(|paths| paths.into_iter().map(ProjectManifest::CargoToml).collect());
101
102         fn find_cargo_toml(path: &AbsPath) -> io::Result<Vec<AbsPathBuf>> {
103             match find_in_parent_dirs(path, "Cargo.toml") {
104                 Some(it) => Ok(vec![it]),
105                 None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)),
106             }
107         }
108
109         fn find_in_parent_dirs(path: &AbsPath, target_file_name: &str) -> Option<AbsPathBuf> {
110             if path.ends_with(target_file_name) {
111                 return Some(path.to_path_buf());
112             }
113
114             let mut curr = Some(path);
115
116             while let Some(path) = curr {
117                 let candidate = path.join(target_file_name);
118                 if candidate.exists() {
119                     return Some(candidate);
120                 }
121                 curr = path.parent();
122             }
123
124             None
125         }
126
127         fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<AbsPathBuf> {
128             // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects
129             entities
130                 .filter_map(Result::ok)
131                 .map(|it| it.path().join("Cargo.toml"))
132                 .filter(|it| it.exists())
133                 .map(AbsPathBuf::assert)
134                 .collect()
135         }
136     }
137
138     pub fn discover_all(paths: &[impl AsRef<AbsPath>]) -> Vec<ProjectManifest> {
139         let mut res = paths
140             .iter()
141             .filter_map(|it| ProjectManifest::discover(it.as_ref()).ok())
142             .flatten()
143             .collect::<FxHashSet<_>>()
144             .into_iter()
145             .collect::<Vec<_>>();
146         res.sort();
147         res
148     }
149 }
150
151 impl ProjectWorkspace {
152     pub fn load(
153         manifest: ProjectManifest,
154         cargo_features: &CargoConfig,
155         with_sysroot: bool,
156     ) -> Result<ProjectWorkspace> {
157         let res = match manifest {
158             ProjectManifest::ProjectJson(project_json) => {
159                 let file = File::open(&project_json).with_context(|| {
160                     format!("Failed to open json file {}", project_json.display())
161                 })?;
162                 let reader = BufReader::new(file);
163                 let project_location = project_json.parent().unwrap().to_path_buf();
164                 ProjectWorkspace::Json {
165                     project: from_reader(reader).with_context(|| {
166                         format!("Failed to deserialize json file {}", project_json.display())
167                     })?,
168                     project_location,
169                 }
170             }
171             ProjectManifest::CargoToml(cargo_toml) => {
172                 let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_features)
173                     .with_context(|| {
174                         format!(
175                             "Failed to read Cargo metadata from Cargo.toml file {}",
176                             cargo_toml.display()
177                         )
178                     })?;
179                 let sysroot = if with_sysroot {
180                     Sysroot::discover(&cargo_toml).with_context(|| {
181                         format!(
182                             "Failed to find sysroot for Cargo.toml file {}. Is rust-src installed?",
183                             cargo_toml.display()
184                         )
185                     })?
186                 } else {
187                     Sysroot::default()
188                 };
189                 ProjectWorkspace::Cargo { cargo, sysroot }
190             }
191         };
192
193         Ok(res)
194     }
195
196     /// Returns the roots for the current `ProjectWorkspace`
197     /// The return type contains the path and whether or not
198     /// the root is a member of the current workspace
199     pub fn to_roots(&self) -> Vec<PackageRoot> {
200         match self {
201             ProjectWorkspace::Json { project, project_location } => project
202                 .roots
203                 .iter()
204                 .map(|r| PackageRoot::new_member(project_location.join(&r.path)))
205                 .collect(),
206             ProjectWorkspace::Cargo { cargo, sysroot } => cargo
207                 .packages()
208                 .map(|pkg| PackageRoot {
209                     path: cargo[pkg].root().to_path_buf(),
210                     is_member: cargo[pkg].is_member,
211                     out_dir: cargo[pkg].out_dir.clone(),
212                 })
213                 .chain(sysroot.crates().map(|krate| {
214                     PackageRoot::new_non_member(sysroot[krate].root_dir().to_path_buf())
215                 }))
216                 .collect(),
217         }
218     }
219
220     pub fn proc_macro_dylib_paths(&self) -> Vec<AbsPathBuf> {
221         match self {
222             ProjectWorkspace::Json { project, project_location } => project
223                 .crates
224                 .iter()
225                 .filter_map(|krate| krate.proc_macro_dylib_path.as_ref())
226                 .map(|it| project_location.join(it))
227                 .collect(),
228             ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => cargo
229                 .packages()
230                 .filter_map(|pkg| cargo[pkg].proc_macro_dylib_path.as_ref())
231                 .cloned()
232                 .collect(),
233         }
234     }
235
236     pub fn n_packages(&self) -> usize {
237         match self {
238             ProjectWorkspace::Json { project, .. } => project.crates.len(),
239             ProjectWorkspace::Cargo { cargo, sysroot } => {
240                 cargo.packages().len() + sysroot.crates().len()
241             }
242         }
243     }
244
245     pub fn to_crate_graph(
246         &self,
247         target: Option<&str>,
248         proc_macro_client: &ProcMacroClient,
249         load: &mut dyn FnMut(&Path) -> Option<FileId>,
250     ) -> CrateGraph {
251         let mut crate_graph = CrateGraph::default();
252         match self {
253             ProjectWorkspace::Json { project, project_location } => {
254                 let crates: FxHashMap<_, _> = project
255                     .crates
256                     .iter()
257                     .enumerate()
258                     .filter_map(|(seq_index, krate)| {
259                         let file_path = project_location.join(&krate.root_module);
260                         let file_id = load(&file_path)?;
261                         let edition = match krate.edition {
262                             project_json::Edition::Edition2015 => Edition::Edition2015,
263                             project_json::Edition::Edition2018 => Edition::Edition2018,
264                         };
265                         let cfg_options = {
266                             let mut opts = CfgOptions::default();
267                             for cfg in &krate.cfg {
268                                 match cfg.find('=') {
269                                     None => opts.insert_atom(cfg.into()),
270                                     Some(pos) => {
271                                         let key = &cfg[..pos];
272                                         let value = cfg[pos + 1..].trim_matches('"');
273                                         opts.insert_key_value(key.into(), value.into());
274                                     }
275                                 }
276                             }
277                             opts
278                         };
279
280                         let mut env = Env::default();
281                         if let Some(out_dir) = &krate.out_dir {
282                             // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
283                             if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
284                                 env.set("OUT_DIR", out_dir);
285                             }
286                         }
287                         let proc_macro = krate
288                             .proc_macro_dylib_path
289                             .clone()
290                             .map(|it| proc_macro_client.by_dylib_path(&it));
291                         // FIXME: No crate name in json definition such that we cannot add OUT_DIR to env
292                         Some((
293                             project_json::CrateId(seq_index),
294                             crate_graph.add_crate_root(
295                                 file_id,
296                                 edition,
297                                 // FIXME json definitions can store the crate name
298                                 None,
299                                 cfg_options,
300                                 env,
301                                 proc_macro.unwrap_or_default(),
302                             ),
303                         ))
304                     })
305                     .collect();
306
307                 for (id, krate) in project.crates.iter().enumerate() {
308                     for dep in &krate.deps {
309                         let from_crate_id = project_json::CrateId(id);
310                         let to_crate_id = dep.krate;
311                         if let (Some(&from), Some(&to)) =
312                             (crates.get(&from_crate_id), crates.get(&to_crate_id))
313                         {
314                             if crate_graph
315                                 .add_dep(from, CrateName::new(&dep.name).unwrap(), to)
316                                 .is_err()
317                             {
318                                 log::error!(
319                                     "cyclic dependency {:?} -> {:?}",
320                                     from_crate_id,
321                                     to_crate_id
322                                 );
323                             }
324                         }
325                     }
326                 }
327             }
328             ProjectWorkspace::Cargo { cargo, sysroot } => {
329                 let mut cfg_options = get_rustc_cfg_options(target);
330
331                 let sysroot_crates: FxHashMap<_, _> = sysroot
332                     .crates()
333                     .filter_map(|krate| {
334                         let file_id = load(&sysroot[krate].root)?;
335
336                         let env = Env::default();
337                         let proc_macro = vec![];
338                         let crate_name = CrateName::new(&sysroot[krate].name)
339                             .expect("Sysroot crate names should not contain dashes");
340
341                         let crate_id = crate_graph.add_crate_root(
342                             file_id,
343                             Edition::Edition2018,
344                             Some(crate_name),
345                             cfg_options.clone(),
346                             env,
347                             proc_macro,
348                         );
349                         Some((krate, crate_id))
350                     })
351                     .collect();
352
353                 for from in sysroot.crates() {
354                     for &to in sysroot[from].deps.iter() {
355                         let name = &sysroot[to].name;
356                         if let (Some(&from), Some(&to)) =
357                             (sysroot_crates.get(&from), sysroot_crates.get(&to))
358                         {
359                             if crate_graph.add_dep(from, CrateName::new(name).unwrap(), to).is_err()
360                             {
361                                 log::error!("cyclic dependency between sysroot crates")
362                             }
363                         }
364                     }
365                 }
366
367                 let libcore = sysroot.core().and_then(|it| sysroot_crates.get(&it).copied());
368                 let liballoc = sysroot.alloc().and_then(|it| sysroot_crates.get(&it).copied());
369                 let libstd = sysroot.std().and_then(|it| sysroot_crates.get(&it).copied());
370                 let libproc_macro =
371                     sysroot.proc_macro().and_then(|it| sysroot_crates.get(&it).copied());
372
373                 let mut pkg_to_lib_crate = FxHashMap::default();
374                 let mut pkg_crates = FxHashMap::default();
375
376                 // Add test cfg for non-sysroot crates
377                 cfg_options.insert_atom("test".into());
378
379                 // Next, create crates for each package, target pair
380                 for pkg in cargo.packages() {
381                     let mut lib_tgt = None;
382                     for &tgt in cargo[pkg].targets.iter() {
383                         let root = cargo[tgt].root.as_path();
384                         if let Some(file_id) = load(root) {
385                             let edition = cargo[pkg].edition;
386                             let cfg_options = {
387                                 let mut opts = cfg_options.clone();
388                                 for feature in cargo[pkg].features.iter() {
389                                     opts.insert_key_value("feature".into(), feature.into());
390                                 }
391                                 for cfg in cargo[pkg].cfgs.iter() {
392                                     match cfg.find('=') {
393                                         Some(split) => opts.insert_key_value(
394                                             cfg[..split].into(),
395                                             cfg[split + 1..].trim_matches('"').into(),
396                                         ),
397                                         None => opts.insert_atom(cfg.into()),
398                                     };
399                                 }
400                                 opts
401                             };
402                             let mut env = Env::default();
403                             if let Some(out_dir) = &cargo[pkg].out_dir {
404                                 // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
405                                 if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
406                                     env.set("OUT_DIR", out_dir);
407                                 }
408                             }
409                             let proc_macro = cargo[pkg]
410                                 .proc_macro_dylib_path
411                                 .as_ref()
412                                 .map(|it| proc_macro_client.by_dylib_path(&it))
413                                 .unwrap_or_default();
414
415                             let crate_id = crate_graph.add_crate_root(
416                                 file_id,
417                                 edition,
418                                 Some(CrateName::normalize_dashes(&cargo[pkg].name)),
419                                 cfg_options,
420                                 env,
421                                 proc_macro.clone(),
422                             );
423                             if cargo[tgt].kind == TargetKind::Lib {
424                                 lib_tgt = Some((crate_id, cargo[tgt].name.clone()));
425                                 pkg_to_lib_crate.insert(pkg, crate_id);
426                             }
427                             if cargo[tgt].is_proc_macro {
428                                 if let Some(proc_macro) = libproc_macro {
429                                     if crate_graph
430                                         .add_dep(
431                                             crate_id,
432                                             CrateName::new("proc_macro").unwrap(),
433                                             proc_macro,
434                                         )
435                                         .is_err()
436                                     {
437                                         log::error!(
438                                             "cyclic dependency on proc_macro for {}",
439                                             &cargo[pkg].name
440                                         )
441                                     }
442                                 }
443                             }
444
445                             pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
446                         }
447                     }
448
449                     // Set deps to the core, std and to the lib target of the current package
450                     for &from in pkg_crates.get(&pkg).into_iter().flatten() {
451                         if let Some((to, name)) = lib_tgt.clone() {
452                             if to != from
453                                 && crate_graph
454                                     .add_dep(
455                                         from,
456                                         // For root projects with dashes in their name,
457                                         // cargo metadata does not do any normalization,
458                                         // so we do it ourselves currently
459                                         CrateName::normalize_dashes(&name),
460                                         to,
461                                     )
462                                     .is_err()
463                             {
464                                 {
465                                     log::error!(
466                                         "cyclic dependency between targets of {}",
467                                         &cargo[pkg].name
468                                     )
469                                 }
470                             }
471                         }
472                         // core is added as a dependency before std in order to
473                         // mimic rustcs dependency order
474                         if let Some(core) = libcore {
475                             if crate_graph
476                                 .add_dep(from, CrateName::new("core").unwrap(), core)
477                                 .is_err()
478                             {
479                                 log::error!("cyclic dependency on core for {}", &cargo[pkg].name)
480                             }
481                         }
482                         if let Some(alloc) = liballoc {
483                             if crate_graph
484                                 .add_dep(from, CrateName::new("alloc").unwrap(), alloc)
485                                 .is_err()
486                             {
487                                 log::error!("cyclic dependency on alloc for {}", &cargo[pkg].name)
488                             }
489                         }
490                         if let Some(std) = libstd {
491                             if crate_graph
492                                 .add_dep(from, CrateName::new("std").unwrap(), std)
493                                 .is_err()
494                             {
495                                 log::error!("cyclic dependency on std for {}", &cargo[pkg].name)
496                             }
497                         }
498                     }
499                 }
500
501                 // Now add a dep edge from all targets of upstream to the lib
502                 // target of downstream.
503                 for pkg in cargo.packages() {
504                     for dep in cargo[pkg].dependencies.iter() {
505                         if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
506                             for &from in pkg_crates.get(&pkg).into_iter().flatten() {
507                                 if crate_graph
508                                     .add_dep(from, CrateName::new(&dep.name).unwrap(), to)
509                                     .is_err()
510                                 {
511                                     log::error!(
512                                         "cyclic dependency {} -> {}",
513                                         &cargo[pkg].name,
514                                         &cargo[dep.pkg].name
515                                     )
516                                 }
517                             }
518                         }
519                     }
520                 }
521             }
522         }
523         crate_graph
524     }
525
526     pub fn workspace_root_for(&self, path: &Path) -> Option<&Path> {
527         match self {
528             ProjectWorkspace::Cargo { cargo, .. } => {
529                 Some(cargo.workspace_root()).filter(|root| path.starts_with(root))
530             }
531             ProjectWorkspace::Json { project: ProjectJson { roots, .. }, .. } => roots
532                 .iter()
533                 .find(|root| path.starts_with(&root.path))
534                 .map(|root| root.path.as_ref()),
535         }
536     }
537 }
538
539 fn get_rustc_cfg_options(target: Option<&str>) -> CfgOptions {
540     let mut cfg_options = CfgOptions::default();
541
542     // Some nightly-only cfgs, which are required for stdlib
543     {
544         cfg_options.insert_atom("target_thread_local".into());
545         for &target_has_atomic in ["8", "16", "32", "64", "cas", "ptr"].iter() {
546             cfg_options.insert_key_value("target_has_atomic".into(), target_has_atomic.into());
547             cfg_options
548                 .insert_key_value("target_has_atomic_load_store".into(), target_has_atomic.into());
549         }
550     }
551
552     let rustc_cfgs = || -> Result<String> {
553         // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
554         let mut cmd = Command::new(ra_toolchain::rustc());
555         cmd.args(&["--print", "cfg", "-O"]);
556         if let Some(target) = target {
557             cmd.args(&["--target", target]);
558         }
559         let output = output(cmd)?;
560         Ok(String::from_utf8(output.stdout)?)
561     }();
562
563     match rustc_cfgs {
564         Ok(rustc_cfgs) => {
565             for line in rustc_cfgs.lines() {
566                 match line.find('=') {
567                     None => cfg_options.insert_atom(line.into()),
568                     Some(pos) => {
569                         let key = &line[..pos];
570                         let value = line[pos + 1..].trim_matches('"');
571                         cfg_options.insert_key_value(key.into(), value.into());
572                     }
573                 }
574             }
575         }
576         Err(e) => log::error!("failed to get rustc cfgs: {:#}", e),
577     }
578
579     cfg_options.insert_atom("debug_assertions".into());
580
581     cfg_options
582 }
583
584 fn output(mut cmd: Command) -> Result<Output> {
585     let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?;
586     if !output.status.success() {
587         match String::from_utf8(output.stderr) {
588             Ok(stderr) if !stderr.is_empty() => {
589                 bail!("{:?} failed, {}\nstderr:\n{}", cmd, output.status, stderr)
590             }
591             _ => bail!("{:?} failed, {}", cmd, output.status),
592         }
593     }
594     Ok(output)
595 }