Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use same UNC path normalization logic with libuv #306

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["node", "resolve", "cjs", "esm", "enhanced-resolve"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/oxc-project/oxc-resolver"
rust-version = "1.70"
rust-version = "1.74"
include = ["/src", "/examples", "/benches"]

[lib]
Expand Down Expand Up @@ -70,7 +70,6 @@ serde_json = { version = "1", features = [
"preserve_order",
] } # preserve_order: package_json.exports requires order such as `["require", "import", "default"]`
rustc-hash = { version = "2" }
dunce = "1" # Normalize Windows paths to the most compatible format, avoiding UNC where possible
once_cell = "1" # Use `std::sync::OnceLock::get_or_try_init` when it is stable.
thiserror = "1"
json-strip-comments = "1"
Expand Down
3 changes: 3 additions & 0 deletions fixtures/pnpm/longfilename/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const test = 'hello world'

export default test
7 changes: 7 additions & 0 deletions fixtures/pnpm/longfilename/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@oxc-resolver/test-longfilename-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"private": true,
"version": "0.0.0",
"main": "index.js",
"type": "module"
}
1 change: 1 addition & 0 deletions fixtures/pnpm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"private": true,
"devDependencies": {
"@oxc-resolver/test-longfilename-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": "file:./longfilename",
"axios": "1.6.2",
"ipaddr.js": "2.2.0",
"postcss": "8.4.33",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 88 additions & 47 deletions src/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ 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 @@ -164,55 +169,12 @@ impl FileSystem for FileSystemOs {
cfg_if! {
if #[cfg(feature = "yarn_pnp")] {
match VPath::from(path)? {
VPath::Zip(info) => {
dunce::canonicalize(info.physical_base_path().join(info.zip_path))
}
VPath::Virtual(info) => dunce::canonicalize(info.physical_base_path()),
VPath::Native(path) => dunce::canonicalize(path),
VPath::Zip(info) => fast_canonicalize(info.physical_base_path().join(info.zip_path)),
VPath::Virtual(info) => fast_canonicalize(info.physical_base_path()),
VPath::Native(path) => fast_canonicalize(path),
}
} else if #[cfg(windows)] {
dunce::canonicalize(path)
} else {
use std::path::Component;
let mut path_buf = path.to_path_buf();
loop {
let link = fs::read_link(&path_buf)?;
path_buf.pop();
if fs::symlink_metadata(&path_buf)?.is_symlink()
{
path_buf = self.canonicalize(path_buf.as_path())?;
}
for component in link.components() {
match component {
Component::ParentDir => {
path_buf.pop();
}
Component::Normal(seg) => {
#[cfg(target_family = "wasm")]
{
// Need to trim the extra \0 introduces by https://github.com/nodejs/uvwasi/issues/262
path_buf.push(seg.to_string_lossy().trim_end_matches('\0'));
}
#[cfg(not(target_family = "wasm"))]
{
path_buf.push(seg);
}
}
Component::RootDir => {
path_buf = PathBuf::from("/");
}
Component::CurDir | Component::Prefix(_) => {}
}
if fs::symlink_metadata(&path_buf)?.is_symlink()
{
path_buf = self.canonicalize(path_buf.as_path())?;
}
}
if !fs::symlink_metadata(&path_buf)?.is_symlink() {
break;
}
}
Ok(path_buf)
fast_canonicalize(path.to_path_buf())
}
}
}
Expand All @@ -227,3 +189,82 @@ fn metadata() {
);
let _ = meta;
}

#[inline]
// This is A faster fs::canonicalize implementation by reducing the number of syscalls
fn fast_canonicalize(path: PathBuf) -> io::Result<PathBuf> {
use std::path::Component;
let mut path_buf = path;

loop {
let link = {
#[cfg(windows)]
{
node_compatible_raw_canonicalize(fs::read_link(&path_buf)?)
}
#[cfg(not(windows))]
{
fs::read_link(&path_buf)?
}
};
path_buf.pop();
if fs::symlink_metadata(&path_buf)?.is_symlink() {
path_buf = fast_canonicalize(path_buf)?;
}
for component in link.components() {
match component {
Component::ParentDir => {
path_buf.pop();
}
Component::Normal(seg) => {
#[cfg(target_family = "wasm")]
{
// Need to trim the extra \0 introduces by https://github.com/nodejs/uvwasi/issues/262
path_buf.push(seg.to_string_lossy().trim_end_matches('\0'));
}
#[cfg(not(target_family = "wasm"))]
{
path_buf.push(seg);
}
}
Component::RootDir => {
#[cfg(not(windows))]
{
path_buf = PathBuf::from("/");
}
#[cfg(windows)]
{
path_buf.push("");
Brooooooklyn marked this conversation as resolved.
Show resolved Hide resolved
}
}
Component::Prefix(prefix) => {
path_buf = PathBuf::from(prefix.as_os_str());
}
Component::CurDir => {}
}

if fs::symlink_metadata(&path_buf)?.is_symlink() {
path_buf = fast_canonicalize(path_buf)?;
}
}
if !fs::symlink_metadata(&path_buf)?.is_symlink() {
break;
}
}
Ok(path_buf)
}

#[cfg(windows)]
fn node_compatible_raw_canonicalize<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)) }
},
)
}
10 changes: 10 additions & 0 deletions tests/resolve_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,13 @@ fn nested_symlinks() {
Ok(dir.join("nm/index.js"))
);
}

#[test]
fn windows_symlinked_longfilename() {
let dir = dir();
let path = dir.join("fixtures/pnpm");
let module_path = dir.join("node_modules/.pnpm/@oxc-resolver+test-longfilename-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_m464apeldykmdsyzlfhtrggk24/node_modules/@oxc-resolver/test-longfilename-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/index.js");

let resolution = Resolver::new(ResolveOptions::default()).resolve(&path, "@oxc-resolver/test-longfilename-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").map(|r| r.full_path());
assert_eq!(resolution, Ok(module_path));
}
Loading