Repository navigation
File-based apps: avoid processing directives after #if - #56256
Conversation
|
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. |
There was a problem hiding this comment.
🟢 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
|
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. |
|
@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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yes:
- It would be a breaking change, since interleaving ignored directives and normal directives is currently valid.
- I think it's useful to allow that too, consider for example that
#regioncan 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:"); |
There was a problem hiding this comment.
So effectively the package does get restored, but, a compiler error is reported, so user is still blocked by design?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Oh, got it, the semantic error was missing for a different reason than I assumed.
RikkiGibson
left a comment
There was a problem hiding this comment.
Done review pass. Nothing seems objectionable but wondering about the specific choice of semantics on the FBA side.
|
/backport to release/11.0.1xx |
|
Started backporting to |
Fixes dotnet/roslyn#85203.