Istanbul: Jenkins cannot show source code from cobertura report

Created on 23 Apr 2013  路  1Comment  路  Source: gotwarlost/istanbul

Although Jenkins is able to report the coverage metrics on a per-file basis, it is not able to display the original source code from the current coverage-cobertura.xml format. The Jenkins cobertura plugin reports:

Source code is unavailable. Some possible reasons are:

  • This is not the most recent build (to save on disk space, this plugin only keeps the most recent build鈥檚 source code).
  • Cobertura found the source code but did not provide enough information to locate the source code.
  • Cobertura could not find the source code, so this plugin has no hope of finding it.
  • You do not have sufficient permissions to view this file.

Currently the XML report outputs only an empty <sources/> node, and each <file/> node contains the full, absolute path to the file. In order to get Jenkins to display the source code for a file, I have to re-create the full absolute path as if it is relative to the job's cobertura/ directory. Consider this coverage.xml excerpt:

<sources/>
...
<class name="my_file.js" filename="/var/lib/jenkins/workspace/MyProject/lib/my_file.js">
...

Given this, the Jenkins plugin cannot display source, and the only way to get it to show the source is by:

$ cd /var/lib/jenkins/jobs/MyProject/cobertura/
$ mkdir -p ./var/lib/jenkins/workspace/MyProject/lib/
$ cp /var/lib/jenkins/workspace/MyProject/lib/my_file.js /var/lib/jenkins/jobs/MyProject/cobertura/var/lib/jenkins/workspace/MyProject/lib/my_file.js

That allows Jenkins to render the source code properly. I believe instead, the report should generate the coverage.xml file with:

<sources>
  <source>/var/lib/jenkins/workspace/MyProject</source>
</sources>
...
<class name="my_file.js" filename="lib/my_file.js">
...

This allows the Jenkins plugin to archive the source files relative to the <sources/> paths. Looking at the Jenkins plugin, it only looks at the <sources/> nodes for finding the source paths (see https://github.com/jenkinsci/cobertura-plugin/blob/master/src/main/java/hudson/plugins/cobertura/CoberturaPublisher.java#L377). If the sourcePaths set is empty, it has no paths or files to look for. Also see https://github.com/jenkinsci/cobertura-plugin/blob/master/src/main/java/hudson/plugins/cobertura/CoberturaCoverageParser.java#L164 for the actual SAX parser that collects the sourcePaths set from the XML file.

Most helpful comment

Hi All,

I do not think that this fix worked as expected. I still get the Source code is unavailable. error message.

I believe the reason is because I am running my coverage report from a different working directory than the top level repo. In the coverage.xml file, the source directory is listed as the top level repo, but the relative paths in the filenames are relative to the working directory.

To fix this, either the source path should be set to the working directory OR the filenames should be relative to the top level repo.

>All comments

Hi All,

I do not think that this fix worked as expected. I still get the Source code is unavailable. error message.

I believe the reason is because I am running my coverage report from a different working directory than the top level repo. In the coverage.xml file, the source directory is listed as the top level repo, but the relative paths in the filenames are relative to the working directory.

To fix this, either the source path should be set to the working directory OR the filenames should be relative to the top level repo.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

niftylettuce picture niftylettuce  路  29Comments

jasonpincin picture jasonpincin  路  22Comments

dankohn picture dankohn  路  25Comments

amoufaddel picture amoufaddel  路  28Comments

NiGhTTraX picture NiGhTTraX  路  36Comments