Skip to content

Align x86 .NET Framework compatibility overrides with x64 - #15326

Merged
JanProvaznik merged 7 commits into
dotnet:mainfrom
JanProvaznik:fix/netfx-x86-long-path-defaults
Oct 22, 2026
Merged

JanProvaznik merged 7 commits into
dotnet:mainfrom
JanProvaznik:fix/netfx-x86-long-path-defaults

Conversation

@JanProvaznik

@JanProvaznik JanProvaznik commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Related issue(s):

Fixes #15333.
Related: #5331, #6562.

Context

NGEN can affect how .NET Framework loads assemblies and computes compatibility quirks. In some NGEN states, x86 MSBuild.exe uses legacy I/O behavior despite targeting .NET Framework 4.7.2, causing the WriteLinesToFile and Unzip long-path failures reported in #15333.

The x64 configuration has explicitly specified these compatibility settings since #6562; x86 still relied on implicit defaults for most of them.

Changes Made

  • Make the x86 AppContextSwitchOverrides value exactly match the existing eight-entry x64 value, preserving the existing FIPS setting.
  • Document the NGEN-dependent quirk behavior and explicitly specify built-in Framework quirks, using the suggested review wording.

Compatibility

The five non-I/O additions restore the normal .NET Framework 4.7.2 defaults; the existing FIPS override remains unchanged. The x64 and .NET configurations, task implementations, assembly bindings, NGEN registration, public APIs, and MSBuild diagnostic definitions are unchanged. No ChangeWave is needed for this restoration.

Testing

Real installed 18.12 x86 A/B/A: both tasks fail with the original config, pass with the I/O overrides, and fail again after byte-for-byte restoration, while the same MSBuild.ni.exe path remains loaded. See the captured evidence and analysis.

Phase MSBuild native image loaded Legacy handling / block long paths WriteLinesToFile Unzip
Original, COMPLUS_ZapDisable=0 Yes True / True Failed Failed
JIT control, COMPLUS_ZapDisable=1 No False / False Passed, contents verified Passed, contents verified
Patched config, COMPLUS_ZapDisable=0 Yes, same path False / False Passed, contents verified Passed, contents verified
Original config restored, COMPLUS_ZapDisable=0 Yes, same path True / True Failed Failed

Every phase used 291-character output paths, a reported 4.7.2 target framework, x86, and LongPathsEnabled=1. The issue's original project also passed against the aligned bootstrap with an owned BaseDirectory override: 350-character file, 285-character directory, contents verified, with COMPLUS_ZapDisable=0 and 1.

Per review feedback, the synthetic regression tests, generated-host helper, and manifest-copy wiring were removed. They do not reproduce the installed/version/NGEN-dependent trigger faithfully enough to justify retaining that scaffolding. The PR adds no automated regression test or diagnostic utility.

After removal, the affected project rebuilt successfully for both net472 and net11.0:

$out = Join-Path $env:USERPROFILE 'msbuild-longpath-9008d8ff'
$logs = Join-Path $env:USERPROFILE '.copilot\session-state\9008d8ff-eff1-4148-84d2-191cf7bbc325\files'
$env:DOTNET_ROOT = Join-Path (Get-Location) '.dotnet'
$env:PATH = "$env:DOTNET_ROOT;$env:PATH"
$env:MSBUILDUSESERVER = '0'
.\.dotnet\dotnet.exe build src\MSBuild.UnitTests\Microsoft.Build.CommandLine.UnitTests.csproj `
  -c Debug "/p:ArtifactsDir=$out\" -p:BuildProjectReferences=false --no-restore `
  -v:q -clp:ErrorsOnly -nr:false "-bl:$logs\review-remove-tests-build.binlog"
# Passed: both TFMs, 0 errors, existing NuGet audit warnings.

Both test files now match the PR base, and the eight x86/x64 override entries remain identical. Earlier local validation results remain historical evidence, not tests retained in the change set.

Dependencies and Follow-up

No package or runtime update is required. This mitigates the reported x86 .NET Framework MSBuild.exe symptoms once the updated configuration is deployed; it does not repair the underlying CLR initialization defect or configure arbitrary custom hosts. Identifying the first trigger release/commit remains a separate investigation.

Explicitly opt out of legacy I/O quirks before managed startup.
Cover WriteLinesToFile and Unzip at 291 characters in a fresh
.NET 4.0 host using MSBuild's configuration and Win32 manifest.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9008d8ff-eff1-4148-84d2-191cf7bbc325

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.

🟡 Changes recommended

The x86 regression host cannot load the x64 bootstrap executable in Platform=x64 builds.

1 open issue
What changed in this PR

Restores intended long-path behavior for x86 .NET Framework MSBuild when early CLR initialization selects legacy defaults.

Changes:

  • Explicitly disables legacy path handling and long-path blocking.
  • Adds 291-character path regressions for WriteLinesToFile and Unzip.
  • Documents why configuration overrides must precede managed startup.
File Description
src/​MSBuild/​app.config Adds long-path overrides while preserving the FIPS setting.
src/​MSBuild.UnitTests/​XMake_Tests.cs Adds isolated x86 regression cases with content verification.
src/​MSBuild.UnitTests/​Microsoft.Build.CommandLine.UnitTests.csproj Copies the manifest for the regression host.
documentation/​NETFramework-NGEN.md Explains early CLR compatibility initialization.

Review effort: Balanced

Comment thread src/MSBuild.UnitTests/XMake_Tests.cs Resolved
JanProvaznik and others added 3 commits October 19, 2026 16:06
Select the unqualified x86 bootstrap from the architecture-neutral root
instead of using the parent test platform's executable. Explicitly skip
when an x64-only build does not provide the x86 layout.

Reproduced the original two BadImageFormatException failures under an
x64 test host; corrected cases pass under both x86 and x64 hosts.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9008d8ff-eff1-4148-84d2-191cf7bbc325
Run actual installed x86 MSBuild processes at 291 characters and record
I/O switches and native-image usage. Keep the default probe read-only;
offer an opt-in temporary config A/B/A comparison with exact restoration.

Validate PowerShell 5.1 and 7 plus edit/restore behavior on an owned
staged runtime, without changing installed VS files or native images.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9008d8ff-eff1-4148-84d2-191cf7bbc325
TryGetSwitch reports whether a switch was found, not whether it came from a config override.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9008d8ff-eff1-4148-84d2-191cf7bbc325
@JanProvaznik

JanProvaznik commented Oct 19, 2026 •

Copy link
Copy Markdown
Member Author

Note

This comment was generated by GitHub Copilot CLI.

Affected installed 18.12 x86: NGEN-preserving config A/B/A confirmed

An affected-machine run has now supplied the installed-process evidence that was previously missing. Both real tasks fail with the original configuration, pass with the two explicit I/O overrides, and fail again after restoration, while the same MSBuild.ni.exe path remains loaded. This is separate from the earlier synthetic .NET 4.0-host validation; those fixtures were subsequently removed from the PR per review.

Environment and procedure

  • MSBuild 18.12.0-1.26475.12+694f366b505ad7daadd8177fb875d66b1819bea9, running as a 32-bit .NET Framework process.
  • Reported application target framework: .NETFramework,Version=v4.7.2; Windows LongPathsEnabled=1.
  • Both WriteLinesToFile and Unzip used 291-character output paths. Successful runs include file-content verification.
  • A one-off diagnostic harness (snapshot used for this experiment) was run against the actual installed executable with -TestFix, using fresh processes for each task/phase. The harness is not part of this PR's final change set:
& "$env:TEMP\Test-MSBuildLongPaths.ps1" `
  -MSBuildExe 'C:\Program Files\Microsoft Visual Studio\18\Insiders\MSBuild\Current\Bin\MSBuild.exe' `
  -TestFix

