]> git.lizzy.rs Git - rust.git/blob - crates/project_model/src/lib.rs
c2fde00d5216cae18a63000678bf517fee039027
[rust.git] / crates / project_model / src / lib.rs
1 //! In rust-analyzer, we maintain a strict separation between pure abstract
2 //! semantic project model and a concrete model of a particular build system.
3 //!
4 //! Pure model is represented by the [`base_db::CrateGraph`] from another crate.
5 //!
6 //! In this crate, we are conserned with "real world" project models.
7 //!
8 //! Specifically, here we have a representation for a Cargo project
9 //! ([`CargoWorkspace`]) and for manually specified layout ([`ProjectJson`]).
10 //!
11 //! Roughly, the things we do here are:
12 //!
13 //! * Project discovery (where's the relevant Cargo.toml for the current dir).
14 //! * Custom build steps (`build.rs` code generation and compilation of
15 //!   procedural macros).
16 //! * Lowering of concrete model to a [`base_db::CrateGraph`]
17
18 mod cargo_workspace;
19 mod cfg_flag;
20 mod project_json;
21 mod sysroot;
22 mod workspace;
23 mod rustc_cfg;
24 mod build_data;
25
26 use std::{
27     fs::{read_dir, ReadDir},
28     io,
29     process::Command,
30 };
31
32 use anyhow::{bail, Context, Result};
33 use paths::{AbsPath, AbsPathBuf};
34 use rustc_hash::FxHashSet;
35
36 pub use crate::{
37     build_data::{BuildDataCollector, BuildDataResult},
38     cargo_workspace::{
39         CargoConfig, CargoWorkspace, Package, PackageData, PackageDependency, RustcSource, Target,
40         TargetData, TargetKind,
41     },
42     project_json::{ProjectJson, ProjectJsonData},
43     sysroot::Sysroot,
44     workspace::{PackageRoot, ProjectWorkspace},
45 };
46
47 pub use proc_macro_api::ProcMacroClient;
48
49 #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
50 pub enum ProjectManifest {
51     ProjectJson(AbsPathBuf),
52     CargoToml(AbsPathBuf),
53     DetachedFile(AbsPathBuf),
54 }
55
56 impl ProjectManifest {
57     pub fn from_manifest_file(path: AbsPathBuf) -> Result<ProjectManifest> {
58         if path.ends_with("rust-project.json") {
59             return Ok(ProjectManifest::ProjectJson(path));
60         }
61         if path.ends_with("Cargo.toml") {
62             return Ok(ProjectManifest::CargoToml(path));
63         }
64         bail!("project root must point to Cargo.toml or rust-project.json: {}", path.display())
65     }
66
67     pub fn discover_single(path: &AbsPath) -> Result<ProjectManifest> {
68         let mut candidates = ProjectManifest::discover(path)?;
69         let res = match candidates.pop() {
70             None => bail!("no projects"),
71             Some(it) => it,
72         };
73
74         if !candidates.is_empty() {
75             bail!("more than one project")
76         }
77         Ok(res)
78     }
79
80     pub fn discover(path: &AbsPath) -> io::Result<Vec<ProjectManifest>> {
81         if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") {
82             return Ok(vec![ProjectManifest::ProjectJson(project_json)]);
83         }
84         return find_cargo_toml(path)
85             .map(|paths| paths.into_iter().map(ProjectManifest::CargoToml).collect());
86
87         fn find_cargo_toml(path: &AbsPath) -> io::Result<Vec<AbsPathBuf>> {
88             match find_in_parent_dirs(path, "Cargo.toml") {
89                 Some(it) => Ok(vec![it]),
90                 None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)),
91             }
92         }
93
94         fn find_in_parent_dirs(path: &AbsPath, target_file_name: &str) -> Option<AbsPathBuf> {
95             if path.ends_with(target_file_name) {
96                 return Some(path.to_path_buf());
97             }
98
99             let mut curr = Some(path);
100
101             while let Some(path) = curr {
102                 let candidate = path.join(target_file_name);
103                 if candidate.exists() {
104                     return Some(candidate);
105                 }
106                 curr = path.parent();
107             }
108
109             None
110         }
111
112         fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<AbsPathBuf> {
113             // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects
114             entities
115                 .filter_map(Result::ok)
116                 .map(|it| it.path().join("Cargo.toml"))
117                 .filter(|it| it.exists())
118                 .map(AbsPathBuf::assert)
119                 .collect()
120         }
121     }
122
123     pub fn discover_all(paths: &[impl AsRef<AbsPath>]) -> Vec<ProjectManifest> {
124         let mut res = paths
125             .iter()
126             .filter_map(|it| ProjectManifest::discover(it.as_ref()).ok())
127             .flatten()
128             .collect::<FxHashSet<_>>()
129             .into_iter()
130             .collect::<Vec<_>>();
131         res.sort();
132         res
133     }
134 }
135
136 fn utf8_stdout(mut cmd: Command) -> Result<String> {
137     let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?;
138     if !output.status.success() {
139         match String::from_utf8(output.stderr) {
140             Ok(stderr) if !stderr.is_empty() => {
141                 bail!("{:?} failed, {}\nstderr:\n{}", cmd, output.status, stderr)
142             }
143             _ => bail!("{:?} failed, {}", cmd, output.status),
144         }
145     }
146     let stdout = String::from_utf8(output.stdout)?;
147     Ok(stdout.trim().to_string())
148 }