-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add nullable reference type annotations to System.Text.Json source gen #79613
Add nullable reference type annotations to System.Text.Json source gen #79613
Conversation
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsNRE's have been a frequent occurrence when making changes to the STJ source generator. This PR enables nullable reference type annotations to the STJ source generator components. Because source generator projects need to target Additionally, this PR makes the sourcegen test suite target the Roslyn v4.4 instead of v4.0. The changes have been factored into a separate commit fe78570 for easier reviewing.
|
We should use msbuild's multi-targeting feature here which was designed for such use-cases. I don't think that introducing new projects per target framework for source generators is scalable. Every project adds to the overall build graph and restore time.
Why can't you multi-target the existing roslyn4.4 project and add net8.0 there? Did you hit this error? runtime/src/libraries/Directory.Build.targets Lines 234 to 235 in ea44671
|
Multi-targeting the sourcegen project themselves produced a bit of msbuild weirdness which I tried to address, but ultimately I couldn't fix it. That particular error you're referencing wasn't hit actually, it was primarily complaining that v8 version of I should add that the |
I think we should make multi-targeting work here. I can take a look tomorrow morning. |
.../System.Text.Json.SourceGeneration.TestLibrary/System.Text.Json.TestLibrary.Roslyn4.4.csproj
Outdated
Show resolved
Hide resolved
@eiriktsarpalis I just pushed a commit to your branch to allow our source-generator projects to multi-target. Note that this comes with a number of pitfalls:
Before merging, I would like @ericstj to review my changes as this is an entirely new approach with possible unknown implications and/or regressions. |
Would using a standalone project be more reliable in that case? It achieves the same end goal with the inconvenience of one placeholder project being included in the solution. |
No, that doesn't make any difference. It's mostly about using the live shared framework vs. using a prebuilt. Using the live shared framework is a good goal to have but slightly more work. I would hope that with time, .NETCoreApp targeting source generators will become more natural. |
This reverts commit fe78570.
…sarpalis/runtime into stj-sourcegen-nullability
src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.4.csproj
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
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.
.cs files changes look good to me
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 PR is now ready and I added some documentation around multi-targeting source generators and what to watch out for (when consuming a live built source generator).
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.
This is awesome.
Test failures are unrelated. |
NRE's have been a frequent occurrence when making changes to the STJ source generator. This PR enables nullable reference type annotations to the STJ source generator components. Because source generator projects need to target
netstandard2.0
, this is done by adding a new project,System.Text.Json.SourceGenerator.NetCoreApp
that targetsnet8.0
as an unused class library.Additionally, this PR makes the sourcegen test suite target the Roslyn v4.4 instead of v4.0. The changes have been factored into a separate commit fe78570 for easier reviewing.These infrastructural changes are in anticipation of implementing #58770, which needs to make use of APIs only available in Roslyn v4.4.