Skip to content

fix(ext/node): fix fs.glob skipping siblings with **/../* patterns - #33372

Merged
bartlomieju merged 4 commits into
mainfrom
fix/fs-glob-parent-traversal
Apr 23, 2026
Merged

bartlomieju merged 4 commits into
mainfrom
fix/fs-glob-parent-traversal

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

Summary

  • Fix flaky test-fs-glob.mjs node_compat test failure caused by
    fs.glob/fs.globSync returning incomplete results for patterns
    containing **/../*

Root Cause

The cache.seen check inside the children iteration loop in both
#addSubpatterns (sync) and #iterateSubpatterns (async) used return,
which aborted the entire method instead of just skipping the current
child.

When a glob pattern contains **/../*, the .. handler queues both the
current directory and its parent with the remaining * pattern. Due to
recursive directory traversal, the same path can get queued multiple
times. With the LIFO queue (ArrayPrototypePop), a duplicate entry can
be processed first, adding it to the cache. When the parent directory
later iterates its children and encounters this cached entry, the
return cuts off all remaining siblings -- which entries are skipped
depends on the non-deterministic readdir ordering, causing flaky
failures.

Fix

Change return to continue childrenLoop (labeled continue on the
outer children loop) so only the already-processed child is skipped
while remaining siblings are still enumerated.

Note: upstream Node.js has the same return -- this is a latent bug
there too, but it manifests more frequently on Linux CI due to different
readdir ordering on ext4/tmpfs.

Test plan

  • Added unit test in tests/unit_node/fs_test.ts that creates a
    directory structure triggering the duplicate-queue scenario
    (a/c/d/c/ causes the .. handler to re-queue a/c) and asserts all
    siblings appear in a/**/../* results
  • tools/format.js and tools/lint.js --js pass

The cache-seen check inside the children iteration loop used `return`,
which aborted the entire `#addSubpatterns`/`#iterateSubpatterns` method.
When a glob pattern contains `**/../*`, the ".." handler can queue the
same path multiple times via the LIFO queue. If a duplicate entry is
processed first (adding it to the cache), and then the parent directory
iterates its children, a cached sibling would trigger the `return` and
cut off all subsequent siblings -- which entries were skipped depended
on the non-deterministic readdir ordering, causing flaky test failures.

The fix changes `return` to `continue childrenLoop` so only the
already-processed child is skipped while remaining siblings are still
enumerated.

Fixes flaky test-fs-glob.mjs node_compat failure.
The `continue childrenLoop` fix was insufficient: it correctly didn't
abort siblings, but still skipped adding the cached child itself to
results. When a path like `a/b` is processed via its own queue entry
(recursing into its children), `a/b` itself is never added as a result.
When the parent `a` later processes children with `*`, the cache check
would skip `a/b` entirely, preventing it from appearing in results.

The cache.seen check in the children loop is a redundant optimization.
The `cache.add` call at the top of `#addSubpatterns`/`#iterateSubpatterns`
already prevents reprocessing and infinite recursion. Removing the
children-level check allows entries to be correctly added to results
even when they were previously traversed from a different context.
@bartlomieju
bartlomieju merged commit a5b388d into main Apr 23, 2026
220 of 222 checks passed
@bartlomieju
bartlomieju deleted the fix/fs-glob-parent-traversal branch April 23, 2026 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant