microsoft / codecoverage

MIT License
80 stars 11 forks source link

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

Open rainman-63 opened 1 week ago

rainman-63 commented 1 week 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 6 days 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?