The temporary logical configuration change was limited to these entries, preserving other overrides:

Switch.System.IO.UseLegacyPathHandling=false
Switch.System.IO.BlockLongPaths=false

No executable relocation or NGEN registration/regeneration was performed. The script reported that the original configuration was restored byte-for-byte before the final phase.

Observed results

Phase Task COMPLUS_ZapDisable MSBuild native image loaded Legacy handling Block long paths Exit Contents verified
Original WriteLinesToFile 0 Yes True True 1 No
Original Unzip 0 Yes True True 1 No
JIT control WriteLinesToFile 1 No False False 0 Yes
JIT control Unzip 1 No False False 0 Yes
Patched config WriteLinesToFile 0 Yes False False 0 Yes
Patched config Unzip 0 Yes False False 0 Yes
Original restored WriteLinesToFile 0 Yes True True 1 No
Original restored Unzip 0 Yes True True 1 No

The native-image path reported in all six Original/Patched/Restored processes was identical:

%WINDIR%\assembly\NativeImages_v4.0.30319_32\MSBuild\143667f802697853267c33f51fee38b2\MSBuild.ni.exe
Failure excerpts (personal and temporary paths omitted)

Original and restored WriteLinesToFile:

error MSB4018: The "WriteLinesToFile" task failed unexpectedly.
error MSB4018: System.IO.PathTooLongException: The specified path, file name, or both are too long.
    at System.IO.Path.LegacyNormalizePath(...)
    at System.IO.Path.NormalizePath(...)
    at System.IO.Path.InternalGetDirectoryName(...)
    at Microsoft.Build.Tasks.WriteLinesToFile.Execute()

Original and restored Unzip:

error : The specified path, file name, or both are too long.
    at System.IO.PathHelper.GetFullPathName()
    at System.IO.Path.LegacyNormalizePath(...)
    at System.IO.Path.NormalizePath(...)
    at System.IO.Path.GetFullPathInternal(...)
    at System.IO.Path.GetFullPath(...)
    at Microsoft.Build.Framework.AbsolutePath.GetCanonicalForm()
    at Microsoft.Build.Tasks.Unzip.Extract(...)
    at Microsoft.Build.Tasks.Unzip.Execute()

Analysis

  1. The proposed configuration remedy is validated on an affected installed build. The reproducible failure/success/failure reversal follows the configuration change for both tasks, without changing the installed executable or moving it to a bootstrap layout.
  2. The patched success is not explained by losing MSBuild's native image. COMPLUS_ZapDisable=0 and the same loaded MSBuild.ni.exe path are retained throughout A/B/A; the separate JIT control also demonstrates the previously observed native/JIT differential.
  3. The prerequisite settings were already present. Both failing and passing phases report a 4.7.2 target framework, enabled Windows long paths, the same architecture, and the same path length. Explicit switch values remove reliance on implicit compatibility defaults, consistent with the previously documented CLR quirk-initialization issue.

This closes the installed-NGEN remedy-validation gap. It does not by itself identify the first bad release/commit or directly capture the first target-framework lookup; those are separate trigger/initialization investigations. TryGetSwitch reporting a switch as found is not, on its own, evidence that it was explicitly configured.

Keep the PR scoped to the x86 defaults, regression coverage, and the
compatibility note. Preserve installed A/B/A evidence as an external
experiment rather than adding an installed-config editing utility.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9008d8ff-eff1-4148-84d2-191cf7bbc325
@JanProvaznik

Copy link
Copy Markdown
Member Author

rescoping to making parity work

Use the same eight explicit switches as the existing x64 configuration
instead of allowing premature CLR initialization to retain legacy defaults.
Keep the existing FIPS setting and both long-path overrides unchanged.

Cover async culture flow, cancellation registration after disposal, and
independent claims-actor copying in fresh Framework 4.0 hosts. Reuse the
existing architecture-safe host setup for those and the long-path cases.

