Skip to content

Commit 0a4bf3c

Browse files
authored
fix: normalize resolved result on Windows for root (#345)
similar to #322
1 parent 210c923 commit 0a4bf3c

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/cache.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl CachedPath {
249249
self.0.canonicalizing.store(tid, Ordering::Release);
250250

251251
let res = self.parent().map_or_else(
252-
|| Ok(self.clone()),
252+
|| Ok(self.normalize_root(cache)),
253253
|parent| {
254254
parent.canocalize_impl(cache).and_then(|parent_canonical| {
255255
let path = parent_canonical.normalize_with(
@@ -447,6 +447,24 @@ impl CachedPath {
447447
cache.value(path)
448448
})
449449
}
450+
451+
#[inline]
452+
#[cfg(windows)]
453+
pub fn normalize_root<Fs: FileSystem>(&self, cache: &Cache<Fs>) -> Self {
454+
if self.path().as_os_str().as_encoded_bytes().last() == Some(&b'/') {
455+
let mut path_string = self.path.to_string_lossy().into_owned();
456+
path_string.pop();
457+
path_string.push('\\');
458+
cache.value(&PathBuf::from(path_string))
459+
} else {
460+
self.clone()
461+
}
462+
}
463+
#[inline]
464+
#[cfg(not(windows))]
465+
pub fn normalize_root<Fs: FileSystem>(&self, _cache: &Cache<Fs>) -> Self {
466+
self.clone()
467+
}
450468
}
451469

452470
/// Since the cache key is memoized, use an identity hasher

src/tests/resolve.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ fn resolve_hash_as_module() {
121121
#[cfg(windows)]
122122
#[test]
123123
fn resolve_normalized_on_windows() {
124-
use std::path::PathBuf;
125-
126124
use normalize_path::NormalizePath;
127125

128126
let f = super::fixture();
@@ -132,9 +130,15 @@ fn resolve_normalized_on_windows() {
132130
let resolver = Resolver::new(ResolveOptions::default());
133131

134132
let resolution = resolver.resolve(&f, &normalized_absolute).map(|r| r.full_path());
135-
assert_eq!(resolution, Ok(PathBuf::from(absolute_str.as_ref())));
133+
assert_eq!(
134+
resolution.map(|r| r.to_string_lossy().into_owned()),
135+
Ok(absolute_str.clone().into_owned())
136+
);
136137

137138
let normalized_f = f.to_str().unwrap().replace('\\', "/");
138139
let resolution = resolver.resolve(normalized_f, ".\\foo\\index.js").map(|r| r.full_path());
139-
assert_eq!(resolution, Ok(PathBuf::from(absolute_str.as_ref())));
140+
assert_eq!(
141+
resolution.map(|r| r.to_string_lossy().into_owned()),
142+
Ok(absolute_str.clone().into_owned())
143+
);
140144
}

0 commit comments

Comments
 (0)