Skip to content

Fix dotnet-new template samples test and dotnet-watch .slnx sharing violation - #57089

Merged
MiYanni merged 5 commits into
dotnet:mainfrom
MiYanni:miyanni-fix-watch-aspire-slnx-race
Nov 13, 2026
Merged

MiYanni merged 5 commits into
dotnet:mainfrom
MiYanni:miyanni-fix-watch-aspire-slnx-race

Conversation

@MiYanni

@MiYanni MiYanni commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

🤖Copilot🤖
This PR combines two independent CI fixes so they can be reviewed and merged together. CI build 1678390 passed with both changes applied.

Fixes #57031
Fixes #57065


Part 1: Test the in-repo template engine samples instead of the published package

TemplateEngineSamplesTest installed the latest published Microsoft.TemplateEngine.Samples package, so publishing 11.0.100 (which drops FSharp.NET.Sdk from the F# sample) broke the checked-in sample06 --language F# snapshot.

The samples live in this repo (documentation/TemplateEngine/Samples), so the test now uses them directly instead of any package:

  • Microsoft.TemplateEngine.Samples.csproj marks its template content CopyToOutputDirectory (linked under TemplateEngineSamples/). The package layout is unchanged (verified the nupkg entries are identical).
  • dotnet-new.IntegrationTests.csproj has a ProjectReference (ReferenceOutputAssembly="false") to the samples project, so building the tests copies the templates into the test output.
  • TemplateEngineSamplesTest installs the TemplateEngineSamples folder from its output directory.
  • The F# snapshot is updated to the in-repo sample (plain Microsoft.NET.Sdk, no legacy package references).

Snapshots now always match the template content in the same commit.

Validated locally: all 16 TemplateEngineSamplesTest cases pass.


Part 2: Release the generated .slnx write handle before building

Root cause

Commit 2133b17 ("Create dotnet-watch solution files privately") changed dotnet-watch to keep the generated multi-project .slnx open with FileAccess.ReadWrite + FileShare.Read for the duration of the build. MSBuild's solution parser (Microsoft.VisualStudio.SolutionPersistence) opens the file with File.OpenRead, which requests FileShare.Read; Windows denies that open while another handle holds write access. From the Helix console of the failing runs:

aspire watch server ❌ ...\dotnet-watch-h42e2re5.vgl\1.slnx : error MSB4025: The project file could not be loaded. System.IO.IOException: The process cannot access the file '...\1.slnx' because it is being used by another process.
   at System.IO.File.OpenRead(String path)
   at Microsoft.VisualStudio.SolutionPersistence.Serializer.SingleFileSerializerBase`1...OpenAsync(...)
   at Microsoft.Build.Construction.SolutionFile.ParseUsingNewParser()

The initial build of the Aspire watch server failed, so AspireLauncherIntegrationTests.ServerAndResources waited for Capabilities output that never came until the test host was killed (hang dumps of Microsoft.DotNet.HotReload.Watch.Aspire / dotnet). Any dotnet watch session that builds multiple projects through a generated solution on Windows hits the same build failure. The existing unit test masked the bug by reading with FileShare.ReadWrite instead of File.OpenRead.

Fix

TemporaryFileManager.Create now takes a content writer: it writes through an exclusive handle, closes it, and then keeps only a read-only, FileShare.Read handle open until disposal. Readers using File.OpenRead succeed, while the file still can't be modified or deleted during the build (and stays in the owner-only private directory). TemporaryFile exposes Path instead of the underlying stream, so callers can't use a closed or read-only stream by mistake.

Validation

  • TemporaryFileManagerTests now reads via File.OpenRead (what MSBuild uses) and, on Windows, asserts that write access is still denied.
  • Ran TemporaryFileManagerTests + AspireLauncherIntegrationTests locally on Windows against a rebuilt redist layout: 3/3 passed, and the server log shows Build succeeded: ...\1.slnx.

Note: #57066 (draft, Copilot coding agent) addresses the same dotnet-watch issue by reopening the handle at the call site; this PR instead makes the helper API safe by construction. Only one of the two should be merged.

MiYanni and others added 3 commits November 11, 2026 14:30
The test installed the latest package, so publishing 11.0.100 changed the generated F# project (FSharp.NET.Sdk removed) and broke the checked-in snapshot. Pin to 10.0.404, which the snapshots match.

Fixes dotnet#57031

Co-authored-by: Copilot App <[email protected]>
…he published one

Pack documentation/TemplateEngine/Samples as part of building dotnet-new.IntegrationTests and install that nupkg in TemplateEngineSamplesTest, replacing the unversioned install of the latest published package. Update the F# snapshot to match the in-repo sample.

Fixes dotnet#57031

Co-authored-by: Copilot App <[email protected]>
…kage

Mark the samples content CopyToOutputDirectory (under TemplateEngineSamples/) so a ProjectReference copies it into the dotnet-new.IntegrationTests output, and install that folder in TemplateEngineSamplesTest. The package layout is unchanged.

Co-authored-by: Copilot App <[email protected]>
@MiYanni
MiYanni requested a review from a team as a code owner November 12, 2026 17:47
Copilot AI balanced review requested due to automatic review settings November 12, 2026 17:47
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 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.

🟢 Approval recommended

No active review issues block the Windows temporary-file sharing fix.

No open issues 🎉

What changed in this PR

This PR fixes Windows file-sharing failures when dotnet watch builds multiple projects through a generated .slnx, addressing the build failure linked to #57065.

Changes:

  • Close the temporary file’s write handle before retaining a read-only handle for the build.
  • Update solution generation to use a content-writing callback and expose the file path instead of its stream.
  • Exercise File.OpenRead compatibility and verify that Windows still denies write access.
File Description
test/​dotnet-watch.Tests/​Utilities/​TemporaryFileManagerTests.cs Tests reader compatibility and Windows write protection with the updated API.
src/​Dotnet.Watch/​Watch/​Utilities/​TemporaryFileManager.cs Separates writing from the retained read-only handle and cleans up on creation failure.
src/​Dotnet.Watch/​Watch/​HotReload/​HotReloadDotNetWatcher.cs Uses the updated helper to write and build generated solutions.

Review effort: Balanced

@MiYanni

MiYanni commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

CI triage for build 1678012: the failures are unrelated to this change.

dotnet-watch held the generated multi-project .slnx open with FileAccess.ReadWrite
and FileShare.Read while building it. MSBuild's solution parser opens solution files
with File.OpenRead, which requests FileShare.Read and therefore fails on Windows while
another handle has write access (MSB4025 sharing violation). The failed initial build
left the Aspire ServerAndResources test waiting until the test host timed out.

TemporaryFileManager.Create now writes the content through an exclusive handle and then
keeps only a read-only, read-shared handle open until disposal, so readers can open the
file while it still can't be modified or deleted.

Fixes dotnet#57065

Co-authored-by: Copilot App <[email protected]>
@MiYanni
MiYanni force-pushed the miyanni-fix-watch-aspire-slnx-race branch from 6fb5f7e to 0447796 Compare November 12, 2026 19:34
@MiYanni
MiYanni requested review from a team as code owners November 12, 2026 19:34
@MiYanni

MiYanni commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

/azp run dotnet-sdk-public-ci

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@MiYanni MiYanni changed the title [dotnet-watch] Release generated .slnx write handle before building Fix dotnet-new template samples test and dotnet-watch .slnx sharing violation Nov 12, 2026
@MiYanni
MiYanni requested a review from lbussell November 12, 2026 22:51
Address review feedback

- Rename TemporaryFile.Path to FilePath to avoid shadowing System.IO.Path.
- Use an intention-revealing comment for best-effort cleanup.
- Document why write access is only denied on Windows (Unix FileShare is advisory flock()).
- Use Path.Join in TemplateEngineSamplesTest.

Co-authored-by: Copilot App <[email protected]>
@MiYanni
MiYanni enabled auto-merge November 12, 2026 23:54
@MiYanni
MiYanni merged commit 2764dd0 into dotnet:main Nov 13, 2026
36 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 14, 2026
MiYanni added a commit that referenced this pull request Nov 17, 2026
Keep approval snapshots deterministic by installing the Template Engine samples version aligned with this servicing branch. Backport of #57089 (issue #57031).

Co-authored-by: Copilot App <[email protected]>
nagilson added a commit that referenced this pull request Nov 17, 2026
… the published package

Backport of the samples changes from #57089 (main). TemplateEngineSamplesTest installed the latest published Microsoft.TemplateEngine.Samples, so publishing 11.0.100 GA (SDK-style F# project) broke the checked-in snapshot on every branch. Build the samples project into the test output and install from that folder, and update the F# snapshot to the in-repo content.

Fixes #57042 on release/11.0.1xx.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 3a9812a1-ab94-4bad-b412-d16ef17dc6fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants