Skip to content

Fix CoreCLR browser-wasm native relink from a packaged SDK - #131646

Merged
akoeplinger merged 4 commits into
dotnet:mainfrom
pavelsavara:fix-coreclr-wasm-emcache
Jul 31, 2026
Merged

akoeplinger merged 4 commits into
dotnet:mainfrom
pavelsavara:fix-coreclr-wasm-emcache

Conversation

@pavelsavara

@pavelsavara pavelsavara commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Makes CoreCLR browser-wasm native relink work from a restored/packaged SDK (e.g. the daily wasm-tools workload). Found and validated end-to-end while diagnosing a failing native-relink benchmark build — after these fixes the CoreCLR app builds, loads in Chrome, and produces benchmark results.

Fix 1 — Set EM_CACHE for CoreCLR native relink

_CoreCLRSetupEmscripten in BrowserWasmApp.CoreCLR.targets populated EmscriptenEnvVars without EM_CACHE/EM_FROZEN_CACHE. Because FROZEN_CACHE is forced on by the dotnet emscripten config, emcc fell back to the (empty) cache dir inside the Emscripten SDK pack instead of the prebuilt sysroot that ships in the separate Emscripten Cache pack, and aborted:

Exception: FROZEN_CACHE is set, but cache file is missing: "sysroot_install.stamp"

The Mono path (BrowserWasmApp.targets) already sets these. Fix mirrors it, pointing EM_CACHE at $(WasmCachePath).

Fix 2 — Package coreclr_compat.h in the WebAssembly.Sdk pack

BrowserWasmApp.CoreCLR.targets force-includes $(MSBuildThisFileDirectory)coreclr_compat.h, but the header was never added to the pack's Sdk/ folder, so after Fix 1 the compile failed with coreclr_compat.h file not found. It's a build-time shim that lives next to the targets, so it's added to the PackageFile list in the SDK pack (not a runtime artifact → not in the platform manifest).

Fix 3 — Ship the VM/minipal headers in the CoreCLR runtime pack

The generated managed-to-native sources include <callhelpers.hpp>, <minipal/entrypoints.h> and <minipal/utils.h>. These were only resolvable from the in-repo source tree or via CORECLR_VM_WASM_INCLUDE_DIR / MINIPAL_INCLUDE_DIR overrides, so a restored SDK failed with 'callhelpers.hpp' file not found.

Mirroring how Mono ships its relink headers in the runtime pack (runtime.h/pinvoke.h/… under native/include/wasm, declared in the platform manifest):

  • Install the three headers into the CoreCLR browser-wasm runtime pack under native/include (callhelpers.hpp) and native/include/minipal (entrypoints.h, utils.h) via the browserhost CMakeLists.txt.
  • Declare them in the Microsoft.NETCore.App platform manifest (PlatformManifestFileEntry).
  • Resolve _VmWasmIncludeDir / _MinipalIncludeDir from the runtime pack native dir ($(MicrosoftNetCoreAppRuntimePackRidNativeDir)), keeping the in-repo fallback and env-var overrides intact.

Validation

Applied all edits to the restored SDK + runtime packs and ran native-relink build + measure end-to-end: the build completes, the CoreCLR wasm app loads in headless Chrome, and benchmarks run (Measurement complete: 1/1 succeeded). Each fix individually moved the build past its corresponding error (FROZEN_CACHE → coreclr_compat.h → callhelpers.hpp → success), with the headers resolved from the runtime pack native/include.

Related: #128362

The CoreCLR wasm _CoreCLRSetupEmscripten target populated EmscriptenEnvVars without EM_CACHE/EM_FROZEN_CACHE. With FROZEN_CACHE forced on by the emscripten config, emcc fell back to the empty cache dir inside the Emscripten SDK pack and failed with 'FROZEN_CACHE is set, but cache file is missing: sysroot_install.stamp'. Point EM_CACHE at WasmCachePath (the prebuilt Emscripten Cache pack), mirroring the Mono BrowserWasmApp.targets.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI 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

Updates the CoreCLR browser-wasm native relink (native-relink) MSBuild targets to align Emscripten cache environment variable setup with the existing Mono path, ensuring emcc uses the intended cache location.

Changes:

  • Add EM_CACHE to CoreCLR’s EmscriptenEnvVars so emcc uses $(WasmCachePath) when available.
  • Add EM_FROZEN_CACHE to CoreCLR’s EmscriptenEnvVars, matching the existing conditional behavior used by BrowserWasmApp.targets.

BrowserWasmApp.CoreCLR.targets force-includes \coreclr_compat.h, but the header was never added to the pack's Sdk/ folder, so CoreCLR native relink from a restored SDK failed with 'coreclr_compat.h file not found'. Add it to the PackageFile list next to the targets.
Copilot AI review requested due to automatic review settings July 31, 2026 14:29
@pavelsavara pavelsavara changed the title Set EM_CACHE for CoreCLR browser-wasm native relink Fix CoreCLR browser-wasm native relink from a packaged SDK (EM_CACHE + coreclr_compat.h) Jul 31, 2026
The generated managed-to-native sources include callhelpers.hpp, minipal/entrypoints.h and minipal/utils.h. These were only resolvable from the in-repo source tree or via env-var overrides, so native relink from a restored SDK failed with file-not-found. Package the three headers next to the targets (Sdk and Sdk/minipal) and make the include-dir fallbacks prefer that packaged location when present.
@pavelsavara pavelsavara changed the title Fix CoreCLR browser-wasm native relink from a packaged SDK (EM_CACHE + coreclr_compat.h) Fix CoreCLR browser-wasm native relink from a packaged SDK Jul 31, 2026

