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

Source Generator Hack - Write to disk instead of the syntax tree #102

Merged
merged 15 commits into from
Aug 11, 2023

Conversation

christianhelle
Copy link
Owner

@christianhelle christianhelle commented Aug 10, 2023

I simply can't, for the life of me, figure out how to trigger a 3rd party source generator from another source generator. So instead, as a bit of a dirty hack, the "rosyln source generator" writes to disk instead. The "source generator" will create a folder called Generated relative to the .refitter file and create a .g.cs (overrides existing files). This means that unless the .g.cs files are already on disk, the user might need to build twice. The first time experience will be quite crappy but the Refit interface will automagically update itself on build time

This sort of resolves #100 but it's still not a "real" fix

Here's are some short clips of how it behaves:

Recording 2023-08-10 at 21 34 37

Recording 2023-08-10 at 21 50 25

@christianhelle christianhelle added the enhancement New feature, bug fix, or request label Aug 10, 2023
Removed the source code generation from context and instead write the generated code directly to a file. This change allows us to save the refit generated code as standalone files. This approach could be beneficial for future debugging and preview purposes.
A condition has been added to prevent generation of invalid filenames. Previously, the filename for generated source files would be .g.cs when the input file is only called ".refitter". Now, in this case, a default filename ("Refitter.g.cs") is used. This prevents errors when generating source files.
In this change, we've added a new Source Generator to generate Refit HTTP APIs interface stubs. This helps in consuming REST APIs by handling all the HTTP operations, allowing users to use them like ordinary C# interfaces. It benefits users with reduced boilerplate code and improvements in compile-time safety and IDE support.
The .gitignore file has been updated to include the 'src/Refitter.SourceGenerator.Tests/AdditionalFiles/Generated' path. This was necessary to ensure that Git doesn't track changes to these dynamically generated files, which can result in unnecessary noise in commits.
The function `context.AddSource` in `RefitterSourceGenerator.cs` has been commented out. The code was causing unnecessary repopulation of diagnostic reports leading to performance hits, hence it has been temporarily disabled for optimization till a long-term solution is found.
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@christianhelle christianhelle merged commit 0a532d6 into main Aug 11, 2023
@christianhelle christianhelle deleted the source-generators branch August 11, 2023 21:14
@kirides
Copy link
Contributor

kirides commented Aug 29, 2023

Btw.

I simply can't, for the life of me, figure out how to trigger a 3rd party source generator from another source generator.

This is by design, Source Generators are free to run concurrently and are not supposed to depends on other source generators output.

Proposal
As a reminder, the high level design goals of source generators are:
[...]

  • Run un-ordered, each generator will see the same input compilation, with no access to files created by other source generators.

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

i guess by writing files you might actually needlessly trigger constant re-runs of source generators (due to new sources appearing), which may be especially bad for non IncrementalSourceGenerators

@christianhelle
Copy link
Owner Author

Thanks for pointing this out @kirides

I'm not entirely sure about the consequences of writing files but it seems that the files first appear at the end of the build. Refit at least doesn't pick it up when the files are just generated, you need to build again once the generated files appear

Definitely some room for improvement in the Refitter soruce generator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, bug fix, or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refitter Source Generator - generated code not being picked up by Refit's Source Generator
2 participants