Enable CA2007 (ConfigureAwait) analyzer for src/ libraries#367
Open
Copilot wants to merge 3 commits into
Open
Conversation
Agent-Logs-Url: https://github.com/dotnet/android-tools/sessions/8a3bfc4f-856e-49b7-867f-4090467a9e93 Co-authored-by: rmarinho <1235097+rmarinho@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Enable ConfigureAwait(false) analyzer for consistency
Enable CA2007 (ConfigureAwait) analyzer for src/ libraries
May 20, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Enables the SDK’s built-in CA analyzers for src/ library projects (including netstandard2.0) and enforces CA2007 as an error to require ConfigureAwait(false) usage in library code, reducing deadlock risk when consumed under a SynchronizationContext (MSBuild/IDEs).
Changes:
- Enforce
CA2007as an error forsrc/**.csvia.editorconfig(tests excluded). - Add
src/Directory.Build.propsto turn on SDK analyzers (EnableNETAnalyzers=true,AnalysisLevel=latest) forsrc/projects. - Document the
ConfigureAwait(false)convention in.github/copilot-instructions.md.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Directory.Build.props |
Enables SDK analyzers for src/ projects (including netstandard2.0). |
.github/copilot-instructions.md |
Documents the required ConfigureAwait(false) convention for src/. |
.editorconfig |
Sets CA2007 to error for src/ C# source files. |
| - **No whitespace-only diffs**: before committing, run `git diff --stat` and verify only files with intentional code changes appear. If a file shows as fully rewritten (all lines removed and re-added) you have introduced line-ending or trailing-whitespace changes — revert that file with `git checkout -- <file>` and re-apply only your code change. Never commit whitespace-only or line-ending-only changes. | ||
| - **File-scoped namespaces**: all new files should use file-scoped namespaces (`namespace Foo;` instead of `namespace Foo { ... }`). | ||
| - **Static `HttpClient`**: `HttpClient` instances must be `static` to avoid socket exhaustion. See [HttpClient guidelines](https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use). Do not create per-instance `HttpClient` fields or dispose them in `IDisposable`. | ||
| - **`ConfigureAwait(false)`**: every `await` in `src/` must end with `.ConfigureAwait (false)` — these libraries run inside hosts (MSBuild, IDEs) that have a `SynchronizationContext`, and resuming on it can cause deadlocks on `netstandard2.0`. This is enforced as `error CA2007` for source code (see `.editorconfig`); tests are exempt. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Enforces
ConfigureAwait(false)on awaited tasks in library code via the CA2007 analyzer, mirroring dotnet/sdk#54116. MissingConfigureAwait(false)can deadlock thesenetstandard2.0libraries when consumed by hosts with aSynchronizationContext(MSBuild, IDEs).Changes
.editorconfig—dotnet_diagnostic.CA2007.severity = errorscoped to[src/**.cs]. Tests are intentionally exempt.src/Directory.Build.props(new) — setsEnableNETAnalyzers=trueandAnalysisLevel=latestso the SDK's CA analyzers also run againstnetstandard2.0(they only auto-enable fornet5.0+, butnetstandard2.0is precisely where the rule matters most)..github/copilot-instructions.md— documents the convention.Notes
ConfigureAwait(false)on everyawaitundersrc/.netstandard2.0andnet10.0by temporarily dropping a.ConfigureAwait(false)call and confirmingerror CA2007from each target.