Skip to content

Commit a0a4e82

Browse files
committed
Some PR feedback
1 parent 21dbf0d commit a0a4e82

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

src/Compilers/Test/Core/SourceGeneration/TestGenerators.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,16 @@ public IncrementalAndSourceCallbackGenerator(Action<GeneratorInitializationConte
202202

203203
namespace Microsoft.NET.Sdk.Razor.SourceGenerators
204204
{
205-
internal sealed class RazorSourceGenerator : ISourceGenerator
205+
/// <summary>
206+
/// We check for the presence of the razor SG by full name
207+
/// so we have to make sure this is the right name in the right namespace.
208+
/// </summary>
209+
internal sealed class RazorSourceGenerator(Action<GeneratorExecutionContext> execute) : ISourceGenerator
206210
{
207211
private int _callCount = 0;
208212

209213
public void Initialize(GeneratorInitializationContext context) { }
210214

211-
public void Execute(GeneratorExecutionContext context)
212-
{
213-
context.AddSource("file.cs", $"// callCount: {_callCount++}");
214-
}
215+
public void Execute(GeneratorExecutionContext context) => execute(context);
215216
}
216217
}

src/VisualStudio/Core/Test.Next/Services/ServiceHubServicesTests.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,9 +1488,11 @@ internal async Task TestSourceGenerationExecution_RazorGeneratorAlwaysRuns_Other
14881488
var globalOptionService = workspace.ExportProvider.GetExportedValue<IGlobalOptionService>();
14891489
globalOptionService.SetGlobalOption(WorkspaceConfigurationOptionsStorage.SourceGeneratorExecution, executionPreference);
14901490

1491-
var callCount = 0;
1492-
var generator1 = new CallbackGenerator(() => ("hintName.cs", "// callCount: " + callCount++));
1493-
var generator2 = new Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator();
1491+
var callBackCallCount = 0;
1492+
var generator1 = new CallbackGenerator(() => ("hintName.cs", "// callCount: " + callBackCallCount++));
1493+
1494+
var razorCallCount = 0;
1495+
var generator2 = new Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator((c) => c.AddSource("file.cs", "// callCount: " + razorCallCount++));
14941496

14951497
var projectId = ProjectId.CreateNewId();
14961498
var project = workspace.CurrentSolution
@@ -1506,25 +1508,29 @@ internal async Task TestSourceGenerationExecution_RazorGeneratorAlwaysRuns_Other
15061508
Assert.True(workspace.SetCurrentSolution(_ => tempDoc.Project.Solution, WorkspaceChangeKind.SolutionChanged));
15071509

15081510
// Initial: all generators run
1509-
await ValidateSourceGeneratorDocuments(expectedCallback: 0,
1510-
expectedRazor: 0);
1511+
await ValidateSourceGeneratorDocuments(
1512+
expectedCallback: 0,
1513+
expectedRazor: 0);
15111514

15121515
// Now, make a simple edit to the main document.
15131516
Contract.ThrowIfFalse(workspace.TryApplyChanges(workspace.CurrentSolution.WithDocumentText(tempDoc.Id, SourceText.From("// new text"))));
15141517

1515-
await ValidateSourceGeneratorDocuments(expectedCallback: executionPreference == SourceGeneratorExecutionPreference.Automatic ? 1 : 0,
1516-
expectedRazor: 1);
1518+
await ValidateSourceGeneratorDocuments(
1519+
expectedCallback: executionPreference == SourceGeneratorExecutionPreference.Automatic ? 1 : 0,
1520+
expectedRazor: 1);
15171521

15181522
// Get the documents again and ensure nothing ran
1519-
await ValidateSourceGeneratorDocuments(expectedCallback: executionPreference == SourceGeneratorExecutionPreference.Automatic ? 1 : 0,
1520-
expectedRazor: 1);
1523+
await ValidateSourceGeneratorDocuments(
1524+
expectedCallback: executionPreference == SourceGeneratorExecutionPreference.Automatic ? 1 : 0,
1525+
expectedRazor: 1);
15211526

15221527
// Make another change, but this time enqueue an update too
15231528
Contract.ThrowIfFalse(workspace.TryApplyChanges(workspace.CurrentSolution.WithDocumentText(tempDoc.Id, SourceText.From("// more new text"))));
15241529
workspace.EnqueueUpdateSourceGeneratorVersion(projectId: null, forceRegeneration: false);
15251530

1526-
await ValidateSourceGeneratorDocuments(expectedCallback: executionPreference == SourceGeneratorExecutionPreference.Automatic ? 2 : 1,
1527-
expectedRazor: 2);
1531+
await ValidateSourceGeneratorDocuments(
1532+
expectedCallback: executionPreference == SourceGeneratorExecutionPreference.Automatic ? 2 : 1,
1533+
expectedRazor: 2);
15281534

15291535
async Task ValidateSourceGeneratorDocuments(int expectedCallback, int expectedRazor)
15301536
{

src/Workspaces/Core/Portable/Workspace/Solution/SolutionCompilationState.RegularCompilationTracker_Generators.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private sealed partial class RegularCompilationTracker : ICompilationTracker
211211
if (creationPolicy == GeneratedDocumentCreationPolicy.CreateRequired)
212212
{
213213
// the documents we got back are only for the required generators, meaning any documents from other generators remain the same.
214-
var generatorsThatRan = infos.Select(di => di.DocumentIdentity.Generator).Distinct().ToImmutableHashSet();
214+
var generatorsThatRan = infos.Select(di => di.DocumentIdentity.Generator).Distinct().ToArray();
215215

216216
// go through the old docs and add any that aren't in the new set
217217
foreach (var (_, oldDocumentState) in oldGeneratedDocuments.States)
@@ -300,6 +300,8 @@ await newGeneratedDocuments.States.Values.SelectAsArrayAsync(
300300
var generationDateTime = DateTime.Now;
301301
foreach (var generatorResult in runResult.Results)
302302
{
303+
// When we only run required generators, there may be generators that didn't run at all, and so didn't produce any sources.
304+
// Note that this is different from running and producing zero documents, in which case we still need to consider it.
303305
if (generatorResult.GeneratedSources.IsDefault)
304306
continue;
305307

0 commit comments

Comments
 (0)