-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add CA2266 analyzer documentation #53992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
16
commits into
main
Choose a base branch
from
copilot/add-docs-for-ca2266
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c64e4c7
Add CA2266 analyzer docs
Copilot 1bc3493
Link CA2266 from .NET 10 SDK notes
Copilot ae418f6
Address CA2266 doc review feedback
Copilot 422ad3d
Polish CA2266 rule wording
Copilot d5d9f02
Refine CA2266 guidance wording
Copilot 47b4d84
Adjust CA2266 related link
Copilot 49381ed
Tighten CA2266 cause wording
Copilot 0b8bea3
Add ai-usage key
jjonescz cb33788
Remove from what's new
jjonescz 7a6e07b
Refine CA2266 suppression guidance
Copilot 7cba116
Clarify CA2266 suppression scope
Copilot 1303c3c
Polish CA2266 suppression wording
Copilot ed1accf
Refine CA2266 suppression sentence
Copilot a59e17a
Adjust CA2266 suppression phrasing
Copilot a30c06e
Remove invalid suggestion
jjonescz 4ff142f
Improve NoWarn
jjonescz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --- | ||
| title: "CA2266: File-based program entry point should start with '#!'" | ||
| description: "Learn about code analysis rule CA2266 - File-based program entry point should start with '#!'" | ||
| ms.date: 05/21/2026 | ||
| f1_keywords: | ||
| - CA2266 | ||
| helpviewer_keywords: | ||
| - CA2266 | ||
| dev_langs: | ||
| - CSharp | ||
|
jjonescz marked this conversation as resolved.
|
||
| ai-usage: ai-assisted | ||
| --- | ||
|
|
||
| # CA2266: File-based program entry point should start with `#!` | ||
|
|
||
| | Property | Value | | ||
| |-------------------------------------|----------------------------------------------------| | ||
| | **Rule ID** | CA2266 | | ||
| | **Title** | File-based program entry point should start with `#!` | | ||
| | **Category** | [Usage](usage-warnings.md) | | ||
| | **Fix is breaking or non-breaking** | Non-breaking | | ||
| | **Enabled by default in .NET 10** | As warning | | ||
| | **Applicable languages** | C# | | ||
|
|
||
| ## Cause | ||
|
|
||
| Your entry point file in a multi-file file-based program doesn't start with a shebang (`#!`) line. | ||
|
|
||
| ## Rule description | ||
|
|
||
| For correct tool identification and direct shell execution support, start the entry point file with a shebang (`#!`) line to clearly distinguish it from files brought in with `#:include`. | ||
|
|
||
| ## How to fix violations | ||
|
|
||
| Add a shebang line to the beginning of the entry point file, for example, `#!/usr/bin/env dotnet`. | ||
|
|
||
| ## Example | ||
|
|
||
| The following code snippet shows a violation of CA2266. | ||
|
|
||
| In `app.cs`: | ||
|
|
||
| ```csharp | ||
| #:include "helpers.cs" | ||
|
|
||
| Console.WriteLine(GetMessage()); | ||
| ``` | ||
|
|
||
| In `helpers.cs`: | ||
|
|
||
| ```csharp | ||
| string GetMessage() => "Hello"; | ||
| ``` | ||
|
|
||
| The following code snippet fixes the violation. | ||
|
|
||
| In `app.cs`: | ||
|
|
||
| ```csharp | ||
| #!/usr/bin/env dotnet | ||
| #:include "helpers.cs" | ||
|
|
||
| Console.WriteLine(GetMessage()); | ||
| ``` | ||
|
|
||
| In `helpers.cs`: | ||
|
|
||
| ```csharp | ||
| string GetMessage() => "Hello"; | ||
| ``` | ||
|
|
||
| ## When to suppress warnings | ||
|
|
||
| If you intentionally omit the shebang line and have confirmed that your tooling can still identify the entry point correctly, it is safe to suppress this warning. | ||
|
|
||
| ## Suppress a warning | ||
|
|
||
| If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
|
||
| ```csharp | ||
| #pragma warning disable CA2266 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore CA2266 | ||
|
jjonescz marked this conversation as resolved.
Outdated
|
||
| ``` | ||
|
|
||
| To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
|
||
| ```ini | ||
| [*.cs] | ||
| dotnet_diagnostic.CA2266.severity = none | ||
| ``` | ||
|
|
||
| For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). | ||
|
|
||
| ## See also | ||
|
|
||
| [Build file-based apps](../../../csharp/fundamentals/tutorials/file-based-programs.md) | ||
|
jjonescz marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.