5473: Changes to rust-project.json r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-07-23 13:58:45 +00:00 committed by GitHub
commit 7bada8a76d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 118 additions and 112 deletions

View file

@ -222,7 +222,7 @@ impl From<Fixture> for FileMeta {
.edition .edition
.as_ref() .as_ref()
.map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()), .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
env: Env::from(f.env.iter()), env: f.env.into_iter().collect(),
} }
} }
} }

View file

@ -6,7 +6,7 @@
//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how //! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
//! actual IO is done and lowered to input. //! actual IO is done and lowered to input.
use std::{fmt, ops, str::FromStr, sync::Arc}; use std::{fmt, iter::FromIterator, ops, str::FromStr, sync::Arc};
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_syntax::SmolStr; use ra_syntax::SmolStr;
@ -298,18 +298,9 @@ impl fmt::Display for Edition {
} }
} }
impl<'a, T> From<T> for Env impl FromIterator<(String, String)> for Env {
where fn from_iter<T: IntoIterator<Item = (String, String)>>(iter: T) -> Self {
T: Iterator<Item = (&'a String, &'a String)>, Env { entries: FromIterator::from_iter(iter) }
{
fn from(iter: T) -> Self {
let mut result = Self::default();
for (k, v) in iter {
result.entries.insert(k.to_owned(), v.to_owned());
}
result
} }
} }

View file

