Skip to content

node:fs: call the fs.glob callback from the promise reaction, as Node does - #48788

Open
robobun wants to merge 5 commits into
mainfrom
robobun/a93b74ae/fs-glob-callback-promise-job
Open

robobun wants to merge 5 commits into
mainfrom
robobun/a93b74ae/fs-glob-callback-promise-job

Conversation

@robobun

@robobun robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • A process.nextTick queued by the fs.glob callback runs before its microtask: cb tick job. Node v26.3.0 prints cb job tick.
  • glob (src/js/node/fs.ts:1254) sends the callback through process.nextTick. Node's callback is the promise reaction.
  • fs.globSync builds its result with 2 Array.from calls (src/js/internal/fs/glob.ts:454, src/js/node/fs.ts:1258), which a program can replace. No user reported either problem.

Fix

  • glob wraps the callback with ensureCallback and gives $then Node's two handlers, in both texts of node:fs.
  • Glob#globAll() is new, as on Node's main branch. Glob#globSync() gathers with $forEach, and fs.globSync returns that array.
  • The six other settleCallback users keep the hop, as in Node. Self-reviewed twice: 36 concerns raised, 34 addressed, 2 rejected (Notes).
  • Verified: fs.test.ts (8 of 10 new tests fail on main), glob.test.ts (3 of 3), AOT runs, Node's test-fs-glob*.mjs.

Background

  • A checkpoint runs process.nextTick ticks, then microtasks. A promise reaction is a microtask, so its microtasks run first.
  • bun build --compile --aot compiles the second text, src/js/ahead-of-time/node/fs.ts.
  • $then and $forEach are the engine's private copies of built-ins: no program can replace them.
  • Considered Node's text with no guard. A throw then rejects then()'s promise, and by default Bun tells no uncaughtException listener (A default-mode unhandled rejection does not reach an uncaughtException listener #44019).

Downsides

  • A callback's throw line shows for 1 of 3 thrown values (main: 3). Pause on uncaught exceptions stops 0 times (main: 1).
  • A rejected promise that the callback returns is now an unhandled rejection, as in Node.
  • Interpreted, fs.globSync runs 1.4% more instructions over 100 matches. A kept result of 1,004 matches holds 3,200 bytes more. Binary text: +349 bytes.
Notes

Repro

const fs = require("node:fs");
process.nextTick(() => {
  const log = [];
  fs.glob("*.cjs", { cwd: __dirname }, (err, res) => {
    log.push("cb");
    process.nextTick(() => log.push("tick"));
    queueMicrotask(() => log.push("job"));
    setImmediate(() => console.log(log.join(" ")));
  });
});
with the outer process.nextTick without it
Node v26.3.0, Node v28 nightly (2026-11-11) cb job tick cb job tick
Bun 1.3.14 cb job tick cb tick job
Bun 1.4.0, Bun 1.4.4, main (d0f4fdd65fb) cb tick job cb tick job
this PR cb job tick cb job tick

No issue reports this. It came out of a comparison with Node in the work on #48713, which lists it as separate work. It rests on the Node compatibility rule that callback timing is contract, as #43728 did. Only the callback form changes: util.promisify(fs.glob) and fs.promises.glob print the same on Node, on main and with this PR.

The second column of Bun 1.3.14 is another defect: with no tick queue yet, the first tick that a job queues runs before the jobs that wait. #33088 and #43366 are open for it. It does not reach fs.glob after this PR, because node:fs reads process.nextTick when it loads and that read creates the queue. A new test holds that.

What the order decides

fs.glob("*.cjs", { cwd: __dirname }, async () => {
  const stream = new PassThrough();
  stream.destroy(); // 'close' comes from a process.nextTick
  await null; // a job
  await once(stream, "close");
  console.log("saw close");
});

Node v26.3.0 and this PR print saw close. Bun 1.4.4 and main never do: the tick runs before the job, so the listener is late.

Node's code (lib/fs.js#L3207-L3214)

callback = makeCallback(callback);

const Glob = lazyGlob();
PromisePrototypeThen(
  ArrayFromAsync(new Glob(pattern, options).glob()),
  (res) => callback(null, res),
  callback,
);

Node's main branch has new Glob(pattern, options).globAll() in place of the ArrayFromAsync(...) line.

History

Order against Node

One script prints 12 lines: both overloads, a list of patterns, withFileTypes, a walk that fails, nested calls, calls from a timer, an immediate, a job and an fs callback, and a call with no pattern. It runs as CommonJS, as ESM with the default import, as ESM with named imports, and as a bun build --compile --aot executable. Node v26.3.0 and the Node nightly print the same 12 lines.

lines equal to Node
main 0 of 48
this PR 48 of 48

The other users of settleCallback

The repro with each API in place of fs.glob:

Node v26.3.0 main this PR
glob cb job tick cb tick job cb job tick
cp, rm, recursive rm, opendir, Dir.read, Dir.read (buffered), Dir.close cb tick job cb tick job cb tick job

The 43 rows of the existing order table in fs.test.ts pass without a change.

A callback that throws

The callback queues a tick and a job, then throws. The line lists what runs, in order.

listeners Node v26.3.0 main this PR
uncaughtException cb job tick uncaughtException(unhandledRejection) cb uncaughtException(uncaughtException) tick job cb uncaughtException(uncaughtException) job tick
uncaughtException and unhandledRejection cb job tick unhandledRejection cb uncaughtException(uncaughtException) tick job cb uncaughtException(uncaughtException) job tick
none cb job tick, exit code 1 cb tick job, exit code 1 cb job tick, exit code 1

Without the guard, on a debug build of this branch: the two throw tests fail with exit code 1 and no call of the listener, and test-fs-glob-throw.mjs fails with Mismatched <anonymous> function calls. Expected exactly 1, actual 0. Bun prints an unhandled rejection and sets the exit code. It calls uncaughtException listeners only with --unhandled-rejections=throw or strict. #44019 asked for the default to change and is closed as not planned.

What the report of the throw shows:

main fs.glob this PR fs.glob main fs.stat
the report names the throw line, for throw new Error(), throw "a string", throw anErrorCreatedEarlier 3 of 3 1 of 3 1 of 3
bun test names the throw line, for a failed expect and the same three throws 4 of 4 2 of 4 not measured
Debugger.paused events with setPauseOnExceptions uncaught 1 0 0

A job of its own for the callback (queueMicrotask(() => callback(...))) needs no guard. It was not used. The callback then runs one job later than in Node. And in Bun an uncaughtException listener reads no AsyncLocalStorage store for a throw from queueMicrotask. For a throw of the fs.glob callback it reads the store of the caller, on Node, on main and with this PR. The throw tests hold that.

A callback that returns a promise

Node's first handler returns what the callback returns, so the promise of then() takes the state of a promise that the callback returns. This PR does the same. Main ignores the return value.

fs.glob("*.txt", { cwd }, () => {
  const returned = Promise.reject(new Error("returned by the callback"));
  returned.catch(() => console.log("handled by the program"));
  return returned;
});
unhandledRejection listener no listener
Node v26.3.0 called exit code 1
main not called exit code 0
this PR called exit code 1

Array.fromAsync

Main calls Array.fromAsync when fs.glob runs, so a program that replaced it changes fs.glob. Node v26.3.0 takes it from its primordials and Node's main branch does not use it. With Glob#globAll() this PR does not use it either: fs.glob calls back with Array.fromAsync replaced by a function that throws, before or after node:fs loads.

fs.globSync

The automatic review of this PR found the sync form of the Array.fromAsync point. It marked the finding as older than this PR and as optional. No user reported it. On main each fs.globSync call does this:

Node v26.3.0 has return new Glob(pattern, options).globSync(), and its Glob#globSync gathers from a SafeSet with the primordial ArrayFrom. Node's main branch has a native walk (nodejs/node#65392) and builds the Dirents in an index loop.

The table shows what fs.globSync("*.txt", { cwd }) returns over a.txt and b.txt while the program has one built-in replaced. Each cell gives the result as strings, then the names with withFileTypes.

replaced main this PR Node v26.3.0
Array.from, by a function that returns ["replaced"] replaced, then replaced the matches, both times the matches, both times
Function.prototype.bind the matches, then replaced twice the matches, both times the matches, both times
Set.prototype[Symbol.iterator], for a set that is not a plain Set replaced, then null the matches, both times the matches, both times
Array.prototype[Symbol.iterator], for the array of the matches replaced, then the matches the matches, both times the matches, both times

Now Glob#globSync pushes each match from the private $forEach of the set, with a named function and no bound one. With withFileTypes an index loop then puts each Dirent in the place of its path. fs.globSync returns that array in both texts.

Costs for each fs.globSync call, on the same builds as the table of costs below:

main this PR
calls of the public Array.from 2 0 (Node: 0)
calls of Function.prototype.bind, with withFileTypes 1 0 (Node: 0)
cells allocated, no pattern (BUN_JSC_useGC=0, heapStats) 21.79 18.79
cells allocated over 100 matches, in calls 51 to 250 of a process Array 32.02, Object 217.88, Set Iterator 103 Array 31.02, Object 116.88, Set Iterator 102
cells allocated over 100 matches, in calls 3,001 to 3,200 822.74 821.75
the same with withFileTypes 1,025.75 1,022.74
instructions, BUN_JSC_useJIT=0, no pattern 19,599 17,770
instructions, BUN_JSC_useJIT=0, 100 matches 2,252,682 2,283,305 (+1.4%)
the same with withFileTypes 2,513,659 2,535,380 (+0.9%)
bytes that a kept result holds, for 100, 110, 502, 1,000 and 1,004 matches 4,437, 5,041, 17,979, 33,040, 33,181 4,282, 5,386, 20,082, 32,290, 36,402
  • The 101 Object cells that main allocates in the first calls are the iterator results of Array.from over the set. The optimizing JIT removes them on main too. After 3,000 calls the difference is the one array of the copy, and the bound function with withFileTypes.
  • $forEach calls a function for each match, which costs about 300 instructions for each match in the interpreter. A for...of over the set costs less than main's Array.from (2,248,240 to 2,218,525 instructions on the earlier base). But it reads Set.prototype[Symbol.iterator], and the third row of the first table then prints replaced.
  • A kept result is the array that the pushes grew, with the room it grew into. Main's second Array.from made a copy of exactly the length. The bytes are the heap size for each of 300 kept results, after a full collection.

Limits of this part:

  • The walk of glob.ts still calls public built-ins, for example Array.prototype.push, Set.prototype.add and for...of over a Map. Node v26's text uses primordials there. Only the step that gathers the result changed.
  • No file of the glob path (node/fs.ts, its second text, internal/fs/glob.ts) reads Array.from or Array.fromAsync now. Main has 5 such lines there. See "Not in this PR" for the 21 lines elsewhere in src/js.

Costs, on release builds of main (d0f4fdd65fb, merged into this branch) and of this PR, Linux x64

main this PR
heap cells held per pending fs.glob([], cb) call (N = 20,000, bun:jsc heapStats) 28.01 (Function 4, JSLexicalEnvironment 5, Promise 2, reaction 1), 1,481 B 27.01 (Function 4, JSLexicalEnvironment 4, Promise 2, reaction 1), 1,449 B
objects allocated per completion (N = 5,000, no collection in a window) 35.31 (Array 7.01, Promise 3, Arguments 0) 33.27 (Array 6.01, Promise 2, Arguments 1)
the same, for a callback that throws 38.31 36.30
TickObject that async_hooks sees per 100 completions, returning and throwing 100 and 100 0 and 0 (Node: 0 and 0)
Function cells after require("node:fs") 803 803
size bun: text 155,094,093 155,094,442 (+349)
size bun: data, bss 125,296, 1,864,616 the same
bun on disk 155,285,776 B 155,285,776 B (+0)
instructions per completion, main thread, BUN_JSC_useJIT=0 32,488 (runs: 32,500 and 32,476) 28,142 (runs: 28,140 and 28,143)

The instruction count is the slope between 200 and 1,200 chained fs.glob([], cb) completions, two runs of each. perf and valgrind are not installed here. The count comes from qemu-x86_64 -one-insn-per-tb -d exec,nochain, with the lines of the main thread counted. The two runs of one build differ by 24 instructions per completion on main and by 3 with this PR. The fs.globSync counts above are the slope between 40 and 120 calls, one run of each. With no pattern the slope is between 200 and 1,200 calls.

Clauses of the fix, and what fails without each

Each line is one edit to a build of this branch.

  • Main's src/: 8 of the 10 Bun tests of the new describe fail. The 3 new tests of glob.test.ts fail too. The 2 that pass are the throw tests.
  • validateFunction in place of ensureCallback: the 2 throw tests fail, and test-fs-glob-throw.mjs fails with Mismatched <anonymous> function calls. Expected exactly 1, actual 0.
  • Braces around the two calls of the handlers, so that they return nothing: the 2 returned-promise tests fail.
  • Array.fromAsync(this.glob()) as the body of globAll(): the Array.fromAsync test fails with error: the replaced Array.fromAsync ran.
  • Main's statement in the second text alone: test/aot/run-parallel.ts fails the new program. The executable prints fulfilled: file.txt, tick, microtask where plain Bun prints fulfilled: file.txt, microtask, tick.
  • A for...of over the set in place of $forEach: the fs.globSync script of fs.test.ts fails. It prints replaced.

Also different after this PR

  • fs.glob with a callback that is not a function names the argument "cb", as Node does. Main names it "callback".
  • A process.nextTick that holds every tick, put in place before node:fs loads, no longer holds the fs.glob callback (table below).
  • async_hooks sees no TickObject for an fs.glob completion.
  • fs.globSync makes no call of the public Array.from or Function.prototype.bind, and it returns the array that Glob#globSync built.
callback with such a process.nextTick Node v26.3.0 Bun 1.3.14 main this PR
glob runs runs held runs
cp held runs held held
rm, recursive rm, opendir, Dir.read runs runs held held
Dir.close runs held held held

Not in this PR

Self-review

A review of the first form of this change raised 16 concerns. That form kept a copy of Array.fromAsync taken when node:fs loaded, and caught a throw in each handler. The main finding: a program that replaced Array.fromAsync before node:fs loaded made fs.glob throw a TypeError, where main and Node call back. All 16 are addressed in the code, the tests or this text. Two asks inside them are not done:

  • A second bun test --aot test, with a stand-in for Array.fromAsync in a preload. No text of node:fs calls Array.fromAsync now, fs.test.ts holds the first text, and the new deed of the census test compares the two texts.
  • Tracking issues for the rows under "Not in this PR". No user reported them.

A second review read the fs.globSync commit and raised 20 concerns. 18 are addressed:

  • The two tests of glob.test.ts replace a built-in by assignment and put it back in finally. The file gets no new import.
  • Function.prototype.bind has a test, and one script replaces Array.from and the Set iterator before node:fs loads. That script also runs on Node.
  • The read of the Set iterator is closed with $forEach. The first form of the commit had a for...of there.
  • Main is merged into the branch, and the AOT tests ran again on that build.
  • The comment names the three built-ins, and this text gives the limits, the costs and the lines left out.

2 are rejected. Both ask to keep fs.globSync out of this PR: one by a reply alone, one by a later PR. The review rules count a sync and an async form as one concern. A separate PR changes the same hash line of the second text, so the two conflict.

Verification

All runs are on this branch with main (d0f4fdd65fb) merged in. "Main" is a build of the same tree with main's three src/js files.

  • bun bd test test/js/node/fs/fs.test.ts -t "except for fs.glob": with main's src/, 8 of the 10 Bun tests fail. With this PR all 10 pass. The 2 that pass both ways are the throw tests. They fail without ensureCallback.
  • The same describe runs 7 of its scripts on the installed Node when that is v24.3.0 or newer. They pass on Node v26.3.0, v26.12.1 and a v28 nightly. The harness takes the version of a nightly for too old, so that run used a wrapper that reports 28.0.0.
  • test/js/node/fs/glob.test.ts: 37 pass on the ASAN debug build. The 3 new tests fail with main's src/. One names the callback argument. Two replace Array.from and Function.prototype.bind: main makes 4 calls and 1 call, this PR makes 0.
  • The whole of fs.test.ts on release builds: 649 pass and 1 fail with this PR. With main the 8 tests above fail too.
  • That 1 failure is keeps a non-throwing callback in the same place in the event loop. This PR does not touch it, and it fails on main in the same runs. It races fs.stat against setImmediate. Before the merge of main its script printed the expected order in 77 of 120 runs with this PR. With main it did so in 80 of 120.
  • The whole of fs.test.ts on the ASAN debug build with this PR: 645 pass, 5 fail. Four are readdir(path, {recursive: true}) ... x 100 tests. Two reach their 10 s limit, and the two behind them fail after that. The fifth is fs/promises > rmdir > removes a dir. It reached its 5 s limit while two other jobs ran on the machine, and it passes alone. None of the five calls fs.glob.
  • test/aot/run-parallel.ts with the new program: pass in all four builds on the release build. Node v26.3.0 and the Node nightly print the same six lines. On the ASAN debug build three of the four builds passed, and the fourth ended with the SEGV below.
  • test/aot/the-library-calls-the-same-built-ins-compiled-as-plain.test.ts with the two new deeds: 10 pass, on the release build and on the ASAN debug build. The deeds pass on main too. They hold the two texts of glob() and of globSync() to each other.
  • test/aot/under-the-inspector-the-console-changes-what-is-public-and-no-record-of-an-internal-file.test.ts finds the record that internal/fs/glob exports in a heap snapshot, by the names of its keys. It now names globAll in place of glob. With main's src/ it finds no record and fails. With this PR it passes on the release build.
  • test/aot/embedded-assets.test.ts, whose executables call fs.globSync: 149 pass on the release build. test/aot/parallel/under-proof-checks-a-unit-names-nothing-out-of-its-reach/a-record-of-one-member-holds-a-function-under-a-name-that-the-glob-library-reads.cjs passes in its three builds.
  • On the ASAN debug build the AOT compiler itself can end with SEGV in WTF::removeIterator, under JSC::AOT::Typed::LinkFlow. For the program of the inspector test (--compile-inspector) it does so in 4 of 6 builds with this PR, and for the new program under --aot-harden=proof-checks in 1 of 6. Before the merge of main the counts were 8 of 10 with this PR. With main's src/ they were 6 of 10. WebKit compiles WTF::removeIterator only without NDEBUG, so a release build has no such call.
  • Node's tests on the release build: test-fs-glob.mjs 448 pass, and test-fs-glob-throw.mjs passes. All 337 test-fs-* files of test/js/node/test/parallel pass, as on main.
  • test/aot/other-suites/run.ts node test/js/node/test --only test-fs-glob on the release build: same 2, DIFFERS 0.
  • test/js/node/fs/dir.test.ts, cp.test.ts, test/internal/second-texts-of-modules.test.ts, test/internal/export-lists-of-both-texts-of-modules.test.ts, test/internal/arms-of-modules-for-compiled-programs.test.ts: pass.
  • bun lint: no problems in 257 files. bun src/codegen/check-types-of-modules.ts: 3 errors, all for files of vendor/WebKit, which this checkout does not hold. Main prints the same 3.
  • Windows and macOS: not run here.

no test proof · iteration 4 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/node/fs/glob.test.ts, test/js/node/fs/fs.test.ts

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

globAll() collects glob results into an array. Both fs.glob implementations pass promise fulfillment or rejection directly to the callback. Tests cover callback scheduling, arguments, errors, and related behavior.

Changes

Glob callback flow

Layer / File(s) Summary
Collect glob results
src/js/internal/fs/glob.ts, test/aot/under-the-inspector-...test.ts, test/js/node/fs/glob.test.ts
Glob.globAll() collects values yielded by Glob.glob(). The module-level export now provides globAll instead of glob. globSync() collects results without Array.from or a bound mapper. Tests check the updated export keys and verify synchronous results when built-ins are replaced.
Wire fs.glob callbacks
src/js/node/fs.ts, src/js/ahead-of-time/node/fs.ts
Both implementations use ensureCallback and globAll, then pass promise fulfillment or rejection directly to the callback. They no longer use Array.fromAsync or schedule the callback through nextTick. Comments and the recorded source hash are updated.
Validate callback behavior
test/js/node/fs/*, test/aot/parallel/node-fs/*, test/aot/built-ins-that-the-library-calls/deeds.fixture.js, test/aot/the-library-calls-the-same-built-ins-compiled-as-plain.test.ts
Tests cover callback timing, results and failures, thrown callbacks, async context, rejected callback promises, invalid callbacks, and compiled-versus-plain behavior. The AOT scheduling test logs observations.

Suggested reviewers: jarred-sumner

Merge Risk: Minimal · up to 3e169

This change makes fs.glob callback timing match Node and stops fs.globSync from depending on replaced built-ins. No concrete regression was found in the changed code or its consumers. The PR adds tests for the new behavior, so the merge risk looks minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "node:fs: call the fs.glob callback from the promise reaction, as Node does" matches the main change. The PR moves the fs.glob callback from a process.nextTick hop into a promise reaction, a…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the repository template's two sections. "What does this PR do?" is covered by a detailed Problem, Fix, and Background, with clear reasons for the change. "How did you verify yo…
Full details: Title check

Explanation

The title "node:fs: call the fs.glob callback from the promise reaction, as Node does" matches the main change. The PR moves the fs.glob callback from a process.nextTick hop into a promise reaction, as Node does. The PR also adds Glob#globAll and changes fs.globSync gathering, which the title does not mention. That omission is acceptable because the title covers the primary change.

Full details: Description check

Explanation

The description follows the repository template's two sections. "What does this PR do?" is covered by a detailed Problem, Fix, and Background, with clear reasons for the change. "How did you verify your code works?" is covered by a detailed Verification section listing test commands, pass/fail counts on main versus this PR, Node comparisons, and platform gaps. Its scope is large, and the main text is dense, but the required information is present and specific. The description also notes that Windows and macOS were not tested. The check passes.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fb75bb5 and 3e169b5.

Files selected for processing: 7

  • src/js/ahead-of-time/node/fs.ts
  • src/js/internal/fs/glob.ts
  • src/js/node/fs.ts
  • test/aot/built-ins-that-the-library-calls/deeds.fixture.js
  • test/aot/the-library-calls-the-same-built-ins-compiled-as-plain.test.ts
  • test/js/node/fs/fs.test.ts
  • test/js/node/fs/glob.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

@robobun

robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 4:02 PM PT - Nov 13th, 2026

❌ @robobun, your commit 3e169b5 has 1 failures in Build #141200 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 48788

That installs a local version of the PR into your bun-48788 executable, so you can run:

bun-48788 --bun

@robobun

robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: reproduced on main (d0f4fdd65fb), on Bun 1.4.0 and on Bun 1.4.4, Linux x64.

const fs = require("node:fs");
process.nextTick(() => {
  const log = [];
  fs.glob("*.cjs", { cwd: __dirname }, (err, res) => {
    log.push("cb");
    process.nextTick(() => log.push("tick"));
    queueMicrotask(() => log.push("job"));
    setImmediate(() => console.log(log.join(" ")));
  });
});

The second problem, which the review of this PR found, in a file named sync.cjs:

const fs = require("node:fs");
Array.from = () => ["replaced"];
console.log(fs.globSync("*.cjs", { cwd: __dirname }).join());
  • Node v26.3.0: sync.cjs
  • Bun 1.3.14, Bun 1.4.0, Bun 1.4.4, main: replaced
  • this PR: sync.cjs

bun bd test test/js/node/fs/fs.test.ts -t "except for fs.glob" fails 8 of the 10 Bun tests with main's src/ and passes all 10 with this PR. bun bd test test/js/node/fs/glob.test.ts fails its 3 new tests with main's src/ and passes all 37 with this PR.

CI: two Buildkite runs of the same tree. No test of this PR fails on either run, and each of the 188 jobs passed on at least one of them.

  • 141139 on 32aa27d23eb: 187 of 188 jobs passed. The one other job, a darwin x64 - test-bun shard, got no agent and expired.
  • 141200 on 3e169b50674, an empty commit on top: 187 of 188 jobs passed. The ASAN lane fails on one test. test/js/web/fetch/fetch-leak.test.ts reports fetch(file://) leaked 20.5 MB over 20000 iterations against its limit of 20 MB. That test does not call fs.glob or fs.globSync. On 141139 it measured 20.1 MB and passed on a retry. It is reported as a failure of main.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

The record that internal/fs/glob hands out is now { globAll, globSync, Glob }. The inspector test finds that record in a heap snapshot by the names of its keys, so it names globAll in place of glob.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline note on globSync, I also checked that nothing still imports the removed glob export of internal/fs/glob (fs.promises.glob calls Glob#glob() directly), that settleCallback keeps its rm/rmdir/Dir.read callers, and that guardCallback returns the callback's value, so a promise returned from the callback flows into then() as the description says. I did not run the AOT header-hash test (test/internal/second-texts-of-modules.test.ts) or the new fs.test.ts cases; those were checked by reading only.

Extended reasoning...

The diff changes how fs.glob invokes its callback in src/js/node/fs.ts and its AOT twin, adds Glob#globAll in src/js/internal/fs/glob.ts, and adds ordering/throw/returned-promise tests; it touches no security-sensitive surface. A human look is still worthwhile because it changes user-observable callback timing and where a callback throw is reported, a tradeoff the description itself acknowledges.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🟣 src/js/node/fs.ts — Pre-existing, sync twin left out: a program that replaces Array.from still changes what fs.globSync returns, while fs.glob is now shielded from a replaced Array.fromAsync. src/js/node/fs.ts:1259 wraps an array that is already an array in the user-replaceable Array.from, and Glob#globSync at src/js/internal/fs/glob.ts:454 builds its result with it too. Fix: build the globSync result without user-overridable machinery at every site, e.g. collect with $arrayPush in Glob#globSync as globAll does and return that array directly from fs.globSync, then refresh the AOT header hash. Same pattern at 3 sites (src/js/node/fs.ts:1259, src/js/ahead-of-time/node/fs.ts:1346, src/js/internal/fs/glob.ts:454).

    Why this was flagged

    A program assigns Array.from (a polyfill, a test spy, or the replacement the AOT suite's deeds fixture style performs) before calling fs.globSync. fs.globSync at src/js/node/fs.ts:1259 and src/js/ahead-of-time/node/fs.ts:1346 calls Array.from on the array that Glob#globSync already returned, and Glob#globSync at src/js/internal/fs/glob.ts:454 calls Array.from over its ResultSet with the withFileTypes map function. The replaced function decides what the caller receives: a throwing replacement makes fs.globSync throw, a wrong one returns wrong matches. The base branch behaves the same for globSync; this PR removes the equivalent exposure only from the async path (Glob#globAll at src/js/internal/fs/glob.ts:764 uses $arrayPush and the new test 'the program replaced Array.fromAsync before node:fs loaded' guards it), so the sync twin is the one site of the class left unfixed. No safeguard applies: the ensureCallback guard only wraps the async callback, and nothing captures Array.from at module load.

    Verification: pre-existing — the base already reaches fs.globSync through the same route; this PR fixes only the async twin. Triggering condition: a program assigns Array.from before calling fs.globSync. src/js/node/fs.ts:1259 re-wraps with Array.from an array Glob#globSync (src/js/internal/fs/glob.ts:454) built with Array.from. A replacement decides what fs.globSync returns (or throws).

Each call of fs.globSync called the public Array.from two times. Glob#globSync built the result with it, from a Set and with a bound mapper. fs.globSync copied that array with it. A program that replaces Array.from, Function.prototype.bind or the iterator of Set decided the result. Node uses its own copies and a SafeSet.

Glob#globSync now pushes the matches from the private $forEach of the set, and fs.globSync returns that array. Both texts of node:fs change.
@robobun

robobun commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

The fs.globSync finding of the review is correct. 113a6f0 fixes it in this PR.

  • Checked by a run. With Array.from replaced, fs.globSync returns what the replacement returns, on main and on the earlier head of this PR. A spy counts 2 calls for each fs.globSync call. Node v26.3.0 makes 0 calls and returns the matches.
  • Two more built-ins on the same statement. With withFileTypes the mapper came from Function.prototype.bind, and Array.from read the iterator of the result set. A program that replaces one of them decided the result too. Node uses its primordials and a SafeSet there.
  • The change. Glob#globSync pushes each match from the private $forEach of the set, with a named function. fs.globSync returns that array with no copy, in both texts of node:fs. The copy was left from feat(node/fs): add fs.glob, fs.globSync, and fs.promises.glob #16676, where globSync was a generator.
  • Tests. fs.globSync > makes no call of a replaced Array.from and makes no call of a replaced Function.prototype.bind in test/js/node/fs/glob.test.ts fail on main, with 4 calls and 1 call. One script in fs.test.ts replaces Array.from and the Set iterator before node:fs loads. It fails on main and passes on Node v26.3.0, v26.12.1 and a v28 nightly.
  • Cost. $forEach calls a function for each match. With the JIT off, a call over 100 matches runs 1.4% more instructions than on main. It allocates fewer cells than main in every state that I measured.
  • Not changed. The walk of internal/fs/glob.ts still calls public built-ins such as Array.prototype.push. 21 lines elsewhere in src/js still read Array.from or Array.fromAsync. The PR body lists them under "Not in this PR".

Main (d0f4fdd65fb) is now merged into the branch. The numbers in the PR body are from builds of that tree.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline finding, I checked the second-text sync for the latest push: the SHA-256 recorded in the src/js/ahead-of-time/node/fs.ts header matches the current src/js/node/fs.ts (computed locally), and the glob/globSync edits in both texts are identical. I also confirmed the removed glob export of internal/fs/glob has no remaining callers (fs.promises.glob uses Glob#glob() directly), and that settleCallback/validateFunction keep their other users in node:fs, so nothing is left dead. I did not run the test suite or the AOT compile.

Extended reasoning...

The change rewires fs.glob's callback to be the promise reaction (guarded by ensureCallback) and makes Glob#globSync/globAll gather results with $forEach/$arrayPush instead of Array.from/Array.fromAsync, in both the plain and ahead-of-time texts of node:fs, with tests for ordering, throw handling and replaced built-ins. It touches no security-sensitive surface. The one surviving finding is a pre-existing null-options validation gap posted inline; the second-text hash check and dead-code audit named in the body are the new items ruled out in this run.

Comment thread src/js/node/fs.ts Resolved

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant