From 7fcbc98082c83e9c45b3bbfe3bbc3065e95b1f2d Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Thu, 10 Apr 2025 23:03:56 +0200 Subject: [PATCH] Check CSS scope when comparing source generator items --- .../SourceGeneratorProjectItem.cs | 10 ++++-- .../RazorSourceGeneratorTests.cs | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/SourceGeneratorProjectItem.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/SourceGeneratorProjectItem.cs index 2c98017e827..b2ec91d063f 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/SourceGeneratorProjectItem.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/SourceGeneratorProjectItem.cs @@ -54,7 +54,13 @@ public override Stream Read() public bool Equals(SourceGeneratorProjectItem? other) { - if (ReferenceEquals(AdditionalText, other?.AdditionalText)) + if (other is null || + CssScope != other.CssScope) + { + return false; + } + + if (ReferenceEquals(AdditionalText, other.AdditionalText)) { return true; } @@ -64,7 +70,7 @@ public bool Equals(SourceGeneratorProjectItem? other) // It's technically possible for these hashes to collide, but other things would // also break in those cases, so for now we're okay with this. var thisHash = AdditionalText.GetText()?.GetContentHash() ?? []; - var otherHash = other?.AdditionalText.GetText()?.GetContentHash() ?? []; + var otherHash = other.AdditionalText.GetText()?.GetContentHash() ?? []; return thisHash.SequenceEqual(otherHash); } diff --git a/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs b/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs index 52e355e0e9a..c1d87adbfee 100644 --- a/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs +++ b/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs @@ -1203,6 +1203,38 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) {} } } + [Fact] + public async Task IncrementalCompilation_RazorFiles_CssScopeRemoved() + { + // Compile with CssScope set. + var project = CreateTestProject(new() + { + ["Pages/Index.razor"] = "

Hello world

", + }); + var compilation = await project.GetCompilationAsync(); + var (driver, _, options) = await GetDriverWithAdditionalTextAndProviderAsync(project, static options => + { + options.AdditionalTextOptions["Pages/Index.razor"]["build_metadata.AdditionalFiles.CssScope"] = "test-css-scope"; + }); + + var result = RunGenerator(compilation!, ref driver); + result.Diagnostics.Verify(); + + // CSS isolation is enabled. + Assert.Contains("

Hello world

", result.GeneratedSources.Single().SourceText.ToString()); + + // Unset CssScope. + options = options.Clone(); + options.AdditionalTextOptions["Pages/Index.razor"].Options.Remove("build_metadata.AdditionalFiles.CssScope"); + driver = driver.WithUpdatedAnalyzerConfigOptions(options); + + result = RunGenerator(compilation!, ref driver); + result.Diagnostics.Verify(); + + // CSS isolation is disabled. + Assert.Contains("

Hello world

", result.GeneratedSources.Single().SourceText.ToString()); + } + [Fact] public async Task SourceGenerator_CshtmlFiles_Works() {