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

Move source-generators out of ProjectState and entirely into SolutinoCompilationState #72834

Merged
merged 5 commits into from
Apr 2, 2024

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented Apr 1, 2024

This will help us control the lifetimes of these objects. The goal here is to only instantiate them on the OOP side. That way we don't have any issues where we create and cache the generators once on the VS side. In VS we can't reload them as we're netfx. Isolating the places where we access the generators means we can always defer to OOP to do anything we need in this regard.

This PR is just about isolating. Next will be actually moving the in proc calls to not instantiate the generators.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner April 1, 2024 22:31
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 1, 2024
}
}

public IEnumerable<ISourceGenerator> SourceGenerators
Copy link
Member Author

Choose a reason for hiding this comment

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

this was too broad a surface area. we def did not want to expose inside the workspace that you could get to ISourceGenerator instances.

@@ -422,7 +422,7 @@ async Task<InProgressState> CollapseInProgressStateAsync(InProgressState initial
// Also transform the compilation that has generated files; we won't do that though if the transformation either would cause problems with
// the generated documents, or if don't have any source generators in the first place.
if (translationAction.CanUpdateCompilationWithStaleGeneratedTreesIfGeneratorsGiveSameOutput &&
translationAction.OldProjectState.SourceGenerators.Any())
GetSourceGenerators(translationAction.OldProjectState).Any())
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: this needs to not instantiate generators, but instead call into OOP to determine this info.

Copy link
Member

@sharwell sharwell Apr 2, 2024

Choose a reason for hiding this comment

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

Considering that the SDK delivers source generators that are enabled by default, it might make sense to just assume the .Any() is true and focus on optimizing the specific case where none of the source generators adds any documents to the compilation.

@@ -774,7 +774,7 @@ public ICompilationTracker FreezePartialState(CancellationToken cancellationToke
SolutionCompilationState compilationState, CancellationToken cancellationToken)
{
// If we don't have any generators, then we know we have no generated files, so we can skip the computation entirely.
if (!this.ProjectState.SourceGenerators.Any())
if (!GetSourceGenerators(this.ProjectState).Any())
Copy link
Member Author

Choose a reason for hiding this comment

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

same.

@@ -787,7 +787,7 @@ public ICompilationTracker FreezePartialState(CancellationToken cancellationToke
public async ValueTask<ImmutableArray<Diagnostic>> GetSourceGeneratorDiagnosticsAsync(
SolutionCompilationState compilationState, CancellationToken cancellationToken)
{
if (!this.ProjectState.SourceGenerators.Any())
if (!GetSourceGenerators(this.ProjectState).Any())
Copy link
Member Author

Choose a reason for hiding this comment

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

same

@@ -816,7 +816,7 @@ public ICompilationTracker FreezePartialState(CancellationToken cancellationToke

public async ValueTask<GeneratorDriverRunResult?> GetSourceGeneratorRunResultAsync(SolutionCompilationState compilationState, CancellationToken cancellationToken)
{
if (!this.ProjectState.SourceGenerators.Any())
if (!GetSourceGenerators(this.ProjectState).Any())
Copy link
Member Author

Choose a reason for hiding this comment

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

same.

@@ -233,7 +233,7 @@ private partial class CompilationTracker : ICompilationTracker
// the "InCurrentProcess" call so that it will normally run only in the OOP process, thus ensuring that we
// get accurate information about what SourceGenerators we actually have (say, in case they they are rebuilt
// by the user while VS is running).
if (!this.ProjectState.SourceGenerators.Any())
if (!GetSourceGenerators(this.ProjectState).Any())
Copy link
Member Author

Choose a reason for hiding this comment

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

this call is fine. We're already in OOP.

@@ -293,7 +295,7 @@ private partial class CompilationTracker : ICompilationTracker
if (IsGeneratorRunResultToIgnore(generatorResult))
continue;

var generatorAnalyzerReference = this.ProjectState.GetAnalyzerReferenceForGenerator(generatorResult.Generator);
var generatorAnalyzerReference = GetAnalyzerReference(this.ProjectState, generatorResult.Generator);
Copy link
Member Author

Choose a reason for hiding this comment

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

this call is fine. We're already in OOP.

@@ -394,7 +396,7 @@ static GeneratorDriver CreateGeneratorDriver(ProjectState projectState)

return compilationFactory.CreateGeneratorDriver(
projectState.ParseOptions!,
projectState.SourceGenerators.ToImmutableArray(),
GetSourceGenerators(projectState),
Copy link
Member Author

Choose a reason for hiding this comment

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

this call is fine. We're already in OOP.

@@ -355,7 +355,7 @@ public override GeneratorDriver TransformGeneratorDriver(GeneratorDriver _)
.ReplaceAdditionalTexts(this.NewProjectState.AdditionalDocumentStates.SelectAsArray(static documentState => documentState.AdditionalText))
.WithUpdatedParseOptions(this.NewProjectState.ParseOptions!)
.WithUpdatedAnalyzerConfigOptions(this.NewProjectState.AnalyzerOptions.AnalyzerConfigOptionsProvider)
.ReplaceGenerators(this.NewProjectState.SourceGenerators.ToImmutableArray());
.ReplaceGenerators(GetSourceGenerators(this.NewProjectState));
Copy link
Member Author

Choose a reason for hiding this comment

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

this need to not happen on the VS side.

@CyrusNajmabadi
Copy link
Member Author

@ToddGrun ptal :-)

Copy link
Member

@sharwell sharwell left a comment

Choose a reason for hiding this comment

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

Overall seems acceptable. Would like the perf question answered before proceeding.

Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

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

:shipit:

@CyrusNajmabadi CyrusNajmabadi merged commit 055d1d4 into dotnet:main Apr 2, 2024
27 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the sgOnlyOop2 branch April 2, 2024 17:23
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Apr 2, 2024
@CyrusNajmabadi
Copy link
Member Author

@jasonmalinowski For review when you get back.

@dibarbet dibarbet modified the milestones: Next, 17.11 P1 Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants