Skip to content

Commit

Permalink
fix: normalize resolved result on Windows for root (#345)
Browse files Browse the repository at this point in the history
similar to #322
  • Loading branch information
sapphi-red authored Dec 13, 2024
1 parent 210c923 commit 0a4bf3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl CachedPath {
self.0.canonicalizing.store(tid, Ordering::Release);

let res = self.parent().map_or_else(
|| Ok(self.clone()),
|| Ok(self.normalize_root(cache)),
|parent| {
parent.canocalize_impl(cache).and_then(|parent_canonical| {
let path = parent_canonical.normalize_with(
Expand Down Expand Up @@ -447,6 +447,24 @@ impl CachedPath {
cache.value(path)
})
}

#[inline]
#[cfg(windows)]
pub fn normalize_root<Fs: FileSystem>(&self, cache: &Cache<Fs>) -> Self {
if self.path().as_os_str().as_encoded_bytes().last() == Some(&b'/') {
let mut path_string = self.path.to_string_lossy().into_owned();
path_string.pop();
path_string.push('\\');
cache.value(&PathBuf::from(path_string))
} else {
self.clone()
}
}
#[inline]
#[cfg(not(windows))]
pub fn normalize_root<Fs: FileSystem>(&self, _cache: &Cache<Fs>) -> Self {
self.clone()
}
}

/// Since the cache key is memoized, use an identity hasher
Expand Down
12 changes: 8 additions & 4 deletions src/tests/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ fn resolve_hash_as_module() {
#[cfg(windows)]
#[test]
fn resolve_normalized_on_windows() {
use std::path::PathBuf;

use normalize_path::NormalizePath;

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

let resolution = resolver.resolve(&f, &normalized_absolute).map(|r| r.full_path());
assert_eq!(resolution, Ok(PathBuf::from(absolute_str.as_ref())));
assert_eq!(
resolution.map(|r| r.to_string_lossy().into_owned()),
Ok(absolute_str.clone().into_owned())
);

let normalized_f = f.to_str().unwrap().replace('\\', "/");
let resolution = resolver.resolve(normalized_f, ".\\foo\\index.js").map(|r| r.full_path());
assert_eq!(resolution, Ok(PathBuf::from(absolute_str.as_ref())));
assert_eq!(
resolution.map(|r| r.to_string_lossy().into_owned()),
Ok(absolute_str.clone().into_owned())
);
}

0 comments on commit 0a4bf3c

Please sign in to comment.