@ -2,7 +2,7 @@
use std::sync::Arc; use std::sync::Arc;
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath}; use ra_db::{CrateName, FileSet, SourceRoot, VfsPath};
use test_utils::{ use test_utils::{
extract_annotations, extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER, extract_annotations, extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER,
}; };
@ -110,7 +110,7 @@ impl MockAnalysis {
data.edition.and_then(|it| it.parse().ok()).unwrap_or(Edition::Edition2018); data.edition.and_then(|it| it.parse().ok()).unwrap_or(Edition::Edition2018);
let file_id = FileId(i as u32 + 1); let file_id = FileId(i as u32 + 1);
let env = Env::from(data.env.iter()); let env = data.env.into_iter().collect();
if path == "/lib.rs" || path == "/main.rs" { if path == "/lib.rs" || path == "/main.rs" {
root_crate = Some(crate_graph.add_crate_root( root_crate = Some(crate_graph.add_crate_root(
file_id, file_id,

View file

@ -7,7 +7,6 @@ mod sysroot;
use std::{ use std::{
fs::{self, read_dir, ReadDir}, fs::{self, read_dir, ReadDir},
io, io,
path::Path,
process::{Command, Output}, process::{Command, Output},
}; };
@ -35,30 +34,12 @@ pub enum ProjectWorkspace {
/// `PackageRoot` describes a package root folder. /// `PackageRoot` describes a package root folder.
/// Which may be an external dependency, or a member of /// Which may be an external dependency, or a member of
/// the current workspace. /// the current workspace.
#[derive(Debug, Clone)] #[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct PackageRoot { pub struct PackageRoot {
/// Path to the root folder
path: AbsPathBuf,
/// Is a member of the current workspace /// Is a member of the current workspace
is_member: bool, pub is_member: bool,
out_dir: Option<AbsPathBuf>, pub include: Vec<AbsPathBuf>,
} pub exclude: Vec<AbsPathBuf>,
impl PackageRoot {
pub fn new_member(path: AbsPathBuf) -> PackageRoot {
Self { path, is_member: true, out_dir: None }
}
pub fn new_non_member(path: AbsPathBuf) -> PackageRoot {
Self { path, is_member: false, out_dir: None }
}
pub fn path(&self) -> &AbsPath {
&self.path
}
pub fn out_dir(&self) -> Option<&AbsPath> {
self.out_dir.as_deref()
}
pub fn is_member(&self) -> bool {
self.is_member
}
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
@ -195,18 +176,40 @@ impl ProjectWorkspace {
/// the root is a member of the current workspace /// the root is a member of the current workspace
pub fn to_roots(&self) -> Vec<PackageRoot> { pub fn to_roots(&self) -> Vec<PackageRoot> {
match self { match self {
ProjectWorkspace::Json { project } => { ProjectWorkspace::Json { project } => project
project.roots.iter().map(|r| PackageRoot::new_member(r.path.clone())).collect() .crates
} .iter()
.map(|krate| PackageRoot {
is_member: krate.is_workspace_member,
include: krate.include.clone(),
exclude: krate.exclude.clone(),
})
.collect::<FxHashSet<_>>()
.into_iter()
.collect::<Vec<_>>(),
ProjectWorkspace::Cargo { cargo, sysroot } => cargo ProjectWorkspace::Cargo { cargo, sysroot } => cargo
.packages() .packages()
.map(|pkg| PackageRoot { .map(|pkg| {
path: cargo[pkg].root().to_path_buf(), let is_member = cargo[pkg].is_member;
is_member: cargo[pkg].is_member, let pkg_root = cargo[pkg].root().to_path_buf();
out_dir: cargo[pkg].out_dir.clone(),
let mut include = vec![pkg_root.clone()];
include.extend(cargo[pkg].out_dir.clone());
let mut exclude = vec![pkg_root.join(".git")];
if is_member {
exclude.push(pkg_root.join("target"));
} else {
exclude.push(pkg_root.join("tests"));
exclude.push(pkg_root.join("examples"));
exclude.push(pkg_root.join("benches"));
}
PackageRoot { is_member, include, exclude }
}) })
.chain(sysroot.crates().map(|krate| { .chain(sysroot.crates().map(|krate| PackageRoot {
PackageRoot::new_non_member(sysroot[krate].root_dir().to_path_buf()) is_member: false,
include: vec![sysroot[krate].root_dir().to_path_buf()],
exclude: Vec::new(),
})) }))
.collect(), .collect(),
} }
@ -255,13 +258,7 @@ impl ProjectWorkspace {
let file_path = &krate.root_module; let file_path = &krate.root_module;
let file_id = load(&file_path)?; let file_id = load(&file_path)?;
let mut env = Env::default(); let env = krate.env.clone().into_iter().collect();
if let Some(out_dir) = &krate.out_dir {
// NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
env.set("OUT_DIR", out_dir);
}
}
let proc_macro = krate let proc_macro = krate
.proc_macro_dylib_path .proc_macro_dylib_path
.clone() .clone()
@ -503,18 +500,6 @@ impl ProjectWorkspace {
} }
crate_graph crate_graph
} }
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))
}
ProjectWorkspace::Json { project: ProjectJson { roots, .. }, .. } => roots
.iter()
.find(|root| path.starts_with(&root.path))
.map(|root| root.path.as_path()),
}
}
} }
fn get_rustc_cfg_options(target: Option<&str>) -> CfgOptions { fn get_rustc_cfg_options(target: Option<&str>) -> CfgOptions {

View file

@ -5,24 +5,16 @@ use std::path::PathBuf;
use paths::{AbsPath, AbsPathBuf}; use paths::{AbsPath, AbsPathBuf};
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_db::{CrateId, CrateName, Dependency, Edition}; use ra_db::{CrateId, CrateName, Dependency, Edition};
use rustc_hash::FxHashSet; use rustc_hash::{FxHashMap, FxHashSet};
use serde::{de, Deserialize}; use serde::{de, Deserialize};
use stdx::split_delim; use stdx::split_delim;
/// Roots and crates that compose this Rust project. /// Roots and crates that compose this Rust project.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProjectJson { pub struct ProjectJson {
pub(crate) roots: Vec<Root>,
pub(crate) crates: Vec<Crate>, pub(crate) crates: Vec<Crate>,
} }
/// 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, Eq, PartialEq)]
pub struct Root {
pub(crate) path: AbsPathBuf,
}
/// A crate points to the root module of a crate and lists the dependencies of the crate. This is /// 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. /// useful in creating the crate graph.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
@ -32,15 +24,16 @@ pub struct Crate {
pub(crate) deps: Vec<Dependency>, pub(crate) deps: Vec<Dependency>,
pub(crate) cfg: CfgOptions, pub(crate) cfg: CfgOptions,
pub(crate) target: Option<String>, pub(crate) target: Option<String>,
pub(crate) out_dir: Option<AbsPathBuf>, pub(crate) env: FxHashMap<String, String>,
pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>, pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
pub(crate) is_workspace_member: bool, pub(crate) is_workspace_member: bool,
pub(crate) include: Vec<AbsPathBuf>,
pub(crate) exclude: Vec<AbsPathBuf>,
} }
impl ProjectJson { impl ProjectJson {
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson { pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
ProjectJson { ProjectJson {
roots: data.roots.into_iter().map(|path| Root { path: base.join(path) }).collect(),
crates: data crates: data
.crates .crates
.into_iter() .into_iter()
@ -50,8 +43,19 @@ impl ProjectJson {
&& !crate_data.root_module.starts_with("..") && !crate_data.root_module.starts_with("..")
|| crate_data.root_module.starts_with(base) || crate_data.root_module.starts_with(base)
}); });
let root_module = base.join(crate_data.root_module);
let (include, exclude) = match crate_data.source {
Some(src) => {
let absolutize = |dirs: Vec<PathBuf>| {
dirs.into_iter().map(|it| base.join(it)).collect::<Vec<_>>()
};
(absolutize(src.include_dirs), absolutize(src.exclude_dirs))
}
None => (vec![root_module.parent().unwrap().to_path_buf()], Vec::new()),
};
Crate { Crate {
root_module: base.join(crate_data.root_module), root_module,
edition: crate_data.edition.into(), edition: crate_data.edition.into(),
deps: crate_data deps: crate_data
.deps .deps
@ -74,11 +78,13 @@ impl ProjectJson {
cfg cfg
}, },
target: crate_data.target, target: crate_data.target,
out_dir: crate_data.out_dir.map(|it| base.join(it)), env: crate_data.env,
proc_macro_dylib_path: crate_data proc_macro_dylib_path: crate_data
.proc_macro_dylib_path .proc_macro_dylib_path
.map(|it| base.join(it)), .map(|it| base.join(it)),
is_workspace_member, is_workspace_member,
include,
exclude,
} }
}) })
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
@ -88,7 +94,6 @@ impl ProjectJson {
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct ProjectJsonData { pub struct ProjectJsonData {
roots: Vec<PathBuf>,
crates: Vec<CrateData>, crates: Vec<CrateData>,
} }
@ -100,9 +105,11 @@ struct CrateData {
#[serde(default)] #[serde(default)]
cfg: FxHashSet<String>, cfg: FxHashSet<String>,
target: Option<String>, target: Option<String>,
out_dir: Option<PathBuf>, #[serde(default)]
env: FxHashMap<String, String>,
proc_macro_dylib_path: Option<PathBuf>, proc_macro_dylib_path: Option<PathBuf>,
is_workspace_member: Option<bool>, is_workspace_member: Option<bool>,
source: Option<CrateSource>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -132,6 +139,12 @@ struct DepData {
name: CrateName, name: CrateName,
} }
#[derive(Deserialize)]
struct CrateSource {
include_dirs: Vec<PathBuf>,
exclude_dirs: Vec<PathBuf>,
}
fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error> fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error>
where where
D: de::Deserializer<'de>, D: de::Deserializer<'de>,

View file

@ -5,7 +5,7 @@ use flycheck::FlycheckHandle;
use ra_db::{CrateGraph, SourceRoot, VfsPath}; use ra_db::{CrateGraph, SourceRoot, VfsPath};
use ra_ide::AnalysisChange; use ra_ide::AnalysisChange;
use ra_prof::profile; use ra_prof::profile;
use ra_project_model::{PackageRoot, ProcMacroClient, ProjectWorkspace}; use ra_project_model::{ProcMacroClient, ProjectWorkspace};
use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind}; use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
use crate::{ use crate::{
@ -149,8 +149,10 @@ impl GlobalState {
watchers: workspaces watchers: workspaces
.iter() .iter()
.flat_map(ProjectWorkspace::to_roots) .flat_map(ProjectWorkspace::to_roots)
.filter(PackageRoot::is_member) .filter(|it| it.is_member)
.map(|root| format!("{}/**/*.rs", root.path().display())) .flat_map(|root| {
root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display()))
})
.map(|glob_pattern| lsp_types::FileSystemWatcher { glob_pattern, kind: None }) .map(|glob_pattern| lsp_types::FileSystemWatcher { glob_pattern, kind: None })
.collect(), .collect(),
}; };
@ -261,31 +263,23 @@ impl ProjectFolders {
let mut local_filesets = vec![]; let mut local_filesets = vec![];
for root in workspaces.iter().flat_map(|it| it.to_roots()) { for root in workspaces.iter().flat_map(|it| it.to_roots()) {
let path = root.path().to_owned(); let file_set_roots: Vec<VfsPath> =
root.include.iter().cloned().map(VfsPath::from).collect();
let mut file_set_roots: Vec<VfsPath> = vec![]; let entry = {
let mut dirs = vfs::loader::Directories::default();
let entry = if root.is_member() { dirs.extensions.push("rs".into());
vfs::loader::Entry::local_cargo_package(path.to_path_buf()) dirs.include.extend(root.include);
} else { dirs.exclude.extend(root.exclude);
vfs::loader::Entry::cargo_package_dependency(path.to_path_buf()) vfs::loader::Entry::Directories(dirs)
}; };
if root.is_member {
res.watch.push(res.load.len());
}
res.load.push(entry); res.load.push(entry);
if root.is_member() {
res.watch.push(res.load.len() - 1);
}
if let Some(out_dir) = root.out_dir() { if root.is_member {
let out_dir = out_dir.to_path_buf();
res.load.push(vfs::loader::Entry::rs_files_recursively(out_dir.clone()));
if root.is_member() {
res.watch.push(res.load.len() - 1);
}
file_set_roots.push(out_dir.into());
}
file_set_roots.push(path.to_path_buf().into());
if root.is_member() {
local_filesets.push(fsc.len()); local_filesets.push(fsc.len());
} }
fsc.add_file_set(file_set_roots) fsc.add_file_set(file_set_roots)

View file

@ -17,7 +17,7 @@ pub enum Entry {
/// * it is not under `exclude` path /// * it is not under `exclude` path
/// ///
/// If many include/exclude paths match, the longest one wins. /// If many include/exclude paths match, the longest one wins.
#[derive(Debug, Clone)] #[derive(Debug, Clone, Default)]
pub struct Directories { pub struct Directories {
pub extensions: Vec<String>, pub extensions: Vec<String>,
pub include: Vec<AbsPathBuf>, pub include: Vec<AbsPathBuf>,

View file

@ -273,9 +273,6 @@ However, if you use some other build system, you'll have to describe the structu
[source,TypeScript] [source,TypeScript]
---- ----
interface JsonProject { interface JsonProject {
/// The set of paths containing the crates for this project.
/// Any `Crate` must be nested inside some `root`.
roots: string[];
/// The set of crates comprising the current project. /// The set of crates comprising the current project.
/// Must include all transitive dependencies as well as sysroot crate (libstd, libcore and such). /// Must include all transitive dependencies as well as sysroot crate (libstd, libcore and such).
crates: Crate[]; crates: Crate[];
@ -288,11 +285,37 @@ interface Crate {
edition: "2015" | "2018"; edition: "2015" | "2018";
/// Dependencies /// Dependencies
deps: Dep[]; deps: Dep[];
/// Should this crate be treated as a member of current "workspace".
///
/// By default, inferred from the `root_module` (members are the crates which reside
/// inside the directory opened in the editor).
///
/// Set this to `false` for things like standard library and 3rd party crates to
/// enable performance optimizations (rust-analyzer assumes that non-member crates
/// don't change).
is_workspace_member?: boolean;
/// Optionally specify the (super)set of `.rs` files comprising this crate.
///
/// By default, rust-analyzer assumes that only files under `root_module.parent` can belong to a crate.
/// `include_dirs` are included recursively, unless a subdirectory is in `exclude_dirs`.
///
/// Different crates can share the same `source`.
/// If two crates share an `.rs` file in common, they *must* have the same `source`.
/// rust-analyzer assumes that files from one source can't refer to files in another source.
source?: {
include_dirs: string[],
exclude_dirs: string[],
},
/// The set of cfgs activated for a given crate, like `["unix", "feature=foo", "feature=bar"]`. /// The set of cfgs activated for a given crate, like `["unix", "feature=foo", "feature=bar"]`.
cfg: string[]; cfg: string[];
/// Target triple for this Crate.
///
/// Used when running `rustc --print cfg` to get target-specific cfgs.
target?: string;
/// Environment variables, used for the `env!` macro
env: : { [key: string]: string; },
/// value of the OUT_DIR env variable.
out_dir?: string;
/// For proc-macro crates, path to compiles proc-macro (.so file). /// For proc-macro crates, path to compiles proc-macro (.so file).
proc_macro_dylib_path?: string; proc_macro_dylib_path?: string;
} }