question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Source Generator generated files cause coverage generation error with source link + deterministic builds

See original GitHub issue

This is an odd combination of a few things but it seems that you get the following error when the following combination of things happens:

The Problem

If you have a project that uses a source generator (The attached example uses Generator.Equals for equality checks), uses sourcelink and deterministic builds then when running

dotnet test --collect:“XPlat Code Coverage” --settings “.\coverlet.runsettings”

you get

Data collector 'XPlat code coverage' message: [coverlet]Coverlet.Collector.Utilities.CoverletDataCollectorException: CoverletCoverageDataCollector: Failed to get coverage result
 ---> System.Collections.Generic.KeyNotFoundException: The given key '' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key) in System.Private.CoreLib.dll:token 0x600675e+0x17
   at Coverlet.Core.Coverage.GetSourceLinkUrl(Dictionary`2 sourceLinkDocuments, String document) in /_/src/coverlet.core/Coverage.cs:line 510
   at Coverlet.Core.Coverage.CalculateCoverage() in /_/src/coverlet.core/Coverage.cs:line 391
   at Coverlet.Core.Coverage.GetCoverageResult() in /_/src/coverlet.core/Coverage.cs:line 173
   at Coverlet.Collector.DataCollection.CoverageWrapper.GetCoverageResult(Coverage coverage) in /_/src/coverlet.collector/DataCollection/CoverageWrapper.cs:line 52
   at Coverlet.Collector.DataCollection.CoverageManager.GetCoverageResult() in /_/src/coverlet.collector/DataCollection/CoverageManager.cs:line 93
   --- End of inner exception stack trace ---
   at Coverlet.Collector.DataCollection.CoverageManager.GetCoverageResult() in /_/src/coverlet.collector/DataCollection/CoverageManager.cs:line 98
   at Coverlet.Collector.DataCollection.CoverletCoverageCollector.OnSessionEnd(Object sender, SessionEndEventArgs e) in /_/src/coverlet.collector/DataCollection/CoverletCoverageCollector.cs:line 161.

Workaround

The workaround is that you can add an ExcludeByFile for *.g.cs which stops them hitting this case although there’s obviously something unexpected happening.

Reproduction

CoverletRepoProject.zip

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
sharwellcommented, Apr 28, 2022

Outputs from source generators are generally embedded in the PDB. Coverlet should always check the PDB for embedded source before checking for files on disk; when present, the embedded sources are always a more accurate source of truth than files on disk.

0reactions
sharwellcommented, Apr 29, 2022

But are non-generated source files embedded in the PDB too?

It can be configured either way. You should be able to check the source location for the specific file and still determine its origin.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source generator not working correctly in Visual Studio 2022
I am writing a source generator that automatically writes JsonObject classes, ... it shows "This generator is not generating files.
Read more >
Source Generators
Source Generators is a C# compiler feature that lets C# developers inspect user code as it is being compiled. Source generators create new ......
Read more >
Bulletin of the Atomic Scientists
Weizmann records in "Trial and Error" that when the Institute was first built, in what was still the desert, it seemed about to...
Read more >
Learning the Basics
Gradle has built-in support for dependency management. Let's explore the main concepts with the help of a theoretical but common project: This project...
Read more >
Chapter 2 Server Error Message Reference
Occurs for failure to create or copy a file needed for some operation. Possible causes: Permissions problem for source file; destination file already...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found