Skip to content

File-based apps: extend "missing shebang" analyzer to #:ref directives - #54553

Merged
jjonescz merged 4 commits into
dotnet:release/10.0.4xxfrom
jjonescz:53749-shebang-analyzer-refs
Jun 30, 2026
Merged

jjonescz merged 4 commits into
dotnet:release/10.0.4xxfrom
jjonescz:53749-shebang-analyzer-refs

Conversation

@jjonescz

@jjonescz jjonescz commented Jun 2, 2026 •

Copy link
Copy Markdown
Member

Resolves #53749.
Corresponding doc update: dotnet/docs#54120

@jjonescz jjonescz added the Area-run-file Items related to the "dotnet run <file>" effort label Jun 2, 2026

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

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 IsIncludeDirective to IsShebangRequiringDirective and matches both include and ref via a small IsDirective helper.
  • Parameterizes analyzer unit tests with [Theory]/[InlineData] to cover include, ref, project, and package directive variants.
  • Adds shebangs to existing run-file and project-convert tests that use #:ref, plus a new MissingShebangWarning_RefDirective integration 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.

@jjonescz

jjonescz commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

@333fred @RikkiGibson for reviews, thanks

1 similar comment
@jjonescz

Copy link
Copy Markdown
Member Author

@333fred @RikkiGibson for reviews, thanks

IsDirective(trimmedContent, "ref");
}

private static bool IsDirective(ReadOnlySpan<char> content, string directiveName)

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.

Can this be a local function?

private static bool IsDirective(ReadOnlySpan<char> content, string directiveName)
{
return content.StartsWith(directiveName, StringComparison.Ordinal) &&
(content.Length == directiveName.Length || char.IsWhiteSpace(content[directiveName.Length]));

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.

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?

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.

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.Length is at most content.Length + 1?

The content.StartsWith(directiveName, StringComparison.Ordinal) guarantees that.

@jjonescz

Copy link
Copy Markdown
Member Author

@RikkiGibson for a second review, thanks

1 similar comment
@jjonescz

Copy link
Copy Markdown
Member Author

@RikkiGibson for a second review, thanks

@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.

I believe this is good to merge with a single review

@jjonescz
jjonescz enabled auto-merge (squash) June 30, 2026 09:02
@jjonescz
jjonescz merged commit 9af702c into dotnet:release/10.0.4xx Jun 30, 2026
28 checks passed
@jjonescz
jjonescz deleted the 53749-shebang-analyzer-refs branch June 30, 2026 10:05
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 6, 2026
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.

4 participants