Skip to content

[browser][coreclr] Fix stale R2R prune deleting fresh images with unnormalized intermediate paths - #137229

Merged
lewing merged 1 commit into
mainfrom
lewing-wasm-r2r-prune-path-normalization
Nov 10, 2026
Merged

lewing merged 1 commit into
mainfrom
lewing-wasm-r2r-prune-path-normalization

Conversation

@lewing

@lewing lewing commented Nov 9, 2026

Copy link
Copy Markdown
Member

_WasmCoreClrPruneStalePerAppR2R (runs AfterTargets=_CreateR2RImages) globs $(_WasmPublishR2RDir)*.wasm;*.dll and excludes what crossgen2 just emitted, taken from %(OutputR2RImage). _WasmPublishR2RDir is normalized (NormalizeDirectory), but OutputR2RImage keeps any .. segments from $(IntermediateOutputPath). Exclude compares the strings literally, so when a project's intermediate path is unnormalized (for example, UseArtifactsOutput=true with <ArtifactsPath>$(MSBuildThisFileDirectory)../artifacts</ArtifactsPath>), nothing is excluded and the target deletes the images it was meant to keep:

  • Non-composite: publish succeeds, but every assembly ships as plain IL webcil with no R2R code.
  • Composite: publish fails with error : No file exists for the asset at either location '.../R2R/<Entry>.r2r.wasm'. The component stubs survive because they're built from the normalized directory.

This was found while publishing richlander/dotnet-inspect's src/DotnetInspect.Web.

Fix

Exclude by %(FullPath), which collapses .. segments, so the comparison matches the normalized glob.

I checked the other OutputR2RImage-derived comparisons in the pack and in BrowserWasmApp.CoreCLR.targets. None has the same problem:

  • The ResolvedFileToPublish Remove operations compare against the same SDK-provided strings on both sides.
  • The %(RootDir)%(Directory) checks are derived from the full path.

Testing

I added ReadyToRunTests.PublishWithUnnormalizedIntermediatePathKeepsReadyToRunImages for composite and non-composite publishes. It sets IntermediateOutputPath to $(MSBuildProjectDirectory)/obj/../obj/$(Configuration)/, which resolves to the default location but leaves a literal .. in the path.

Local run on osx-arm64, after ./build.sh -s clr+libs+packs -os browser -c Release /p:BuildCrossgen2HostPackForWorkloadTesting=true:

./dotnet.sh build src/mono/wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj -c Release -t:Test -p:TargetOS=browser -p:TargetArchitecture=wasm -p:RuntimeFlavor=CoreCLR -p:TestUsingWorkloads=false -p:XUnitMethodName=Wasm.Build.Tests.ReadyToRunTests.PublishWithUnnormalizedIntermediatePathKeepsReadyToRunImages
  • With the fix: Total: 2, Failed: 0.
  • Without the fix (the old targets swapped into the locally built pack nupkg): Total: 2, Failed: 2. The non-composite case reports "Expected a ReadyToRun table in .../System.Private.CoreLib...", and the composite case reports "No file exists for the asset", matching the symptoms above.

Note

This PR was generated with the assistance of GitHub Copilot.

_WasmCoreClrPruneStalePerAppR2R globs the normalized _WasmPublishR2RDir and
excludes the images crossgen2 emitted, taken from %(OutputR2RImage). That
metadata keeps any '..' segments from IntermediateOutputPath (for example an
ArtifactsPath of $(MSBuildThisFileDirectory)../artifacts), and Exclude compares
the strings literally, so nothing was excluded and the target deleted the
fresh images. Non-composite publishes then shipped plain IL; composite
publishes failed with "No file exists for the asset".

Exclude by %(FullPath) instead, and add a Wasm.Build.Tests regression test
that publishes with an IntermediateOutputPath containing '..'.

Co-authored-by: Copilot App <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 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.

@lewing
lewing requested a balanced review from Copilot November 10, 2026 00:03
@lewing
lewing marked this pull request as ready for review November 10, 2026 00:03
@lewing lewing added the arch-wasm WebAssembly architecture label Nov 10, 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.

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.

🟢 Approval recommended

No active review issues remain for the intermediate-path normalization fix and its regression coverage.

No open issues 🎉

What changed in this PR

This PR fixes CoreCLR browser-wasm publishing when intermediate paths contain .. segments, preventing stale-image cleanup from deleting freshly generated ReadyToRun images.

Changes:

  • Uses normalized FullPath metadata to exclude current images from stale-file deletion.
  • Adds regression coverage for composite and non-composite publishing with an unnormalized intermediate path.
File Description
src/​mono/​wasm/​Wasm.Build.Tests/​ReadyToRunTests.cs Adds a parameterized regression test verifying ReadyToRun images survive publishing.
src/​mono/​nuget/​Microsoft.NET.Sdk.WebAssembly.Pack/​build/​Microsoft.NET.Sdk.WebAssembly.Browser.CoreCLR.targets Normalizes exclusion paths and documents why normalization is necessary.

Review effort: Balanced

@lewing
lewing enabled auto-merge (squash) November 10, 2026 01:25
@maraf

maraf commented Nov 10, 2026

Copy link
Copy Markdown
Member

/ba-g Failures are unrelated. The Windows CoreCLR browser-wasm work item WBT-NoWorkload-CLR-Wasm.Build.Tests in build 1671953 was dead-lettered by Helix, with synthetic exit code -1 and no uploaded test results. Its console contains only the generic dead-letter explanation, not a test assertion failure. The corresponding Linux work item completed successfully with exit code 0.

Note

This comment was generated with the assistance of GitHub Copilot.

@lewing
lewing merged commit 275ff40 into main Nov 10, 2026
56 of 58 checks passed
@lewing
lewing deleted the lewing-wasm-r2r-prune-path-normalization branch November 10, 2026 09:58
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 11, 2026
maraf pushed a commit that referenced this pull request Nov 12, 2026
…ILLink (#137394)

`ReadyToRunTests.PublishWithUnnormalizedIntermediatePathKeepsReadyToRunImages`
(added in #137229) fails on every run in the CoreCLR no-workload WBT
lane on Windows (for example, builds 1675602, 1675474 and 1676088):

```
Did not expect to find any .dll in ...\App\bin\Release\net12.0\publish but found ...\publish\BlazorBasicTestApp.dll, ...\publish\Microsoft.AspNetCore.Components.dll, ...
```

## Cause

`_WasmFeedReadyToRunCompileList` adds the trimmed closure again from the
glob `$(IntermediateLinkDir)*.dll`. ILLink adds the same files as
`$(IntermediateLinkDir)<name>`. A glob rewrites an unnormalized
directory: it collapses `//`, and on Windows it flips `/` to `\`. The
Helix binlog shows both spellings of each assembly:

```
App/obj/../obj/Release/net12.0\linked\Microsoft.AspNetCore.Components.Web.dll   (ILLink)
App\obj\..\obj\Release\net12.0\linked\Microsoft.AspNetCore.Components.Web.dll   (glob)
```

`ComputeWasmPublishAssets` treats the two as duplicates, but
`ResolvedFileToPublish Remove="@(_PublishResolvedFilesToRemove)"`
removes only ILLink's spelling. The glob copy is then copied loose into
the publish root. Composite publishes fail the same way. On Unix,
MSBuild already rewrites ILLink's `linked\` property value, so the two
spellings match and Linux CI passes.

## Fix

Pass the glob results through the same
`$(IntermediateLinkDir)%(Filename)%(Extension)` transform that ILLink
uses, so both items are spelled identically. With a normalized path, the
result is the same string as before.

The test now also sets `IntermediateLinkDir` to
`$(IntermediateOutputPath)$(TargetFramework)/linked/`, with `//` in
`IntermediateOutputPath`. Because the value contains no backslash,
MSBuild keeps the `//` on Unix, so the bug also reproduces on Linux and
macOS, not only on Windows.

## Testing

Validated on osx-arm64 after `./build.sh -s clr+libs+packs -os browser
-c Release /p:BuildCrossgen2HostPackForWorkloadTesting=true`, running:

```
./dotnet.sh build src/mono/wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj -c Release -t:Test -p:TargetOS=browser -p:TargetArchitecture=wasm -p:RuntimeFlavor=CoreCLR -p:TestUsingWorkloads=false -p:XUnitMethodName=Wasm.Build.Tests.ReadyToRunTests.PublishWithUnnormalizedIntermediatePathKeepsReadyToRunImages
```

- Without the targets fix: Total 2, Failed 2, with the same message as
CI.
- With the fix: Total 2, Failed 0.
- Whole `ReadyToRunTests` class with the fix
(`-p:XUnitClassName=Wasm.Build.Tests.ReadyToRunTests`): Total 31, Failed
2.
- The two failures are `PublishRunAllPages(Release, trimmed: True)` and
`PublishRunAllPagesComposite(Release, trimmed: True, nativeRelink:
False)`. Both report "Duplicate assemblies staged" after the incremental
second publish.
- Without the fix, `PublishRunAllPages(Release, trimmed: True)` fails
the same way locally, and both pass on Linux CI, so these failures
predate this change on macOS.

The code comments were shortened after this validation; the code is the
same.

> [!NOTE]
> This PR description was generated with the assistance of GitHub
Copilot.

---------

Co-authored-by: Copilot App <[email protected]>
lewing added a commit that referenced this pull request Nov 12, 2026
#137360)

This makes
`ReadyToRunTests.PublishCrossgen2ArgumentsReflectWasmDebugInfoProperties`
(added in #136506) much cheaper while keeping its coverage.

## Why

The Windows Helix work item `WBT-NoWorkload-CLR-Wasm.Build.Tests` keeps
hitting its 90-minute timeout. #137291 removed `-v:diag` from this test,
but that only shrank the logs. On #137291's own CI (Helix job
`96967939-699a-48d4-a0d9-ab808c96a280`) the work item still ran 1h34m
and timed out with exit -3.

In the last passing slow run (Helix job
`a5e1304b-0299-470a-a3ce-9a2289a5b925`), this test's 12 cases took
**1464 s**. Each case runs a full per-app or composite ReadyToRun
publish (~120 s), just to read back `Crossgen2ExtraCommandLineArgs`.

The flags under test (`--wasm-debug-info=*`, `--strip-inlining-info`,
`--strip-debug-info`, `--opt-cross-module:*`) are all set in static
`PropertyGroup`s, in
`Microsoft.NET.Sdk.WebAssembly.Browser.CoreCLR.targets` and
`Microsoft.NET.CrossGen.targets`. `RunReadyToRunCompiler` receives
`$(PublishReadyToRunCrossgen2ExtraArgs);$(_PublishReadyToRunCrossgen2ExtraArgs)`,
and none of the targets touch these flags. In a CI binlog of the old
test (Release, per-app, `DebuggerSupport=true`), the evaluated property
was `;--wasm-debug-info=none;--opt-cross-module:*`. The task received
`;--wasm-debug-info=none;--opt-cross-module:*;`, the same value plus the
empty underscore property.

## Change

- `PublishCrossgen2ArgumentsReflectWasmDebugInfoProperties` now runs
`dotnet msbuild -t:Restore` and then `dotnet msbuild
-getProperty:PublishReadyToRunCrossgen2ExtraArgs`, both with
`-p:_IsPublishing=true` and the same project properties and `-p:`
arguments, writing a binlog for each step. Restore has to be a separate
invocation: MSBuild skips `-restore` when `-getProperty` runs without a
target, and the WebAssembly SDK pack that sets these arguments is
imported from the test's restored packages.
  - The 12-case matrix is unchanged.
- Composite cases still pass the Crossgen2 SDK override props and
targets, so both import orders are still covered.
- Each case's cost drops from a full R2R publish (~120 s) to a restore
plus an evaluation. The restore took ~8.7 s inside the old publishes.
- `PublishRunAllPagesCore` now reads its existing publish binlog and
asserts the same flags on the `RunReadyToRunCompiler` task. That covers
per-app Debug, per-app Release (trimmed, untrimmed, native relink), and
composite Release, so we still prove crossgen2 receives the flags,
without extra publishes.

Expected savings: about 20 minutes on the Windows no-workload CLR work
item. That's an estimate from the timings above, not a measurement.

## Validation

- `./dotnet.sh build
src/mono/wasm/Wasm.Build.Tests/Wasm.Build.Tests.csproj -c Release
-p:TargetOS=browser -p:TargetArchitecture=wasm -p:RuntimeFlavor=CoreCLR
-p:InstallWorkloadForTesting=false`: **passed** (0 warnings, 0 errors).
- Repo SDK, scratch project: `dotnet msbuild -restore
-getProperty:NuGetPackageRoot` produced no `obj/` and an empty value.
Running `dotnet msbuild -t:Restore` first, then `dotnet msbuild
-getProperty:NuGetPackageRoot -getProperty:Foo`, **returned the restored
value**, and stdout contained only the JSON result.
- CI on the first commit (build 1675766):
- All 12 evaluation cases failed on Linux and Windows with an empty
value, because restore never ran. The second commit fixes that.
- The new `RunReadyToRunCompiler` assertions in `PublishRunAllPages`,
`PublishRunAllPagesComposite` and `PublishRunAllPagesNativeRelink`
passed on both platforms.
- On Windows, `WBT-NoWorkload-CLR` finished in **1h16m**, under the
90-minute limit; the previous run timed out at 1h34m. The 12 cases took
about 10 s total there. With the fix, expect roughly 2–3 more minutes
for restore and evaluation.
- `PublishWithUnnormalizedIntermediatePathKeepsReadyToRunImages`
(Release, and Release composite) failed on Windows only: `AssertBundle`
found loose `.dll` files in `publish/`. That test (#137229) doesn't use
any code this PR changes, and it passed on Linux.
- I did **not** run the WBT tests locally, because they need a
browser-wasm CoreCLR build. The CI `WBT-NoWorkload-CLR` lanes are the
real check.

> [!NOTE]
> This PR description was generated with GitHub Copilot.

---------

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-Build-mono

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants