Skip to content

File-based apps: avoid processing directives after #if - #56256

Merged
jjonescz merged 2 commits into
dotnet:mainfrom
jjonescz:roslyn85203-disabled-region
Sep 21, 2026
Merged

jjonescz merged 2 commits into
dotnet:mainfrom
jjonescz:roslyn85203-disabled-region

Conversation

@jjonescz

Copy link
Copy Markdown
Member

@jjonescz
jjonescz requested a balanced review from Copilot September 11, 2026 12:13
@jjonescz jjonescz added the Area-run-file Items related to the "dotnet run <file>" effort label Sep 11, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
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.

🟢 Approval recommended

The implementation matches the required semantics and includes focused regression coverage.

Pull request overview

Prevents file-based app directives after #if from affecting generated projects, fixing dotnet/roslyn#85203.

Changes:

  • Stops directive discovery at the first #if.
  • Adds active, conditional, inactive, and conversion regression coverage.
  • Documents the updated scanning boundary.
File summaries
File Description
src/Cli/Microsoft.DotNet.FileBasedPrograms/FileLevelDirectiveHelpers.cs Stops directive processing at #if.
test/dotnet.Tests/CommandTests/Run/RunFileTests_Directives.cs Tests runtime behavior across conditional regions.
test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs Tests project conversion behavior.
documentation/general/dotnet-run-file.md Documents the #if boundary.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Balanced

@jjonescz
jjonescz marked this pull request as ready for review September 11, 2026 13:11
@jjonescz
jjonescz requested a review from a team as a code owner September 11, 2026 13:11
@azure-pipelines

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

@jjonescz

Copy link
Copy Markdown
Member Author

@333fred @RikkiGibson for a review, thanks

// Stop when the trivia contains an error (e.g., because it's after #if).
if (trivia.ContainsDiagnostics)
// Inactive directives after #if may have no diagnostics, but must not affect the project.
if (trivia.ContainsDiagnostics || trivia.IsKind(SyntaxKind.IfDirectiveTrivia))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Aleksey had this comment in the linked issue:
dotnet/roslyn#85203 (comment)

Basically, I think that any directive that isn't an "ignored directive" should stop search for "ignored directives".

This approach feels reasonable to me. Was there a reason to specifically condition behavior on an if-directive?

@jjonescz jjonescz Sep 18, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes:

  1. It would be a breaking change, since interleaving ignored directives and normal directives is currently valid.
  2. I think it's useful to allow that too, consider for example that #region can be used to group your #: directives.

// error CS9299: '#:' directives cannot be after '#if' directive
.And.HaveStdOutContaining("error CS9299:")
// NO error CS0234: The type or namespace name 'CommandLine' does not exist in the namespace 'System'
.And.NotHaveStdOutContaining("error CS0234:");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So effectively the package does get restored, but, a compiler error is reported, so user is still blocked by design?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Was there a specific reason that the directive that we reported an error about should still affect teh virtual project? I don't have a deep objection, but, my first instinct was to essentially drop such directives after reporting the error, so I'm curious.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The directive should not affect the virtual project, and the package should not be restored. (See also DotnetProjectConvertTests.Directives_AfterIf.)

The compiler just returns early after reporting the syntax error and so the semantic error is not reported. I agree that's confusing, I will try to improve the test, thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, got it, the semantic error was missing for a different reason than I assumed.

@RikkiGibson RikkiGibson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Done review pass. Nothing seems objectionable but wondering about the specific choice of semantics on the FBA side.

@jjonescz
jjonescz merged commit 5d62f8f into dotnet:main Sep 21, 2026
28 checks passed
@jjonescz
jjonescz deleted the roslyn85203-disabled-region branch September 21, 2026 10:25
@jjonescz

Copy link
Copy Markdown
Member Author

/backport to release/11.0.1xx

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0.1xx (link to workflow run)

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

Labels

Area-run-file Items related to the "dotnet run <file>" effort

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler ignores preprocessor directives surrounding MSBuild directives in file-based apps

3 participants