Skip to content

[browser][coreCLR] running main/exit/abort/logging and infra for unit testing - #122646

Merged
pavelsavara merged 14 commits into
dotnet:mainfrom
pavelsavara:browser_diag
Dec 30, 2025
Merged

[browser][coreCLR] running main/exit/abort/logging and infra for unit testing#122646
pavelsavara merged 14 commits into
dotnet:mainfrom
pavelsavara:browser_diag

Conversation

@pavelsavara

@pavelsavarapavelsavara commented Dec 18, 2025

Copy link
Copy Markdown
Member
  • Added dotnet.runMainAndExit() and dotnet.runMain() APIs. For Mono too.
  • make dotnet.run() to not call exit() and keep runtime alive, only for CoreCLR
    • This will be breaking change for CoreCLR vs Mono.
    • Because previous behavior was just confusing everyone.
    • Make it deprecated and point to runMain()
  • new internal MSbuild property <WasmTestSupport>true</WasmTestSupport> which deploys dotnet.diagnostics.js
  • load dotnet.diagnostics.js early when available in the boot config
  • add internal support for unit testing via dotnet.diagnostics.js
    • skeleton of symbolicateStackTrace
    • exitOnUnhandledError
    • appendElementOnExit
    • logExitCode
    • asyncFlushOnExit
    • forwardConsole
  • new internal MSbuild properties WasmTestExitOnUnhandledError, WasmTestAppendElementOnExit, WasmTestLogExitCode, WasmTestAsyncFlushOnExit, WasmTestForwardConsole
  • set WasmTestSupport for library tests and WBT
  • set -sEXIT_RUNTIME=1 - this enables C++ global destructors
  • implemented abortStartup(), abortTimers(), abortPosix()
  • implemented isExited(), isRuntimeRunning()
  • invoke LoadBootResourceCallback for Blazor
  • invoke Module.onConfigLoaded and Module.onDotnetReady
  • captures ExitStatus type
  • capture posix/emscripten abort()/exit() events
  • fixed locateFile - for query string propagation
  • hides BrowserHost_InitializeCoreCLR, BrowserHost_ExecuteAssembly from Module exports
  • reporting not found assemblies to debug log

Fixes#101169
Fixes#75335
On top of #122616
On top of #122495

@pavelsavarapavelsavara added this to the 11.0.0 milestone Dec 18, 2025
@pavelsavarapavelsavara self-assigned this Dec 18, 2025
@pavelsavarapavelsavara added arch-wasm WebAssembly architecture area-Host os-browser Browser variant of arch-wasm labels Dec 18, 2025
@pavelsavara
pavelsavaraforce-pushed the browser_diag branch 3 times, most recently from 6b91c6a to 3a18853CompareDecember 19, 2025 21:03
@pavelsavara
pavelsavara marked this pull request as ready for review December 22, 2025 12:21
CopilotAI review requested due to automatic review settings December 22, 2025 12:21

CopilotAI 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.

Pull request overview

This PR adds comprehensive exit/abort/logging infrastructure for unit testing in browser/CoreCLR environments, enabling better diagnostics and test harness integration.

Key Changes:

  • Introduces new test instrumentation via dotnet.diagnostics.js module with exit handlers, console forwarding, and symbolication skeleton
  • Breaking API change: dotnet.run() now keeps the runtime alive; new dotnet.runAndExit() method exits runtime after Main() completes
  • Adds internal MSBuild properties (WasmTestSupport, WasmTestExitOnUnhandledError, etc.) to control test behavior and diagnostics deployment

Reviewed changes

Copilot reviewed 37 out of 38 changed files in this pull request and generated 2 comments.

