Skip to content

Comments

Rename a .razor file when Roslyn renames the component type name#12606

Merged
davidwengier merged 8 commits intodotnet:mainfrom
davidwengier:RenameRazorFromCSharp
Jan 7, 2026
Merged

Rename a .razor file when Roslyn renames the component type name#12606
davidwengier merged 8 commits intodotnet:mainfrom
davidwengier:RenameRazorFromCSharp

Conversation

@davidwengier
Copy link
Member

Part of #8541

Responds to Roslyn renaming C# symbols, and if it's a Razor component, defined in a Razor file, then it renames the file (and associated files)

Needs a Roslyn bump to get the notification interface, but I'm putting this up or I'll lose track of the branch until next year :)

@davidwengier davidwengier requested a review from a team as a code owner December 17, 2025 03:59
@davidwengier davidwengier marked this pull request as draft December 17, 2025 20:52
@davidwengier davidwengier marked this pull request as ready for review December 20, 2025 03:07
@davidwengier
Copy link
Member Author

davidwengier commented Dec 31, 2025


namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

using Rename = Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers.Rename;
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, which change made the using changes in this file necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

Something in Roslyn changed, I can't remember the exact thing that conflicted, but the Roslyn version bump alone introduced the issue.

if (classDecl.Parent is null ||
classDecl.Parent.GetLeadingTrivia() is not [{ } firstTrivia, ..] ||
!firstTrivia.IsKind(CodeAnalysis.CSharp.SyntaxKind.PragmaChecksumDirectiveTrivia) ||
firstTrivia.ToString().Split(' ') is not ["#pragma", "checksum", { } quotedRazorFileName, ..])
Copy link
Contributor

Choose a reason for hiding this comment

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

..

Worth verifying there are two more entries in the trivia?

Copy link
Member Author

Choose a reason for hiding this comment

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

From my quick debug, there are 3 more, which makes me wonder if this would just be being a little too fussy about what the exact code gen should be. I think what does make sense though, now that I think about it, is validating that quotedRazorFileName is actually a .razor file name.

In other words, I don't think we should necessarily care here if the compiler starts emitting more leading trivia (eg, suppressing some new C# compiler warning), or if the .NET 9 compiler emits 3 bits of trivia and the .NET 10 compiler emits 4. If we find a pragma we can use, we should use it IMO.

}

var razorFileName = quotedRazorFileName.Trim('"');
var newFileName = Path.Combine(Path.GetDirectoryName(razorFileName), newName + ".razor");
Copy link
Contributor

Choose a reason for hiding this comment

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

".razor"

Is it worth getting the extension from razorFileName?

Copy link
Member Author

@davidwengier davidwengier Jan 6, 2026

Choose a reason for hiding this comment

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

If we get here and the extension from that is anything but .razor, then just about every other assumption we have about the world is probably wrong too. But I'll add an assert just in case :)

Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

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

:shipit:

@davidwengier davidwengier merged commit 3fae1e8 into dotnet:main Jan 7, 2026
10 checks passed
@davidwengier davidwengier deleted the RenameRazorFromCSharp branch January 7, 2026 05:59
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jan 7, 2026
@davidwengier davidwengier modified the milestones: Next, 18.4 Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants