rust-analyzer/crates/project_model/src/lib.rs

158 lines
5.3 KiB
Rust
Raw Normal View History

2021-05-22 13:53:47 +00:00
//! In rust-analyzer, we maintain a strict separation between pure abstract
//! semantic project model and a concrete model of a particular build system.
//!
//! Pure model is represented by the [`base_db::CrateGraph`] from another crate.
//!
//! In this crate, we are conserned with "real world" project models.
//!
//! Specifically, here we have a representation for a Cargo project
//! ([`CargoWorkspace`]) and for manually specified layout ([`ProjectJson`]).
//!
//! Roughly, the things we do here are:
//!
//! * Project discovery (where's the relevant Cargo.toml for the current dir).
//! * Custom build steps (`build.rs` code generation and compilation of
//! procedural macros).
//! * Lowering of concrete model to a [`base_db::CrateGraph`]
mod manifest_path;
mod cargo_workspace;
mod cfg_flag;
2020-06-24 12:57:37 +00:00
mod project_json;
mod sysroot;
mod workspace;
mod rustc_cfg;
mod build_scripts;
#[cfg(test)]
mod tests;
use std::{
2021-07-17 13:43:33 +00:00
fs::{self, read_dir, ReadDir},
2020-06-24 13:52:07 +00:00
io,
process::Command,
};
use anyhow::{bail, format_err, Context, Result};
2020-08-13 08:32:19 +00:00
use paths::{AbsPath, AbsPathBuf};
use rustc_hash::FxHashSet;
pub use crate::{
build_scripts::WorkspaceBuildScripts,
cargo_workspace::{
CargoConfig, CargoWorkspace, Package, PackageData, PackageDependency, RustcSource, Target,
TargetData, TargetKind, UnsetTestCrates,
},
manifest_path::ManifestPath,
2020-06-24 13:52:07 +00:00
project_json::{ProjectJson, ProjectJsonData},
sysroot::Sysroot,
workspace::{CfgOverrides, PackageRoot, ProjectWorkspace},
};
2020-06-03 09:44:51 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
2020-06-03 10:05:50 +00:00
pub enum ProjectManifest {
ProjectJson(ManifestPath),
CargoToml(ManifestPath),
}
2020-06-03 10:05:50 +00:00
impl ProjectManifest {
pub fn from_manifest_file(path: AbsPathBuf) -> Result<ProjectManifest> {
let path = ManifestPath::try_from(path)
.map_err(|path| format_err!("bad manifest path: {}", path.display()))?;
2021-07-17 13:43:33 +00:00
if path.file_name().unwrap_or_default() == "rust-project.json" {
2020-06-03 10:05:50 +00:00
return Ok(ProjectManifest::ProjectJson(path));
}
2021-07-17 13:43:33 +00:00
if path.file_name().unwrap_or_default() == "Cargo.toml" {
2020-06-03 10:05:50 +00:00
return Ok(ProjectManifest::CargoToml(path));
}
bail!("project root must point to Cargo.toml or rust-project.json: {}", path.display())
2019-08-19 12:41:18 +00:00
}
pub fn discover_single(path: &AbsPath) -> Result<ProjectManifest> {
2020-06-03 10:05:50 +00:00
let mut candidates = ProjectManifest::discover(path)?;
let res = match candidates.pop() {
None => bail!("no projects"),
Some(it) => it,
};
if !candidates.is_empty() {
bail!("more than one project")
}
Ok(res)
}
pub fn discover(path: &AbsPath) -> io::Result<Vec<ProjectManifest>> {
2020-05-08 23:51:59 +00:00
if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") {
2020-06-03 10:05:50 +00:00
return Ok(vec![ProjectManifest::ProjectJson(project_json)]);
}
return find_cargo_toml(path)
2020-06-03 10:05:50 +00:00
.map(|paths| paths.into_iter().map(ProjectManifest::CargoToml).collect());
fn find_cargo_toml(path: &AbsPath) -> io::Result<Vec<ManifestPath>> {
2020-05-08 23:51:59 +00:00
match find_in_parent_dirs(path, "Cargo.toml") {
Some(it) => Ok(vec![it]),
None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)),
}
2020-05-08 23:51:59 +00:00
}
fn find_in_parent_dirs(path: &AbsPath, target_file_name: &str) -> Option<ManifestPath> {
2021-07-17 13:43:33 +00:00
if path.file_name().unwrap_or_default() == target_file_name {
if let Ok(manifest) = ManifestPath::try_from(path.to_path_buf()) {
return Some(manifest);
}
}
let mut curr = Some(path);
2020-05-08 23:51:59 +00:00
while let Some(path) = curr {
2020-05-08 23:51:59 +00:00
let candidate = path.join(target_file_name);
2021-07-17 13:43:33 +00:00
if fs::metadata(&candidate).is_ok() {
if let Ok(manifest) = ManifestPath::try_from(candidate) {
return Some(manifest);
}
}
curr = path.parent();
}
None
}
fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<ManifestPath> {
// Only one level down to avoid cycles the easy way and stop a runaway scan with large projects
2020-05-08 23:51:59 +00:00
entities
.filter_map(Result::ok)
.map(|it| it.path().join("Cargo.toml"))
.filter(|it| it.exists())
.map(AbsPathBuf::assert)
.filter_map(|it| it.try_into().ok())
2020-05-08 23:51:59 +00:00
.collect()
}
}
2020-06-03 09:44:51 +00:00
pub fn discover_all(paths: &[AbsPathBuf]) -> Vec<ProjectManifest> {
2020-06-03 09:44:51 +00:00
let mut res = paths
.iter()
2020-06-03 10:05:50 +00:00
.filter_map(|it| ProjectManifest::discover(it.as_ref()).ok())
2020-06-03 09:44:51 +00:00
.flatten()
.collect::<FxHashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
res.sort();
res
}
}
fn utf8_stdout(mut cmd: Command) -> Result<String> {
2020-05-08 12:54:29 +00:00
let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?;
if !output.status.success() {
2020-05-08 16:53:53 +00:00
match String::from_utf8(output.stderr) {
Ok(stderr) if !stderr.is_empty() => {
bail!("{:?} failed, {}\nstderr:\n{}", cmd, output.status, stderr)
}
_ => bail!("{:?} failed, {}", cmd, output.status),
}
2020-05-08 12:54:29 +00:00
}
let stdout = String::from_utf8(output.stdout)?;
Ok(stdout.trim().to_string())
2020-05-08 12:54:29 +00:00
}