Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public override Stream Read()
public bool Equals(SourceGeneratorProjectItem? other)
{
if (other is null ||
CssScope != other.CssScope)
CssScope != other.CssScope ||
PhysicalPath != other.PhysicalPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this compare using PlatformUtilities.OSSpecificComparer? Or, is it OK if a case-insensitive rename on Windows evicts the cached item?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The casing of the filename actually flows through to the component name, which is case sensitive.

Counter.razor and CouNter.razor produce <Counter /> and <CouNter /> respectively and they won't match as valid components if the casing doesn't also match.

So it's correct that we should evict with a case sensitive rename, as its effectively now a different component.

I should add a test to cover that. Will do so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that at least helps explain why tooling and compiler differ in their approaches to case sensitivity. I suspect tooling might have a few edge case bugs around this. I might log an issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation @chsienki! That was super helpful to me.

{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3508,5 +3508,95 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.
#pragma warning restore 1591
");
}

[Fact]
public async Task IncrementalCompilation_RerunsGenerator_When_AdditionalFileRenamed()
{
// Arrange
using var eventListener = new RazorEventListener();
var project = CreateTestProject(new()
{
["Pages/Index.razor"] = "<h1>Hello world</h1>",
["Pages/Counter.razor"] = "<h1>Counter</h1>",
});
var compilation = await project.GetCompilationAsync();
var (driver, additionalTexts, analyzerConfigOptionProvider) = await GetDriverWithAdditionalTextAndProviderAsync(project);

var result = RunGenerator(compilation!, ref driver);
Assert.Empty(result.Diagnostics);
Assert.Equal(2, result.GeneratedSources.Length);

eventListener.Clear();

// Verify no changes when re-running
result = RunGenerator(compilation!, ref driver)
.VerifyOutputsMatch(result);

Assert.Empty(result.Diagnostics);
Assert.Equal(2, result.GeneratedSources.Length);
Assert.Empty(eventListener.Events);

// Rename Counter.razor to NewCounter.razor by removing and re-adding with same content
var counterText = additionalTexts.First(f => f.Path.EndsWith("Counter.razor", StringComparison.OrdinalIgnoreCase));
var renamedText = new TestAdditionalText("Pages/NewCounter.razor", counterText.GetText()!);
driver = driver.RemoveAdditionalTexts([counterText])
.AddAdditionalTexts([renamedText]);

// Update the analyzer config options with the new target path
analyzerConfigOptionProvider.AdditionalTextOptions[renamedText.Path] = new TestAnalyzerConfigOptions
{
["build_metadata.AdditionalFiles.TargetPath"] = Convert.ToBase64String(Encoding.UTF8.GetBytes(renamedText.Path))
};
driver = driver.WithUpdatedAnalyzerConfigOptions(analyzerConfigOptionProvider);

result = RunGenerator(compilation!, ref driver);

Assert.Empty(result.Diagnostics);
Assert.Equal(2, result.GeneratedSources.Length);

// Verify the new file was processed
Assert.Collection(eventListener.Events,
e => e.AssertSingleItem("ParseRazorDocumentStart", "Pages/NewCounter.razor"),
e => e.AssertSingleItem("ParseRazorDocumentStop", "Pages/NewCounter.razor"),
e => e.AssertSingleItem("GenerateDeclarationCodeStart", "/Pages/NewCounter.razor"),
e => e.AssertSingleItem("GenerateDeclarationCodeStop", "/Pages/NewCounter.razor"),
e => Assert.Equal("DiscoverTagHelpersFromCompilationStart", e.EventName),
e => Assert.Equal("DiscoverTagHelpersFromCompilationStop", e.EventName),
e => e.AssertSingleItem("RewriteTagHelpersStart", "Pages/NewCounter.razor"),
e => e.AssertSingleItem("RewriteTagHelpersStop", "Pages/NewCounter.razor"),
e => e.AssertSingleItem("CheckAndRewriteTagHelpersStart", "Pages/Index.razor"),
e => e.AssertSingleItem("CheckAndRewriteTagHelpersStop", "Pages/Index.razor"),
e => e.AssertSingleItem("CheckAndRewriteTagHelpersStart", "Pages/NewCounter.razor"),
e => e.AssertSingleItem("CheckAndRewriteTagHelpersStop", "Pages/NewCounter.razor"),
e => e.AssertPair("RazorCodeGenerateStart", "Pages/Index.razor", "Runtime"),
e => e.AssertPair("RazorCodeGenerateStop", "Pages/Index.razor", "Runtime"),
e => e.AssertPair("RazorCodeGenerateStart", "Pages/NewCounter.razor", "Runtime"),
e => e.AssertPair("RazorCodeGenerateStop", "Pages/NewCounter.razor", "Runtime"),
e => e.AssertSingleItem("AddSyntaxTrees", "Pages_NewCounter_razor.g.cs")
);

// Verify the generated source has the correct namespace and class name
var newCounterSource = result.GeneratedSources.FirstOrDefault(s => s.HintName.Contains("NewCounter"));
Assert.Contains("namespace MyApp.Pages", newCounterSource.SourceText.ToString());
Assert.Contains("public partial class NewCounter", newCounterSource.SourceText.ToString());

// Do a case-only rename and make sure we update the generated class name still
// as component names are case sensitive even on Windows.
var renamedText2 = new TestAdditionalText("Pages/NewCouNter.razor", counterText.GetText()!);
driver = driver.RemoveAdditionalTexts([renamedText])
.AddAdditionalTexts([renamedText2]);

// Update the analyzer config options with the new target path
analyzerConfigOptionProvider.AdditionalTextOptions[renamedText2.Path] = new TestAnalyzerConfigOptions
{
["build_metadata.AdditionalFiles.TargetPath"] = Convert.ToBase64String(Encoding.UTF8.GetBytes(renamedText2.Path))
};
driver = driver.WithUpdatedAnalyzerConfigOptions(analyzerConfigOptionProvider);

result = RunGenerator(compilation!, ref driver);

var newCouNterSource = result.GeneratedSources.FirstOrDefault(s => s.HintName.Contains("NewCouNter"));
Assert.Contains("public partial class NewCouNter", newCouNterSource.SourceText.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;
using Microsoft.AspNetCore.Razor.Language;
using Xunit;

Expand Down Expand Up @@ -115,5 +116,34 @@ public void PathWithoutExtension_ExcludesExtension(string path, string expected)
// Assert
Assert.Equal(expected, fileName);
}

[Fact]
public void ProjectItems_WithDifferentPaths_SameContent_AreNotEqual()
{
// Two additional texts with same contents, but different paths
var content = "<h1>Hello World</h1>";
var additionalText1 = new TestAdditionalText(content, Encoding.UTF8, "File1.cshtml");
var additionalText2 = new TestAdditionalText(content, Encoding.UTF8, "File2.cshtml");

var projectItem1 = new SourceGeneratorProjectItem(
filePath: "/Views/Home/Index.cshtml",
basePath: "/",
relativePhysicalPath: "/Views/Home",
fileKind: RazorFileKind.Legacy,
additionalText: additionalText1,
cssScope: null);

var projectItem2 = new SourceGeneratorProjectItem(
filePath: "/Views/About/Index.cshtml",
basePath: "/",
relativePhysicalPath: "/Views/About",
fileKind: RazorFileKind.Legacy,
additionalText: additionalText2,
cssScope: null);

// Act & Assert
Assert.NotEqual(projectItem1, projectItem2);
Assert.False(projectItem1.Equals(projectItem2));
}
}
}