Skip to content

fix(runtime): bridge console to the inspector regardless of --inspect flag - #35795

Merged
bartlomieju merged 6 commits into
denoland:mainfrom
wilkmaciej:fix-inspector-console-wrap
Jul 20, 2026
Merged

bartlomieju merged 6 commits into
denoland:mainfrom
wilkmaciej:fix-inspector-console-wrap

Conversation

@wilkmaciej

@wilkmaciej wilkmaciej commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

The main-worker bootstrap wrapped the console into the V8 inspector console only when an --inspect* flag was present at startup. When the inspector is activated later at runtime - via node:inspector open() (as the vscode-js-debug bootloader does) or SIGUSR1 - Runtime.consoleAPICalled is never emitted, so debugger clients (e.g. the VS Code Debug Console) show no console output even though the session is otherwise fully functional.

The worker bootstrap already wraps unconditionally; do the same for the main worker. The bootstrap options slot (3) is left in place, only its consumer is removed.

Includes a regression test (inspector_console_api_late_open in tests/unit/inspector_test.ts, next to the other CDP protocol tests): it spawns a child that opens the inspector via a --require preload, connects over CDP, and asserts Runtime.consoleAPICalled arrives for console.log. The wait is bounded by a 10 s deadline so a regression fails with a diagnostic instead of hanging, and the child is killed in a finally block.

Part of a series fixing VS Code debugging with --watch (#35791).

wilkmaciej and others added 2 commits July 5, 2026 15:34
… flag

The main-worker bootstrap wrapped the console into the V8 inspector
console only when an --inspect* flag was present at startup. When the
inspector is activated later at runtime - via node:inspector open() (as
the vscode-js-debug bootloader does) or SIGUSR1 - Runtime.consoleAPICalled
is never emitted, so debugger clients (e.g. the VS Code Debug Console)
show no console output even though the session is otherwise fully
functional. The worker bootstrap already wraps unconditionally; do the
same for the main worker. The bootstrap options slot (3) is left in
place, only its consumer is removed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Spec test for the previous commit: a --require preload opens the
inspector at runtime, the test connects a CDP session, enables the
Runtime domain and asserts consoleAPICalled events arrive for
console.log calls.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@wilkmaciej
wilkmaciej marked this pull request as ready for review July 7, 2026 10:42
wilkmaciej and others added 3 commits July 7, 2026 21:47
…tor_test.ts

Match fd6fc7a from denoland#35796: drop the hand-rolled spec test in favor of
a unit test next to the other inspector protocol tests, reusing the
extractWsUrl helper and cleaning up the child process in a finally block.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…f hanging

Race the wait against a 10s deadline (the child logs every 100ms) and
clear the timer in the teardown block. Verified both paths: passes in
~215ms, and with the fixture silenced fails at 10s with a diagnostic.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@bartlomieju

Copy link
Copy Markdown
Member

The failing node_compat (2/3) jobs (deterministic on every platform) are caused by this change, not flakiness. The offending test is test-console-methods.js:

error: Uncaught (in promise) AssertionError: Expected values to be strictly equal:
    assert.strictEqual(console[method].name, alternateNames[method]);
    at assertEqualName (.../test/parallel/test-console-methods.js:47:12)
1 failed of 1226

(The one test integration (2/2) release linux-x86_64 failure is unrelated — an apt-get network flake fetching packages.microsoft.com.)

Root cause: core.wrapConsole replaces each method with a bound function via FunctionPrototypeBind, whose .name becomes "bound callConsole". Previously the main worker only wrapped under --inspect*, so in a default run console.debug.name === "debug" and the test passed. Now that we wrap unconditionally, every method's name is "bound callConsole", which matches neither the method name nor its alternate, so the assertion fails.

This is exactly what Node hits, and they solve it by restoring the name right after binding (lib/internal/util/inspector.js, wrapConsole):

consoleFromNode[key] = FunctionPrototypeBind(
  consoleCall,
  consoleFromNode,
  consoleFromVM[key],
  consoleFromNode[key],
);
ObjectDefineProperty(consoleFromNode[key], 'name', {
  __proto__: null,
  value: key,
});

deno_core's wrapConsole is missing that ObjectDefineProperty(..., 'name', ...) call. (Worth noting Node itself wraps the console unconditionally whenever inspector support is compiled in — not gated on --inspect — so the direction here matches Node; it just needs the name restore.)

The fix belongs upstream in deno_core's 01_core.js wrapConsole:

customConsole[key] = FunctionPrototypeBind(
  callConsole,
  customConsole,
  consoleFromV8[key],
  customConsole[key],
);
ObjectDefineProperty(customConsole[key], "name", {
  __proto__: null,
  value: key,
});

Once deno_core carries that, the worker bootstrap (which already wraps unconditionally) and this change will both keep the correct method names.

…Console

core.wrapConsole binds each console method to callConsole, which clobbers
its .name to "bound callConsole" and, because callConsole was constructable,
made `new console.log()` succeed. With the main worker now wrapping the
console unconditionally this broke node_compat's test-console-methods.js.

Match Node's wrapConsole: restore the original method name after binding
(ObjectDefineProperty) and create callConsole with ConstructorBehavior::Throw
like Node's consoleCall, so bound methods throw when invoked with `new`.
@wilkmaciej

Copy link
Copy Markdown
Contributor Author

Thanks for the diagnosis - fixed in 4b82407. Since deno_core lives in this repo now (libs/core), the fix could go directly into this PR:

  • libs/core/01_core.js: wrapConsole restores each method's name right after the bind, exactly as Node does in lib/internal/util/inspector.js.
  • libs/core/runtime/bindings.rs: Deno.core.callConsole is now created with v8::ConstructorBehavior::Throw.

The second change is needed because the CI run only ever hit the .name assertion and died there - test-console-methods.js also asserts (line 59) that new console[method]() throws a TypeError. A bound function is a constructor iff its bind target is one, and callConsole was created via v8::Function::new, which defaults to ConstructorBehavior::Allow - so with just the name restored the test would have kept failing a few lines further down (verified against the old --inspect-gated wrap path: new console.log() succeeded there). Node's consoleCall is created with ConstructorBehavior::kThrow, so this part also just matches Node.

Verified locally: test-console-methods.js passes, as do the other 14 test-console-* tests enabled in config.jsonc and tests/unit/inspector_test.ts (including the regression test from this PR).

@bartlomieju
bartlomieju merged commit 5419c02 into denoland:main Jul 20, 2026
268 of 270 checks passed
bartlomieju pushed a commit that referenced this pull request Jul 23, 2026
… flag (#35795)

The main-worker bootstrap wrapped the console into the V8 inspector
console only when an `--inspect*` flag was present at startup. When the
inspector is activated later at runtime - via `node:inspector` `open()`
(as the vscode-js-debug bootloader does) or SIGUSR1 -
`Runtime.consoleAPICalled` is never emitted, so debugger clients (e.g.
the VS Code Debug Console) show no console output even though the
session is otherwise fully functional.

The worker bootstrap already wraps unconditionally; do the same for the
main worker. The bootstrap options slot (3) is left in place, only its
consumer is removed.

Includes a regression test (`inspector_console_api_late_open` in
`tests/unit/inspector_test.ts`, next to the other CDP protocol tests):
it spawns a child that opens the inspector via a `--require` preload,
connects over CDP, and asserts `Runtime.consoleAPICalled` arrives for
`console.log`. The wait is bounded by a 10 s deadline so a regression
fails with a diagnostic instead of hanging, and the child is killed in a
`finally` block.

Part of a series fixing VS Code debugging with `--watch` (#35791).

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
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