3549: Implement env! macro r=matklad a=edwin0cheng

This PR implements `env!` macro by adding following things:

1. Added `additional_outdirs` settings in vscode. (naming to be bikeshed)
2. Added `ExternSourceId` which is a wrapping for SourceRootId but only used in extern sources. It is because `OUT_DIR` is not belonged to any crate and we have to access it behind an `AstDatabase`.
3. This PR does not implement the `OUT_DIR` parsing from `cargo check`. I don't have general design about this,  @kiljacken could we reuse some cargo watch code for that ?

~~Block on [#3536]~~

PS: After this PR , we (kind of) completed the `include!(concat!(env!('OUT_DIR'),  "foo.rs")` macro call combo. [Exodia Obliterate!](https://www.youtube.com/watch?v=RfqNH3FoGi0)

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
This commit is contained in:
bors[bot] 2020-03-11 10:51:07 +00:00 committed by GitHub
commit c48dcf7411
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 322 additions and 74 deletions

View file

@ -61,7 +61,14 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
}; };
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
crate_graph.add_crate_root(file_id, meta.edition, meta.krate, meta.cfg, meta.env); crate_graph.add_crate_root(
file_id,
meta.edition,
meta.krate,
meta.cfg,
meta.env,
Default::default(),
);
crate_graph crate_graph
} else { } else {
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
@ -71,6 +78,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
crate_graph crate_graph
}; };
@ -119,6 +127,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
Some(krate.clone()), Some(krate.clone()),
meta.cfg, meta.cfg,
meta.env, meta.env,
Default::default(),
); );
let prev = crates.insert(krate.clone(), crate_id); let prev = crates.insert(krate.clone(), crate_id);
assert!(prev.is_none()); assert!(prev.is_none());
@ -155,6 +164,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
} else { } else {
for (from, to) in crate_deps { for (from, to) in crate_deps {

View file

@ -113,6 +113,7 @@ pub struct CrateData {
pub display_name: Option<String>, pub display_name: Option<String>,
pub cfg_options: CfgOptions, pub cfg_options: CfgOptions,
pub env: Env, pub env: Env,
pub extern_source: ExternSource,
pub dependencies: Vec<Dependency>, pub dependencies: Vec<Dependency>,
} }
@ -122,11 +123,22 @@ pub enum Edition {
Edition2015, Edition2015,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ExternSourceId(pub u32);
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct Env { pub struct Env {
entries: FxHashMap<String, String>, entries: FxHashMap<String, String>,
} }
// FIXME: Redesign vfs for solve the following limitation ?
// Note: Some env variables (e.g. OUT_DIR) are located outside of the
// crate. We store a map to allow remap it to ExternSourceId
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct ExternSource {
extern_paths: FxHashMap<String, ExternSourceId>,
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dependency { pub struct Dependency {
pub crate_id: CrateId, pub crate_id: CrateId,
@ -141,6 +153,7 @@ impl CrateGraph {
display_name: Option<String>, display_name: Option<String>,
cfg_options: CfgOptions, cfg_options: CfgOptions,
env: Env, env: Env,
extern_source: ExternSource,
) -> CrateId { ) -> CrateId {
let data = CrateData { let data = CrateData {
root_file_id: file_id, root_file_id: file_id,
@ -148,6 +161,7 @@ impl CrateGraph {
display_name, display_name,
cfg_options, cfg_options,
env, env,
extern_source,
dependencies: Vec::new(), dependencies: Vec::new(),
}; };
let crate_id = CrateId(self.arena.len() as u32); let crate_id = CrateId(self.arena.len() as u32);
@ -271,6 +285,27 @@ impl Env {
} }
} }
impl ExternSource {
pub fn extern_path(&self, path: &str) -> Option<(ExternSourceId, RelativePathBuf)> {
self.extern_paths.iter().find_map(|(root_path, id)| {
if path.starts_with(root_path) {
let mut rel_path = &path[root_path.len()..];
if rel_path.starts_with("/") {
rel_path = &rel_path[1..];
}
let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
Some((id.clone(), rel_path))
} else {
None
}
})
}
pub fn set_extern_path(&mut self, root_path: &str, root: ExternSourceId) {
self.extern_paths.insert(root_path.to_owned(), root);
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct ParseEditionError { pub struct ParseEditionError {
invalid_input: String, invalid_input: String,
@ -300,6 +335,7 @@ mod tests {
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
let crate2 = graph.add_crate_root( let crate2 = graph.add_crate_root(
FileId(2u32), FileId(2u32),
@ -307,6 +343,7 @@ mod tests {
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
let crate3 = graph.add_crate_root( let crate3 = graph.add_crate_root(
FileId(3u32), FileId(3u32),
@ -314,6 +351,7 @@ mod tests {
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@ -329,6 +367,7 @@ mod tests {
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
let crate2 = graph.add_crate_root( let crate2 = graph.add_crate_root(
FileId(2u32), FileId(2u32),
@ -336,6 +375,7 @@ mod tests {
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
let crate3 = graph.add_crate_root( let crate3 = graph.add_crate_root(
FileId(3u32), FileId(3u32),
@ -343,6 +383,7 @@ mod tests {
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@ -357,6 +398,7 @@ mod tests {
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
let crate2 = graph.add_crate_root( let crate2 = graph.add_crate_root(
FileId(2u32), FileId(2u32),
@ -364,6 +406,7 @@ mod tests {
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
assert!(graph assert!(graph
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)

View file

@ -11,7 +11,8 @@ use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit};
pub use crate::{ pub use crate::{
cancellation::Canceled, cancellation::Canceled,
input::{ input::{
CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, ExternSourceId,
FileId, SourceRoot, SourceRootId,
}, },
}; };
pub use relative_path::{RelativePath, RelativePathBuf}; pub use relative_path::{RelativePath, RelativePathBuf};
@ -87,6 +88,12 @@ pub trait FileLoader {
fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath) fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath)
-> Option<FileId>; -> Option<FileId>;
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>; fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;
fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId>;
} }
/// Database which stores all significant input facts: source code and project /// Database which stores all significant input facts: source code and project
@ -164,4 +171,13 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
let source_root = self.0.file_source_root(file_id); let source_root = self.0.file_source_root(file_id);
self.0.source_root_crates(source_root) self.0.source_root_crates(source_root)
} }
fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
let source_root = self.0.source_root(SourceRootId(extern_id.0));
source_root.file_by_relative_path(&relative_path)
}
} }

View file

@ -6,7 +6,7 @@ use std::{
}; };
use crate::db::DefDatabase; use crate::db::DefDatabase;
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; use ra_db::{salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath};
#[salsa::database( #[salsa::database(
ra_db::SourceDatabaseExtStorage, ra_db::SourceDatabaseExtStorage,
@ -52,6 +52,14 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
} }
impl TestDB { impl TestDB {

View file

@ -90,15 +90,15 @@ register_builtin! {
(line, Line) => line_expand, (line, Line) => line_expand,
(stringify, Stringify) => stringify_expand, (stringify, Stringify) => stringify_expand,
(format_args, FormatArgs) => format_args_expand, (format_args, FormatArgs) => format_args_expand,
(env, Env) => env_expand,
(option_env, OptionEnv) => option_env_expand,
// format_args_nl only differs in that it adds a newline in the end, // format_args_nl only differs in that it adds a newline in the end,
// so we use the same stub expansion for now // so we use the same stub expansion for now
(format_args_nl, FormatArgsNl) => format_args_expand, (format_args_nl, FormatArgsNl) => format_args_expand,
EAGER: EAGER:
(concat, Concat) => concat_expand, (concat, Concat) => concat_expand,
(include, Include) => include_expand (include, Include) => include_expand,
(env, Env) => env_expand,
(option_env, OptionEnv) => option_env_expand
} }
fn line_expand( fn line_expand(
@ -137,31 +137,6 @@ fn stringify_expand(
Ok(expanded) Ok(expanded)
} }
fn env_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
// dummy implementation for type-checking purposes
// we cannot use an empty string here, because for
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become
// `include!("foo.rs"), which maybe infinite loop
let expanded = quote! { "__RA_UNIMPLEMENTATED__" };
Ok(expanded)
}
fn option_env_expand(
_db: &dyn AstDatabase,
_id: LazyMacroId,
_tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
// dummy implementation for type-checking purposes
let expanded = quote! { std::option::Option::None::<&str> };
Ok(expanded)
}
fn column_expand( fn column_expand(
_db: &dyn AstDatabase, _db: &dyn AstDatabase,
_id: LazyMacroId, _id: LazyMacroId,
@ -278,14 +253,29 @@ fn concat_expand(
fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> { fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> {
let call_site = call_id.as_file().original_file(db); let call_site = call_id.as_file().original_file(db);
let path = RelativePath::new(&path);
let res = db.resolve_relative_path(call_site, &path)?; // Handle trivial case
// Prevent include itself if let Some(res) = db.resolve_relative_path(call_site, &RelativePath::new(&path)) {
if res == call_site { // Prevent include itself
return None; return if res == call_site { None } else { Some(res) };
} }
Some(res)
// Extern paths ?
let krate = db.relevant_crates(call_site).get(0)?.clone();
let (extern_source_id, relative_file) =
db.crate_graph()[krate].extern_source.extern_path(path)?;
db.resolve_extern_path(extern_source_id, &relative_file)
}
fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> {
tt.token_trees
.get(0)
.and_then(|tt| match tt {
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => unquote_str(&it),
_ => None,
})
.ok_or_else(|| mbe::ExpandError::ConversionError)
} }
fn include_expand( fn include_expand(
@ -293,15 +283,7 @@ fn include_expand(
arg_id: EagerMacroId, arg_id: EagerMacroId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> { ) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
let path = tt let path = parse_string(tt)?;
.token_trees
.get(0)
.and_then(|tt| match tt {
tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => unquote_str(&it),
_ => None,
})
.ok_or_else(|| mbe::ExpandError::ConversionError)?;
let file_id = let file_id =
relative_file(db, arg_id.into(), &path).ok_or_else(|| mbe::ExpandError::ConversionError)?; relative_file(db, arg_id.into(), &path).ok_or_else(|| mbe::ExpandError::ConversionError)?;
@ -314,12 +296,58 @@ fn include_expand(
Ok((res, FragmentKind::Items)) Ok((res, FragmentKind::Items))
} }
fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
let call_id: MacroCallId = arg_id.into();
let original_file = call_id.as_file().original_file(db);
let krate = db.relevant_crates(original_file).get(0)?.clone();
db.crate_graph()[krate].env.get(key)
}
fn env_expand(
db: &dyn AstDatabase,
arg_id: EagerMacroId,
tt: &tt::Subtree,
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
let key = parse_string(tt)?;
// FIXME:
// If the environment variable is not defined int rustc, then a compilation error will be emitted.
// We might do the same if we fully support all other stuffs.
// But for now on, we should return some dummy string for better type infer purpose.
// However, we cannot use an empty string here, because for
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become
// `include!("foo.rs"), which might go to infinite loop
let s = get_env_inner(db, arg_id, &key).unwrap_or("__RA_UNIMPLEMENTATED__".to_string());
let expanded = quote! { #s };
Ok((expanded, FragmentKind::Expr))
}
fn option_env_expand(
db: &dyn AstDatabase,
arg_id: EagerMacroId,
tt: &tt::Subtree,
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
let key = parse_string(tt)?;
let expanded = match get_env_inner(db, arg_id, &key) {
None => quote! { std::option::Option::None::<&str> },
Some(s) => quote! { std::option::Some(#s) },
};
Ok((expanded, FragmentKind::Expr))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{name::AsName, test_db::TestDB, AstNode, MacroCallId, MacroCallKind, MacroCallLoc}; use crate::{
name::AsName, test_db::TestDB, AstNode, EagerCallLoc, MacroCallId, MacroCallKind,
MacroCallLoc,
};
use ra_db::{fixture::WithFixture, SourceDatabase}; use ra_db::{fixture::WithFixture, SourceDatabase};
use ra_syntax::ast::NameOwner; use ra_syntax::ast::NameOwner;
use std::sync::Arc;
fn expand_builtin_macro(ra_fixture: &str) -> String { fn expand_builtin_macro(ra_fixture: &str) -> String {
let (db, file_id) = TestDB::with_single_file(&ra_fixture); let (db, file_id) = TestDB::with_single_file(&ra_fixture);
@ -330,27 +358,61 @@ mod tests {
let ast_id_map = db.ast_id_map(file_id.into()); let ast_id_map = db.ast_id_map(file_id.into());
let expander = find_by_name(&macro_calls[0].name().unwrap().as_name()).unwrap(); let expander = find_by_name(&macro_calls[0].name().unwrap().as_name()).unwrap();
let expander = expander.left().unwrap();
// the first one should be a macro_rules let file_id = match expander {
let def = MacroDefId { Either::Left(expander) => {
krate: Some(CrateId(0)), // the first one should be a macro_rules
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))), let def = MacroDefId {
kind: MacroDefKind::BuiltIn(expander), krate: Some(CrateId(0)),
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))),
kind: MacroDefKind::BuiltIn(expander),
};
let loc = MacroCallLoc {
def,
kind: MacroCallKind::FnLike(AstId::new(
file_id.into(),
ast_id_map.ast_id(&macro_calls[1]),
)),
};
let id: MacroCallId = db.intern_macro(loc).into();
id.as_file()
}
Either::Right(expander) => {
// the first one should be a macro_rules
let def = MacroDefId {
krate: Some(CrateId(0)),
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))),
kind: MacroDefKind::BuiltInEager(expander),
};
let args = macro_calls[1].token_tree().unwrap();
let parsed_args = mbe::ast_to_token_tree(&args).unwrap().0;
let arg_id = db.intern_eager_expansion({
EagerCallLoc {
def,
fragment: FragmentKind::Expr,
subtree: Arc::new(parsed_args.clone()),
file_id: file_id.into(),
}
});
let (subtree, fragment) = expander.expand(&db, arg_id, &parsed_args).unwrap();
let eager = EagerCallLoc {
def,
fragment,
subtree: Arc::new(subtree),
file_id: file_id.into(),
};
let id: MacroCallId = db.intern_eager_expansion(eager.into()).into();
id.as_file()
}
}; };
let loc = MacroCallLoc { db.parse_or_expand(file_id).unwrap().to_string()
def,
kind: MacroCallKind::FnLike(AstId::new(
file_id.into(),
ast_id_map.ast_id(&macro_calls[1]),
)),
};
let id: MacroCallId = db.intern_macro(loc).into();
let parsed = db.parse_or_expand(id.as_file()).unwrap();
parsed.text().to_string()
} }
#[test] #[test]

View file

@ -5,7 +5,7 @@ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; use ra_db::{salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath};
#[salsa::database( #[salsa::database(
ra_db::SourceDatabaseExtStorage, ra_db::SourceDatabaseExtStorage,
@ -51,4 +51,11 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn resolve_extern_path(
&self,
anchor: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(anchor, relative_path)
}
} }

View file

@ -67,6 +67,13 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn resolve_extern_path(
&self,
extern_id: ra_db::ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
} }
impl TestDB { impl TestDB {

View file

@ -549,6 +549,26 @@ fn main() {
); );
} }
#[test]
fn infer_builtin_macros_env() {
assert_snapshot!(
infer(r#"
//- /main.rs env:foo=bar
#[rustc_builtin_macro]
macro_rules! env {() => {}}
fn main() {
let x = env!("foo");
}
"#),
@r###"
![0; 5) '"bar"': &str
[88; 116) '{ ...o"); }': ()
[98; 99) 'x': &str
"###
);
}
#[test] #[test]
fn infer_derive_clone_simple() { fn infer_derive_clone_simple() {
let (db, pos) = TestDB::with_position( let (db, pos) = TestDB::with_position(

View file

@ -212,6 +212,7 @@ impl Analysis {
None, None,
cfg_options, cfg_options,
Env::default(), Env::default(),
Default::default(),
); );
change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text)); change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
change.set_crate_graph(crate_graph); change.set_crate_graph(crate_graph);

View file

@ -102,6 +102,7 @@ impl MockAnalysis {
None, None,
cfg_options, cfg_options,
Env::default(), Env::default(),
Default::default(),
)); ));
} else if path.ends_with("/lib.rs") { } else if path.ends_with("/lib.rs") {
let crate_name = path.parent().unwrap().file_name().unwrap(); let crate_name = path.parent().unwrap().file_name().unwrap();
@ -111,6 +112,7 @@ impl MockAnalysis {
Some(crate_name.to_owned()), Some(crate_name.to_owned()),
cfg_options, cfg_options,
Env::default(), Env::default(),
Default::default(),
); );
if let Some(root_crate) = root_crate { if let Some(root_crate) = root_crate {
crate_graph crate_graph

View file

@ -136,6 +136,7 @@ mod tests {
None, None,
CfgOptions::default(), CfgOptions::default(),
Env::default(), Env::default(),
Default::default(),
); );
let mut change = AnalysisChange::new(); let mut change = AnalysisChange::new();
change.set_crate_graph(crate_graph); change.set_crate_graph(crate_graph);

View file

@ -55,6 +55,13 @@ impl FileLoader for RootDatabase {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id) FileLoaderDelegate(self).relevant_crates(file_id)
} }
fn resolve_extern_path(
&self,
extern_id: ra_db::ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
} }
impl salsa::Database for RootDatabase { impl salsa::Database for RootDatabase {

View file

@ -14,7 +14,7 @@ use std::{
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_db::{CrateGraph, CrateName, Edition, Env, FileId}; use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use serde_json::from_reader; use serde_json::from_reader;
@ -162,6 +162,7 @@ impl ProjectWorkspace {
pub fn to_crate_graph( pub fn to_crate_graph(
&self, &self,
default_cfg_options: &CfgOptions, default_cfg_options: &CfgOptions,
outdirs: &FxHashMap<String, (ExternSourceId, String)>,
load: &mut dyn FnMut(&Path) -> Option<FileId>, load: &mut dyn FnMut(&Path) -> Option<FileId>,
) -> CrateGraph { ) -> CrateGraph {
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
@ -185,6 +186,8 @@ impl ProjectWorkspace {
} }
opts opts
}; };
// FIXME: No crate name in json definition such that we cannot add OUT_DIR to env
crates.insert( crates.insert(
crate_id, crate_id,
crate_graph.add_crate_root( crate_graph.add_crate_root(
@ -194,6 +197,7 @@ impl ProjectWorkspace {
None, None,
cfg_options, cfg_options,
Env::default(), Env::default(),
Default::default(),
), ),
); );
} }
@ -231,12 +235,20 @@ impl ProjectWorkspace {
opts opts
}; };
let mut env = Env::default();
let mut extern_source = ExternSource::default();
if let Some((id, path)) = outdirs.get(krate.name(&sysroot)) {
env.set("OUT_DIR", path.clone());
extern_source.set_extern_path(&path, *id);
}
let crate_id = crate_graph.add_crate_root( let crate_id = crate_graph.add_crate_root(
file_id, file_id,
Edition::Edition2018, Edition::Edition2018,
Some(krate.name(&sysroot).to_string()), Some(krate.name(&sysroot).to_string()),
cfg_options, cfg_options,
Env::default(), env,
extern_source,
); );
sysroot_crates.insert(krate, crate_id); sysroot_crates.insert(krate, crate_id);
} }
@ -275,12 +287,19 @@ impl ProjectWorkspace {
opts.insert_features(pkg.features(&cargo).iter().map(Into::into)); opts.insert_features(pkg.features(&cargo).iter().map(Into::into));
opts opts
}; };
let mut env = Env::default();
let mut extern_source = ExternSource::default();
if let Some((id, path)) = outdirs.get(pkg.name(&cargo)) {
env.set("OUT_DIR", path.clone());
extern_source.set_extern_path(&path, *id);
}
let crate_id = crate_graph.add_crate_root( let crate_id = crate_graph.add_crate_root(
file_id, file_id,
edition, edition,
Some(pkg.name(&cargo).to_string()), Some(pkg.name(&cargo).to_string()),
cfg_options, cfg_options,
Env::default(), env,
extern_source,
); );
if tgt.kind(&cargo) == TargetKind::Lib { if tgt.kind(&cargo) == TargetKind::Lib {
lib_tgt = Some(crate_id); lib_tgt = Some(crate_id);

View file

@ -52,7 +52,10 @@ pub(crate) fn load_cargo(
opts opts
}; };
let crate_graph = ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { // FIXME: outdirs?
let outdirs = FxHashMap::default();
let crate_graph = ws.to_crate_graph(&default_cfg_options, &outdirs, &mut |path: &Path| {
let vfs_file = vfs.load(path); let vfs_file = vfs.load(path);
log::debug!("vfs file {:?} -> {:?}", path, vfs_file); log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
vfs_file.map(vfs_file_to_id) vfs_file.map(vfs_file_to_id)

View file

@ -44,6 +44,9 @@ pub struct ServerConfig {
/// Fine grained feature flags to disable specific features. /// Fine grained feature flags to disable specific features.
pub feature_flags: FxHashMap<String, bool>, pub feature_flags: FxHashMap<String, bool>,
/// Fine grained controls for additional `OUT_DIR` env variables
pub additional_out_dirs: FxHashMap<String, String>,
pub rustfmt_args: Vec<String>, pub rustfmt_args: Vec<String>,
/// Cargo feature configurations. /// Cargo feature configurations.
@ -64,6 +67,7 @@ impl Default for ServerConfig {
cargo_watch_all_targets: true, cargo_watch_all_targets: true,
with_sysroot: true, with_sysroot: true,
feature_flags: FxHashMap::default(), feature_flags: FxHashMap::default(),
additional_out_dirs: FxHashMap::default(),
cargo_features: Default::default(), cargo_features: Default::default(),
rustfmt_args: Vec::new(), rustfmt_args: Vec::new(),
} }

View file

@ -196,6 +196,7 @@ pub fn main_loop(
Watch(!config.use_client_watching), Watch(!config.use_client_watching),
options, options,
feature_flags, feature_flags,
config.additional_out_dirs,
) )
}; };

View file

@ -26,6 +26,8 @@ use crate::{
vfs_glob::{Glob, RustPackageFilterBuilder}, vfs_glob::{Glob, RustPackageFilterBuilder},
LspError, Result, LspError, Result,
}; };
use ra_db::ExternSourceId;
use rustc_hash::{FxHashMap, FxHashSet};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Options { pub struct Options {
@ -78,6 +80,7 @@ impl WorldState {
watch: Watch, watch: Watch,
options: Options, options: Options,
feature_flags: FeatureFlags, feature_flags: FeatureFlags,
additional_out_dirs: FxHashMap<String, String>,
) -> WorldState { ) -> WorldState {
let mut change = AnalysisChange::new(); let mut change = AnalysisChange::new();
@ -99,6 +102,19 @@ impl WorldState {
RootEntry::new(pkg_root.path().clone(), filter.into_vfs_filter()) RootEntry::new(pkg_root.path().clone(), filter.into_vfs_filter())
})); }));
} }
let extern_dirs: FxHashSet<_> =
additional_out_dirs.iter().map(|(_, path)| (PathBuf::from(path))).collect();
let mut extern_source_roots = FxHashMap::default();
roots.extend(additional_out_dirs.iter().map(|(_, path)| {
let mut filter = RustPackageFilterBuilder::default().set_member(false);
for glob in exclude_globs.iter() {
filter = filter.exclude(glob.clone());
}
RootEntry::new(PathBuf::from(&path), filter.into_vfs_filter())
}));
let (task_sender, task_receiver) = unbounded(); let (task_sender, task_receiver) = unbounded();
let task_sender = Box::new(move |t| task_sender.send(t).unwrap()); let task_sender = Box::new(move |t| task_sender.send(t).unwrap());
let (mut vfs, vfs_roots) = Vfs::new(roots, task_sender, watch); let (mut vfs, vfs_roots) = Vfs::new(roots, task_sender, watch);
@ -108,6 +124,11 @@ impl WorldState {
let is_local = folder_roots.iter().any(|it| vfs_root_path.starts_with(it)); let is_local = folder_roots.iter().any(|it| vfs_root_path.starts_with(it));
change.add_root(SourceRootId(r.0), is_local); change.add_root(SourceRootId(r.0), is_local);
change.set_debug_root_path(SourceRootId(r.0), vfs_root_path.display().to_string()); change.set_debug_root_path(SourceRootId(r.0), vfs_root_path.display().to_string());
// FIXME: add path2root in vfs to simpily this logic
if extern_dirs.contains(&vfs_root_path) {
extern_source_roots.insert(vfs_root_path, ExternSourceId(r.0));
}
} }
// FIXME: Read default cfgs from config // FIXME: Read default cfgs from config
@ -125,11 +146,20 @@ impl WorldState {
vfs_file.map(|f| FileId(f.0)) vfs_file.map(|f| FileId(f.0))
}; };
workspaces.iter().map(|ws| ws.to_crate_graph(&default_cfg_options, &mut load)).for_each( let mut outdirs = FxHashMap::default();
|graph| { for (name, path) in additional_out_dirs {
let path = PathBuf::from(&path);
if let Some(id) = extern_source_roots.get(&path) {
outdirs.insert(name, (id.clone(), path.to_string_lossy().replace("\\", "/")));
}
}
workspaces
.iter()
.map(|ws| ws.to_crate_graph(&default_cfg_options, &outdirs, &mut load))
.for_each(|graph| {
crate_graph.extend(graph); crate_graph.extend(graph);
}, });
);
change.set_crate_graph(crate_graph); change.set_crate_graph(crate_graph);
// FIXME: Figure out the multi-workspace situation // FIXME: Figure out the multi-workspace situation

View file

@ -224,6 +224,11 @@
"default": true, "default": true,
"description": "Whether to ask for permission before downloading any files from the Internet" "description": "Whether to ask for permission before downloading any files from the Internet"
}, },
"rust-analyzer.additionalOutDirs": {
"type": "object",
"default": {},
"markdownDescription": "Fine grained controls for OUT_DIR `env!(\"OUT_DIR\")` variable. e.g. `{\"foo\":\"/path/to/foo\"}`, "
},
"rust-analyzer.serverPath": { "rust-analyzer.serverPath": {
"type": [ "type": [
"null", "null",

View file

@ -37,6 +37,7 @@ export async function createClient(config: Config, serverPath: string): Promise<
excludeGlobs: config.excludeGlobs, excludeGlobs: config.excludeGlobs,
useClientWatching: config.useClientWatching, useClientWatching: config.useClientWatching,
featureFlags: config.featureFlags, featureFlags: config.featureFlags,
additionalOutDirs: config.additionalOutDirs,
withSysroot: config.withSysroot, withSysroot: config.withSysroot,
cargoFeatures: config.cargoFeatures, cargoFeatures: config.cargoFeatures,
rustfmtArgs: config.rustfmtArgs, rustfmtArgs: config.rustfmtArgs,

View file

@ -154,6 +154,7 @@ export class Config {
get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; } get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; }
get useClientWatching() { return this.cfg.get("useClientWatching") as boolean; } get useClientWatching() { return this.cfg.get("useClientWatching") as boolean; }
get featureFlags() { return this.cfg.get("featureFlags") as Record<string, boolean>; } get featureFlags() { return this.cfg.get("featureFlags") as Record<string, boolean>; }
get additionalOutDirs() { return this.cfg.get("additionalOutDirs") as Record<string, string>; }
get rustfmtArgs() { return this.cfg.get("rustfmtArgs") as string[]; } get rustfmtArgs() { return this.cfg.get("rustfmtArgs") as string[]; }
get cargoWatchOptions(): CargoWatchOptions { get cargoWatchOptions(): CargoWatchOptions {