Skip to content
New issue

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

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

Already on GitHub? # to your account

perf: bring back the symlink optimization #298

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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"))
);
}