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

Run a single generator at a time #74086

Open
chsienki opened this issue Jun 20, 2024 · 1 comment · May be fixed by #74127
Open

Run a single generator at a time #74086

chsienki opened this issue Jun 20, 2024 · 1 comment · May be fixed by #74127
Assignees
Labels
api-approved API was approved in API review, it can be implemented Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request New Feature - Source Generators Source Generators untriaged Issues and PRs which have not yet been triaged by a lead
Milestone

Comments

@chsienki
Copy link
Contributor

chsienki commented Jun 20, 2024

Background and Motivation

Today the generator driver only has the ability to run all generators it contains. As part of the razor cohosting work we would like the ability to only update a single generator (the razor generator in this case).

While we could remove the existing generators, run the driver, and then re-add them, this throws away any saved state associated with the removed generators meaning subsequent runs would not be incremental.

Proposed API

namespace Microsoft.CodeAnalysis
{
    public abstract class GeneratorDriver
    {
        public GeneratorDriver RunGenerators(Compilation compilation, CancellationToken cancellationToken = default) 

+       public GeneratorDriver RunGenerator(ISourceGenerator generator, Compilation compilation, CancellationToken cancellationToken = default)  
    }
}

Usage Examples

var compilation = GetCompilation();
var generator1 = new MySourceGenerator();
var generator2 = new MySourceGenerator();
var driver = new CSharpGeneratorDriver([generator1, generator2]);

// run all generators
driver = driver.RunGenerators(compilation);

// run only a single generator
driver = driver.RunGenerator(generator1, compilation);

var runResult = driver.GetRunResult();

var gen1Result = runResult.Results[0]; // result from second run
var gen2Result = runResult.Results[1]; // result from initial 'all generators' run

Open question: What happens if generator passed to RunGenerator isn't part of the driver? Exception, or just nothing happens?

Alternative Designs

// choose generator to run by type
// May run multiple generators, need to consider constraint on TGenerator to handle both ISourceGenerator and IIncrementalGenerator
public GeneratorDriver RunGenerator<TGenerator>(Compilation compilation, CancellationToken cancellationToken = default) 

// Add a param to RunGenerators that specifies a set of generators to run. 
// Generally, don't see a need to run a subset greater than a single generator.
// Passing null to say 'all' generators seems weird.
public GeneratorDriver RunGenerators(Compilation compilation, CancellationToken cancellationToken = default, IEnumerable<ISourceGenerator> specificGenerators = null) 

// Add a param to RunGenerators that has a filter
// Less weird that the filter being null equals all generators
public GeneratorDriver RunGenerators(Compilation compilation, CancellationToken cancellationToken = default, Func<ISourceGenerator, bool> generatorFilter = null) 
@chsienki chsienki added Concept-API This issue involves adding, removing, clarification, or modification of an API. Area-Compilers Feature Request New Feature - Source Generators Source Generators api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jun 20, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Jun 20, 2024
@333fred
Copy link
Member

333fred commented Jun 20, 2024

API Review

  • We think a multiple-generator option is the better approach given balanced mode may want to run multiple generators in the future
  • We'll change the filter parameter to have a context future-proofing the signature
  • We'll also reorder the parameters to put the Func second and make it non-optional
public GeneratorDriver RunGenerators(Compilation compilation, Func<RunGeneratorsFilterContext, bool> generatorFilter, CancellationToken cancellationToken = default);

Conclusion: Above API shape is approved.

@333fred 333fred added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jun 20, 2024
@jaredpar jaredpar added this to the 17.12 milestone Jun 21, 2024
@chsienki chsienki linked a pull request Jun 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request New Feature - Source Generators Source Generators untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants