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

[Source Generators] ISyntaxReceiver instances should be able to access AdditionalFiles #74167

Closed
raffaeler opened this issue Jun 26, 2024 · 3 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@raffaeler
Copy link
Contributor

Version Used: 4.10.0

Steps to Reproduce:

  1. Create a generator
  2. Create a SyntaxReceiver and register it in the generator
  3. Add a file in the AdditionalFiles
  4. Try to access the AdditionalFiles from the SyntaxReceiver

Expected Behavior:
I would expect to be able to access the AdditionalFiles

Actual Behavior:
I am not able to access the AdditionalFiles

Rationale:
The standard workflow for code generators is to use a SyntaxReceiver to identify the portions of the syntax trees useful to generate new code.
In many cases, we can use a generated attribute to annotate the portions of code that a SyntaxReceiver should capture.
In other cases, it is not possible to annotate the code because it has been generated using T4Templates or because it comes from external tools that can generate file.

In these cases, it would make sense to access the AdditionalFiles from the SyntaxReceiver in order to minimize the amount of data provided to the generator.

Since the AdditionalFiles are not available in a SyntaxReceiver, the generator must do all the processing inside the Execute method which forces processing all the SyntaxTrees instead of just the ones that are captured by a SyntaxReceiver.
It's not even possible to make the pre-processing in the Initialize method as the AdditionalFiles are not accessible there.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 26, 2024
@jaredpar
Copy link
Member

Create a generator

What kind of generator: incremental, ISourceGenerator?

Create a SyntaxReceiver and register it in the generator

Can you show us a sample of whhere you are trying to access this?

@jaredpar
Copy link
Member

Closing due to lack of response.

@jaredpar jaredpar closed this as not planned Won't fix, can't repro, duplicate, stale Jul 11, 2024
@raffaeler
Copy link
Contributor Author

Sorry for the late response @jaredpar. One week in a conference, another sick :-(

  • I am using Source Generators

Here is the example of the SyntaxReceiver.
I have other cases where the _rootInterfaceName cannot be hardcoded but should be configured at project level.
AdditionalFiles look like a greate place to put this string, but apparently I can't access the AdditionalFiles from a ISyntaxReceiver

internal class SiblingConfigurationSyntaxReceiver : ISyntaxReceiver
{
    private const string _rootInterfaceName = "IEntityTypeConfiguration";

    public List<ClassDeclarationSyntax> ConfigurationClasses = new();

    public SiblingConfigurationSyntaxReceiver()
    {
    }

    public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
    {
        BaseTypeDeclarationSyntax? typeDeclaration = GeneratorsCommon.GetTypeDeclaration(syntaxNode);

        // dbcontext configurations are always classes
        if (typeDeclaration is not ClassDeclarationSyntax classDeclaration) return;

        //((classDeclaration.BaseList?.Types[0] as BaseTypeSyntax).Type as SimpleNameSyntax).Identifier.Text
        bool isConfiguration = classDeclaration.BaseList?.Types
            .Any(t => (t?.Type as SimpleNameSyntax)?.Identifier.Text == _rootInterfaceName) ?? false;

        if(!isConfiguration) return;

        ConfigurationClasses.Add(classDeclaration);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants