microsoft / codecoverage

MIT License
84 stars 11 forks source link

MS CodeCoverage + SourceLink + ReportGenerator = No Code Coverage Report #141

Open rainman-63 opened 1 month ago

rainman-63 commented 1 month ago

When using MS CodeCoverage with SourceLink (Microsoft.SourceLink.GitHub in this case), and setting the ContinuousIntegrationBuild element in the csproj to true, the output reports will have the file path removed from the filename attributes.

Using SourceLink will cause the PDB to be altered, adding the CustomDebugAttribute and removing part of the file paths to be replaced with /_, which will result in the following code coverage report:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<coverage line-rate="0.5" branch-rate="1" complexity="2" version="1.9" timestamp="1728997951" lines-covered="1" lines-valid="2">
  <packages>
    <package line-rate="0.5" branch-rate="1" complexity="2" name="NoCodeCoverage">
      <classes>
        <class line-rate="0.5" branch-rate="1" complexity="2" name="NoCodeCoverage.MyMath" filename="/_/src/NoCodeCoverage/MyMath.cs">
          <methods>
            <method line-rate="1" branch-rate="1" complexity="1" name="Add" signature="(int, int)">
              <lines>
                <line number="15" hits="1" branch="False" />
              </lines>
            </method>
            <method line-rate="0" branch-rate="1" complexity="1" name="Subtract" signature="(int, int)">
              <lines>
                <line number="24" hits="0" branch="False" />
              </lines>
            </method>
          </methods>
          <lines>
            <line number="15" hits="1" branch="False" />
            <line number="24" hits="0" branch="False" />
          </lines>
        </class>
      </classes>
    </package>
  </packages>
</coverage>

Tooling like ReportGenerator use the filename to access the file to build the code coverage report, but no such file path exists on the local machine for /_/src/NoCodeCoverage/MyMath.cs.

Other tooling like coverlet do not seem to have the same issue when using SourceLink.

I've attached a sample application that will exhibit the issue, it just needs to be setup with a git repo. NoCodeCoverage.zip

Thanks,

Raymond

jakubch1 commented 1 month ago

@rainman-63 thanks for reporting this. ReportGenerator works good with /_/ we are using it in our pipelines. You can maybe try to upgrade ReportGenerator in your pipeline and/or provide argument -sourcedirs.

You can also try to use: <Configuration><DeterministicReport>False</DeterministicReport></Configuration> in your configuration to try to resolve those paths. I think you need to upgrade Microsoft.CodeCoverage package to latest in your project to take effect.

@fhnaseer could you please add DeterministicReport option to our public documentation?

jbe2277 commented 2 weeks ago

I see the same issue with my bat file here: https://github.com/jbe2277/waf/blob/34629b5f4a9d83d320050fa0b8b4da0528b07fd5/build/BuildRelease.bat

Output of reportgenerator:

reportgenerator -reports:../out/CodeCoverageReport/System.Waf.cobertura.xml -targetdir:../out/CodeCoverageReport -reporttypes:"MarkdownSummaryGithub"
2024-11-06T21:42:42: Arguments
2024-11-06T21:42:42:  -reports:../out/CodeCoverageReport/System.Waf.cobertura.xml
2024-11-06T21:42:42:  -targetdir:../out/CodeCoverageReport
2024-11-06T21:42:42:  -reporttypes:MarkdownSummaryGithub
2024-11-06T21:42:42: File '/_/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Presentation/App.xaml.cs' does not exist (any more).
2024-11-06T21:42:42: File '/_/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Presentation/Converters/LanguageToStringConverter.cs' does not exist (any more).
2024-11-06T21:42:42: File '/_/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Presentation/Converters/StringToUriConverter.cs' does not exist (any more).
...

I'm using the latest version of dotnet-coverage and reportgenerator.