Skip to content

Commit

Permalink
windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 11, 2024
1 parent aa12e6b commit 147c728
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
},
};

use cfg_if::cfg_if;
use dashmap::{DashMap, DashSet};
use once_cell::sync::OnceCell as OnceLock;
use rustc_hash::FxHasher;
Expand Down Expand Up @@ -240,6 +241,7 @@ impl CachedPath {
self.path().strip_prefix(parent.path()).unwrap(),
cache,
);

if cache.fs.symlink_metadata(self.path()).is_ok_and(|m| m.is_symlink) {
let link = cache.fs.read_link(path.path())?;
if link.is_absolute() {
Expand All @@ -250,6 +252,7 @@ impl CachedPath {
return dir.normalize_with(&link, cache).canonicalize(cache);
}

Check warning on line 253 in src/cache.rs

View check run for this annotation

Codecov / codecov/patch

src/cache.rs#L253

Added line #L253 was not covered by tests
}

Ok(path)
})
},
Expand Down Expand Up @@ -412,7 +415,14 @@ impl CachedPath {
path.pop();
}
Component::Normal(c) => {
path.push(c);
cfg_if! {
if #[cfg(target_family = "wasm")] {
// Need to trim the extra \0 introduces by https://github.com/nodejs/uvwasi/issues/262
path.push(c.to_string_lossy().trim_end_matches('\0'));
} else {
path.push(c);
}
}
}
Component::Prefix(..) | Component::RootDir => {
unreachable!("Path {:?} Subpath {:?}", self.path, subpath)
Expand Down
22 changes: 17 additions & 5 deletions src/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ use cfg_if::cfg_if;
#[cfg(feature = "yarn_pnp")]
use pnp::fs::{LruZipCache, VPath, VPathInfo, ZipCache};

#[cfg(windows)]
const UNC_PATH_PREFIX: &[u8] = b"\\\\?\\UNC\\";
#[cfg(windows)]
const LONG_PATH_PREFIX: &[u8] = b"\\\\?\\";

/// File System abstraction used for `ResolverGeneric`
pub trait FileSystem: Send + Sync {
/// See [std::fs::read_to_string]
Expand Down Expand Up @@ -168,13 +163,30 @@ impl FileSystem for FileSystemOs {
VPath::Virtual(info) => fs::read_link(info.physical_base_path()),
VPath::Native(path) => fs::read_link(path),
}
} else if #[cfg(windows)] {
Ok(strip_windows_prefix(fs::read_link(path)?))
} else {
fs::read_link(path)
}
}
}
}

#[cfg(windows)]
fn strip_windows_prefix<P: AsRef<Path>>(path: P) -> PathBuf {
let path_bytes = path.as_ref().as_os_str().as_encoded_bytes();
path_bytes
.strip_prefix(UNC_PATH_PREFIX)
.or_else(|| path_bytes.strip_prefix(LONG_PATH_PREFIX))
.map_or_else(
|| path.as_ref().to_path_buf(),
|p| {
// SAFETY: `as_encoded_bytes` ensures `p` is valid path bytes
unsafe { PathBuf::from(std::ffi::OsStr::from_encoded_bytes_unchecked(p)) }
},
)
}

#[test]
fn metadata() {
let meta = FileMetadata { is_file: true, is_dir: true, is_symlink: true };
Expand Down

0 comments on commit 147c728

Please sign in to comment.