Copilot AI 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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/mono/browser/build/BrowserWasmApp.CoreCLR.targets:497

  • BrowserWasmApp.targets ensures the EM_CACHE directory exists (via a MakeDir) before invoking emcc. The CoreCLR targets now set EM_CACHE too, but never create the directory. If WasmCachePath is overridden to a non-existent location, emcc can fail when trying to use/populate the cache. Consider mirroring the MakeDir behavior here for parity and robustness.
      <EmscriptenEnvVars Include="EM_CACHE=$(WasmCachePath)" Condition="'$(WasmCachePath)' != ''" />
      <EmscriptenEnvVars Include="EM_FROZEN_CACHE=1" Condition="'$(WasmCachePath)' == '$(EmscriptenCacheSdkCacheDir)'" />
      <EmscriptenEnvVars Include="EM_FROZEN_CACHE=0" Condition="'$(WasmCachePath)' != '' and '$(WasmCachePath)' != '$(EmscriptenCacheSdkCacheDir)'" />

Copilot AI review requested due to automatic review settings July 31, 2026 14:39

Copilot AI 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

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

Reworks the header shipping to mirror Mono: callhelpers.hpp and minipal/entrypoints.h + utils.h are installed into the CoreCLR browser-wasm runtime pack under native/include, declared in the NetCore.App platform manifest, and resolved by the targets from the runtime pack native dir. coreclr_compat.h stays in the WebAssembly.Sdk pack since it is a build-time shim next to the targets.
Copilot AI review requested due to automatic review settings July 31, 2026 15:04

Copilot AI 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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/mono/browser/build/BrowserWasmApp.CoreCLR.targets:568

  • The Exists(...) path uses backslashes (include\minipal\utils.h). On non-Windows builds MSBuild typically treats \ as a literal character (not a directory separator), so this check can fail and prevent using the runtime-pack include dir even when the file is present. Use $([MSBuild]::NormalizePath(...)) (or forward slashes) to build an OS-correct path for Exists.
      <!-- Restored SDK: headers ship in the runtime pack under native/include/minipal. -->
      <_MinipalIncludeDir Condition="'$(_MinipalIncludeDir)' == '' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)include\minipal\utils.h')">$([MSBuild]::NormalizeDirectory('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)', 'include', 'minipal'))</_MinipalIncludeDir>
      <_MinipalIncludeDir Condition="'$(_MinipalIncludeDir)' == ''">$([MSBuild]::NormalizeDirectory('$(MSBuildThisFileDirectory)..', '..', '..', 'native', 'minipal'))</_MinipalIncludeDir>

src/mono/browser/build/BrowserWasmApp.CoreCLR.targets:573

  • Same issue as above: the Exists(...) check uses a backslash path (include\callhelpers.hpp), which can be treated as a literal on Unix/macOS and make the condition false even when the header exists in the runtime pack. Build the path via $([MSBuild]::NormalizePath(...)) so this works cross-platform.
      <!-- Restored SDK: callhelpers.hpp ships in the runtime pack under native/include. -->
      <_VmWasmIncludeDir Condition="'$(_VmWasmIncludeDir)' == '' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)include\callhelpers.hpp')">$([MSBuild]::NormalizeDirectory('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)', 'include'))</_VmWasmIncludeDir>
      <_VmWasmIncludeDir Condition="'$(_VmWasmIncludeDir)' == ''">$([MSBuild]::NormalizeDirectory('$(MSBuildThisFileDirectory)..', '..', '..', 'coreclr', 'vm', 'wasm'))</_VmWasmIncludeDir>

@pavelsavara
pavelsavara marked this pull request as ready for review July 31, 2026 15:13
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@pavelsavara pavelsavara added the arch-wasm WebAssembly architecture label Jul 31, 2026
@pavelsavara pavelsavara added this to the 11.0.0 milestone Jul 31, 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.

@akoeplinger

Copy link
Copy Markdown
Member

/ba-g unrelated failure on openbsd

@akoeplinger
akoeplinger merged commit 9c1812d into dotnet:main Jul 31, 2026
111 of 113 checks passed
<PackageFile Include="$(BrowserProjectRoot)build\BrowserWasmApp.props" TargetPath="Sdk" />
<PackageFile Include="$(BrowserProjectRoot)build\BrowserWasmApp.targets" TargetPath="Sdk" />
<PackageFile Include="$(BrowserProjectRoot)build\BrowserWasmApp.CoreCLR.targets" TargetPath="Sdk" />
<PackageFile Include="$(BrowserProjectRoot)build\coreclr_compat.h" TargetPath="Sdk" />

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.

I would expect this in the same place as callhelpers, etc

@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-rc1 Aug 2, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 2, 2026
@pavelsavara
pavelsavara deleted the fix-coreclr-wasm-emcache branch September 30, 2026 14:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-wasm WebAssembly architecture area-Build-mono

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants