Fix repeat builds with persisted source-generated files#27115
Fix repeat builds with persisted source-generated files#27115powercode wants to merge 2 commits intoPowerShell:masterfrom
Conversation
There was a problem hiding this comment.
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\SourceGeneratedat the start of the build (removesRemoveDirfrom the early build target). - Explicitly exclude
gen\SourceGenerated\**\*.csfrom compilation inSystem.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. |
|
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 |
|
Open the solution in Rider or Visual Studio. Build 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. |
|
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. |
|
@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
That is, everytime I build, without removing the sources, I have a different ProductVersion. |
|
Without this fix, every N+1, N+3, N+5... build will produce: |
16ee1b2 to
f653a11
Compare
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
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header