0

I'm writing a source generator which will generate factories to create instances of object(s).

Since the source generator generates code that's related to tests, I'm going to install the source generator in the actual test project.

Now, assume that the source project contains a simple record, such as a Person, I want the source generator to find this one, and then based on that one, generate some code.

Here's the source generator currently:

    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        IncrementalValueProvider<ImmutableArray<TypeBuilderMetadata?>> provider = context
            .SyntaxProvider.CreateSyntaxProvider(
                (node, token) =>
                {
                    token.ThrowIfCancellationRequested();

                    return node is RecordDeclarationSyntax;
                },
                (ctx, token) =>
                {
                    token.ThrowIfCancellationRequested();

                    var symbol = (RecordDeclarationSyntax)ctx.Node;

                    if (ctx.SemanticModel.GetDeclaredSymbol(symbol, token) is not INamedTypeSymbol recordSymbol)
                    {
                        return null;
                    }

                    var fullTypeName = recordSymbol.ToDisplayString();
                    var typeNamespace = recordSymbol.GetFullNamespace();

                    return NewTypeBuilderDefinition(recordSymbol, recordSymbol);
                }
            )
            .Where(builder => builder is not null)
            .Collect();

        context.RegisterSourceOutput(provider, this.GenerateSource);
    }

Now, since the test project doesn't contain any 'Record' definitions, the source generator will never generate code. This is because the SyntaxProvider does look at the current project, and not at the entire compilation.

Is there a way to look at the syntax trees of the entire compilation?

5
  • 1
    maybe the compilation-provider instead of the syntax provider, and access the syntax-trees directly to see if the external refs are there? Commented Mar 14 at 10:16
  • or perhaps better: the metadata-references provider, include everything, and call back the the top-level compilation when changes get triggered (which should be rarely) Commented Mar 14 at 10:29
  • If I loop over the CompilationProvider and then go over Compilation.SyntaxTrees, it's empty. Commented Mar 14 at 12:03
  • "This is because the SyntaxProvider does look at the current project, and not at the entire compilation." The phrasing doesn't make sense here: a compilation is a single project. By 'compilation' do you mean the entire solution and all the projects? Commented Mar 14 at 22:17
  • I have a source generator installed in project B, and project B has a project reference to A. I want the source generator to look at the source in A and generate code in B. Commented Mar 15 at 16:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.