Source Generator generated files cause coverage generation error with source link + deterministic builds
See original GitHub issueThis 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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
It can be configured either way. You should be able to check the source location for the specific file and still determine its origin.