Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Don't generate a nearly empty file
When generators are running in the Visual Studio IDE, there's some
overhead to having to manage a project that actually has generated
output. It requires us to maintain two Roslyn Compilation objects,
each which have their own symbol information. These interop generators
are producing a file that's effectively empty (just an <auto-generated>
comment on the top), and since they're installed in all .NET 7.0
applications, they are the reason we'll be having to manage more memory
than before. Since the fix is simple enough to only generate the output
if necessary, we should do so.

This also will help with telemetry, since we have telemetry that lets us
tracking in the wild which generators are producing how many files;
if we're always producing exactly one file we won't know how many
sessions out there are actually using this generator in a meaningful
way.
  • Loading branch information
jasonmalinowski authored and github-actions committed Oct 18, 2022
commit c56b109d122be739c17e65d527493abd8bfb786f
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,29 @@ 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);
}
}
}