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

Migrate to IIncrementalSourceGenerator #57

Open
wants to merge 39 commits into
base: stable
Choose a base branch
from

Conversation

ProphetLamb
Copy link

@ProphetLamb ProphetLamb commented Jun 20, 2023

Migrate to IIncrementalSourceGenerator

Synopsis

.NET source generation using the ISourceGenerator interface is officially deprecated 12.

This PR aims to migrate dotvariant to the IIncrementalSourceGenerator ecosystem.

Subject

Migration of the implementation and test environment.

  • Migrate SourceGenerator implementation
    • Modify source generation process
    • Source generator compiles
    • Do not pass Compilation into cache, ever
    • Add more Diagnostics to CreateSyntaxProvider.transform instead of return default.
  • Migrate Test implementation
    • Modify test case fixtures
    • Test cases compile
    • Test cases generate sources
    • Successful: dotVariant.Generator.Test
    • Successful: dotVariant.Test-net45
    • Successful: dotVariant.Test-net5.0
    • Resolve hack used to obtain RenderInfos from stateless IIncrementalGenerator when debugging.

Postscriptum

I've been using this lovely package for a while now. So I took some time this beautiful afternoon to do a good chunk of the dirty work required to keep this project up to date.
@mknejp Any and all help, in the form of reviews, or with the remaining open points is much appreciated.

Footnotes

  1. https://github.com/dotnet/roslyn-analyzers/pull/6478

  2. https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md

@mknejp
Copy link
Owner

mknejp commented Jun 22, 2023

Thank you very much for tackling this. It might take a few days before I am able to do a review.

@ProphetLamb
Copy link
Author

Alighty, thanks:)

@ProphetLamb
Copy link
Author

@mknejp Is there any chance you could review the PR?
If your personal life doesn't allow you the time, that's fine. In that case we can either check, if you can add me as a maintainer, or I can publish my fork separately.

Best regards
Prophet

Copy link
Owner

@mknejp mknejp left a comment

Choose a reason for hiding this comment

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

Sorry it took me so long to take a look at this. I appreciate your patience.

Regarding your open points:

Add more Diagnostics to CreateSyntaxProvider.transform instead of return default.

What kind of diagnostics are you referring to? Do you mean the ones in CreateSyntaxProvider? Would these errors not indicate that either the predicate is wrong, or that there's a bug in Roslyn?

Resolve hack used to obtain RenderInfos from stateless IIncrementalGenerator when debugging.

I think the only "clean" way to resolve this (technically the previous solution wasn't "clean" either) would be have a static function that builds the transformation graph and then trigger it manually in the tests. However I have no idea how feasible this is so I'm willing to accept the current "hack". However I would sleep better if it was a thread-safe list as I have no idea how the generator transformations are threaded.

I also think it would be nicer to move some of the larger lambda expressions in Initialize to dedicated named functions to make the structure of the transformers stand out more. Do you agree?

Lastly, we need to find a solution for the copyright headers. You used my name in the new files, which I assume is a consequence of the .editorconfig file, but that is technically wrong. I also don't want to erase your effort. This is definitely a priority.

src/dotVariant.Generator/Descriptor.cs Outdated Show resolved Hide resolved
src/dotVariant.Generator/DiagnosedResult.cs Outdated Show resolved Hide resolved
src/dotVariant.Generator/DiagnosedResult.cs Show resolved Hide resolved
src/dotVariant.Generator/SourceGenerator.cs Outdated Show resolved Hide resolved
@ProphetLamb
Copy link
Author

ProphetLamb commented Jul 8, 2023

Add more Diagnostics to CreateSyntaxProvider.transform instead of return default.

What kind of diagnostics are you referring to? Do you mean the ones in CreateSyntaxProvider? Would these errors not indicate that either the predicate is wrong, or that there's a bug in Roslyn?

Generally I think a fatal error should be produced, instead of no output. For example, the user imports the package into F#.

Resolve hack used to obtain RenderInfos from stateless IIncrementalGenerator when debugging.

I think the only "clean" way to resolve this (technically the previous solution wasn't "clean" either) would be have a static function that builds the transformation graph and then trigger it manually in the tests. However I have no idea how feasible this is so I'm willing to accept the current "hack". However I would sleep better if it was a thread-safe list as I have no idea how the generator transformations are threaded.

I think this is best solved this using a static class annotated with debug only using a Monitor on a list. This way the SourceGenerator code is not contaminated. See #15c3c76

I also think it would be nicer to move some of the larger lambda expressions in Initialize to dedicated named functions to make the structure of the transformers stand out more. Do you agree?

Agreed. Done.

Lastly, we need to find a solution for the copyright headers. You used my name in the new files, which I assume is a consequence of the .editorconfig file, but that is technically wrong. I also don't want to erase your effort. This is definitely a priority.

Since .editorconfig does not support per file headers, I would go for the 1970s way of adding contributes to the bottom of the copyright headers used. Do you have any better ideas?

@mknejp
Copy link
Owner

mknejp commented Jul 8, 2023

Since .editorconfig does not support per file headers, I would go for the 1970s way of adding contributes to the bottom of the copyright headers used. Do you have any better ideas?

I was thinking of removing all the names from source files (including mine) and just keeping it in the git history. I would create a PR to change the existing files over and you can then rebase yours on top, if that is an acceptabl solution for you.

@mknejp
Copy link
Owner

mknejp commented Jul 8, 2023

I personally hate these giant license comments in files that just keep growing and growing.

@mknejp
Copy link
Owner

mknejp commented Jul 8, 2023

I had to move the project to net6.0 to make the CI work again and took the liberty of taking along your package updates as it otherwise wouldn't build anymore.

@ProphetLamb
Copy link
Author

If this is such a critical property of the struct then please add a test to enshrine it for future contributors. This is something I can easily see someone trying to "cleanup".

I added a separate test project for isolated tests of internal functionality.

@ProphetLamb
Copy link
Author

Since .editorconfig does not support per file headers, I would go for the 1970s way of adding contributes to the bottom of the copyright headers used. Do you have any better ideas?

I was thinking of removing all the names from source files (including mine) and just keeping it in the git history. I would create a PR to change the existing files over and you can then rebase yours on top, if that is an acceptabl solution for you.

Agreed. I will manually update files added by this PR once the rebase is incoming.

@mknejp
Copy link
Owner

mknejp commented Jul 10, 2023

I added a separate test project for isolated tests of internal functionality.

I don't think we need a separate test project. dotVariant.Generator.Test is intended to be the test project for the internals of dotVariant.Generator (as it doesn't reference the generator as Anazlyer).

The other test projects ensure that the generator can be used externally as Analyzer, by either <ProjectReference> or <PackageReference>.

Agreed. I will manually update files added by this PR once the rebase is incoming.

This work is now done. The file headers are adjusted and you can add yourself to the /AUTHORS.txt file.

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.

2 participants