Skip to content

[release/7.0] Don't generate a nearly empty file #77187

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

Merged
merged 2 commits into from
Nov 9, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
Expand All @@ -27,26 +28,31 @@ public static void RegisterDiagnostics(this IncrementalGeneratorInitializationCo
public static void RegisterConcatenatedSyntaxOutputs<TNode>(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider<TNode> nodes, string fileName)
where TNode : SyntaxNode
{
IncrementalValueProvider<string> generatedMethods = nodes
IncrementalValueProvider<ImmutableArray<string>> generatedMethods = nodes
.Select(
static (node, ct) => node.NormalizeWhitespace().ToFullString())
.Collect()
.Select(static (generatedSources, ct) =>
.Collect();

context.RegisterSourceOutput(generatedMethods,
(context, generatedSources) =>
{
// Don't generate a file if we don't have to, to avoid the extra IDE overhead once we have generated
// files in play.
if (generatedSources.IsEmpty)
{
return;
}

StringBuilder source = new();
// Mark in source that the file is auto-generated.
source.AppendLine("// <auto-generated/>");
foreach (string generated in generatedSources)
{
source.AppendLine(generated);
}
return source.ToString();
});

context.RegisterSourceOutput(generatedMethods,
(context, source) =>
{
context.AddSource(fileName, source);
// Once https://github.com/dotnet/roslyn/issues/61326 is resolved, we can avoid the ToString() here.
context.AddSource(fileName, source.ToString());
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,33 +641,30 @@ public async Task ValidateSnippetsWithMultipleSources(string id, string[] source
TestUtils.AssertPostSourceGeneratorCompilation(newComp);
}

public static IEnumerable<object[]> CodeSnippetsToCompileToValidateAllowUnsafeBlocks()
public static IEnumerable<object[]> CodeSnippetsToVerifyNoTreesProduced()
{
yield return new object[] { ID(), CodeSnippets.TrivialClassDeclarations, TestTargetFramework.Net, true };

{
string source = @"
string source = @"
using System.Runtime.InteropServices;
public class Basic { }
";
yield return new object[] { ID(), source, TestTargetFramework.Standard, false };
yield return new object[] { ID(), source, TestTargetFramework.Framework, false };
yield return new object[] { ID(), source, TestTargetFramework.Net, false };
}
yield return new object[] { ID(), source, TestTargetFramework.Standard };
yield return new object[] { ID(), source, TestTargetFramework.Framework };
yield return new object[] { ID(), source, TestTargetFramework.Net };
}

[Theory]
[MemberData(nameof(CodeSnippetsToCompileToValidateAllowUnsafeBlocks))]
public async Task ValidateRequireAllowUnsafeBlocksDiagnosticNoTrigger(string id, string source, TestTargetFramework framework, bool allowUnsafe)
[MemberData(nameof(CodeSnippetsToVerifyNoTreesProduced))]
public async Task ValidateNoGeneratedOuptutForNoImport(string id, string source, TestTargetFramework framework)
{
TestUtils.Use(id);
Compilation comp = await TestUtils.CreateCompilation(source, framework, allowUnsafe: allowUnsafe);
Compilation comp = await TestUtils.CreateCompilation(source, framework, allowUnsafe: false);
TestUtils.AssertPreSourceGeneratorCompilation(comp);

var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.LibraryImportGenerator());
Assert.Empty(generatorDiags);

TestUtils.AssertPostSourceGeneratorCompilation(newComp);
// Assert we didn't generate any syntax trees, even empty ones
Assert.Same(comp, newComp);
}
}
}