Skip to content

node:repl: domain routing, newListener throws, namespace inspect (+4 tests) - #35388

Draft
cirospaciari wants to merge 9 commits into
claude/repl-tab-complete-unaryfrom
claude/node-repl-v26b
Draft

cirospaciari wants to merge 9 commits into
claude/repl-tab-complete-unaryfrom
claude/node-repl-v26b

Conversation

@cirospaciari

Copy link
Copy Markdown
Member

Stacked on #34642 — review that first. Base branch: claude/repl-tab-complete-unary.

What this adds

Four more Node v26.3.0 REPL tests, vendored verbatim, plus the runtime fixes they needed:

  • test/js/node/test/parallel/test-repl-domain.js
  • test/js/node/test/parallel/test-repl-import-referrer.js
  • test/js/node/test/parallel/test-repl-uncaught-exception-async.js
  • test/js/node/test/parallel/test-repl-user-error-handler.js

Why now

The base stack replaced Bun's REPL stub with Node v26's lib/repl.js, so what is left in the
node:repl test cluster is not missing REPL code — it is the runtime underneath the REPL behaving
differently from Node. Each fix below is a general Bun bug that the REPL happens to be the first
caller to notice.

What changed

A throwing newListener handler now aborts the registration (EventEmitter.cpp,
EventEmitter.h, JSEventEmitter.cpp). Node emits newListener before storing the listener, and a
throw from that handler comes back out of .on() with the listener never added. Bun's native
EventEmitter routed the exception to the 'error' listener / unhandled-error reporter, so the
listener was added anyway and the caller saw nothing:

process.prependListener('newListener', ev => { if (ev === 'uncaughtException') throw new Error('blocked'); });
try { process.on('uncaughtException', () => {}); } catch (e) { console.log('threw:', e.message); }
console.log('count', process.listenerCount('uncaughtException'));
// node: threw: blocked / count 0
// bun before: (uncaught "blocked" printed) / count 1

emitPropagatingExceptions leaves the exception pending on the VM instead; only the newListener
emit inside addListener uses it, so every other native emit keeps its current behaviour.
node:repl uses exactly this mechanism to reject process.on('uncaughtException') typed into a
REPL.

util.inspect prints ESM namespace objects like Node (internal/util/inspect.js). Bun hangs an
internal { __esModule: true } object off module namespaces for CJS interop, where the spec (and
V8) uses a null [[Prototype]]. getConstructorName's prototype walk picked that up, so
await import('./message.mjs') printed
Module <[Object: null prototype] {}> [Module] { message: 'A message' } instead of Node's
[Module: null prototype] { message: 'A message' }. The interop prototype is an implementation
detail, so namespaces are now treated as having no constructor, matching
getPrefix(null, 'Module', 'Module').

node:domain no longer swallows synchronous throws (node/domain.ts).
Domain.prototype.run has no
try/catch and does not exit() on the throwing path: the exception leaves run() with the domain
still entered, reaches the fatal-exception handler, and
_errorHandler decides what
happens. Bun caught it inside run() and emitted 'error' itself, which is observable — the
statement after a throwing d.run(...) kept executing — and it hides the error from anything that
reads process.domain afterwards, which is how node:repl decides whether an eval error belongs to
a user domain. _errorHandler is ported including the nested case where the 'error' handler
itself throws and the next domain on the stack gets a turn (covered by the already-vendored
test-domain-nested-throw.js).

Shared uncaught-exception capture slot (internal/uncaught_exception_capture.ts).
process.setUncaughtExceptionCaptureCallback is exclusive; node:domain and node:repl both need
it, and upstream relies on an internal stacking variant so they coexist. The dispatcher node:repl
already had is now a small shared internal module both install into. No behaviour change for
node:repl; the code moved.

process.setUncaughtExceptionCaptureCallback works under bun test (VirtualMachine.rs,
BunProcess.cpp). uncaught_exception short-circuited to the test reporter whenever isBunTest
was set, so a user-installed capture callback was silently dead:

// inside a bun test
process.setUncaughtExceptionCaptureCallback(e => { console.log('CAPTURED', e.message); return true; });
setImmediate(() => { throw new Error('boom'); });   // before: test failure, callback never ran

The callback now gets the exception first, exactly as it does outside the test runner. When no
callback is installed — every existing test — the path is unchanged.

REPL error-code stacks carry [CODE] (internal/repl/node-errors.js). Node's stacks read
Error [ERR_INVALID_STATE]: …; JSC materialises .stack from the bare .name. The shim already
had a decorateNodeErrorStack helper for some codes; it now applies to all of them, since the REPL
prints stacks verbatim.

How this was verified

Every vendored file was run one-process-per-file the way scripts/runner.node.mjs does it
(bun run/bun test + --config=bunfig.node-test.toml, BUN_GARBAGE_COLLECTOR_LEVEL=1,
BUN_FEATURE_FLAG_INTERNAL_FOR_TESTING=1, NO_COLOR=1, FORCE_COLOR=0) against a debug build, and
each is byte-identical to ~/code/node/test/parallel/. test/expectations.txt is untouched.

Regression sweep, same harness, 211 files — every vendored test-repl-*, test-domain*,
test-readline*, test-event* and test-process-*: 209 pass. The two that did not are pre-existing
debug-build timeouts unrelated to this change and pass with a longer deadline
(test-repl-tab-complete-buffer.js spends ~31 s per completion of a 1e6-element buffer in a debug
build; test-process-exit-code-validation.js takes ~63 s because it spawns ~30 children).

Two caveats worth knowing before merge:

  • test-repl-domain.js does not fail when a throw is appended to it, i.e. it looks like a
    silent pass to a canary check. It is not: the test prints ok, and the appended throw is
    swallowed by the user domain the test leaves entered. Real node v26.3.0 behaves identically
    (ok, ok, exit 0) — Bun now matches it byte for byte, which is the point of the domain change.
  • The async: true, handleErrorReturn: badvalue case in test-repl-user-error-handler.js spawns a
    child interpreter, and loading node:repl in a debug build costs ~3.8 s, close to bun test's
    5 s per-test default. It passed 4 of 5 local runs on a machine at load average 130+. A release
    interpreter starts ~25x faster (0.014 s vs 0.36 s for -e 1), so this should not be marginal in
    CI — but it is the one file here with a timing dependency.

Still failing in this cluster

16 of the 20 node:repl upstream files are not vendored. Grouped by what blocks them:

  • REPL previews need inspector Runtime.evaluate with throwOnSideEffect —
    test-repl-preview.js, test-repl-preview-newlines.js, test-repl-strict-mode-previews.js,
    test-repl-custom-eval-previews.js, test-repl-mode.js (its testStrictModeTerminal third),
    test-repl-inspector.js. Bun's node:inspector answers every Runtime.* method with
    Inspector method "Runtime.evaluate" is not supported, which is why
    internal/repl/node-shims.js's sendInspectorCommand is a stub. Previews evaluate the
    half-typed line on every keystroke, so they are only safe with V8's side-effect-free evaluation;
    JSC's inspector protocol has no equivalent flag. test-repl-inspector.js additionally asserts on
    a Runtime.evaluate response shape.
  • Engine error-message text — test-repl.js and test-repl-pretty-stack.js each fail on one
    line, in opposite directions, because the REPL prefixes Uncaught vs Uncaught:\n depending on
    whether the message fits in breakLength (80). JSC's JSON Parse error: Expected '}' is shorter
    than V8's Expected property name or '}' in JSON at position 1 …, and JSC's
    Unexpected identifier 'y'. Expected ';' after variable declaration. is one character longer than
    the 80-column budget where V8's Unexpected identifier 'y' fits.
    test-repl-unsafe-array-iteration.js is the same class ({} is not iterable vs
    [3,2,1] is not iterable), and test-repl-underscore.js wants V8's receiver-qualified frame name
    Object.readdirSync, which JSC does not produce.
  • module.builtinModules contains Bun's own builtins — test-repl-tab-complete-require.js,
    test-repl-tab-complete-import.js. Both iterate builtinModules minus _*/node:* and require
    each entry to appear in completions together with node:<entry>. That would mean offering
    node:bun:ffi, node:undici, node:ws. Passing means dropping bun, bun:*, undici and ws
    from module.builtinModules — a user-visible Bun API change that does not belong in a REPL PR.
  • common.hasIntl is false — test-repl-tab-complete.js (27 of 29 subtests pass). It gates
    Intl in the expected completion list on process.config.variables.v8_enable_i18n_support, which
    Bun leaves undefined. The base stack has a commit that fixes exactly this key and then reverts it
    (ce31de7797) because flipping hasIntl exposes 11 other vendored tests; that fix is the base
    author's to land. The other failing subtest needs Runtime.globalLexicalScopeNames, same
    inspector gap as above.
  • Module-not-found error shape — test-repl-require.js wants
    Uncaught Error: Cannot find module './bar' plus requireStack: [ '<repl>' ]. Bun throws
    ResolveMessage (correct code: 'MODULE_NOT_FOUND', no requireStack) from every require, not
    just the REPL's.
  • fs callbacks that throw do not become uncaught exceptions — test-repl-timeout-throw.js.
    See below; this one is a general Bun bug, not a REPL one.
  • Error.prepareStackTrace set outside the REPL context is not applied to REPL errors —
    test-repl-pretty-custom-stack.js.

Cross-cutting bug found, not fixed here

A throw from a node:fs async callback bypasses process.on('uncaughtException') and takes the
process down, because src/js/node/fs.ts delivers callbacks from a promise continuation
(fs.readFile(...).then(d => callback(null, d), callback)) rather than from a completion hook:

process.on('uncaughtException', e => console.log('CAUGHT:', e.message));
require('node:fs').readFile(__filename, () => { throw new Error('from-fs'); });
setTimeout(() => { throw new Error('from-timeout'); }, 5);
// node: CAUGHT: from-fs / CAUGHT: from-timeout
// bun:  error: from-fs  (fatal; the timeout one is caught)

Fixing it means changing how every async node:fs entry point invokes its callback, which is a
node:fs change rather than a REPL one.

…ke node

A throwing 'newListener' handler is how node lets code veto a listener
registration: emit('newListener') runs before the listener is stored, and
the throw comes back out of .on(). Bun's native EventEmitter routed the
exception to the 'error' listener / unhandled-error reporter instead, so
the listener was registered anyway and the caller saw nothing. Add an
emit variant that leaves the exception pending and use it for the
newListener emit only.

util.inspect printed ESM namespace objects as
'Module <[Object: null prototype] {}> [Module] { ... }' because Bun hangs
an internal '{ __esModule: true }' object off them for CJS interop and
the constructor walk picks it up. Node prints
'[Module: null prototype] { ... }'.

Adds test-repl-uncaught-exception-async.js and test-repl-import-referrer.js.
node:domain's run() was swallowing synchronous throws and emitting
'error' itself. Node does not: the throw leaves run() with the domain
still entered, reaches the fatal-exception handler, and Domain's
_errorHandler decides what to do. That difference is observable — the
statement after the throwing d.run(...) keeps running under Bun — and it
hides the error from anything that inspects process.domain afterwards,
which is how node:repl decides whether an eval error belongs to a user
domain.

Port Domain.prototype._errorHandler, including the nested case where the
'error' handler itself throws and the next domain on the stack gets a
turn, and drop the try/catch from run().

Both node:domain and node:repl need to own process's single
uncaught-exception capture slot, so the dispatcher node:repl had is now
a shared internal module that either can install into.

Two more fixes the vendored tests pin:
- process.setUncaughtExceptionCaptureCallback was dead under `bun test`;
  uncaught exceptions went straight to the test reporter. A user callback
  now gets the exception first, as it does outside the test runner.
- Errors from the ported REPL's error-code shims now carry [CODE] in
  their stack, which is what node prints.

Adds test-repl-domain.js and test-repl-user-error-handler.js.
@robobun

robobun commented Jul 24, 2026 •

Copy link
Copy Markdown
Collaborator
Updated 10:21 PM PT - Aug 20th, 2026

⏳ @robobun, your commit 1e4539e is still building in Build #102313, but has 1 failures so far (All Failures):

  • 📦 Binary size — 12 over 0.50 MB
  • targetthis build canary: main #102263
    sizeΔ
    ❌ bun-darwin-aarch6460.97 MB58.65 MB+2.32 MB
    ❌ bun-darwin-x6466.89 MB64.71 MB+2.17 MB
    ❌ bun-linux-aarch6476.87 MB74.87 MB+2.00 MB
    ❌ bun-linux-x6477.04 MB75.02 MB+2.02 MB
    ❌ bun-linux-aarch64-musl70.20 MB68.20 MB+2.00 MB
    ❌ bun-linux-x64-musl71.06 MB69.06 MB+2.00 MB
    ❌ bun-linux-aarch64-android83.84 MB81.78 MB+2.06 MB
    ❌ bun-linux-x64-android86.25 MB84.28 MB+1.97 MB
    ❌ bun-freebsd-x6488.27 MB86.27 MB+2.00 MB
    ❌ bun-freebsd-aarch6491.44 MB89.33 MB+2.11 MB
    ❌ bun-windows-x6484.70 MB82.32 MB+2.38 MB
    ❌ bun-windows-aarch6474.94 MB73.03 MB+1.91 MB

    Add [skip size check] to the commit message if this increase is intentional.

@github-actions

Copy link
Copy Markdown
Contributor

Found 2 issues this PR may fix:

  1. Missing functionality of 'domain' API: control of async unhandled exceptions #6045 - PR rewrites Domain.prototype.run and adds ensureDomainExceptionCapture() to route async exceptions (e.g. from setTimeout) to the active domain's error handler, which is the missing behavior reported
  2. domain does not catch exceptions thrown inside setTimeout() callbacks #30672 - Same root cause as Missing functionality of 'domain' API: control of async unhandled exceptions #6045; the new _errorHandler and ensureDomainExceptionCapture() ensure exceptions thrown in timer callbacks within a domain are caught by the domain's error handler

If this is helpful, copy the block below into the PR description to auto-close these issues on merge.

Fixes #6045
Fixes #30672

🤖 Generated with Claude Code

@cirospaciari cirospaciari changed the title node:repl: four more v26.3.0 tests (domain routing, newListener throws, namespace inspect) node:repl: domain routing, newListener throws, namespace inspect (+4 tests) Jul 24, 2026
oxlint's no-duplicate-conditional-property-access rejects reading
stack.length in the condition and again in the body.

[allow size]

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants