Skip to content

SyntaxProvider.ForAttributeWithMetadataName returns duplicate values when there are multiple partial classes. #79519

@m-nash

Description

@m-nash

Version Used: 4.3.0

Steps to Reproduce:

If you compile this code

using System;
using System.ClientModel.Primitives;
using System.Text.Json;

namespace TestProject
{
    [ModelReaderWriterBuildable(typeof(JsonModel))]
    public partial class LocalContext : ModelReaderWriterContext { }

    public class JsonModel : IJsonModel<JsonModel>
    {
        public JsonModel Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => new JsonModel();
        public JsonModel Create(BinaryData data, ModelReaderWriterOptions options) => new JsonModel();
        public string GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
        public void Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) { }
        public BinaryData Write(ModelReaderWriterOptions options) => BinaryData.Empty;
    }

    [ModelReaderWriterBuildable(typeof(JsonModel2))]
    public partial class LocalContext : ModelReaderWriterContext { }

    public class JsonModel2 : IJsonModel<JsonModel2>
    {
        public JsonModel2 Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => new JsonModel2();
        public JsonModel2 Create(BinaryData data, ModelReaderWriterOptions options) => new JsonModel2();
        public string GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
        public void Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) { }
        public BinaryData Write(ModelReaderWriterOptions options) => BinaryData.Empty;
    }
}

Into a source generator that registers this increment values provider

        var typesFromAttribute = context.SyntaxProvider.ForAttributeWithMetadataName(BuildableAttributeName,
                predicate: (node, _) => true,
                transform: (ctx, _) => (ctx.TargetSymbol as INamedTypeSymbol, ctx.Attributes))
            .Collect();

You will get 2 entries both have the exact same symbol and both of those symbols 2 attributes. This means I have to dedupe either in the transform or the register source callback.

Expected Behavior:

I should get 1 symbol with 2 attributes.

Actual Behavior:

I get 2 duplicate symbols with 2 attributes each.

Metadata

Metadata

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions