Skip to content

Fix repeat builds with persisted source-generated files#27115

Open
powercode wants to merge 2 commits intoPowerShell:masterfrom
powercode:fix_sourcegen
Open

Fix repeat builds with persisted source-generated files#27115
powercode wants to merge 2 commits intoPowerShell:masterfrom
powercode:fix_sourcegen

Conversation

@powercode
Copy link
Copy Markdown
Collaborator

@powercode powercode commented Mar 28, 2026

PR Summary

Remove the removal of existing generated files by source generators to allow persisted generator output to remain visible on disk without compiling it as project source.

PR Context

This change addresses issues with repeat builds by ensuring that source-generated files are not deleted at the start of the build process, thus maintaining their availability for subsequent builds.

PR Checklist

Copilot AI review requested due to automatic review settings March 28, 2026 20:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the build configuration so source-generator emitted files can persist on disk across builds without being treated as project source, addressing issues seen on repeat builds.

Changes:

  • Stop deleting gen\SourceGenerated at the start of the build (removes RemoveDir from the early build target).
  • Explicitly exclude gen\SourceGenerated\**\*.cs from compilation in System.Management.Automation.csproj.

Reviewed changes

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

File Description
src/System.Management.Automation/System.Management.Automation.csproj Excludes persisted source-generator output under gen\SourceGenerated from Compile items to prevent it from being compiled as project source.
PowerShell.Common.props Removes the build-time deletion of gen\SourceGenerated, allowing generator output to persist across builds.

@daxian-dbw
Copy link
Copy Markdown
Member

The generated source will be different when building with a different release tag or a different commit, and we do want to compile the generated partial PSVersionInfo source code into the binary. So, I'm not sure I understand the purpose of this change. @powercode can you please elaborate what is the "repeat build" you are talking about and what is the issue with it?

@powercode
Copy link
Copy Markdown
Collaborator Author

Open the solution in Rider or Visual Studio.

Build
Build again.

Every other build fails with errors saying that source-generated files are missing.

Or if you first build and then try to run tests, the test run fails because it tries to build first. If you rerun the tests, the build will work, and the tests will run.

But the experience is beyond annoying.

Getting the commit into the binary is only interesting for the binary we actually release.

A condition on the Remove-element, that could be set in the environment before opening the solution would probably work.

@powercode
Copy link
Copy Markdown
Collaborator Author

Or have some artifact that remembers the last version, and only remove the files when the actual version has changed.

The problem I have is iterating with a dirty status, where the version doesn't make sense anyway.

@mhdipozn-cmd mhdipozn-cmd mentioned this pull request Mar 31, 2026
@powercode
Copy link
Copy Markdown
Collaborator Author

@daxian-dbw Also, the file is regenerated whenever the commit changes. I don't think there is any need to remove the files.

With this fix:

$genSrc = "src\System.Management.Automation\gen\SourceGenerated\SMA.Generator\SMA.PSVersionInfoGenerator\PSVersionInfo.g.cs"
function get-generatedversion {start-psbuild *>$null && sls '(?<=string ProductVersion = ).+' $genSrc | % {$_.Matches[0].Value}}

get-generatedversion
git checkout HEAD~1 2>$null | out-null
get-generatedversion
git switch - 2>$null | out-null
get-generatedversion
"7.6.0-preview.5 Commits: 240 SHA: 992a08d5c3ce4c0922cc48f2b1ab2d95476ce712";
"7.6.0-preview.5 Commits: 239 SHA: da27208400255e43a364ee1073b415b101ee6b09";
"7.6.0-preview.5 Commits: 240 SHA: 992a08d5c3ce4c0922cc48f2b1ab2d95476ce712";

That is, everytime I build, without removing the sources, I have a different ProductVersion.

@powercode
Copy link
Copy Markdown
Collaborator Author

Without this fix, every N+1, N+3, N+5... build will produce:

C:\Users\staff\AppData\Local\Microsoft\dotnet\sdk\11.0.100-preview.1.26104.118\Roslyn\bincore\csc.exe /noconfig ...obj\Debug\net11.0\System.Management.Automation.AssemblyInfo.cs /warnaserror+:NU1605,SYSLIB0011
12>CSC: Error CS2001 : Source file 'E:\repos\PowerShell\src\System.Management.Automation\gen\SourceGenerated\Microsoft.Interop.LibraryImportGenerator\Microsoft.Interop.LibraryImportGenerator\LibraryImports.g.cs' could not be found.
12>CSC: Error CS2001 : Source file 'E:\repos\PowerShell\src\System.Management.Automation\gen\SourceGenerated\SMA.Generator\SMA.PSVersionInfoGenerator\PSVersionInfo.g.cs' could not be found.
12>CSC: Error CS2001 : Source file 'E:\repos\PowerShell\src\System.Management.Automation\gen\SourceGenerated\System.Text.RegularExpressions.Generator\System.Text.RegularExpressions.Generator.RegexGenerator\RegexGenerator.g.cs' could not be found.

@powercode powercode requested review from a team and jshigetomi as code owners March 31, 2026 17:21
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.

4 participants