Repository navigation
Description
Problem
For a trimmed Release nested publish (all library-test legs, and any published Browser CoreCLR app), WasmBuildNative defaults to true in _CoreCLRSetWasmBuildNativeDefaults (src/mono/browser/build/BrowserWasmApp.CoreCLR.targets):
<PropertyGroup Condition="'$(WasmBuildingForNestedPublish)' == 'true'">
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(PublishTrimmed)' != 'true'">false</WasmBuildNative>
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(Configuration)' == 'Release'">true</WasmBuildNative>
</PropertyGroup>This triggers a full emcc relink of dotnet.native.wasm and the ManagedToNativeGenerator scan of the entire bundled managed closure.
But the relink only bakes app-contributed reverse thunks / module imports. The runtime pack already ships every framework [UnmanagedCallersOnly] reverse thunk and P/Invoke table. So when the app adds:
- no
[UnmanagedCallersOnly]/[JSExport]callbacks, - no
[WasmImportLinkage]P/Invokes, and - no
NativeFileReference/NativeLibrary,
…the relinked dotnet.native.wasm is effectively identical to the pack's, and both the relink and the scan are wasted work.
Side effects of the unnecessary relink
- Large build-time cost per test/app (
emccrelink). - The closure scan surfaces
WASM0066for every foreign-platformDllImportin the closure (kernel32,ntdll,advapi32,Security.framework,libsecret,libc, …). These are correctly handled at runtime (throw-if-called), but fail the build under warn-as-error.
Proposed solution
Make the relink demand-driven:
- Add a cheap detection pass over the app's own assemblies (not the whole framework closure) for:
[UnmanagedCallersOnly]/[JSExport]callbacks, and[WasmImportLinkage]P/Invokes.
- Gate
WasmBuildNativeon(that detection) OR (NativeFileReference/NativeLibrary present). TheNativeFileReference/NativeLibrarytrigger already exists; this replaces the coarseConfiguration == Releaseheuristic with a real "does the app add native surface?" signal.
The detection can reuse PInvokeCollector restricted to the app's assemblies, or a lighter attribute-only metadata check — scanning only the app assemblies is sufficient because the pack already contains all framework thunks.
Where
src/mono/browser/build/BrowserWasmApp.CoreCLR.targets—_CoreCLRSetWasmBuildNativeDefaults,_CoreCLRGenerateManagedToNative.src/tasks/WasmAppBuilder/coreclr/PInvokeCollector.cs.- The WASI sibling
src/mono/wasi/build/WasiApp.CoreCLR.targetshas the same shape and should get the same treatment.
Note
This issue was drafted with GitHub Copilot.
Activity
dotnet-policy-service commentedon Aug 26, 2026
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.
pavelsavara commentedon Aug 26, 2026
cc @maraf
maraf commentedon Aug 27, 2026
Trimming can result in fewer PInvokes/UCOs and thus relink can reduce the size. Isn't that desired?
Is this different from Mono?
pavelsavara commentedon Aug 27, 2026
That was what @lewing hinted at yesterday.
IL trimming helps and that already happens when we build browser specific runtime pack.
And then the native build of default
dotnet.native.wasmalready should not contain PInvoke into POSIX/socks for example.Then IL trimming on app level could further remove stuff. Maybe your app never calls
Consoleand so we trim related PInvoke.And that allows emscripten to link out
stdout.In reality, IL trimming is not perfect and stdout is probably used by emscipten itself.
No, Mono does the same thing, but I have not think it thru before.
I will move this to Future and we can reconsider/revive this later.
[browser][coreclr] Support WasmRunWasmOpt for ReadyToRun images (#136772