Change existing OUT_DIR override config to make use of new infrastructure

This commit is contained in:
Emil Lauridsen 2020-03-16 14:10:13 +01:00
parent 33c6c7abc6
commit f5a2fcf8f5
8 changed files with 26 additions and 57 deletions

View file

@ -39,6 +39,9 @@ pub struct CargoFeatures {
/// Runs cargo check on launch to figure out the correct values of OUT_DIR
pub load_out_dirs_from_check: bool,
/// Fine grained controls for additional `OUT_DIR` env variables
pub out_dir_overrides: FxHashMap<PackageId, PathBuf>,
}
impl Default for CargoFeatures {
@ -48,6 +51,7 @@ impl Default for CargoFeatures {
all_features: true,
features: Vec::new(),
load_out_dirs_from_check: false,
out_dir_overrides: FxHashMap::default(),
}
}
}
@ -191,6 +195,10 @@ impl CargoWorkspace {
if cargo_features.load_out_dirs_from_check {
out_dir_by_id = load_out_dirs(cargo_toml, cargo_features);
}
// We explicitly extend afterwards to allow overriding the value returned by cargo
out_dir_by_id.extend(
cargo_features.out_dir_overrides.iter().map(|(id, path)| (id.clone(), path.clone())),
);
let mut pkg_by_id = FxHashMap::default();
let mut packages = Arena::default();

View file

@ -177,7 +177,6 @@ impl ProjectWorkspace {
pub fn to_crate_graph(
&self,
default_cfg_options: &CfgOptions,
additional_out_dirs: &FxHashMap<String, PathBuf>,
extern_source_roots: &FxHashMap<PathBuf, ExternSourceId>,
load: &mut dyn FnMut(&Path) -> Option<FileId>,
) -> CrateGraph {
@ -251,15 +250,8 @@ impl ProjectWorkspace {
opts
};
let mut env = Env::default();
let mut extern_source = ExternSource::default();
if let Some(path) = additional_out_dirs.get(krate.name(&sysroot)) {
env.set("OUT_DIR", path.to_string_lossy().to_string());
if let Some(extern_source_id) = extern_source_roots.get(path) {
extern_source.set_extern_path(&path, *extern_source_id);
}
}
let env = Env::default();
let extern_source = ExternSource::default();
let crate_id = crate_graph.add_crate_root(
file_id,
Edition::Edition2018,
@ -310,19 +302,11 @@ impl ProjectWorkspace {
};
let mut env = Env::default();
let mut extern_source = ExternSource::default();
if let Some(out_dir) = dbg!(pkg.out_dir(cargo)) {
if let Some(out_dir) = pkg.out_dir(cargo) {
// FIXME: We probably mangle non UTF-8 paths here, figure out a better solution
env.set("OUT_DIR", out_dir.to_string_lossy().to_string());
if let Some(extern_source_id) =
dbg!(dbg!(&extern_source_roots).get(out_dir))
{
extern_source.set_extern_path(&out_dir, *extern_source_id);
}
} else {
if let Some(path) = additional_out_dirs.get(pkg.name(&cargo)) {
env.set("OUT_DIR", path.to_string_lossy().to_string());
if let Some(extern_source_id) = extern_source_roots.get(path) {
extern_source.set_extern_path(&path, *extern_source_id);
}
if let Some(&extern_source_id) = extern_source_roots.get(out_dir) {
extern_source.set_extern_path(&out_dir, extern_source_id);
}
}
let crate_id = crate_graph.add_crate_root(

View file

@ -53,19 +53,14 @@ pub(crate) fn load_cargo(
};
// FIXME: outdirs?
let outdirs = FxHashMap::default();
let extern_source_roots = FxHashMap::default();
let crate_graph = ws.to_crate_graph(
&default_cfg_options,
&outdirs,
&extern_source_roots,
&mut |path: &Path| {
let crate_graph =
ws.to_crate_graph(&default_cfg_options, &extern_source_roots, &mut |path: &Path| {
let vfs_file = vfs.load(path);
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
vfs_file.map(vfs_file_to_id)
},
);
});
log::debug!("crate graph: {:?}", crate_graph);
let source_roots = roots

View file

@ -48,9 +48,6 @@ pub struct ServerConfig {
/// Fine grained feature flags to disable specific features.
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>,
/// Cargo feature configurations.
@ -76,7 +73,6 @@ impl Default for ServerConfig {
cargo_watch_all_targets: true,
with_sysroot: true,
feature_flags: FxHashMap::default(),
additional_out_dirs: FxHashMap::default(),
cargo_features: Default::default(),
rustfmt_args: Vec::new(),
vscode_lldb: false,

View file

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

View file

@ -82,7 +82,6 @@ impl WorldState {
watch: Watch,
options: Options,
feature_flags: FeatureFlags,
additional_out_dirs: FxHashMap<String, String>,
) -> WorldState {
let mut change = AnalysisChange::new();
@ -105,8 +104,7 @@ impl WorldState {
}));
}
let mut extern_dirs: FxHashSet<_> =
additional_out_dirs.iter().map(|(_, path)| (PathBuf::from(path))).collect();
let mut extern_dirs = FxHashSet::default();
for ws in workspaces.iter() {
extern_dirs.extend(ws.out_dirs());
}
@ -152,21 +150,9 @@ impl WorldState {
vfs_file.map(|f| FileId(f.0))
};
let additional_out_dirs: FxHashMap<String, PathBuf> = additional_out_dirs
.into_iter()
.map(|(name, path)| (name, PathBuf::from(&path)))
.collect();
workspaces
.iter()
.map(|ws| {
ws.to_crate_graph(
&default_cfg_options,
&additional_out_dirs,
&extern_source_roots,
&mut load,
)
})
.map(|ws| ws.to_crate_graph(&default_cfg_options, &extern_source_roots, &mut load))
.for_each(|graph| {
crate_graph.extend(graph);
});

View file

@ -237,11 +237,6 @@
"default": true,
"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": {
"type": [
"null",
@ -367,6 +362,11 @@
"type": "boolean",
"default": false,
"markdownDescription": "Run `cargo check` on startup to get the correct value for package OUT_DIRs"
},
"rust-analyzer.cargoFeatures.outDirOverrides": {
"type": "object",
"default": {},
"markdownDescription": "Fine grained controls for OUT_DIR `env!(\"OUT_DIR\")` variable. e.g. `{\"foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)\":\"/path/to/foo\"}`, "
}
}
},

View file

@ -23,6 +23,7 @@ export interface CargoFeatures {
allFeatures: boolean;
features: string[];
loadOutDirsFromCheck: boolean;
outDirOverrides: Record<string, string>;
}
export const enum UpdatesChannel {
@ -203,7 +204,6 @@ export class Config {
get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; }
get useClientWatching() { return this.cfg.get("useClientWatching") as 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 loadOutDirsFromCheck() { return this.cfg.get("loadOutDirsFromCheck") as boolean; }
@ -222,6 +222,7 @@ export class Config {
allFeatures: this.cfg.get("cargoFeatures.allFeatures") as boolean,
features: this.cfg.get("cargoFeatures.features") as string[],
loadOutDirsFromCheck: this.cfg.get("cargoFeatures.loadOutDirsFromCheck") as boolean,
outDirOverrides: this.cfg.get("cargoFeatures.outDirOverrides") as Record<string, string>,
};
}