Skip to content
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
4 changes: 4 additions & 0 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ pub trait CachedPath: Sized {

fn parent(&self) -> Option<&Self>;

fn is_node_modules(&self) -> bool;

fn inside_node_modules(&self) -> bool;

/// Find package.json of a path by traversing parent directories.
#[allow(clippy::type_complexity)]
fn find_package_json<C: Cache<Cp = Self>>(
Expand Down
25 changes: 24 additions & 1 deletion src/fs_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ impl<Fs: FileSystem> Cache for FsCache<Fs> {
return entry.clone();
}
let parent = path.parent().map(|p| self.value(p));
let is_node_modules = path.file_name().as_ref().is_some_and(|&name| name == "node_modules");
let inside_node_modules =
is_node_modules || parent.as_ref().is_some_and(|parent| parent.inside_node_modules);
let cached_path = FsCachedPath(Arc::new(CachedPathImpl::new(
hash,
path.to_path_buf().into_boxed_path(),
is_node_modules,
inside_node_modules,
parent,
)));
paths.insert(cached_path.clone());
Expand Down Expand Up @@ -262,18 +267,28 @@ pub struct CachedPathImpl {
hash: u64,
path: Box<Path>,
parent: Option<FsCachedPath>,
is_node_modules: bool,
inside_node_modules: bool,
meta: OnceLock<Option<FileMetadata>>,
canonicalized: OnceLock<Result<FsCachedPath, ResolveError>>,
canonicalizing: AtomicU64,
package_json: OnceLock<Option<(FsCachedPath, Arc<PackageJsonSerde>)>>,
}

impl CachedPathImpl {
const fn new(hash: u64, path: Box<Path>, parent: Option<FsCachedPath>) -> Self {
fn new(
hash: u64,
path: Box<Path>,
is_node_modules: bool,
inside_node_modules: bool,
parent: Option<FsCachedPath>,
) -> Self {
Self {
hash,
path,
parent,
is_node_modules,
inside_node_modules,
meta: OnceLock::new(),
canonicalized: OnceLock::new(),
canonicalizing: AtomicU64::new(0),
Expand Down Expand Up @@ -303,6 +318,14 @@ impl CachedPath for FsCachedPath {
self.0.parent.as_ref()
}

fn is_node_modules(&self) -> bool {
self.is_node_modules
}

fn inside_node_modules(&self) -> bool {
self.inside_node_modules
}

/// Find package.json of a path by traversing parent directories.
///
/// # Errors
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,11 @@ impl<C: Cache> ResolverGeneric<C> {
// Algorithm:
// Find `node_modules/package/package.json`
// or the first package.json if the path is not inside node_modules.
// TODO(perf): cache whether a directory is inside `node_modules`?
let inside_node_modules = iter::successors(Some(cached_path), |cp| cp.parent())
.any(|cp| cp.path().ends_with("node_modules"));
let inside_node_modules = cached_path.inside_node_modules();
if inside_node_modules {
let mut last = None;
for cp in iter::successors(Some(cached_path), |cp| cp.parent()) {
if cp.path().ends_with("node_modules") {
if cp.is_node_modules() {
break;
}
if self.cache.is_dir(cp, ctx) {
Expand Down
Loading