Skip to content

Commit

Permalink
perf: bring back the symlink optimization (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Nov 8, 2024
1 parent edc503c commit 96825a3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions fixtures/nested-symlink/apps/tooling
Empty file.
Empty file.
1 change: 1 addition & 0 deletions fixtures/nested-symlink/tooling/typescript-config/index.js
23 changes: 18 additions & 5 deletions src/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,34 +172,47 @@ impl FileSystem for FileSystemOs {
}
} else if #[cfg(windows)] {
dunce::canonicalize(path)
} else if #[cfg(target_family = "wasm")] {
} 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) => {
// 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(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)
} else {
fs::canonicalize(path)
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/resolve_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,24 @@ fn decimal_js_from_mathjs() {
assert_eq!(resolution, Ok(module_path.clone()));
}
}

#[test]
// regression: https://github.com/NicholasLYang/oxc-repro
fn nested_symlinks() {
let dir = dir();
let dir = dir.join("fixtures/nested-symlink");
assert_eq!(
Resolver::new(ResolveOptions::default())
// ./apps/web/nm/@repo/typescript-config is a symlink
.resolve(&dir, "./apps/web/nm/@repo/typescript-config/index.js")
.map(oxc_resolver::Resolution::into_path_buf),
Ok(dir.join("nm/index.js"))
);
assert_eq!(
Resolver::new(ResolveOptions::default())
// ./apps/tooling is a symlink
.resolve(&dir, "./apps/tooling/typescript-config/index.js")
.map(oxc_resolver::Resolution::into_path_buf),
Ok(dir.join("nm/index.js"))
);
}

0 comments on commit 96825a3

Please sign in to comment.