Show a summary per file
FileDescription
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.csAdds five new boolean properties for test instrumentation flags, gated by version 11.0+
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonData.csDefines the five new test instrumentation properties in boot config JSON schema
src/native/libs/System.Native.Browser/utils/index.tsReplaces exit export with abortTimers and abortPosix for better separation of concerns
src/native/libs/System.Native.Browser/utils/host.tsRemoves old exit() function, adds abortTimers() and abortPosix() for cleanup
src/native/libs/System.Native.Browser/native/cross-linked.tsDeclares global ABORT and EXITSTATUS variables for emscripten integration
src/native/libs/System.Native.Browser/diagnostics/types.tsNew file defining types for marshaling, diagnostics exports, and measured blocks
src/native/libs/System.Native.Browser/diagnostics/symbolicate.tsSkeleton implementation of stack trace symbolication (placeholder for future work)
src/native/libs/System.Native.Browser/diagnostics/per-module.tsRe-exports common per-module definitions for diagnostics
src/native/libs/System.Native.Browser/diagnostics/index.tsMain entry point for diagnostics module initialization
src/native/libs/System.Native.Browser/diagnostics/exit.tsImplements exit handlers, unhandled error handlers, and exit code logging
src/native/libs/System.Native.Browser/diagnostics/cross-module.tsRe-exports common cross-module definitions for diagnostics
src/native/libs/System.Native.Browser/diagnostics/console-proxy.tsImplements console forwarding to WebSocket for test runners
src/native/libs/Common/JavaScript/types/public-api.tsUpdates API: removes withConfigSrc() docs, adds run() and runAndExit() with clear behavior distinction
src/native/libs/Common/JavaScript/types/internal.tsAdds DiagnosticsExportsTable to internal exchange, updates asset entry fields, adds OnExitListener type
src/native/libs/Common/JavaScript/types/exchange.tsAdds diagnostics exports, exit lifecycle functions, and additional browser host exports
src/native/libs/Common/JavaScript/cross-module/index.tsIntegrates diagnostics exports into cross-module exchange system
src/native/libs/Common/JavaScript/CMakeLists.txtAdds new diagnostics TypeScript source files to build
src/native/corehost/browserhost/loader/run.tsMajor refactoring: moves runtime creation logic here, adds createRuntime() and abortStartup(), improved initializeCoreCLR()
src/native/corehost/browserhost/loader/lib-initializers.tsAdds comment clarifying expected function names
src/native/corehost/browserhost/loader/index.tsRegisters exit handlers, adds exit-related exports, changes default runtimeId to undefined
src/native/corehost/browserhost/loader/host-builder.tsImplements new run() (keeps runtime alive) and runAndExit() methods, exports loadBootResourceCallback
src/native/corehost/browserhost/loader/exit.tsComplete rewrite with runtimeState, exit listeners, Emscripten integration, proper exit/abort handling
src/native/corehost/browserhost/loader/dotnet.d.tsUpdates type definitions for run() and runAndExit() methods, removes withConfigSrc()
src/native/corehost/browserhost/loader/config.tsRenames netLoaderConfig to loaderConfig for consistency
src/native/corehost/browserhost/loader/bootstrap.tsFixes locateFile() to properly handle query strings for modules vs. assets
src/native/corehost/browserhost/loader/assets.tsMoves runtime creation to run.ts, implements Blazor loadBootResourceCallback support, adds proper cache/integrity handling
src/native/corehost/browserhost/libBrowserHost.footer.jsHides BrowserHost_InitializeCoreCLR and BrowserHost_ExecuteAssembly from Module exports
src/native/corehost/browserhost/host/index.tsExports getExitStatus and initializeCoreCLR functions
src/native/corehost/browserhost/host/host.tsImplements getExitStatus(), initializeCoreCLR(), improves assembly probing with debug logging, refactors runMain() and runMainAndExit() error handling
src/native/corehost/browserhost/host/cross-linked.tsDeclares _BrowserHost_InitializeCoreCLR, _BrowserHost_ExecuteAssembly, and ExitStatus globals
src/native/corehost/browserhost/CMakeLists.txtRemoves specific functions from exported list, relying on generic export configuration
src/mono/wasm/testassets/WasmBrowserRunMainOnly/wwwroot/main.jsSimplifies to single runAndExit() call instead of manual error handling
src/mono/wasm/testassets/WasmBrowserRunMainOnly/WasmBrowserRunMainOnly.csprojEnables all test instrumentation flags
src/mono/wasm/Wasm.Build.Tests/Common/BuildEnvironment.csEnables WasmTestSupport for CoreCLR builds
src/mono/sample/wasm/Directory.Build.targetsAdds test support properties to nested build
src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targetsWires up test instrumentation properties to boot config generation, conditionally includes diagnostics module
src/mono/browser/test-main.jsChanges default forwardConsole to only enable on Firefox (not for other browsers in debug mode)
eng/testing/tests.browser.targetsEnables WasmTestSupport for CoreCLR library tests

Comment threadsrc/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonData.cs Outdated
@pavelsavarapavelsavara changed the title [browser][coreCLR] exit/abort/logging infra for unit testing[browser][coreCLR] running main/exit/abort/logging and infra for unit testingDec 23, 2025

@javiercnjaviercn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me. Is it fair to assume that most of the testing is essentially existing tests continue to pass?

Comment threadsrc/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js Outdated
Comment threadsrc/native/corehost/browserhost/host/host.ts Outdated
Comment threadsrc/native/corehost/browserhost/loader/bootstrap.ts
@pavelsavara

Copy link
Copy Markdown
MemberAuthor

/ba-g unrelated failures

@pavelsavara
pavelsavara merged commit 3cacfdd into dotnet:mainDec 30, 2025
161 of 164 checks passed
@pavelsavara
pavelsavara deleted the browser_diag branch December 30, 2025 21:19
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Jan 30, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-wasmWebAssembly architecturearea-Hostos-browserBrowser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[browser] move console proxy from runtime code into test-main [wasm] Better communication between apphost and browser

3 participants

@pavelsavara@javiercn