Skip to content

Prevent odd document Uris from causing inadvertent syntax errors - #85886

Open
davidwengier wants to merge 3 commits into
dotnet:mainfrom
davidwengier:razor-diff-diagnostics
Open

davidwengier wants to merge 3 commits into
dotnet:mainfrom
davidwengier:razor-diff-diagnostics

Conversation

@davidwengier

@davidwengier davidwengier commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Fixes dotnet/vscode-csharp#9831

For the left hand side of a diff, VS Code will create documents with Uris like git://filename.razor?{"path":"filename.razor"}, and the quotes (and other things) can end up causing syntax errors due to them incorrectly terminating line directives, and at the same time our file kind detection can end up seeing the .razor? and falling back to the legacy (cshtml) file kind, which then can cause other errors because the directive set is different.

This fixes both by encoding file paths as appropriate for generated C#, and using a method we already had to find the "real" file path from a Uri in a few more places.

Microsoft Reviewers: Open in CodeFlow

Copilot AI balanced review requested due to automatic review settings October 2, 2026 03:43
@davidwengier
davidwengier requested review from a team as code owners October 2, 2026 03:43
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The line-break regression test does not verify that each character receives the correct identity-preserving encoding.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Prevents virtual Razor document URIs from corrupting generated C# or causing incorrect file-kind detection.

Changes:

  • Normalizes URI-shaped logical paths while preserving document identity.
  • Safely escapes generated directives and metadata literals.
  • Adds regression coverage for virtual documents and URI variants.
File Description
.github/​instructions/​Razor.instructions.md Documents virtual-URI handling.
FileUtilities.cs Adds URI-path normalization heuristics.
CodeWriterExtensions.cs Escapes URI directive filenames.
RazorCSharpDocumentWriter.cs Safely writes checksum paths.
MetadataAttributeTargetExtension.cs Escapes metadata literals.
FileKinds.cs Classifies normalized URI paths.
RazorSourceGenerator.RazorProviders.cs Uses normalized logical project paths.
SourceGeneratorProjectItem.cs Normalizes source relative paths.
RazorSourceGeneratorVirtualDocumentTests.cs Tests virtual-document generation.
FileKindsTest.cs Tests URI file-kind detection.
MetadataAttributeTargetExtensionTest.cs Tests metadata escaping.
CSharpCodeWriterTest.cs Tests directive URI encoding.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False Razor diagnostics on historical file in Git diff editor

2 participants