Fixes dotnet#15333.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9008d8ff-eff1-4148-84d2-191cf7bbc325
@JanProvaznik JanProvaznik changed the title Restore long-path defaults for x86 .NET Framework MSBuild Align x86 .NET Framework compatibility overrides with x64 Oct 20, 2026
@JanProvaznik

JanProvaznik commented Oct 20, 2026 •

Copy link
Copy Markdown
Member Author

Note

This comment was generated by GitHub Copilot CLI.

Expanded x86/x64 quirk parity and scope of #15333

Review update: the synthetic tests and host scaffolding described below were used for earlier local validation, but are no longer part of the PR. The final change set retains only the executable configuration alignment and the reworded NGEN documentation; installed A/B/A evidence remains the direct validation.

The x86 configuration now exactly matches the existing eight-entry x64 AppContextSwitchOverrides value, including the five remaining non-I/O settings. The FIPS setting was already explicit in both configurations and remains unchanged.

The discrepancy dates to June 18, 2021, when dotnet/msbuild#6562 added the workaround only to x64. The five newly explicit false values are already the normal defaults for a correctly recognized .NET Framework 4.7.2 application; alignment removes dependence on the premature-default-selection path rather than introducing a newer compatibility policy.

Additional red/green behavior coverage

The following real BCL behavior probes run in fresh x86 processes declaring a .NET 4.0 target, with the selected production MSBuild configuration copied alongside the host:

Behavior Previous I/O-only x86 config Eight-switch-aligned x86 config
Custom current culture flows into Task.Run Failed: culture lost Passed
Register callback after cancellation-source disposal Failed: ObjectDisposedException Passed
Copy claims identity without sharing/mutating the original actor Failed: actor aliased Passed

Together with the two existing actual-task long-path cases, the focused set passes 5/5 with no skips, from both x86 and x64 parent test hosts while always selecting the x86 bootstrap/configuration. Full CLI results after alignment: 720 passed / 8 skipped on net472; 704 passed / 8 skipped on net11.0; zero failures. The default full repository build passed with only the existing NuGet audit warnings.

CSP window-handle interop and portable-PDB decoding retain the longstanding x64 values and Framework-default source support; they do not have new hardware/symbol-specific end-to-end coverage here.

Does this resolve #15333?

Yes, for the reported Windows x86 .NET Framework MSBuild.exe long-path regression in WriteLinesToFile and Unzip, with the stated long-path prerequisites enabled. The affected installed 18.12 A/B/A run already proves that the two I/O overrides reverse both failures while preserving the same loaded MSBuild.ni.exe; full alignment retains those values.

The issue's original project was also run against the aligned x86 bootstrap with its XML property values checked against the report. Only an owned BaseDirectory override was used to avoid touching existing temp data: a 350-character output file in a 285-character directory was written and its contents verified with COMPLUS_ZapDisable=0 and 1.

The PR now uses Fixes dotnet/msbuild#15333 for that product symptom. It does not repair the CLR initialization-order bug, configure arbitrary custom API hosts or separately configured task hosts, enable OS policy, remove unrelated native/tool path limits, or identify the first trigger commit. The additional five overrides are parity/hardening against other effects of the same fallback state, not a claim that every Framework compatibility switch is normalized.

Comment thread src/MSBuild.UnitTests/XMake_Tests.cs Resolved
Comment thread documentation/NETFramework-NGEN.md Resolved
Remove the synthetic regression tests, generated Framework host helper,
and test-project manifest wiring. Keep the installed NGEN A/B/A evidence
as validation without retaining costly synthetic test scaffolding.

Use the suggested documentation wording about NGEN-dependent quirk
calculation and explicitly specified built-in Framework quirks.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9008d8ff-eff1-4148-84d2-191cf7bbc325
@OvesN

OvesN commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

/backport to vs18.11

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to vs18.11 (link to workflow run)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PathTooLongException in WriteLinesToFile and Unzip

4 participants