Skip to content

Commit 00ef962

Browse files
authored
Check CSS scope when comparing source generator items (#11728)
1 parent b80b58f commit 00ef962

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/SourceGenerators/SourceGeneratorProjectItem.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ public override Stream Read()
6060

6161
public bool Equals(SourceGeneratorProjectItem? other)
6262
{
63-
if (ReferenceEquals(AdditionalText, other?.AdditionalText))
63+
if (other is null ||
64+
CssScope != other.CssScope)
65+
{
66+
return false;
67+
}
68+
69+
if (ReferenceEquals(AdditionalText, other.AdditionalText))
6470
{
6571
return true;
6672
}
@@ -70,7 +76,7 @@ public bool Equals(SourceGeneratorProjectItem? other)
7076
// It's technically possible for these hashes to collide, but other things would
7177
// also break in those cases, so for now we're okay with this.
7278
var thisHash = AdditionalText.GetText()?.GetContentHash() ?? [];
73-
var otherHash = other?.AdditionalText.GetText()?.GetContentHash() ?? [];
79+
var otherHash = other.AdditionalText.GetText()?.GetContentHash() ?? [];
7480
return thisHash.SequenceEqual(otherHash);
7581
}
7682

src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,38 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) {}
12031203
}
12041204
}
12051205

1206+
[Fact]
1207+
public async Task IncrementalCompilation_RazorFiles_CssScopeRemoved()
1208+
{
1209+
// Compile with CssScope set.
1210+
var project = CreateTestProject(new()
1211+
{
1212+
["Pages/Index.razor"] = "<h1>Hello world</h1>",
1213+
});
1214+
var compilation = await project.GetCompilationAsync();
1215+
var (driver, _, options) = await GetDriverWithAdditionalTextAndProviderAsync(project, static options =>
1216+
{
1217+
options.AdditionalTextOptions["Pages/Index.razor"]["build_metadata.AdditionalFiles.CssScope"] = "test-css-scope";
1218+
});
1219+
1220+
var result = RunGenerator(compilation!, ref driver);
1221+
result.Diagnostics.Verify();
1222+
1223+
// CSS isolation is enabled.
1224+
Assert.Contains("<h1 test-css-scope>Hello world</h1>", result.GeneratedSources.Single().SourceText.ToString());
1225+
1226+
// Unset CssScope.
1227+
options = options.Clone();
1228+
options.AdditionalTextOptions["Pages/Index.razor"].Options.Remove("build_metadata.AdditionalFiles.CssScope");
1229+
driver = driver.WithUpdatedAnalyzerConfigOptions(options);
1230+
1231+
result = RunGenerator(compilation!, ref driver);
1232+
result.Diagnostics.Verify();
1233+
1234+
// CSS isolation is disabled.
1235+
Assert.Contains("<h1>Hello world</h1>", result.GeneratedSources.Single().SourceText.ToString());
1236+
}
1237+
12061238
[Fact]
12071239
public async Task SourceGenerator_CshtmlFiles_Works()
12081240
{

0 commit comments

Comments
 (0)