Skip to content

[browser][wasm] Fix CoreCLR Emscripten out/err override#127315

Draft
pavelsavara wants to merge 3 commits intodotnet:mainfrom
pavelsavara:fix/coreclr-emscripten-out-err-override
Draft

[browser][wasm] Fix CoreCLR Emscripten out/err override#127315
pavelsavara wants to merge 3 commits intodotnet:mainfrom
pavelsavara:fix/coreclr-emscripten-out-err-override

Conversation

@pavelsavara
Copy link
Copy Markdown
Member

Summary

Bridge user-provided out/err overrides to Emscripten's print/printErr in the CoreCLR browser WASM loader, matching Mono's existing behavior.

Note

This PR was AI/Copilot-generated.

Problem

When using dotnet.withModuleConfig({ out: fn, err: fn }) with the CoreCLR runtime on browser WASM, the custom output handlers were not being called. The Mono runtime handled this correctly via configureRuntimeStartup() in startup.ts, but the CoreCLR loader had no equivalent bridging logic.

Changes

  • src/native/libs/Common/JavaScript/types/internal.ts: Added print and printErr to EmscriptenModuleInternal type
  • src/native/libs/Common/JavaScript/loader/run.ts: Wire Module.out/Module.err to Module.print/Module.printErr before the native module loads, so Emscripten picks them up during initialization
  • src/mono/wasm/Wasm.Build.Tests/ModuleConfigTests.cs: Removed Mono-only restriction from OutErrOverrideWorks test so it also runs for CoreCLR

Testing

Wasm.Build.Tests.ModuleConfigTests — Total: 5, Errors: 0, Failed: 0, Skipped: 0 (with RuntimeFlavor=CoreCLR)

Fixes #124945

Bridge user-provided out/err overrides to Emscripten's print/printErr
in the CoreCLR loader, matching Mono's existing behavior.

- Add print/printErr to EmscriptenModuleInternal type
- Wire Module.out/err to Module.print/printErr before native module loads
- Enable OutErrOverrideWorks test for CoreCLR

Fixes dotnet#124945
Copilot AI review requested due to automatic review settings April 23, 2026 10:07
@pavelsavara pavelsavara added arch-wasm WebAssembly architecture os-browser Browser variant of arch-wasm labels Apr 23, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Bridges user-supplied Module.out/Module.err overrides to Emscripten’s print/printErr in the CoreCLR browser WASM loader so custom output handlers are honored during Emscripten initialization (matching Mono behavior).

Changes:

  • Extend CoreCLR loader’s internal Emscripten module typing to include print/printErr.
  • In CoreCLR loader startup, map Module.outModule.print and Module.errModule.printErr before the native module initializes.
  • Enable the existing OutErrOverrideWorks WASM build test for CoreCLR by removing the Mono-only conditional.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/native/libs/Common/JavaScript/types/internal.ts Adds print/printErr to the internal module type to support bridging code.
src/native/libs/Common/JavaScript/loader/run.ts Wires out/err to print/printErr before native module load so Emscripten picks them up.
src/mono/wasm/Wasm.Build.Tests/ModuleConfigTests.cs Runs OutErrOverrideWorks for CoreCLR as well (no longer Mono-only).

Comment thread src/native/libs/Common/JavaScript/loader/run.ts
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 23, 2026 22:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a behavioral gap in the CoreCLR browser WASM loader by ensuring user-provided out / err overrides (passed via dotnet.withModuleConfig) are forwarded to Emscripten’s print / printErr, aligning CoreCLR behavior with the existing Mono runtime behavior.

Changes:

  • Extend CoreCLR’s internal Emscripten module typing to include print / printErr.
  • In the CoreCLR JS loader, map Module.out/Module.err to Module.print/Module.printErr before the native module initializes.
  • Enable the existing OutErrOverrideWorks WASM build test to run for CoreCLR (not Mono-only).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/native/libs/Common/JavaScript/types/internal.ts Adds internal typing for print / printErr on the Emscripten module shape.
src/native/libs/Common/JavaScript/loader/run.ts Bridges out/err to print/printErr early during runtime creation.
src/mono/wasm/Wasm.Build.Tests/ModuleConfigTests.cs Removes Mono-only guard so the out/err override test executes for CoreCLR too.

Comment on lines +40 to +51
if (!Module.out) {
// eslint-disable-next-line no-console
Module.out = console.log.bind(console);
}
if (!Module.err) {
// eslint-disable-next-line no-console
Module.err = console.error.bind(console);
}
if (!Module.print) {
Module.print = Module.out;
}
if (!Module.printErr) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-System.Runtime.InteropServices.JavaScript os-browser Browser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[coreclr] Emscripten out override doesn't work

2 participants