Skip to content

Two different metadata files with the same name in different folders: the generator silently uses whichever comes first, with no diagnostic #34

Description

@matt-edmondson

What's wrong

GeneratorBase (SourceGeneratorToolkit/GeneratorBase.cs ~lines 70–76) groups the AdditionalTexts by file name and keeps the first non-empty one:

// A duplicate name means the same metadata reached the compilation twice; the first with
// content wins.
.GroupBy(file => file.FileName, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g => g.FirstOrDefault(file => file.Text.Length > 0) ?? g.First(), ...)

The comment assumes both copies hold the same metadata. Nothing checks that. When two files share a name but have different content, one is dropped silently.

Repro (verified by driving CSharpGeneratorDriver directly against current main)

Give a generator that wants things.json two AdditionalTexts:

  • /a/Legacy/things.json defining Old
  • /a/Metadata/things.json defining New

The run reports 0 diagnostics and emits public class Old. Which file wins depends on item order. The easy ways to hit this are <AdditionalFiles Include="**/*.json" /> and a stale copy left in another folder.

GeneratorTestHarness can't show this, because it keys its input files by name.

Why it matters

The package's own rule, stated a few lines further down, is that producing no output or the wrong output with no explanation must always be reported. This path generates the wrong code with no explanation.

Suggested fix

When a group holds more than one non-empty file and their texts differ, report a diagnostic that names both paths. That could be a new overridable descriptor such as MetadataFileDuplicated, or MetadataParseFailed with a "supplied more than once: , " reason. Identical duplicates can stay silent, as they are today.

Acceptance criteria

  • Two same-named metadata files with different content produce a diagnostic naming both paths.
  • Two same-named files with identical content still produce none.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions