Repository navigation
File-based apps: extend "missing shebang" analyzer to #:ref directives - #54553
Conversation
There was a problem hiding this comment.
Pull request overview
Extends the MissingShebangInFileBasedProgram analyzer (CA2266) so it also flags entry-point files that contain #:ref directives without a shebang line, mirroring the existing behavior for #:include. Tests across the run-file/convert suites are updated to add the shebang where appropriate, and analyzer unit tests are parameterized to cover the new directive. #:project and #:package are explicitly verified to NOT require a shebang.
Changes:
- Renames
IsIncludeDirectivetoIsShebangRequiringDirectiveand matches bothincludeandrefvia a smallIsDirectivehelper. - Parameterizes analyzer unit tests with
[Theory]/[InlineData]to coverinclude,ref,project, andpackagedirective variants. - Adds shebangs to existing run-file and project-convert tests that use
#:ref, plus a newMissingShebangWarning_RefDirectiveintegration test; updates expected error line numbers (1 → 2).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.CSharp.NetAnalyzers/Microsoft.NetCore.Analyzers/Usage/CSharpMissingShebangInFileBasedProgram.cs | Extends shebang-required detection to include #:ref in addition to #:include. |
| src/Microsoft.CodeAnalysis.NetAnalyzers/tests/.../Usage/MissingShebangInFileBasedProgramTests.cs | Parameterizes analyzer tests; adds package/project no-diagnostic cases and ref warning/codefix cases. |
| test/dotnet.Tests/CommandTests/Run/RunFileTests_BuildOptions.cs | Adds a new integration test asserting CA2266 fires for a #:ref entry point and disappears once a shebang is added. |
| test/dotnet.Tests/CommandTests/Run/RunFileTests_Directives.cs | Adds shebangs to existing #:ref tests and updates expected directive error line number. |
| test/dotnet.Tests/CommandTests/Run/RunFileTests_General.cs | Adds shebangs and bumps expected error line for #:ref tests. |
| test/dotnet.Tests/CommandTests/Run/RunFileTests_CscOnlyAndApi.cs | Adds shebangs to entry-point sources that use #:ref. |
| test/dotnet.Tests/CommandTests/Project/Convert/DotnetProjectConvertTests.cs | Adds shebangs to dotnet project convert test inputs that use #:ref. |
|
@333fred @RikkiGibson for reviews, thanks |
1 similar comment
|
@333fred @RikkiGibson for reviews, thanks |
| IsDirective(trimmedContent, "ref"); | ||
| } | ||
|
|
||
| private static bool IsDirective(ReadOnlySpan<char> content, string directiveName) |
| private static bool IsDirective(ReadOnlySpan<char> content, string directiveName) | ||
| { | ||
| return content.StartsWith(directiveName, StringComparison.Ordinal) && | ||
| (content.Length == directiveName.Length || char.IsWhiteSpace(content[directiveName.Length])); |
There was a problem hiding this comment.
I don't understand this check. What guarantees that directiveName.Length is at most content.Length + 1? Why not just call Trim() on context.Text and then do a simple equals check?
There was a problem hiding this comment.
For a directive like #:ref Util.cs, content will be "ref Util.cs". So in this check we want to verify that the content starts with the directive name followed by a space. There is nothing to trim, we would need to split the content but this seems simpler.
What guarantees that
directiveName.Lengthis at mostcontent.Length + 1?
The content.StartsWith(directiveName, StringComparison.Ordinal) guarantees that.
|
@RikkiGibson for a second review, thanks |
1 similar comment
|
@RikkiGibson for a second review, thanks |
RikkiGibson
left a comment
There was a problem hiding this comment.
I believe this is good to merge with a single review
Resolves #53749.
Corresponding doc update: dotnet/docs#54120