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

Add fine-grained disabling of SourceGenerators #71049

Merged

Conversation

alexdlm
Copy link
Contributor

@alexdlm alexdlm commented Jan 8, 2023

This allows alternate source generators to provide similar functionality without conflict. Primarily I would like to disable the ScriptMethods generator to replace it with my own, but figured it made sense to extend to all generators.

Example: Add a <GodotDisabledSourceGenerators>ScriptMethods;ScriptSerialization</GodotDisableSourceGenerators> property to your csproj to disable those two generators, but leave the rest functional.

@alexdlm alexdlm requested a review from a team as a code owner January 8, 2023 05:01
@alexdlm
Copy link
Contributor Author

alexdlm commented Jan 8, 2023

Hmm I suspect the Windows Builds / Editor check is a flake, is there a way to re-run?

Copy link
Member

@raulsntos raulsntos left a comment

Choose a reason for hiding this comment

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

Looks good. I think this can be very useful, I don't expect many users would disable source generators but those that do are likely not interested in re-implementing them all just to replace some small functionality.

@akien-mga akien-mga requested a review from neikeq January 8, 2023 21:39
@alexdlm alexdlm force-pushed the dotnet-generator-selective-disable branch from 4adeead to 560ba84 Compare March 25, 2023 02:32
This allows manual testing and/or alternate source generators to
provide functionality without conflict.
@alexdlm alexdlm force-pushed the dotnet-generator-selective-disable branch from 560ba84 to 8ab3295 Compare March 25, 2023 02:46
@alexdlm
Copy link
Contributor Author

alexdlm commented Mar 25, 2023

8ab3295 rebased to current master, fixed merged conflict in Godot.SourceGenerators.props and added disable to GodotPluginsInitializerGenerator

public static bool IsGodotSourceGeneratorDisabled(this GeneratorExecutionContext context, string generatorName) =>
AreGodotSourceGeneratorsDisabled(context) ||
(context.TryGetGlobalAnalyzerProperty("GodotDisabledSourceGenerators", out string? disabledGenerators) &&
disabledGenerators != null &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the string be empty, or would it return null in that case? If it can be empty, we should use IsNullOrEmpty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it could be empty, but the split below would return an empty array in that case so it's not strictly necessary.

I tried it, however !string.IsNullOrEmpty(disabledGenerators) strangely doesn't seem to pass the non-nullablility through to the following line so I'm inclined to leave it as is. It seems like there are other nullability warnings in this project already though so I'm happy to push it if you prefer @neikeq ?

Copy link
Member

@raulsntos raulsntos Mar 26, 2023

Choose a reason for hiding this comment

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

The string.IsNullOrEmpty method is not annotated for nullability in netstandard2.0 which is the TFM that source generators must target.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe checking if it's empty early should be faster than going through the Split and Contains calls, at the very least because those are not inlined.

I don't remember how we deal with this nullability issue elsewhere. We probably check for null and Length manually.

Copy link
Contributor

Choose a reason for hiding this comment

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

What's the status here? I can't tell from this discussion if the PR should be merged or should be worked on further.

Copy link
Member

Choose a reason for hiding this comment

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

This discussion is about saving one allocation and maybe 100 instructions executed in a code path that is executed exactly 7 times for each build of the csharp user project (saving like a microsecond on an action usually taking a few seconds) so it doesn't really matter all that much.

Copy link
Contributor

Choose a reason for hiding this comment

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

I approved the PR, this requested change is optional.

(...) a code path that is executed exactly 7 times for each build of the csharp user project (...) so it doesn't really matter all that much.

Source generators are also run very frequently by IDEs while editing code, so performance is very important. Still, just nitpicking in this case.

@YuriSizov
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants