Skip to content

fs: resolve glob exclude Dirent paths against cwd option - #67268

Open
yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:fs-glob-exclude-dirent-cwd
Open

yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:fs-glob-exclude-dirent-cwd

Conversation

@yohannesjoya

Copy link
Copy Markdown
Contributor

When withFileTypes is true, an exclude function receives a Dirent. For a path that a pattern reaches directly, such as a in a/**, the walker passes a path relative to options.cwd. The shim then called lstatSync() on it, and that resolved against process.cwd().

So the callback saw the wrong entry when the same name exists in the process working directory. In the test below, a is a directory in cwd and a file in the process working directory, and exclude: (d) => d.isDirectory() failed to exclude it. If no such name exists, lstatSync() throws, and the callback is skipped for that path.

This change resolves the path against options.cwd before calling lstatSync(), the same way #toDirent() does. Absolute paths are unchanged, and so is the default cwd.

fallintoplace reported this in #63535 and sent #63538. Both were closed as stale without a review. #63538 targeted the JavaScript glob implementation that was later replaced by the native one, so this is a new patch against the current shim. The test scenario comes from their report.

Refs: #63535
Refs: #63538

The new test fails on main and passes with this change. ESLint is clean on the changed files, and tools/test.py -j1 passes for parallel/test-fs-glob* and parallel/test-path-glob.

AI disclosure: I used a coding agent to help find the bug and draft the test. I reproduced the behavior on main, reviewed the change line by line, and ran the tests above locally.

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Nov 5, 2026
@yohannesjoya

Copy link
Copy Markdown
Contributor Author

@lpinca could you take a look at this one? It's a small fix for fs.glob exclude callbacks with options.cwd, with a regression test and the original reporter credited. It needs a collaborator review and CI.

@yohannesjoya

Copy link
Copy Markdown
Contributor Author

Friendly ping: this has an approval from @jasnell but no CI run yet. Could a collaborator add request-ci? Thanks.

@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 26, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 26, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@yohannesjoya

Copy link
Copy Markdown
Contributor Author

CI run 79529 failed only on node-test-linux-alpine-last-latest-x64; the other platforms passed. I can't access the Jenkins logs to see which test failed. Could a collaborator check whether it's related to this change, and resume CI if it's a flake? Thanks.

@jasnell

jasnell commented Nov 27, 2026

Copy link
Copy Markdown
Member

the test failure is relevant:

test-fs-glob-exclude-dirent-cwd:

---
duration_ms: 191.264
exitcode: 1
severity: fail
stack: |2-

  node:internal/event_target:1138
    process.nextTick(() => { throw err; });
                             ^
  TypeError [ERR_WORKER_UNSUPPORTED_OPERATION]: process.chdir() is not supported in workers
      at process.unavailableInWorker [as chdir] (node:internal/process/worker_thread_only:14:11)
      at file:///home/iojs/build/workspace/node/test/parallel/test-fs-glob-exclude-dirent-cwd.mjs:20:9
      at ModuleJobSync.run (node:internal/modules/esm/module_job:556:46)
      at node:internal/modules/run_main:191:19
      at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:98:5) {
    code: 'ERR_WORKER_UNSUPPORTED_OPERATION'
  }

  Node.js v28.0.0-pre
...

When withFileTypes is true, an exclude function receives a Dirent. For
a path that a glob pattern reaches directly, that Dirent was built by
calling lstat on a path relative to the cwd option, but lstat resolved
it against the process working directory. The function could then see
the type and parent path of an unrelated file, or see nothing at all
if no such file existed.

Resolve the path against the cwd option before calling lstat.

Refs: nodejs#63535
Co-authored-by: Minh Vu <vuhoangminh97@gmail.com>
Signed-off-by: yohannesjoya <yohannesdestagebru10@gmail.com>
@yohannesjoya
yohannesjoya force-pushed the fs-glob-exclude-dirent-cwd branch from d33378d to 80c60b2 Compare November 27, 2026 17:39
@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.51%. Comparing base (c99ebf2) to head (80c60b2).
⚠️ Report is 474 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67268      +/-   ##
==========================================
- Coverage   92.99%   90.51%   -2.49%     
==========================================
  Files         428      805     +377     
  Lines      195633   285881   +90248     
  Branches    30281    55197   +24916     
==========================================
+ Hits       181937   258758   +76821     
- Misses      13369    17212    +3843     
- Partials      327     9911    +9584     
Files with missing lines Coverage Δ
lib/internal/fs/glob.js 96.31% <100.00%> (-1.62%) ⬇️

... and 604 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yohannesjoya

Copy link
Copy Markdown
Contributor Author

Thanks, fixed. The test calls process.chdir(), which isn't available when the test runner uses workers, so it now skips in that case like the other tests that change the working directory. The change to lib/ is the same. Could you take another look and start CI again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants