Skip to content
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

Support Roslyn 3.8 and Roslyn 4.0 source generator scenarios #1216

Merged
merged 6 commits into from
Aug 19, 2021

Conversation

sharwell
Copy link
Contributor

@sharwell sharwell commented Aug 7, 2021

  • Split InterfaceStubGenerator.Core into one project for Roslyn 3.8 and a second project for Roslyn 4.0
  • Update packaging to include both source generators in the refit package
  • Add build logic to selectively include the Roslyn 4.0 source generator for design-time scenarios in Visual Studio 2022
    • Design time builds in Visual Studio 2019 use the Roslyn 3.8 implementation.
    • Design time builds in Visual Studio 2022 use the Roslyn 4.0 implementation.
    • All command line builds use the Roslyn 3.8 implementation. Prior to Add generator driver cache to VBCSCompiler dotnet/roslyn#55023, there is no significant performance penalty for using the Roslyn 3.8 implementation of the source generator. We can decide how to handle command line scenarios later.
  • Add tests verify the V1 and V2 implementations produce the same output
  • Convert the V2 implementation to use incremental generation instead of SyntaxReceiver
    • As a separate change, show how the generator can improve incrementality by collecting an intermediate value to its own value source

@sharwell sharwell force-pushed the multi-target branch 3 times, most recently from 847fc26 to 2422617 Compare August 7, 2021 05:22
@sharwell sharwell marked this pull request as ready for review August 7, 2021 05:35
@clairernovotny
Copy link
Member

Is this ready to merge?

@sharwell
Copy link
Contributor Author

sharwell commented Aug 8, 2021

@clairernovotny as best I can tell, yes. Whether or not you merge now, the next step of testing is getting some sort of beta package into a public feed so it can be tested in a few real-world cases (both inside the IDE and from command lines).

Refit/targets/refit.props Outdated Show resolved Hide resolved
@@ -4,4 +4,17 @@
<CompilerVisibleProperty Include="RefitInternalNamespace" />
</ItemGroup>

<Choose>
<When Condition="'$(VisualStudioVersion)' >= '17.0' AND '$(DesignTimeBuild)' == 'True'">

Choose a reason for hiding this comment

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

Check out dotnet/msbuild#4911. Sorry - In my previous reply I didn't have time to look it up.

Suggested change
<When Condition="'$(VisualStudioVersion)' >= '17.0' AND '$(DesignTimeBuild)' == 'True'">
<When Condition="$([MSBuild]::VersionGreaterThanOrEquals($(VisualStudioVersion), '17.0')) AND '$(DesignTimeBuild)' == 'True'">

@Mike-E-angelo
Copy link

COOL... is this available on a preview feed somewhere? I'd like to test it ASAP 😁

@clairernovotny
Copy link
Member

clairernovotny commented Aug 19, 2021

Refit's CI feed is here: https://pkgs.dev.azure.com/dotnet/ReactiveUI/_packaging/Refit/nuget/v3/index.json

@Mike-E-angelo
Copy link

Awesome, thank you for that, @clairernovotny.

Unfortunately I appear to be running into an issue here. Is there a configuration setting I should be applying, perhaps? Any link/resources you can send would be greatly appreciated. I do not see anything obvious here in this PR. Here is what I see when I try to build with the preview build:

103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\PreserveAttribute.g.cs(8,18,8,35): error CS0101: The namespace 'Starbeam.Features.Production.PresentationRefitInternalGenerated' already contains a definition for 'PreserveAttribute'
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(8,18,8,35): error CS0101: The namespace 'Starbeam.Features.Production.PresentationRefitInternalGenerated' already contains a definition for 'PreserveAttribute'
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\PreserveAttribute.g.cs(5,6,5,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\PreserveAttribute.g.cs(6,6,6,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\PreserveAttribute.g.cs(7,6,7,35): error CS0579: Duplicate 'global::System.AttributeUsage' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(5,6,5,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(6,6,6,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(7,6,7,35): error CS0579: Duplicate 'global::System.AttributeUsage' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\Generated.g.cs(7,6,7,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\Generated.g.cs(8,6,8,52): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\Generated.g.cs(9,6,9,95): error CS0579: Duplicate 'global::Starbeam.Features.Production.PresentationRefitInternalGenerated.PreserveAttribute' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\Generated.g.cs(11,6,11,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(7,6,7,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(8,6,8,52): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(9,6,9,95): error CS0579: Duplicate 'global::Starbeam.Features.Production.PresentationRefitInternalGenerated.PreserveAttribute' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(11,6,11,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(10,6,10,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(11,6,11,52): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(12,6,12,95): error CS0579: Duplicate 'global::Starbeam.Features.Production.PresentationRefitInternalGenerated.PreserveAttribute' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(14,6,14,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(10,6,10,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(11,6,11,52): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(12,6,12,95): error CS0579: Duplicate 'global::Starbeam.Features.Production.PresentationRefitInternalGenerated.PreserveAttribute' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(14,6,14,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(15,19,15,89): error CS8646: 'IPreview.Generate(PreviewDefinition)' is explicitly implemented more than once.
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(20,51,20,57): error CS0102: The type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already contains a definition for 'Client'
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(20,51,20,57): error CS0102: The type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already contains a definition for 'Client'
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(21,48,21,62): error CS0102: The type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already contains a definition for 'requestBuilder'
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(21,48,21,62): error CS0102: The type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already contains a definition for 'requestBuilder'
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(24,16,24,86): error CS0111: Type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already defines a member called 'StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' with the same parameter types
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(24,16,24,86): error CS0111: Type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already defines a member called 'StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' with the same parameter types
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(33,52,33,60): error CS0111: Type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already defines a member called 'Generate' with the same parameter types
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(33,52,33,60): error CS0111: Type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already defines a member called 'Generate' with the same parameter types
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV1\Refit.Generator.InterfaceStubGenerator\IPreview.g.cs(41,131,41,139): error CS0111: Type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already defines a member called 'global::Starbeam.Features.Production.Presentation.Inventory.Issuance.Preview.IPreview.Generate' with the same parameter types
103>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(41,131,41,139): error CS0111: Type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already defines a member called 'global::Starbeam.Features.Production.Presentation.Inventory.Issuance.Preview.IPreview.Generate' with the same parameter types
103>Done building project "Starbeam.Features.Production.Presentation.csproj" -- FAILED.

Interesting to note that the reported directories InterfaceStubGeneratorV1 and InterfaceStubGeneratorV2 do not exist on disk.

If I am not mistaken, it appears it's attempting to use both generators? I've tried clearing all bin obj and .vs folders, as well as restarted VS.

@sharwell
Copy link
Contributor Author

sharwell commented Aug 19, 2021

@Mike-E-angelo thanks for checking this out. It looks like NuGet is including everything under analyzers/cs/**/*.dll where I was only expecting it to use analyzers/cs/*.dll. I think I need to move some files around in the package to avoid this. Trying to get access to a test project to verify.

@Mike-E-angelo
Copy link

Doing some spelunking here @sharwell. 😁

I updated refit.props to be:

<Project>

  <ItemGroup>
    <CompilerVisibleProperty Include="RefitInternalNamespace" />
  </ItemGroup>

	<ItemGroup>
        <Analyzer Include="$(MSBuildThisFileDirectory)../../analyzers/cs/roslyn40/*.dll" />
    </ItemGroup>

</Project>

And deleted the analyzers\roslyn38 directory.

Did a clean and rebuild. I still get the following:

105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(8,18,8,35): error CS0101: The namespace 'Starbeam.Features.Production.PresentationRefitInternalGenerated' already contains a definition for 'PreserveAttribute'
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(5,6,5,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(6,6,6,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\PreserveAttribute.g.cs(7,6,7,35): error CS0579: Duplicate 'global::System.AttributeUsage' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(7,6,7,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(8,6,8,52): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(9,6,9,95): error CS0579: Duplicate 'global::Starbeam.Features.Production.PresentationRefitInternalGenerated.PreserveAttribute' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\Generated.g.cs(11,6,11,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(10,6,10,69): error CS0579: Duplicate 'global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(11,6,11,52): error CS0579: Duplicate 'global::System.Diagnostics.DebuggerNonUserCode' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(12,6,12,95): error CS0579: Duplicate 'global::Starbeam.Features.Production.PresentationRefitInternalGenerated.PreserveAttribute' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(14,6,14,51): error CS0579: Duplicate 'global::System.ComponentModel.EditorBrowsable' attribute
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(15,19,15,89): error CS8646: 'IPreview.Generate(PreviewDefinition)' is explicitly implemented more than once.
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(20,51,20,57): error CS0102: The type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already contains a definition for 'Client'
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(21,48,21,62): error CS0102: The type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already contains a definition for 'requestBuilder'
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(24,16,24,86): error CS0111: Type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already defines a member called 'StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' with the same parameter types
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(33,52,33,60): error CS0111: Type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already defines a member called 'Generate' with the same parameter types
105>...\Starbeam.Features.Production.Presentation\InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IPreview.g.cs(41,131,41,139): error CS0111: Type 'Generated.StarbeamFeaturesProductionPresentationInventoryIssuancePreviewIPreview' already defines a member called 'global::Starbeam.Features.Production.Presentation.Inventory.Issuance.Preview.IPreview.Generate' with the same parameter types

So it appears it is only v2 now but still some duplication going on.

@sharwell
Copy link
Contributor Author

@Mike-E-angelo Try also removing this line:

<Analyzer Include="$(MSBuildThisFileDirectory)../../analyzers/cs/roslyn40/*.dll" />

It looks like NuGet is including the items in roslyn40, and then this line is including them again.

@Mike-E-angelo
Copy link

Mike-E-angelo commented Aug 19, 2021

That worked @sharwell! Thank you. I see what you mean now. That's weird it's including them implicitly. It would seem that it would be an explicit thing such as how you have it with the props file.

In any case, there is indeed a new champ in the CPU Heat Sweepstakes. 😆 Check it out:

This is V1:

So according to dotTrace it went from ~36% CPU time down to 6%.. a 6x differential. DANGGGG. Nice work there. :)

@sharwell
Copy link
Contributor Author

@Mike-E-angelo if you change the folder from analyzers/cs/roslyn40 to analyzers/cs-roslyn40, does it still include it automatically?

@Mike-E-angelo
Copy link

WHEW... this is tricky to figure out @sharwell. Despite my best attempts, it seems as if anything in the analyzers folder is picked up. :S Not sure if there is a weird caching problem, however.

@Mike-E-angelo
Copy link

Mike-E-angelo commented Sep 23, 2021

Hey all... this does not appear to be working with 6.1.11-preview and Visual Studio 2022 Preview 4.1:

Shouldn't the Refit source generator be listed alongside the System.Text.Json.SourceGeneration analyzer?

@Mike-E-angelo
Copy link

I found the problem. Let me create a new issue to track.

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants