-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Description
When trying to return DefMap the code panics without proper explanation regarding the error. Actually, this behavior is exhibited when the FileId was not loaded into the VFS. The code snippet happens to suffice the need of replication, because with it only crate root files are being loaded and at time of modules resolution it panics due to a certain FileId not being present.
How to replicate this behavior:
use camino::Utf8Path;
use la_arena::Idx;
use ra_ap_base_db::{
BuiltCrateData, Crate, CrateBuilder, CrateWorkspaceData, SourceDatabase
};
use ra_ap_ide_db::RootDatabase;
use ra_ap_project_model::{CargoConfig, ProjectManifest, ProjectWorkspace};
use ra_ap_vfs::{AbsPath, Vfs, VfsPath};
use rustc_hash::FxHashMap;
use std::{fs, path::Path};
use ra_ap_hir_def::nameres::crate_def_map;
trait Upcast<T: ?Sized> {
fn upcast(&self) -> &T;
}
impl Upcast<dyn SourceDatabase> for RootDatabase {
fn upcast(&self) -> &dyn SourceDatabase {
self
}
}
fn main() {
analyzer();
}
fn analyzer() {
let mut db = ra_ap_ide_db::RootDatabase::default();
let mut def = Vfs::default();
let mut f_id = |x: &AbsPath| {
let path = VfsPath::new_real_path(x.to_string());
def.set_file_contents(path.clone(), fs::read(path.to_string()).ok());
if let Some(map) = def.file_id(&path) {
Some(map.0)
} else {
None
}
};
let as_absolute = Path::new("Cargo.toml");
let utf8path = AbsPath::assert(Utf8Path::new(as_absolute.to_str().unwrap()));
let manifest = ProjectManifest::discover_single(utf8path).unwrap();
let cargo_config = CargoConfig::default();
let progress = |msg: String| println!("{msg}");
let ws = ProjectWorkspace::load(manifest, &cargo_config, &progress).unwrap();
let mut fx = FxHashMap::default();
let graph = ws.to_crate_graph(&mut f_id, &mut fx).0;
let idxs = graph.iter().map(|f| f).collect::<Vec<Idx<CrateBuilder>>>();
for each in idxs.clone() {
let each = graph[each].clone();
let f = def.file_path(each.basic.root_file_id);
if each.basic.origin.is_local() {
let text = fs::read_to_string(f.to_string()).expect("Couldn't read file to string");
db.set_file_text_with_durability(
each.basic.root_file_id,
&text,
salsa::Durability::LOW,
)
}
}
let ws_data = triomphe::Arc::new(CrateWorkspaceData {
data_layout: ws.target_layout.clone(),
toolchain: ws.toolchain.clone(),
});
let cratedb: &dyn SourceDatabase = db.upcast();
let crates = idxs
.clone()
.into_iter()
.filter_map(|each| {
//cratedb.set_source_root_with_durability(source_root_id, source_root, durability);
let each = graph[each].clone();
if each.basic.origin.is_local() {
let data = BuiltCrateData {
root_file_id: each.basic.root_file_id,
edition: each.basic.edition,
dependencies: vec![],
origin: each.basic.origin,
is_proc_macro: each.basic.is_proc_macro,
proc_macro_cwd: each.basic.proc_macro_cwd,
};
let return_crate = Crate::builder(
data,
each.extra,
ws_data.clone(),
each.cfg_options,
each.env,
)
.new(cratedb);
Some(return_crate)
} else {
None
}
})
.collect::<Vec<Crate>>();
let cr = crates[1];
let _crate_map = crate_def_map(&db, cr);
}
Metadata
Metadata
Assignees
Labels
No labels