Skip to content

[Browser][CoreCLR] publish+workload should not trigger native relink unless there is new UCO/PInvoke #132777

Open

Description

@pavelsavara

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 (emcc relink).
  • The closure scan surfaces WASM0066 for every foreign-platform DllImport in 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:

  1. Add a cheap detection pass over the app's own assemblies (not the whole framework closure) for:
    • [UnmanagedCallersOnly] / [JSExport] callbacks, and
    • [WasmImportLinkage] P/Invokes.
  2. Gate WasmBuildNative on (that detection) OR (NativeFileReference/NativeLibrary present). The NativeFileReference / NativeLibrary trigger already exists; this replaces the coarse Configuration == Release heuristic 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.targets has the same shape and should get the same treatment.

Note

This issue was drafted with GitHub Copilot.

Activity

  1. self-assigned this
    on Aug 26, 2026
  2. added
    arch-wasm
    on Aug 26, 2026
  3. added
    untriaged
    on Aug 26, 2026
  4. dotnet-policy-service commented on Aug 26, 2026

    @dotnet-policy-service
    Contributor

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

  5. added this to the 12.0.0 milestone on Aug 26, 2026
  6. pavelsavara commented on Aug 26, 2026

    @pavelsavara
    MemberAuthor
  7. removed
    untriaged
    on Aug 26, 2026
  8. maraf commented on Aug 27, 2026

    @maraf
    Member

    Trimming can result in fewer PInvokes/UCOs and thus relink can reduce the size. Isn't that desired?

    But the relink only bakes app-contributed reverse thunks / module imports.

    Is this different from Mono?

  9. pavelsavara commented on Aug 27, 2026

    @pavelsavara
    MemberAuthor

    Trimming can result in fewer PInvokes/UCOs and thus relink can reduce the size. Isn't that desired?

    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.wasm already should not contain PInvoke into POSIX/socks for example.

    Then IL trimming on app level could further remove stuff. Maybe your app never calls Console and 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.

    But the relink only bakes app-contributed reverse thunks / module imports.

    Is this different from Mono?

    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.

  10. modified the milestones: 12.0.0, Future on Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

arch-wasmarea-Build-mono

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions