Skip to content

Ignore formatting changes from Html that change non-whitespace characters - #84747

Merged
davidwengier merged 7 commits into
dotnet:mainfrom
davidwengier:import-razor-formatting-logs
Aug 4, 2026
Merged

Ignore formatting changes from Html that change non-whitespace characters#84747
davidwengier merged 7 commits into
dotnet:mainfrom
davidwengier:import-razor-formatting-logs

Conversation

@davidwengier

@davidwengier davidwengier commented Aug 4, 2026

Copy link
Copy Markdown
Member

Fixes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/3042344 / https://devdiv.visualstudio.com/DevDiv/_workitems/edit/3040290

This is an odd one, where the JavaScript formatter is removing legitimate code because its confused by Razor concepts. Not a lot we can do about it in terms of nice formatting, but we can not error out and crash at least. So this does that.

Microsoft Reviewers: Open in CodeFlow

davidwengier and others added 4 commits August 4, 2026 12:07
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4eefa5f3-9ccf-4834-908f-b65ec8b5f77b
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4eefa5f3-9ccf-4834-908f-b65ec8b5f77b
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4eefa5f3-9ccf-4834-908f-b65ec8b5f77b
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4eefa5f3-9ccf-4834-908f-b65ec8b5f77b
Copilot AI review requested due to automatic review settings August 4, 2026 02:57
@davidwengier
davidwengier requested a review from a team as a code owner August 4, 2026 02:57
@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.

Pull request overview

This PR hardens Razor HTML formatting by filtering out HTML-formatter edits that change non-whitespace characters (to avoid destructive “formatting” in Razor/script scenarios), and adds regression coverage via both targeted unit tests and captured formatting logs.

Changes:

  • Filter out HTML-formatter TextChanges whose replacement would alter non-whitespace content in HtmlFormattingPass.
  • Add focused unit tests covering “equivalent non-whitespace” edits and mixed safe/unsafe edits around length changes.
  • Add two real-world formatting-log regressions (PageForGrid / PageForMultiGrid) and corresponding test entries.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPass.cs Adds filtering to drop HTML-formatter edits that change non-whitespace content.
src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Formatting/HtmlFormattingPassTest.cs Adds unit tests validating non-whitespace-preserving edits are kept and unsafe edits don’t disrupt surrounding whitespace edits.
src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Formatting/DocumentFormattingTest.cs Adds an end-to-end regression ensuring non-whitespace corruption in <script> is ignored.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/Cohost/Formatting/FormattingLogTest.cs Registers new formatting-log-based regressions as tests.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForMultiGrid/InitialDocument.txt Adds captured initial document for the MultiGrid formatting regression.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForMultiGrid/HtmlChanges.json Adds captured HTML formatter changes for the MultiGrid regression.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForMultiGrid/Options.json Adds captured formatting options for the MultiGrid regression.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForMultiGrid/Range.json Adds captured range info for the MultiGrid regression.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForMultiGrid/FileKind.json Adds captured file kind for the MultiGrid regression.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForGrid/InitialDocument.txt Adds captured initial document for the Grid formatting regression.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForGrid/HtmlChanges.json Adds captured HTML formatter changes for the Grid regression.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForGrid/Options.json Adds captured formatting options for the Grid regression.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForGrid/Range.json Adds captured range info for the Grid regression.
src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/TestFiles/FormattingLog/PageForGrid/FileKind.json Adds captured file kind for the Grid regression.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 4, 2026 03:05

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.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Formatting/DocumentFormattingTest.cs:1334

  • The expected formatted output has inconsistent indentation on the const ids = ... line (it appears to have extra leading spaces compared to the surrounding <text> block and the input/htmlFormatted strings). This is likely to make the regression test fail for the wrong reason.
                              const ids = grid.getIds();

Copilot AI review requested due to automatic review settings August 4, 2026 03:33

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.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated no new comments.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4eefa5f3-9ccf-4834-908f-b65ec8b5f77b
Copilot AI review requested due to automatic review settings August 4, 2026 08:01

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.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPass.cs:214

  • FilterOutNonWhitespaceChanges allocates changedText via originalText.WithChanges(candidateChanges) even though formattedText (computed earlier in this method) should already represent the result of applying candidateChanges. Reusing formattedText avoids an extra full-text rewrite/allocation in this warning-path.
            var changedText = originalText.WithChanges(candidateChanges);

src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPass.cs:245

  • The warning message is very generic and can be noisy in logs without giving enough diagnostic value. Including how many changes were dropped (and total) would make the warning actionable when this happens in the wild.
            _logger.LogWarning("Ignoring non-whitespace changes returned by the HTML formatter.");

@davidwengier
davidwengier merged commit 012b788 into dotnet:main Aug 4, 2026
25 checks passed
@davidwengier
davidwengier deleted the import-razor-formatting-logs branch August 4, 2026 21:57
@jjonescz jjonescz added this to the 18.11 milestone Aug 25, 2026
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.

4 participants