Skip to content

Commit 889ced8

Browse files
committed
Rust: normalize windows paths
1 parent 7401ae1 commit 889ced8

File tree

2 files changed

+67
-60
lines changed

2 files changed

+67
-60
lines changed

rust/extractor/src/main.rs

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -322,73 +322,80 @@ impl<'a> Extractor<'a> {
322322
// According to the documentation of `CrateGraph`:
323323
// Each crate is defined by the `FileId` of its root module, the set of enabled
324324
// `cfg` flags and the set of dependencies.
325-
let mut crate_id_map = HashMap::<CrateId, (&VfsPath, u64)>::new();
325+
let mut crate_id_map = HashMap::<CrateId, (PathBuf, u64)>::new();
326326
for krate_id in crate_graph.crates_in_topological_order() {
327327
let krate = &crate_graph[krate_id];
328-
let root_module_file = vfs.file_path(krate.root_file_id);
329-
let mut hasher = std::hash::DefaultHasher::new();
330-
krate
331-
.cfg_options
332-
.as_ref()
333-
.into_iter()
334-
.sorted_by(cmp_flag)
335-
.for_each(|x| format!("{x}").hash(&mut hasher));
336-
337-
krate
338-
.dependencies
339-
.iter()
340-
.flat_map(|d| crate_id_map.get(&d.crate_id))
341-
.sorted()
342-
.for_each(|x| x.hash(&mut hasher));
343-
let hash = hasher.finish();
344-
crate_id_map.insert(krate_id, (root_module_file, hash));
345-
}
346-
for krate_id in crate_graph.iter() {
347-
let (root_module_file, hash) = crate_id_map.get(&krate_id).unwrap();
348-
let path: &Path = root_module_file.as_path().unwrap().as_ref();
349-
let path = path.join(format!("{hash:0>16x}"));
350-
let mut trap = self.traps.create("crates", path.as_path());
351-
if trap.path.exists() {
352-
continue;
353-
}
354-
let krate = &crate_graph[krate_id];
355-
let element = generated::Crate {
356-
id: trap::TrapId::Key(format!("crate:{root_module_file}:{hash}")),
357-
name: krate
358-
.display_name
359-
.as_ref()
360-
.map(|x| x.canonical_name().to_string()),
361-
version: krate.version.to_owned(),
362-
cfg_options: krate
328+
let root_module_file: &VfsPath = vfs.file_path(krate.root_file_id);
329+
if let Some(root_module_file) = root_module_file
330+
.as_path()
331+
.map(|p| std::fs::canonicalize(p).unwrap_or(p.into()))
332+
{
333+
let mut hasher = std::hash::DefaultHasher::new();
334+
krate
363335
.cfg_options
364336
.as_ref()
365337
.into_iter()
366-
.map(|x| format!("{x}"))
367-
.collect(),
368-
dependencies: krate
338+
.sorted_by(cmp_flag)
339+
.for_each(|x| format!("{x}").hash(&mut hasher));
340+
341+
krate
369342
.dependencies
370343
.iter()
371-
.flat_map(|x| crate_id_map.get(&x.crate_id))
372-
.map(|(module, hash)| trap.label(format!("crate:{module}:{hash}").into()))
373-
.collect(),
374-
};
375-
let parent = trap.emit(element);
344+
.flat_map(|d| crate_id_map.get(&d.crate_id))
345+
.sorted()
346+
.for_each(|x| x.hash(&mut hasher));
347+
let hash = hasher.finish();
348+
crate_id_map.insert(krate_id, (root_module_file, hash));
349+
}
350+
}
351+
for krate_id in crate_graph.iter() {
352+
if let Some((root_module_file, hash)) = crate_id_map.get(&krate_id) {
353+
let path = root_module_file.join(format!("{hash:0>16x}"));
354+
let mut trap = self.traps.create("crates", path.as_path());
355+
if trap.path.exists() {
356+
continue;
357+
}
358+
let krate = &crate_graph[krate_id];
359+
let element = generated::Crate {
360+
id: trap::TrapId::Key(format!("crate:{}:{hash}", root_module_file.display())),
361+
name: krate
362+
.display_name
363+
.as_ref()
364+
.map(|x| x.canonical_name().to_string()),
365+
version: krate.version.to_owned(),
366+
cfg_options: krate
367+
.cfg_options
368+
.as_ref()
369+
.into_iter()
370+
.map(|x| format!("{x}"))
371+
.collect(),
372+
dependencies: krate
373+
.dependencies
374+
.iter()
375+
.flat_map(|x| crate_id_map.get(&x.crate_id))
376+
.map(|(module, hash)| {
377+
trap.label(format!("crate:{}:{hash}", module.display()).into())
378+
})
379+
.collect(),
380+
};
381+
let parent = trap.emit(element);
376382

377-
go(
378-
db,
379-
db.crate_def_map(krate_id).as_ref(),
380-
parent.into(),
381-
"crate",
382-
DefMap::ROOT,
383-
&mut trap,
384-
);
385-
trap.commit().unwrap_or_else(|err| {
386-
log::error!(
387-
"Failed to write trap file for crate: {}: {}",
388-
root_module_file,
389-
err.to_string()
390-
)
391-
});
383+
go(
384+
db,
385+
db.crate_def_map(krate_id).as_ref(),
386+
parent.into(),
387+
"crate",
388+
DefMap::ROOT,
389+
&mut trap,
390+
);
391+
trap.commit().unwrap_or_else(|err| {
392+
log::error!(
393+
"Failed to write trap file for crate: {}: {}",
394+
root_module_file.display(),
395+
err.to_string()
396+
)
397+
});
398+
}
392399
fn go(
393400
db: &dyn HirDatabase,
394401
map: &DefMap,

rust/extractor/src/rust_analyzer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ fn from_utf8_lossy(v: &[u8]) -> (Cow<'_, str>, Option<SyntaxError>) {
189189
(Cow::Owned(res), Some(error))
190190
}
191191

192-
fn canonicalize_if_on_windows(path: &Path) -> Option<PathBuf> {
192+
pub(crate) fn canonicalize_if_on_windows(path: &Path) -> Option<PathBuf> {
193193
if cfg!(windows) {
194194
dunce::canonicalize(path).ok()
195195
} else {

0 commit comments

Comments
 (0)