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

GetAnalyzerOptions is not being called in SourceGeneratorTest #1203

Open
kzrnm opened this issue Jan 20, 2025 · 0 comments · May be fixed by #1204
Open

GetAnalyzerOptions is not being called in SourceGeneratorTest #1203

kzrnm opened this issue Jan 20, 2025 · 0 comments · May be fixed by #1204

Comments

@kzrnm
Copy link

kzrnm commented Jan 20, 2025

In Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing 1.1.1, I used the following code:

public class CSharpIncrementalGeneratorTest<TIncrementalGenerator> : SourceGeneratorTest<DefaultVerifier>
    where TIncrementalGenerator : IIncrementalGenerator, new()
{
    public CSharpCompilationOptions CompilationOptions { get; set; } = new(OutputKind.DynamicallyLinkedLibrary);
    protected override CompilationOptions CreateCompilationOptions() => CompilationOptions;
    public CSharpParseOptions ParseOptions { get; set; } = new(languageVersion: LanguageVersion.CSharp9, kind: SourceCodeKind.Regular, documentationMode: DocumentationMode.Parse);
    protected override ParseOptions CreateParseOptions() => ParseOptions;
    public AnalyzerConfigOptionsProvider AnalyzerConfigOptionsProvider { get; set; }

    protected override string DefaultFileExt => "cs";
    public override string Language => LanguageNames.CSharp;
    protected override IEnumerable<ISourceGenerator> GetSourceGenerators() => new[] { new TIncrementalGenerator().AsSourceGenerator() };
    protected override GeneratorDriver CreateGeneratorDriver(Project project, ImmutableArray<ISourceGenerator> sourceGenerators)
        => CSharpGeneratorDriver.Create(
            sourceGenerators,
            project.AnalyzerOptions.AdditionalFiles,
            (CSharpParseOptions)project.ParseOptions!,
            AnalyzerConfigOptionsProvider ?? project.AnalyzerOptions.AnalyzerConfigOptionsProvider);
}

In Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing 1.1.2, the CreateGeneratorDriver method has been removed, so I modified the code to use GetAnalyzerOptions instead:

public class CSharpIncrementalGeneratorTest<TGenerator> : SourceGeneratorTest<DefaultVerifier>
{
    public CSharpCompilationOptions CompilationOptions { get; set; } = new(OutputKind.DynamicallyLinkedLibrary);
    protected override CompilationOptions CreateCompilationOptions() => CompilationOptions;
    public CSharpParseOptions ParseOptions { get; set; } = new(languageVersion: LanguageVersion.CSharp9, kind: SourceCodeKind.Regular, documentationMode: DocumentationMode.Parse);
    protected override ParseOptions CreateParseOptions() => ParseOptions;
    public AnalyzerConfigOptionsProvider AnalyzerConfigOptionsProvider { get; set; }

    protected override string DefaultFileExt => "cs";
    public override string Language => LanguageNames.CSharp;
    protected override IEnumerable<Type> GetSourceGenerators() => [typeof(TGenerator)];
    protected override AnalyzerOptions GetAnalyzerOptions(Project project)
    {
        var baseOptions = base.GetAnalyzerOptions(project);
        return AnalyzerConfigOptionsProvider switch
        {
            null => baseOptions,
            _ => new AnalyzerOptions(baseOptions.AdditionalFiles, AnalyzerConfigOptionsProvider),
        };
    }
}

However, it seems that GetAnalyzerOptions is not being called. Is there any workaround?

kzrnm added a commit to kzrnm/roslyn-sdk that referenced this issue Jan 20, 2025
@kzrnm kzrnm linked a pull request Jan 20, 2025 that will close this issue
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 a pull request may close this issue.

1 participant