microsoft / microsoft/codecoverage

dotnet-coverage collect --server-mode leaks coverage across tests when using snapshot -r

Open
#246 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
125
Forks
17
Avg merge
1h 17m
Merged PRs (30d)
2

Description

We are observing that in a long-lived server-mode session, calling snapshot -r does not appear to fully clear prior coverage state. Coverage from an earlier test is still present in the next snapshot, and the raw .coverage file is already corrupted before any mapper/parsing logic executes. This is causing to collect the per test coverage when the test cases needs to run in sequence.

Steps:

  1. Start a shared coverage session:
    cmd : dotnet-coverage collect --server-mode --background --session-id
    2. Connect and run Test A in that same session:
    cmd : dotnet-coverage connect dotnet test
    3. Snapshot Test A with reset:
    cmd : dotnet-coverage snapshot -r -o -t
    dotnet-coverage merge -o -f xml
    4. Connect and run Test B in the same session:
    cmd : dotnet-coverage connect dotnet test
    5. Snapshot Test B with reset:
    cmd : dotnet-coverage snapshot -r -o -t
    dotnet-coverage merge -o -f xml
    6. Observe coverage contamination between Test A and Test B.
    7. Shut down the server at the end
    cmd: dotnet-coverage shutdown -t

Expected:
Each snapshot should produce coverage for only the test that executed since the previous reset. The -r option should clear the previous coverage state after creating the snapshot.

Actual:
The second snapshot still contains coverage from the earlier test or from a previous unrelated run. The raw .coverage output is already incorrect before mapper logic runs, which indicates the issue is in the coverage engine/server-mode reset behavior and not in downstream parsing.

Impact:
Incorrect coverage attribution, false pass/fail evidence, broken file-to-test mapping.

Note:
This issue is specific to dotnet-coverage collect --server-mode with repeated snapshot -r calls in the same process/session. It is not the same as MTP JSON-RPC server mode (--server / --server jsonrpc), which is a separate feature for IDE/tooling integration. The bug is therefore in the coverage collector/server-mode flow, not in the MTP test runner or mapper logic.

Relevant docs:

https://learn.microsoft.com/en-us/dotnet/core/testing/microsoft-testing-platform-code-coverage
https://learn.microsoft.com/en-us/dotnet/core/testing/microsoft-testing-platform-server-mode
https://github.com/microsoft/codecoverage

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the documented server-mode flow and reproduce the issue using the listed collect, connect, snapshot -r, merge, and shutdown commands for Test A and Test B. Inspect the raw snapshot outputs before merge or mapper processing; done means each snapshot contains only coverage collected since the preceding reset without corrupting the .coverage file.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
testing, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.