Skip to content

Reinstate the xUnit1051 analyzer, suppressed for the xUnit v3 migration #3702

Description

@jeremydmiller

xUnit1051 ("Calls to methods which accept CancellationToken should use TestContext.Current.CancellationToken") is new in the xUnit v3 analyzer set and ships at warning severity. Because Directory.Build.props sets TreatWarningsAsErrors=true, it becomes a hard build break on several thousand call sites — every await of anything with a CancellationToken overload. A 498-line pilot project produced 12 errors on its own.

It was suppressed repo-wide in Directory.Build.props as part of the v3 migration (#3699) so the framework swap would not also be a behavioural change to how tests cancel.

This issue tracks bringing it back, so "turn it off now, bring it in later" does not become permanent by default.

Suggested approach

Re-enable per project, smallest suites first, rather than repo-wide in one go — the fix is genuinely behavioural, not cosmetic: passing TestContext.Current.CancellationToken changes what happens when a test is cancelled or times out.

<!-- in the test project, once its call sites are fixed -->
<NoWarn>$(NoWarn.Replace('xUnit1051',''))</NoWarn>

Context

See XUNIT3-MIGRATION-PLAN.md §4 for the measurement and the reasoning behind deferring it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions