Skip to content

Preserve #: file-based app directives during formatting#82996

Merged
jasonmalinowski merged 2 commits intodotnet:mainfrom
dusrdev:main
Apr 2, 2026
Merged

Preserve #: file-based app directives during formatting#82996
jasonmalinowski merged 2 commits intodotnet:mainfrom
dusrdev:main

Conversation

@dusrdev
Copy link
Copy Markdown
Contributor

@dusrdev dusrdev commented Mar 31, 2026

Fix C# formatting for file-based app directives parsed as IgnoredDirectiveTriviaSyntax.

When the formatter touches a line like:

 #:package Newtonsoft.Json@13.0.3

Roslyn was rewriting it to:

# : package Newtonsoft.Json@13.0.3

That breaks file-based app syntax and causes downstream issues such as loss of project/file-based-app recognition and completion failures.

Root cause

File-based app directives are represented as IgnoredDirectiveTriviaSyntax, but StructuredTriviaFormattingRule did not preserve spacing for the syntax-significant token sequence:

  • HashToken -> ColonToken
  • ColonToken -> Content

As a result, generic punctuation spacing rules inserted spaces around : once the directive line became eligible for formatting.

Fix

Add a special-case in StructuredTriviaFormattingRule to force zero spaces for:

  • # followed by :
  • : followed by the directive content

inside IgnoredDirectiveTriviaSyntax.

Tests

Added formatter coverage for file-based app directives:

  • unchanged formatting for a normal #:package ... directive
  • regression test showing that a directive with leading whitespace is normalized to valid #:package ... form instead of # : package ...

This should also fix this vscode-csharp issue

Microsoft Reviewers: Open in CodeFlow

@dusrdev dusrdev requested a review from a team as a code owner March 31, 2026 17:17
@dotnet-policy-service dotnet-policy-service bot added Community The pull request was submitted by a contributor who is not a Microsoft employee. VSCode labels Mar 31, 2026
@jjonescz jjonescz added the Feature - Run File #: and #! directives and file-based C# programs label Apr 1, 2026
@jjonescz jjonescz requested a review from a team April 1, 2026 09:11
@RikkiGibson RikkiGibson self-assigned this Apr 1, 2026
@jasonmalinowski jasonmalinowski self-assigned this Apr 2, 2026
@jasonmalinowski
Copy link
Copy Markdown
Member

The one test failure was a random failure, so rerunning.

@jasonmalinowski jasonmalinowski merged commit b99bcbb into dotnet:main Apr 2, 2026
25 checks passed
@jasonmalinowski
Copy link
Copy Markdown
Member

Thanks @dusrdev for the fix!

@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Apr 2, 2026
@dusrdev
Copy link
Copy Markdown
Contributor Author

dusrdev commented Apr 2, 2026

@jasonmalinowski my pleasure!

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

Labels

Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. Feature - Run File #: and #! directives and file-based C# programs VSCode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants