Skip to content

Commit e00a1e0

Browse files
committed
Setup Env in world
1 parent 22f064c commit e00a1e0

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

crates/ra_project_model/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{
1414

1515
use anyhow::{bail, Context, Result};
1616
use ra_cfg::CfgOptions;
17-
use ra_db::{CrateGraph, CrateName, Edition, Env, FileId};
17+
use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSourceId, FileId};
1818
use rustc_hash::FxHashMap;
1919
use serde_json::from_reader;
2020

@@ -162,6 +162,7 @@ impl ProjectWorkspace {
162162
pub fn to_crate_graph(
163163
&self,
164164
default_cfg_options: &CfgOptions,
165+
outdirs: &FxHashMap<String, (ExternSourceId, String)>,
165166
load: &mut dyn FnMut(&Path) -> Option<FileId>,
166167
) -> CrateGraph {
167168
let mut crate_graph = CrateGraph::default();
@@ -185,6 +186,8 @@ impl ProjectWorkspace {
185186
}
186187
opts
187188
};
189+
190+
// FIXME: No crate name in json definition such that we cannot add OUT_DIR to env
188191
crates.insert(
189192
crate_id,
190193
crate_graph.add_crate_root(
@@ -231,12 +234,17 @@ impl ProjectWorkspace {
231234
opts
232235
};
233236

237+
let mut env = Env::default();
238+
if let Some((id, path)) = outdirs.get(krate.name(&sysroot)) {
239+
env.set_extern_path("OUT_DIR", &path, *id);
240+
}
241+
234242
let crate_id = crate_graph.add_crate_root(
235243
file_id,
236244
Edition::Edition2018,
237245
Some(krate.name(&sysroot).to_string()),
238246
cfg_options,
239-
Env::default(),
247+
env,
240248
);
241249
sysroot_crates.insert(krate, crate_id);
242250
}
@@ -275,12 +283,16 @@ impl ProjectWorkspace {
275283
opts.insert_features(pkg.features(&cargo).iter().map(Into::into));
276284
opts
277285
};
286+
let mut env = Env::default();
287+
if let Some((id, path)) = outdirs.get(pkg.name(&cargo)) {
288+
env.set_extern_path("OUT_DIR", &path, *id);
289+
}
278290
let crate_id = crate_graph.add_crate_root(
279291
file_id,
280292
edition,
281293
Some(pkg.name(&cargo).to_string()),
282294
cfg_options,
283-
Env::default(),
295+
env,
284296
);
285297
if tgt.kind(&cargo) == TargetKind::Lib {
286298
lib_tgt = Some(crate_id);

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ pub(crate) fn load_cargo(
5252
opts
5353
};
5454

55-
let crate_graph = ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| {
55+
// FIXME: outdirs?
56+
let outdirs = FxHashMap::default();
57+
58+
let crate_graph = ws.to_crate_graph(&default_cfg_options, &outdirs, &mut |path: &Path| {
5659
let vfs_file = vfs.load(path);
5760
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
5861
vfs_file.map(vfs_file_to_id)

crates/rust-analyzer/src/world.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use crate::{
2626
vfs_glob::{Glob, RustPackageFilterBuilder},
2727
LspError, Result,
2828
};
29+
use ra_db::ExternSourceId;
30+
use rustc_hash::{FxHashMap, FxHashSet};
2931

3032
#[derive(Debug, Clone)]
3133
pub struct Options {
@@ -98,6 +100,19 @@ impl WorldState {
98100
RootEntry::new(pkg_root.path().clone(), filter.into_vfs_filter())
99101
}));
100102
}
103+
104+
let extern_dirs: FxHashSet<_> =
105+
additional_out_dirs.iter().map(|(_, path)| (PathBuf::from(path))).collect();
106+
let mut extern_source_roots = FxHashMap::default();
107+
108+
roots.extend(additional_out_dirs.iter().map(|(_, path)| {
109+
let mut filter = RustPackageFilterBuilder::default().set_member(false);
110+
for glob in exclude_globs.iter() {
111+
filter = filter.exclude(glob.clone());
112+
}
113+
RootEntry::new(PathBuf::from(&path), filter.into_vfs_filter())
114+
}));
115+
101116
let (task_sender, task_receiver) = unbounded();
102117
let task_sender = Box::new(move |t| task_sender.send(t).unwrap());
103118
let (mut vfs, vfs_roots) = Vfs::new(roots, task_sender, watch);
@@ -107,6 +122,11 @@ impl WorldState {
107122
let is_local = folder_roots.iter().any(|it| vfs_root_path.starts_with(it));
108123
change.add_root(SourceRootId(r.0), is_local);
109124
change.set_debug_root_path(SourceRootId(r.0), vfs_root_path.display().to_string());
125+
126+
// FIXME: add path2root in vfs to simpily this logic
127+
if extern_dirs.contains(&vfs_root_path) {
128+
extern_source_roots.insert(vfs_root_path, ExternSourceId(r.0));
129+
}
110130
}
111131

112132
// FIXME: Read default cfgs from config
@@ -124,11 +144,20 @@ impl WorldState {
124144
vfs_file.map(|f| FileId(f.0))
125145
};
126146

127-
workspaces.iter().map(|ws| ws.to_crate_graph(&default_cfg_options, &mut load)).for_each(
128-
|graph| {
147+
let mut outdirs = FxHashMap::default();
148+
for (name, path) in additional_out_dirs {
149+
let path = PathBuf::from(&path);
150+
if let Some(id) = extern_source_roots.get(&path) {
151+
outdirs.insert(name, (id.clone(), path.to_string_lossy().replace("\\", "/")));
152+
}
153+
}
154+
155+
workspaces
156+
.iter()
157+
.map(|ws| ws.to_crate_graph(&default_cfg_options, &outdirs, &mut load))
158+
.for_each(|graph| {
129159
crate_graph.extend(graph);
130-
},
131-
);
160+
});
132161
change.set_crate_graph(crate_graph);
133162

134163
// FIXME: Figure out the multi-workspace situation

0 commit comments

Comments
 (0)