Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions SourceGeneratorToolkit.Test/GeneratorBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,16 @@ public void AJsonNullDocumentIsReportedRatherThanTreatedAsEmpty()
}

[TestMethod]
public void AnEmptyMetadataFileCountsAsMissing()
[DataRow("")]
[DataRow(" \n\t")]
public void AnEmptyMetadataFileIsReportedAsUnparseableNotMissing(string contents)
{
GeneratorRunResult result = Harness.Run(
new ThingsGenerator(),
new Dictionary<string, string> { ["things.json"] = "" });
new Dictionary<string, string> { ["things.json"] = contents });

Assert.AreEqual(1, result.Diagnostics.Length);
Assert.AreEqual(TestDiagnostics.MetadataFileMissing.Id, result.Diagnostics[0].Id);
Assert.AreEqual(TestDiagnostics.MetadataParseFailed.Id, result.Diagnostics[0].Id, result.Diagnostics[0].GetMessage());
}

[TestMethod]
Expand Down
10 changes: 7 additions & 3 deletions SourceGeneratorToolkit/GeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
SourceText? sourceText = file.GetText(cancellationToken);
return new MetadataFile(NameOf(file.Path), sourceText?.ToString() ?? string.Empty, sourceText, file.Path);
})
.Where(file => file.Text.Length > 0)
.Collect();

context.RegisterSourceOutput(metadataFiles, (productionContext, files) =>
{
// A duplicate name means the same metadata reached the compilation twice; the first wins.
// A duplicate name means the same metadata reached the compilation twice; the first with
// content wins. An empty file is still present, so it is handed on and fails to parse
// rather than being reported as missing.
Dictionary<string, MetadataFile> byName = files
.GroupBy(file => file.FileName, StringComparer.Ordinal)
.ToDictionary(group => group.Key, group => group.First(), StringComparer.Ordinal);
.ToDictionary(
group => group.Key,
group => group.FirstOrDefault(file => file.Text.Length > 0) ?? group.First(),
StringComparer.Ordinal);

// A missing file must say so. Producing no output and no explanation is indistinguishable
// from a generator that simply had nothing to emit.
Expand Down
Loading