Skip to content

Commit

Permalink
[release/7.0] Don't generate a nearly empty file (#77187)
Browse files Browse the repository at this point in the history
* 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.

* Add braces

Co-authored-by: Jason Malinowski <jason.malinowski@microsoft.com>
  • Loading branch information
github-actions[bot] and jasonmalinowski authored Nov 9, 2022
1 parent 3b603f3 commit 00816bf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
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);
}
}
}

0 comments on commit 00816bf

Please sign in to comment.