Cleanup project.json deserialization

This commit is contained in:
Aleksey Kladov 2020-06-24 15:52:07 +02:00
parent a07cad16ab
commit e6c61d5072
15 changed files with 141 additions and 144 deletions

1
Cargo.lock generated
View File

@ -1183,6 +1183,7 @@ dependencies = [
"rustc-hash",
"serde",
"serde_json",
"stdx",
]
[[package]]

View File

@ -19,6 +19,7 @@ ra_db = { path = "../ra_db" }
ra_toolchain = { path = "../ra_toolchain" }
ra_proc_macro = { path = "../ra_proc_macro" }
paths = { path = "../paths" }
stdx = { path = "../stdx" }
serde = { version = "1.0.106", features = ["derive"] }
serde_json = "1.0.48"

View File

@ -260,7 +260,7 @@ impl CargoWorkspace {
.copied()
}
pub fn workspace_root(&self) -> &Path {
pub fn workspace_root(&self) -> &AbsPath {
&self.workspace_root
}

View File

@ -5,8 +5,8 @@ mod project_json;
mod sysroot;
use std::{
fs::{read_dir, File, ReadDir},
io::{self, BufReader},
fs::{self, read_dir, ReadDir},
io,
path::Path,
process::{Command, Output},
};
@ -14,13 +14,12 @@ use std::{
use anyhow::{bail, Context, Result};
use paths::{AbsPath, AbsPathBuf};
use ra_cfg::CfgOptions;
use ra_db::{CrateGraph, CrateName, Edition, Env, FileId};
use ra_db::{CrateGraph, CrateId, CrateName, Edition, Env, FileId};
use rustc_hash::{FxHashMap, FxHashSet};
use serde_json::from_reader;
pub use crate::{
cargo_workspace::{CargoConfig, CargoWorkspace, Package, Target, TargetKind},
project_json::ProjectJson,
project_json::{ProjectJson, ProjectJsonData},
sysroot::Sysroot,
};
pub use ra_proc_macro::ProcMacroClient;
@ -30,7 +29,7 @@ pub enum ProjectWorkspace {
/// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`.
Cargo { cargo: CargoWorkspace, sysroot: Sysroot },
/// Project workspace was manually specified using a `rust-project.json` file.
Json { project: ProjectJson, project_location: AbsPathBuf },
Json { project: ProjectJson },
}
/// `PackageRoot` describes a package root folder.
@ -156,17 +155,15 @@ impl ProjectWorkspace {
) -> Result<ProjectWorkspace> {
let res = match manifest {
ProjectManifest::ProjectJson(project_json) => {
let file = File::open(&project_json).with_context(|| {
format!("Failed to open json file {}", project_json.display())
let file = fs::read_to_string(&project_json).with_context(|| {
format!("Failed to read json file {}", project_json.display())
})?;
let data = serde_json::from_str(&file).with_context(|| {
format!("Failed to deserialize json file {}", project_json.display())
})?;
let reader = BufReader::new(file);
let project_location = project_json.parent().unwrap().to_path_buf();
ProjectWorkspace::Json {
project: from_reader(reader).with_context(|| {
format!("Failed to deserialize json file {}", project_json.display())
})?,
project_location,
}
let project = ProjectJson::new(&project_location, data);
ProjectWorkspace::Json { project }
}
ProjectManifest::CargoToml(cargo_toml) => {
let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_features)
@ -198,11 +195,9 @@ impl ProjectWorkspace {
/// the root is a member of the current workspace
pub fn to_roots(&self) -> Vec<PackageRoot> {
match self {
ProjectWorkspace::Json { project, project_location } => project
.roots
.iter()
.map(|r| PackageRoot::new_member(project_location.join(&r.path)))
.collect(),
ProjectWorkspace::Json { project } => {
project.roots.iter().map(|r| PackageRoot::new_member(r.path.clone())).collect()
}
ProjectWorkspace::Cargo { cargo, sysroot } => cargo
.packages()
.map(|pkg| PackageRoot {
@ -219,11 +214,11 @@ impl ProjectWorkspace {
pub fn proc_macro_dylib_paths(&self) -> Vec<AbsPathBuf> {
match self {
ProjectWorkspace::Json { project, project_location } => project
ProjectWorkspace::Json { project } => project
.crates
.iter()
.filter_map(|krate| krate.proc_macro_dylib_path.as_ref())
.map(|it| project_location.join(it))
.cloned()
.collect(),
ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => cargo
.packages()
@ -246,36 +241,18 @@ impl ProjectWorkspace {
&self,
target: Option<&str>,
proc_macro_client: &ProcMacroClient,
load: &mut dyn FnMut(&Path) -> Option<FileId>,
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
) -> CrateGraph {
let mut crate_graph = CrateGraph::default();
match self {
ProjectWorkspace::Json { project, project_location } => {
ProjectWorkspace::Json { project } => {
let crates: FxHashMap<_, _> = project
.crates
.iter()
.enumerate()
.filter_map(|(seq_index, krate)| {
let file_path = project_location.join(&krate.root_module);
let file_path = &krate.root_module;
let file_id = load(&file_path)?;
let edition = match krate.edition {
project_json::Edition::Edition2015 => Edition::Edition2015,
project_json::Edition::Edition2018 => Edition::Edition2018,
};
let cfg_options = {
let mut opts = CfgOptions::default();
for cfg in &krate.cfg {
match cfg.find('=') {
None => opts.insert_atom(cfg.into()),
Some(pos) => {
let key = &cfg[..pos];
let value = cfg[pos + 1..].trim_matches('"');
opts.insert_key_value(key.into(), value.into());
}
}
}
opts
};
let mut env = Env::default();
if let Some(out_dir) = &krate.out_dir {
@ -290,13 +267,13 @@ impl ProjectWorkspace {
.map(|it| proc_macro_client.by_dylib_path(&it));
// FIXME: No crate name in json definition such that we cannot add OUT_DIR to env
Some((
project_json::CrateId(seq_index),
CrateId(seq_index as u32),
crate_graph.add_crate_root(
file_id,
edition,
krate.edition,
// FIXME json definitions can store the crate name
None,
cfg_options,
krate.cfg.clone(),
env,
proc_macro.unwrap_or_default(),
),
@ -306,8 +283,8 @@ impl ProjectWorkspace {
for (id, krate) in project.crates.iter().enumerate() {
for dep in &krate.deps {
let from_crate_id = project_json::CrateId(id);
let to_crate_id = dep.krate;
let from_crate_id = CrateId(id as u32);
let to_crate_id = dep.crate_id;
if let (Some(&from), Some(&to)) =
(crates.get(&from_crate_id), crates.get(&to_crate_id))
{
@ -523,7 +500,7 @@ impl ProjectWorkspace {
crate_graph
}
pub fn workspace_root_for(&self, path: &Path) -> Option<&Path> {
pub fn workspace_root_for(&self, path: &Path) -> Option<&AbsPath> {
match self {
ProjectWorkspace::Cargo { cargo, .. } => {
Some(cargo.workspace_root()).filter(|root| path.starts_with(root))
@ -531,7 +508,7 @@ impl ProjectWorkspace {
ProjectWorkspace::Json { project: ProjectJson { roots, .. }, .. } => roots
.iter()
.find(|root| path.starts_with(&root.path))
.map(|root| root.path.as_ref()),
.map(|root| root.path.as_path()),
}
}
}

View File

@ -2,11 +2,15 @@
use std::path::PathBuf;
use paths::{AbsPath, AbsPathBuf};
use ra_cfg::CfgOptions;
use ra_db::{CrateId, Dependency, Edition};
use rustc_hash::FxHashSet;
use serde::Deserialize;
use stdx::split_delim;
/// Roots and crates that compose this Rust project.
#[derive(Clone, Debug, Deserialize)]
#[derive(Clone, Debug)]
pub struct ProjectJson {
pub(crate) roots: Vec<Root>,
pub(crate) crates: Vec<Crate>,
@ -14,82 +18,100 @@ pub struct ProjectJson {
/// A root points to the directory which contains Rust crates. rust-analyzer watches all files in
/// all roots. Roots might be nested.
#[derive(Clone, Debug, Deserialize)]
#[serde(transparent)]
#[derive(Clone, Debug)]
pub struct Root {
pub(crate) path: PathBuf,
pub(crate) path: AbsPathBuf,
}
/// 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.
#[derive(Clone, Debug, Deserialize)]
#[derive(Clone, Debug)]
pub struct Crate {
pub(crate) root_module: PathBuf,
pub(crate) root_module: AbsPathBuf,
pub(crate) edition: Edition,
pub(crate) deps: Vec<Dep>,
#[serde(default)]
pub(crate) cfg: FxHashSet<String>,
pub(crate) out_dir: Option<PathBuf>,
pub(crate) proc_macro_dylib_path: Option<PathBuf>,
pub(crate) deps: Vec<Dependency>,
pub(crate) cfg: CfgOptions,
pub(crate) out_dir: Option<AbsPathBuf>,
pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
}
#[derive(Clone, Copy, Debug, Deserialize)]
impl ProjectJson {
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
ProjectJson {
roots: data.roots.into_iter().map(|path| Root { path: base.join(path) }).collect(),
crates: data
.crates
.into_iter()
.map(|crate_data| Crate {
root_module: base.join(crate_data.root_module),
edition: crate_data.edition.into(),
deps: crate_data
.deps
.into_iter()
.map(|dep_data| Dependency {
crate_id: CrateId(dep_data.krate as u32),
name: dep_data.name.into(),
})
.collect::<Vec<_>>(),
cfg: {
let mut cfg = CfgOptions::default();
for entry in &crate_data.cfg {
match split_delim(entry, '=') {
Some((key, value)) => {
cfg.insert_key_value(key.into(), value.into());
}
None => cfg.insert_atom(entry.into()),
}
}
cfg
},
out_dir: crate_data.out_dir.map(|it| base.join(it)),
proc_macro_dylib_path: crate_data.proc_macro_dylib_path.map(|it| base.join(it)),
})
.collect::<Vec<_>>(),
}
}
}
#[derive(Deserialize)]
pub struct ProjectJsonData {
roots: Vec<PathBuf>,
crates: Vec<CrateData>,
}
#[derive(Deserialize)]
struct CrateData {
root_module: PathBuf,
edition: EditionData,
deps: Vec<DepData>,
#[serde(default)]
cfg: FxHashSet<String>,
out_dir: Option<PathBuf>,
proc_macro_dylib_path: Option<PathBuf>,
}
#[derive(Deserialize)]
#[serde(rename = "edition")]
pub enum Edition {
enum EditionData {
#[serde(rename = "2015")]
Edition2015,
#[serde(rename = "2018")]
Edition2018,
}
/// Identifies a crate by position in the crates array.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[serde(transparent)]
pub struct CrateId(pub usize);
/// A dependency of a crate, identified by its id in the crates array and name.
#[derive(Clone, Debug, Deserialize)]
pub struct Dep {
#[serde(rename = "crate")]
pub(crate) krate: CrateId,
pub(crate) name: String,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_crate_deserialization() {
let raw_json = json!( {
"crate_id": 2,
"root_module": "this/is/a/file/path.rs",
"deps": [
{
"crate": 1,
"name": "some_dep_crate"
},
],
"edition": "2015",
"cfg": [
"atom_1",
"atom_2",
"feature=feature_1",
"feature=feature_2",
"other=value",
],
});
let krate: Crate = serde_json::from_value(raw_json).unwrap();
assert!(krate.cfg.contains(&"atom_1".to_string()));
assert!(krate.cfg.contains(&"atom_2".to_string()));
assert!(krate.cfg.contains(&"feature=feature_1".to_string()));
assert!(krate.cfg.contains(&"feature=feature_2".to_string()));
assert!(krate.cfg.contains(&"other=value".to_string()));
impl From<EditionData> for Edition {
fn from(data: EditionData) -> Self {
match data {
EditionData::Edition2015 => Edition::Edition2015,
EditionData::Edition2018 => Edition::Edition2018,
}
}
}
#[derive(Deserialize)]
struct DepData {
/// Identifies a crate by position in the crates array.
#[serde(rename = "crate")]
krate: usize,
name: String,
}

View File

@ -1,8 +1,7 @@
//! See `CargoTargetSpec`
use std::path::PathBuf;
use ra_cfg::CfgExpr;
use ra_db::AbsPathBuf;
use ra_ide::{FileId, RunnableKind, TestId};
use ra_project_model::{self, TargetKind};
@ -14,7 +13,7 @@ use crate::{global_state::GlobalStateSnapshot, Result};
/// build/test/run the target.
#[derive(Clone)]
pub(crate) struct CargoTargetSpec {
pub(crate) workspace_root: PathBuf,
pub(crate) workspace_root: AbsPathBuf,
pub(crate) package: String,
pub(crate) target: String,
pub(crate) target_kind: TargetKind,

View File

@ -1,13 +1,13 @@
//! Loads a Cargo project into a static instance of analysis, without support
//! for incorporating changes.
use std::{convert::TryFrom, path::Path, sync::Arc};
use std::{path::Path, sync::Arc};
use anyhow::Result;
use crossbeam_channel::{unbounded, Receiver};
use ra_db::{AbsPathBuf, CrateGraph};
use ra_ide::{AnalysisChange, AnalysisHost};
use ra_project_model::{CargoConfig, ProcMacroClient, ProjectManifest, ProjectWorkspace};
use vfs::loader::Handle;
use vfs::{loader::Handle, AbsPath};
use crate::global_state::{ProjectFolders, SourceRootConfig};
@ -39,10 +39,9 @@ pub fn load_cargo(
ProcMacroClient::dummy()
};
let crate_graph = ws.to_crate_graph(None, &proc_macro_client, &mut |path: &Path| {
let path = AbsPathBuf::try_from(path.to_path_buf()).unwrap();
let contents = loader.load_sync(&path);
let path = vfs::VfsPath::from(path);
let crate_graph = ws.to_crate_graph(None, &proc_macro_client, &mut |path: &AbsPath| {
let contents = loader.load_sync(path);
let path = vfs::VfsPath::from(path.to_path_buf());
vfs.set_file_contents(path.clone(), contents);
vfs.file_id(&path)
});

View File

@ -14,7 +14,7 @@ use lsp_types::ClientCapabilities;
use ra_db::AbsPathBuf;
use ra_flycheck::FlycheckConfig;
use ra_ide::{AssistConfig, CompletionConfig, HoverConfig, InlayHintsConfig};
use ra_project_model::{CargoConfig, ProjectJson, ProjectManifest};
use ra_project_model::{CargoConfig, ProjectJson, ProjectJsonData, ProjectManifest};
use serde::Deserialize;
#[derive(Debug, Clone)]
@ -273,19 +273,19 @@ impl Config {
self.lens = LensConfig::NO_LENS;
}
if let Some(linked_projects) = get::<Vec<ManifestOrJsonProject>>(value, "/linkedProjects") {
if let Some(linked_projects) = get::<Vec<ManifestOrProjectJson>>(value, "/linkedProjects") {
if !linked_projects.is_empty() {
self.linked_projects.clear();
for linked_project in linked_projects {
let linked_project = match linked_project {
ManifestOrJsonProject::Manifest(it) => {
ManifestOrProjectJson::Manifest(it) => {
let path = self.root_path.join(it);
match ProjectManifest::from_manifest_file(path) {
Ok(it) => it.into(),
Err(_) => continue,
}
}
ManifestOrJsonProject::JsonProject(it) => it.into(),
ManifestOrProjectJson::ProjectJson(it) => ProjectJson::new(&self.root_path, it).into(),
};
self.linked_projects.push(linked_project);
}
@ -371,7 +371,7 @@ impl Config {
#[derive(Deserialize)]
#[serde(untagged)]
enum ManifestOrJsonProject {
enum ManifestOrProjectJson {
Manifest(PathBuf),
JsonProject(ProjectJson),
ProjectJson(ProjectJsonData),
}

View File

@ -3,7 +3,7 @@
//!
//! Each tick provides an immutable snapshot of the state as `WorldSnapshot`.
use std::{convert::TryFrom, path::Path, sync::Arc};
use std::{convert::TryFrom, sync::Arc};
use crossbeam_channel::{unbounded, Receiver};
use lsp_types::Url;
@ -13,7 +13,7 @@ use ra_flycheck::{Flycheck, FlycheckConfig};
use ra_ide::{Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId};
use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target};
use stdx::format_to;
use vfs::{file_set::FileSetConfig, loader::Handle, AbsPathBuf};
use vfs::{file_set::FileSetConfig, loader::Handle, AbsPath, AbsPathBuf};
use crate::{
config::{Config, FilesWatcher},
@ -31,7 +31,7 @@ fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) ->
workspaces.iter().find_map(|w| match w {
ProjectWorkspace::Cargo { cargo, .. } => {
let cargo_project_root = cargo.workspace_root().to_path_buf();
Some(Flycheck::new(config.clone(), cargo_project_root))
Some(Flycheck::new(config.clone(), cargo_project_root.into()))
}
ProjectWorkspace::Json { .. } => {
log::warn!("Cargo check watching only supported for cargo workspaces, disabling");
@ -112,10 +112,9 @@ impl GlobalState {
// Create crate graph from all the workspaces
let mut crate_graph = CrateGraph::default();
let mut load = |path: &Path| {
let path = AbsPathBuf::try_from(path.to_path_buf()).ok()?;
let contents = loader.load_sync(&path);
let path = vfs::VfsPath::from(path);
let mut load = |path: &AbsPath| {
let contents = loader.load_sync(path);
let path = vfs::VfsPath::from(path.to_path_buf());
vfs.set_file_contents(path.clone(), contents);
vfs.file_id(&path)
};

View File

@ -114,10 +114,7 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
.ok()
}
LinkedProject::InlineJsonProject(it) => {
Some(ra_project_model::ProjectWorkspace::Json {
project: it.clone(),
project_location: config.root_path.clone(),
})
Some(ra_project_model::ProjectWorkspace::Json { project: it.clone() })
}
})
.collect::<Vec<_>>()

View File

@ -419,7 +419,7 @@ pub fn handle_runnables(
location: None,
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::CargoRunnable {
workspace_root: Some(spec.workspace_root.clone()),
workspace_root: Some(spec.workspace_root.clone().into()),
cargo_args: vec![
cmd.to_string(),
"--package".to_string(),

View File

@ -663,7 +663,7 @@ pub(crate) fn runnable(
location: Some(location),
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::CargoRunnable {
workspace_root: workspace_root,
workspace_root: workspace_root.map(|it| it.into()),
cargo_args,
executable_args,
},

View File

@ -45,7 +45,7 @@ impl loader::Handle for LoaderHandle {
fn invalidate(&mut self, path: AbsPathBuf) {
self.sender.send(Message::Invalidate(path)).unwrap();
}
fn load_sync(&mut self, path: &AbsPathBuf) -> Option<Vec<u8>> {
fn load_sync(&mut self, path: &AbsPath) -> Option<Vec<u8>> {
read(path)
}
}

View File

@ -1,7 +1,7 @@
//! Object safe interface for file watching and reading.
use std::fmt;
use paths::AbsPathBuf;
use paths::{AbsPath, AbsPathBuf};
#[derive(Debug)]
pub enum Entry {
@ -28,7 +28,7 @@ pub trait Handle: fmt::Debug {
Self: Sized;
fn set_config(&mut self, config: Config);
fn invalidate(&mut self, path: AbsPathBuf);
fn load_sync(&mut self, path: &AbsPathBuf) -> Option<Vec<u8>>;
fn load_sync(&mut self, path: &AbsPath) -> Option<Vec<u8>>;
}
impl Entry {

View File

@ -322,6 +322,8 @@ There are tree ways to feed `rust-project.json` to rust-analyzer:
* Specify `"rust-analyzer.linkedProjects": [ "path/to/rust-project.json" ]` in the settings (and make sure that your LSP client sends settings as a part of initialize request).
* Specify `"rust-analyzer.linkedProjects": [ { "roots": [...], "crates": [...] }]` inline.
Relative paths are interpreted relative to `rust-project.json` file location or (for inline JSON) relative to `rootUri`.
See https://github.com/rust-analyzer/rust-project.json-example for a small example.
== Features