2021-05-22 08:53:47 -05:00
|
|
|
//! `rust-project.json` file format.
|
|
|
|
//!
|
|
|
|
//! This format is spiritually a serialization of [`base_db::CrateGraph`]. The
|
|
|
|
//! idea here is that people who do not use Cargo, can instead teach their build
|
|
|
|
//! system to generate `rust-project.json` which can be ingested by
|
|
|
|
//! rust-analyzer.
|
2023-01-13 05:44:02 -06:00
|
|
|
//!
|
|
|
|
//! This short file is a somewhat big conceptual piece of the architecture of
|
|
|
|
//! rust-analyzer, so it's worth elaborating on the underlying ideas and
|
|
|
|
//! motivation.
|
|
|
|
//!
|
|
|
|
//! For rust-analyzer to function, it needs some information about the project.
|
|
|
|
//! Specifically, it maintains an in-memory data structure which lists all the
|
|
|
|
//! crates (compilation units) and dependencies between them. This is necessary
|
|
|
|
//! a global singleton, as we do want, eg, find usages to always search across
|
|
|
|
//! the whole project, rather than just in the "current" crate.
|
|
|
|
//!
|
|
|
|
//! Normally, we get this "crate graph" by calling `cargo metadata
|
|
|
|
//! --message-format=json` for each cargo workspace and merging results. This
|
|
|
|
//! works for your typical cargo project, but breaks down for large folks who
|
|
|
|
//! have a monorepo with an infitine amount of Rust which is build with bazel or
|
|
|
|
//! some such.
|
|
|
|
//!
|
|
|
|
//! To support this use-case, we need to make _something_ configurable. To avoid
|
|
|
|
//! [midlayer mistake](https://lwn.net/Articles/336262/), we allow configuring
|
|
|
|
//! the lowest possible layer. `ProjectJson` is essentially a hook to just set
|
|
|
|
//! that global singleton in-memory data structure. It is optimized for power,
|
|
|
|
//! not for convenience (you'd be using cargo anyway if you wanted nice things,
|
|
|
|
//! right? :)
|
|
|
|
//!
|
|
|
|
//! `rust-project.json` also isn't necessary a file. Architecturally, we support
|
|
|
|
//! any convenient way to specify this data, which today is:
|
|
|
|
//!
|
|
|
|
//! * file on disk
|
|
|
|
//! * a field in the config (ie, you can send a JSON request with the contents
|
|
|
|
//! of rust-project.json to rust-analyzer, no need to write anything to disk)
|
|
|
|
//!
|
|
|
|
//! Another possible thing we don't do today, but which would be totally valid,
|
|
|
|
//! is to add an extension point to VS Code extension to register custom
|
|
|
|
//! project.
|
|
|
|
//!
|
|
|
|
//! In general, it is assumed that if you are going to use `rust-project.json`,
|
|
|
|
//! you'd write a fair bit of custom code gluing your build system to ra through
|
|
|
|
//! this JSON format. This logic can take form of a VS Code extension, or a
|
|
|
|
//! proxy process which injects data into "configure" LSP request, or maybe just
|
|
|
|
//! a simple build system rule to generate the file.
|
|
|
|
//!
|
|
|
|
//! In particular, the logic for lazily loading parts of the monorepo as the
|
|
|
|
//! user explores them belongs to that extension (it's totally valid to change
|
|
|
|
//! rust-project.json over time via configuration request!)
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2019-03-05 15:29:23 -06:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-10-20 10:13:15 -05:00
|
|
|
use base_db::{CrateDisplayName, CrateId, CrateName, Dependency, Edition};
|
2020-06-24 08:52:07 -05:00
|
|
|
use paths::{AbsPath, AbsPathBuf};
|
2020-07-23 11:57:27 -05:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-07-01 02:53:53 -05:00
|
|
|
use serde::{de, Deserialize};
|
2020-07-23 11:57:27 -05:00
|
|
|
|
2020-09-11 07:48:56 -05:00
|
|
|
use crate::cfg_flag::CfgFlag;
|
2019-03-05 15:29:23 -06:00
|
|
|
|
2020-06-03 03:33:01 -05:00
|
|
|
/// Roots and crates that compose this Rust project.
|
2020-07-01 09:42:14 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2020-06-24 07:57:37 -05:00
|
|
|
pub struct ProjectJson {
|
2022-07-25 09:07:41 -05:00
|
|
|
/// e.g. `path/to/sysroot`
|
|
|
|
pub(crate) sysroot: Option<AbsPathBuf>,
|
|
|
|
/// e.g. `path/to/sysroot/lib/rustlib/src/rust`
|
2020-09-11 07:48:56 -05:00
|
|
|
pub(crate) sysroot_src: Option<AbsPathBuf>,
|
2020-09-16 14:09:44 -05:00
|
|
|
project_root: AbsPathBuf,
|
2020-08-25 11:39:18 -05:00
|
|
|
crates: Vec<Crate>,
|
2020-06-03 03:33:01 -05:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:29:23 -06:00
|
|
|
/// A crate points to the root module of a crate and lists the dependencies of the crate. This is
|
|
|
|
/// useful in creating the crate graph.
|
2020-07-01 09:42:14 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2019-03-05 15:29:23 -06:00
|
|
|
pub struct Crate {
|
2020-10-20 10:13:15 -05:00
|
|
|
pub(crate) display_name: Option<CrateDisplayName>,
|
2020-06-24 08:52:07 -05:00
|
|
|
pub(crate) root_module: AbsPathBuf,
|
2019-03-05 15:29:23 -06:00
|
|
|
pub(crate) edition: Edition,
|
2021-10-30 09:17:04 -05:00
|
|
|
pub(crate) version: Option<String>,
|
2020-06-24 08:52:07 -05:00
|
|
|
pub(crate) deps: Vec<Dependency>,
|
2020-07-23 11:57:27 -05:00
|
|
|
pub(crate) cfg: Vec<CfgFlag>,
|
2020-06-12 12:08:51 -05:00
|
|
|
pub(crate) target: Option<String>,
|
2020-07-21 08:12:12 -05:00
|
|
|
pub(crate) env: FxHashMap<String, String>,
|
2020-06-24 08:52:07 -05:00
|
|
|
pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
|
2020-07-20 10:57:10 -05:00
|
|
|
pub(crate) is_workspace_member: bool,
|
2020-07-21 07:57:20 -05:00
|
|
|
pub(crate) include: Vec<AbsPathBuf>,
|
|
|
|
pub(crate) exclude: Vec<AbsPathBuf>,
|
2021-07-31 17:26:59 -05:00
|
|
|
pub(crate) is_proc_macro: bool,
|
2021-11-22 11:44:46 -06:00
|
|
|
pub(crate) repository: Option<String>,
|
2020-06-24 08:52:07 -05:00
|
|
|
}
|
2020-05-08 18:59:52 -05:00
|
|
|
|
2020-06-24 08:52:07 -05:00
|
|
|
impl ProjectJson {
|
2020-09-20 14:06:12 -05:00
|
|
|
/// Create a new ProjectJson instance.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `base` - The path to the workspace root (i.e. the folder containing `rust-project.json`)
|
|
|
|
/// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via
|
|
|
|
/// configuration.
|
2020-06-24 08:52:07 -05:00
|
|
|
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
|
|
|
|
ProjectJson {
|
2022-07-25 09:07:41 -05:00
|
|
|
sysroot: data.sysroot.map(|it| base.join(it)),
|
2020-09-11 07:48:56 -05:00
|
|
|
sysroot_src: data.sysroot_src.map(|it| base.join(it)),
|
2020-09-16 14:09:44 -05:00
|
|
|
project_root: base.to_path_buf(),
|
2020-06-24 08:52:07 -05:00
|
|
|
crates: data
|
|
|
|
.crates
|
|
|
|
.into_iter()
|
2020-07-20 10:57:10 -05:00
|
|
|
.map(|crate_data| {
|
|
|
|
let is_workspace_member = crate_data.is_workspace_member.unwrap_or_else(|| {
|
|
|
|
crate_data.root_module.is_relative()
|
|
|
|
&& !crate_data.root_module.starts_with("..")
|
|
|
|
|| crate_data.root_module.starts_with(base)
|
|
|
|
});
|
2020-08-25 11:34:00 -05:00
|
|
|
let root_module = base.join(crate_data.root_module).normalize();
|
2020-07-21 07:57:20 -05:00
|
|
|
let (include, exclude) = match crate_data.source {
|
|
|
|
Some(src) => {
|
|
|
|
let absolutize = |dirs: Vec<PathBuf>| {
|
2020-08-25 11:34:00 -05:00
|
|
|
dirs.into_iter()
|
|
|
|
.map(|it| base.join(it).normalize())
|
|
|
|
.collect::<Vec<_>>()
|
2020-07-21 07:57:20 -05:00
|
|
|
};
|
|
|
|
(absolutize(src.include_dirs), absolutize(src.exclude_dirs))
|
|
|
|
}
|
|
|
|
None => (vec![root_module.parent().unwrap().to_path_buf()], Vec::new()),
|
|
|
|
};
|
|
|
|
|
2020-07-20 10:57:10 -05:00
|
|
|
Crate {
|
2020-10-20 10:13:15 -05:00
|
|
|
display_name: crate_data
|
|
|
|
.display_name
|
|
|
|
.map(CrateDisplayName::from_canonical_name),
|
2020-07-21 07:57:20 -05:00
|
|
|
root_module,
|
2020-07-20 10:57:10 -05:00
|
|
|
edition: crate_data.edition.into(),
|
2021-10-30 09:17:04 -05:00
|
|
|
version: crate_data.version.as_ref().map(ToString::to_string),
|
2020-07-20 10:57:10 -05:00
|
|
|
deps: crate_data
|
|
|
|
.deps
|
|
|
|
.into_iter()
|
2021-09-28 14:23:46 -05:00
|
|
|
.map(|dep_data| {
|
|
|
|
Dependency::new(dep_data.name, CrateId(dep_data.krate as u32))
|
2020-07-20 10:57:10 -05:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
2020-07-23 11:57:27 -05:00
|
|
|
cfg: crate_data.cfg,
|
2020-07-20 10:57:10 -05:00
|
|
|
target: crate_data.target,
|
2020-07-21 08:12:12 -05:00
|
|
|
env: crate_data.env,
|
2020-07-20 10:57:10 -05:00
|
|
|
proc_macro_dylib_path: crate_data
|
|
|
|
.proc_macro_dylib_path
|
|
|
|
.map(|it| base.join(it)),
|
|
|
|
is_workspace_member,
|
2020-07-21 07:57:20 -05:00
|
|
|
include,
|
|
|
|
exclude,
|
2021-07-31 17:26:59 -05:00
|
|
|
is_proc_macro: crate_data.is_proc_macro,
|
2021-11-22 11:44:46 -06:00
|
|
|
repository: crate_data.repository,
|
2020-07-20 10:57:10 -05:00
|
|
|
}
|
2020-06-24 08:52:07 -05:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
}
|
|
|
|
}
|
2022-09-19 10:31:08 -05:00
|
|
|
|
2020-09-20 14:06:12 -05:00
|
|
|
/// Returns the number of crates in the project.
|
2020-08-25 11:39:18 -05:00
|
|
|
pub fn n_crates(&self) -> usize {
|
|
|
|
self.crates.len()
|
|
|
|
}
|
2022-09-19 10:31:08 -05:00
|
|
|
|
2020-09-20 14:06:12 -05:00
|
|
|
/// Returns an iterator over the crates in the project.
|
2020-08-25 11:39:18 -05:00
|
|
|
pub fn crates(&self) -> impl Iterator<Item = (CrateId, &Crate)> + '_ {
|
|
|
|
self.crates.iter().enumerate().map(|(idx, krate)| (CrateId(idx as u32), krate))
|
|
|
|
}
|
2022-09-19 10:31:08 -05:00
|
|
|
|
2020-09-20 14:06:12 -05:00
|
|
|
/// Returns the path to the project's root folder.
|
2020-09-16 14:09:44 -05:00
|
|
|
pub fn path(&self) -> &AbsPath {
|
|
|
|
&self.project_root
|
2020-09-15 20:51:57 -05:00
|
|
|
}
|
2020-06-24 08:52:07 -05:00
|
|
|
}
|
2020-05-08 18:59:52 -05:00
|
|
|
|
2021-01-06 04:54:28 -06:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2020-06-24 08:52:07 -05:00
|
|
|
pub struct ProjectJsonData {
|
2022-07-25 09:07:41 -05:00
|
|
|
sysroot: Option<PathBuf>,
|
2020-08-25 11:34:00 -05:00
|
|
|
sysroot_src: Option<PathBuf>,
|
2020-06-24 08:52:07 -05:00
|
|
|
crates: Vec<CrateData>,
|
2019-03-05 15:29:23 -06:00
|
|
|
}
|
|
|
|
|
2021-01-06 04:54:28 -06:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2020-06-24 08:52:07 -05:00
|
|
|
struct CrateData {
|
2020-10-20 10:13:15 -05:00
|
|
|
display_name: Option<String>,
|
2020-06-24 08:52:07 -05:00
|
|
|
root_module: PathBuf,
|
|
|
|
edition: EditionData,
|
2021-10-30 09:17:04 -05:00
|
|
|
#[serde(default)]
|
|
|
|
version: Option<semver::Version>,
|
2020-06-24 08:52:07 -05:00
|
|
|
deps: Vec<DepData>,
|
|
|
|
#[serde(default)]
|
2020-07-23 11:57:27 -05:00
|
|
|
cfg: Vec<CfgFlag>,
|
2020-06-12 12:08:51 -05:00
|
|
|
target: Option<String>,
|
2020-07-21 08:12:12 -05:00
|
|
|
#[serde(default)]
|
|
|
|
env: FxHashMap<String, String>,
|
2020-06-24 08:52:07 -05:00
|
|
|
proc_macro_dylib_path: Option<PathBuf>,
|
2020-07-20 10:57:10 -05:00
|
|
|
is_workspace_member: Option<bool>,
|
2020-07-21 07:57:20 -05:00
|
|
|
source: Option<CrateSource>,
|
2021-07-31 17:26:59 -05:00
|
|
|
#[serde(default)]
|
|
|
|
is_proc_macro: bool,
|
2021-11-22 11:44:46 -06:00
|
|
|
#[serde(default)]
|
|
|
|
repository: Option<String>,
|
2020-06-24 08:52:07 -05:00
|
|
|
}
|
|
|
|
|
2021-01-06 04:54:28 -06:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2019-03-05 15:29:23 -06:00
|
|
|
#[serde(rename = "edition")]
|
2020-06-24 08:52:07 -05:00
|
|
|
enum EditionData {
|
2019-03-05 15:29:23 -06:00
|
|
|
#[serde(rename = "2015")]
|
|
|
|
Edition2015,
|
|
|
|
#[serde(rename = "2018")]
|
|
|
|
Edition2018,
|
2021-01-01 10:22:23 -06:00
|
|
|
#[serde(rename = "2021")]
|
|
|
|
Edition2021,
|
2019-03-05 15:29:23 -06:00
|
|
|
}
|
|
|
|
|
2020-06-24 08:52:07 -05:00
|
|
|
impl From<EditionData> for Edition {
|
|
|
|
fn from(data: EditionData) -> Self {
|
|
|
|
match data {
|
|
|
|
EditionData::Edition2015 => Edition::Edition2015,
|
|
|
|
EditionData::Edition2018 => Edition::Edition2018,
|
2021-01-01 10:22:23 -06:00
|
|
|
EditionData::Edition2021 => Edition::Edition2021,
|
2020-06-24 08:52:07 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 15:29:23 -06:00
|
|
|
}
|
|
|
|
|
2021-01-06 04:54:28 -06:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2020-06-24 08:52:07 -05:00
|
|
|
struct DepData {
|
|
|
|
/// Identifies a crate by position in the crates array.
|
|
|
|
#[serde(rename = "crate")]
|
|
|
|
krate: usize,
|
2020-07-01 02:53:53 -05:00
|
|
|
#[serde(deserialize_with = "deserialize_crate_name")]
|
|
|
|
name: CrateName,
|
|
|
|
}
|
|
|
|
|
2021-01-06 04:54:28 -06:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2020-07-21 07:57:20 -05:00
|
|
|
struct CrateSource {
|
|
|
|
include_dirs: Vec<PathBuf>,
|
|
|
|
exclude_dirs: Vec<PathBuf>,
|
|
|
|
}
|
|
|
|
|
2020-07-01 02:53:53 -05:00
|
|
|
fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error>
|
|
|
|
where
|
|
|
|
D: de::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let name = String::deserialize(de)?;
|
2022-12-23 12:42:58 -06:00
|
|
|
CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {err:?}")))
|
2020-05-08 18:59:52 -05:00
|
|
|
}
|