Skip to content

Disable MA0042 async overload suggestions for TemporaryDirectory in test projects#980

Merged
meziantou merged 2 commits intomainfrom
copilot/disable-async-rule-in-tests
Jan 31, 2026
Merged

Disable MA0042 async overload suggestions for TemporaryDirectory in test projects#980
meziantou merged 2 commits intomainfrom
copilot/disable-async-rule-in-tests

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 31, 2026

The analyzer was suggesting async overloads for Meziantou.Framework.TemporaryDirectory methods (e.g., CreateTextFileAsync) in test projects where synchronous file operations are acceptable and don't benefit from async.

Changes

Analyzer (DoNotUseBlockingCallInAsyncContextAnalyzer.cs)

  • Detect test projects by checking for Xunit, NUnit, or MSTest Assert types in compilation
  • Exclude TemporaryDirectory async method suggestions when IsTestProject is true
  • Non-test projects continue to receive async overload suggestions for TemporaryDirectory

Tests (DoNotUseBlockingCallInAsyncContextAnalyzer_AsyncContextTests.cs)

  • Added coverage for Xunit, NUnit, MSTest test projects (no diagnostic expected)
  • Added coverage for non-test projects (diagnostic still appears)

Example

In a test project with Xunit:

public async Task TestMethod()
{
    var dir = new TemporaryDirectory();
    dir.CreateTextFile("test.txt", "content"); // No MA0042 diagnostic
}

In a regular project:

public async Task RegularMethod()
{
    var dir = new TemporaryDirectory();
    dir.CreateTextFile("test.txt", "content"); // MA0042: Use 'CreateTextFileAsync' instead
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 1s1vsblobprodcus386.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/Meziantou.Analyzer.slnx --packages /home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/Meziantou.Analyzer.Test.csproj --packages /home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • pdfvsblobprodcus380.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/Meziantou.Analyzer.slnx --packages /home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • st8vsblobprodcus339.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/Meziantou.Analyzer.slnx --packages /home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/Meziantou.Analyzer.Test.csproj --packages /home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/home/REDACTED/work/Meziantou.Analyzer/.codeql-scratch/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

For the rule that suggest to use Async overloads, can you disable it the methods from the type Meziantou.Framework.TemporaryDirectory like CreateTextFileAsync. But disable it only for test projects (xunit, mstests, nunit)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
@meziantou meziantou marked this pull request as ready for review January 31, 2026 21:48
Copilot AI changed the title [WIP] Disable async overload rule for test projects Disable MA0042 async overload suggestions for TemporaryDirectory in test projects Jan 31, 2026
Copilot AI requested a review from meziantou January 31, 2026 21:48
@meziantou meziantou merged commit 1b979d5 into main Jan 31, 2026
13 checks passed
@meziantou meziantou deleted the copilot/disable-async-rule-in-tests branch January 31, 2026 23:39
This was referenced Feb 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants