-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Use Source Code Generator for PSVersionInfo class #15603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
It seems test fails are not related to the PR - pwsh.exe has Preview.8 in product version instead of Preview.7. @rjmholt @TravisEz13 Could you please look the issue? |
src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs
Outdated
Show resolved
Hide resolved
src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs
Outdated
Show resolved
Hide resolved
cbb19d0 to
5abcd36
Compare
|
@rjmholt I resolved the issue with release tag. It turned out that ci.psm1 always assigns ReleaseTag property directly to dotnet. |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
5abcd36 to
1e6b4cc
Compare
src/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs
Outdated
Show resolved
Hide resolved
TravisEz13
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pending @wg-security review
1e6b4cc to
72ad218
Compare
daxian-dbw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I integrated the FastParsePSVersion change from #18210 to the source generator, and also refactored the code. After the change, the static constructor of PSVersionTable only takes 2-3 ms on my dev machine (release build with R2R images, using the release tag v7.3.0 to mimic a stable release). It was 24-30 ms without the changes of this PR:
## Measured by wrapping the static constructor with `StopWatch`.
C:\>E:\arena\source\PowerShell\src\powershell-win-core\bin\Release\net7.0\win7-x64\publish\pwsh.exe -noprofile
2.5478
PowerShell 7.3.0
PS C:\> exit
C:\>E:\arena\source\PowerShell\src\powershell-win-core\bin\Release\net7.0\win7-x64\publish\pwsh.exe -noprofile
2.5993
PowerShell 7.3.0
PS C:\> exit
C:\>E:\arena\source\PowerShell\src\powershell-win-core\bin\Release\net7.0\win7-x64\publish\pwsh.exe -noprofile
2.5842
PowerShell 7.3.0
PS C:\> exit
src/System.Management.Automation/System.Management.Automation.csproj
Outdated
Show resolved
Hide resolved
- Turn off EmitCompilerGeneratedFiles - Remove first blank line in generated code
...stem.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs
Outdated
Show resolved
Hide resolved
| <!-- we persist source generator files under 'gen' folder so that they are visible to IDEs --> | ||
| <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
| <CompilerGeneratedFilesOutputPath>gen\SourceGenerated</CompilerGeneratedFilesOutputPath> | ||
| </PropertyGroup> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generate the file, so VSCode and Visual Studio can always resolve the generated members.
Those files will be removed when building again, by RemoveDir in PowerShell.Common.props.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You didn't see my reply to your comment? It's here: #15603 (comment), and quoted below:
It doesn't work in the following scenarios, for both VSCode and Visual Studio, which are quite common in development:
- Run
Start-PSBuildfirst, then openSystem.Management.Automationin VSCode- Open
System.Management.Automationin VSCode to use an existing build, like from yesterday.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, too many comments already....
| </PropertyGroup> | ||
|
|
||
| <!-- Remove existing generated files by source generators because new source code will soon be generated when the build starts --> | ||
| <RemoveDir Directories="gen\SourceGenerated" Condition="Exists('gen\SourceGenerated')" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path gen\SourceGenerated is duplicated in here and System.Management.Automation.csproj. I tried creating a property holding the path in the PropertyGroup right above, and use it in RemoveDir and CompilerGeneratedFilesOutputPath. However, it works in RemoveDir, but not in CompilerGeneratedFilesOutputPath, and I cannot figure out why. I'd love if someone can make it work.
The review was done a long time ago. Will request to re-review.
|
@TravisEz13 and @PaulHigin, can you please review the changes again and re-evaluate the "code injection" concern? Here is the doc (and the sample code) about the source generator: https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview The arguments used to format the source template in PSVersionInfoGenerator.cs are from the build properties defined in |
...stem.Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.cs
Show resolved
Hide resolved
....Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.csproj
Outdated
Show resolved
Hide resolved
....Management.Automation/SourceGenerators/PSVersionInfoGenerator/PSVersionInfoGenerator.csproj
Outdated
Show resolved
Hide resolved
iSazonov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
|
@WG-Security |
daxian-dbw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
c1a4788 to
1e26d0c
Compare
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
@iSazonov Just FYI, I re-did the conflict resolution because you accidentally brough back the |
|
@daxian-dbw Thanks for working on the SG! Since you mentioned saving build time I think you could be interesting in SG for TypeGen. |
Gihub did show only one merge conflict for but did not marked PSCompatibleVersions. I wonder why. 😕 |
I don't know if that would save build time, I doubt it :) |

PR Summary
The custom source generator allows us to exclude reflection and to directly get current PowerShell version from MSBuild.
PerfView doesn't work well on current .Net Preview. A fix has just been merged in .Net Runtime repo for this. I hope we get it in .Net 6.0 Preview6 or Preview7. So today I can not share reliable result from PerfView to confirm perf win. Obviously using static string is more faster then reflection.
PR Context
Contribute to #13540
Related #14268
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).