C
C#4mo ago
Rai

SourceGenerator multiple steps (passing data from one to another)

Is there some way, possibly even documented way, to get let's say an attribute name in one SyntaxProvider Collection and do something with that in another one. As of now I haven't found a way to make this work, as manually calling Collect and Select outside of RegisterSourceOutput results in the local variables being used as temp storage to transport these values between steps to have these values but only as long as they are inside the lambda scope of the Select method, the moment it's outside any change to local variables is lost. Even attempts such as using static fields will not provide a working solution, nor playing around with passing the IncrementalValueProvider around to each and every step doesn't seem to be a possible solution. Furthermore there doesn't seem to any documented examples of this when looking at the dotnet provided examples at: https://github.com/dotnet/roslyn-sdk/tree/main/samples/CSharp/SourceGenerators/SourceGeneratorSamples e.g code along the lines of.:
var attrProvider = attributeProvider.Collect();
attrProvider .Select((array, token) =>
{
attribute = array.First();
return array;
}
);
var classProvider = context.SyntaxProvider.CreateSyntaxProvider(
predicate: (node, _) => node is ClassDeclarationSyntax,
transform: (ctx, _) => GetTypesHavingAttribute(ctx, attribute)
)
.Where(m => m is not null);
var attrProvider = attributeProvider.Collect();
attrProvider .Select((array, token) =>
{
attribute = array.First();
return array;
}
);
var classProvider = context.SyntaxProvider.CreateSyntaxProvider(
predicate: (node, _) => node is ClassDeclarationSyntax,
transform: (ctx, _) => GetTypesHavingAttribute(ctx, attribute)
)
.Where(m => m is not null);
If reasoning may be required: I require to retrieve all attributes that inherit from a generic attribute in order to properly seperate if necessary the later on fetched classes in to their respective groups. Furthermore these provided attributes contain necessary metadata, which are required for the generation of the necessary code.
GitHub
roslyn-sdk/samples/CSharp/SourceGenerators/SourceGeneratorSamples a...
Roslyn-SDK templates and Syntax Visualizer. Contribute to dotnet/roslyn-sdk development by creating an account on GitHub.
7 Replies
Anton
Anton4mo ago
there's Combine not sure it helps
Rai
RaiOP4mo ago
It from what i have tried doesn't seem to help as it's only use is after you have fetched all required data but not during the process As in i need data from a previous step in order to fetch the actual classes based on that data else I'll be generating code based on hopes and dreams that likely to fail
333fred
333fred4mo ago
Sounds like you haven't read $ig
MODiX
MODiX4mo ago
If you want to make an incremental source generator, please make sure to read through both the design document and the cookbook before starting.
333fred
333fred4mo ago
Also, source generator questions are best asked in #roslyn
Rai
RaiOP4mo ago
Most certainly haven't read the design document so far but the cookbook is I'd say lack luster in information. And regarding #roslyn there doesn't seem to be a point in spamming it if those questions are being ignored or vanish into the void and end up never being answered anyway. So I'm still not sure what these information are giving me in regards to solving this issue or rather this task, which I'd argue isn't something that should happen all that rarely. But nonetheless it's very very confusing from an api usage point that code that from just a logical code flow should work doesn't at all.
333fred
333fred4mo ago
I'd say it's pretty clear that you haven't read either the design doc or the cookbook, because, for example, you're not using ForAttributeWithMetadataName I highly suggest asking questions in #roslyn because most questions in there actually do get answered, whereas many roslyn-based questions in #help do not get people looking at them unless I happen to pull open #help My suggestion to you is to look at the examples given in the cookbook, and convert to ForAttributeWithMetadataName. Attempting to do attribute inheritance is not something that roslyn supports particularly well, and will make your generator quite literally 100x slower. That is not a typo, and it is not hyperbole
Want results from more Discord servers?
Add your server