linux-test-project / lcov

LCOV
GNU General Public License v2.0
867 stars 235 forks source link

genhtml (2.0) dies with source files in top directory #245

Closed entrope closed 7 months ago

entrope commented 8 months ago

When there is a source file in the top-level directory of a project (one of the directories passed with --prefix), the die("file error for " ...) call gets triggered: @$short_path has only one element, so nothing gets passed to File::Spec->catdir() and $rel_dir is empty.

Adding unshift @$short_path, '.' if $#$short_path == 0; before the call to catdir() avoids the crash but generates an HTML coverage report with no way to access the source files because the top-level directory has a zero-length (or no?) hyperlink to the per-directory report file.

With lcov 1.16, this would give the name "root" to the top-level directory.

henry2cox commented 8 months ago

better if you could attach a testcase. Other question is what happens when your remove the --prefix option. Thanks.

entrope commented 8 months ago

This was on macOS 14.1 (Sonoma) using Apple's version of LLVM 15 as cc, but should translate well to Linux. (I reproduced my original case, which uses Meson as a build system, in a Kali Linux VM because that was what had lcov 2.0).

% echo "int main() { return 0; }" > main.c
% cc -Wall -Wextra --coverage ../main.c
% ./a.out
% lcov -c -d `pwd` -o lcov.info
[...]
% genhtml -o html --prefix `pwd` lcov.info
Found 1 entries.
Using user-specified filename prefix "/Users/mdpoole/src/tmp"
Generating output.
genhtml: ERROR: file error for main.c

Meson automatically adds --prefix, so it is not easy for my original case to remove it. Without --prefix in the test case above, it finds the parent directory as the prefix:

% genhtml -o html lcov.info 
Found 1 entries.
Found common filename prefix "/Users/mdpoole/src"
Generating output.
Processing file tmp/main.c
  lines=1 hit=1 functions=1 hit=1
Overall coverage rate:
  lines......: 100.0% (1 of 1 line)
  functions......: 100.0% (1 of 1 function)
henry2cox commented 8 months ago

Thanks. I suspect that your actual case is not quite so simple :-) Probably contains code in subdirectories of 'prefix' as well as in the top-level directory. I also wanted to test that my somewhat hacky fix handles --flat and --hierarchical report formats.

In any event: I seem to have a fix which I will push along with some other changes/enhancements - once local tests are complete and things seem to have stabilized.

Henry

entrope commented 8 months ago

My actual case is almost that simple -- I have multiple source files, but all were in the top-level directory. (I have since restructured it to have subdirectories, to clarify library vs executable code.)

I am happy to test your fix on my original test case once you think it is worth sharing. Thank you for the quick responses.

henry2cox commented 7 months ago

should be fixed in the above commit: bf135ca

henry2cox commented 7 months ago

Closing this issue as I think that it is fixed. If there is still a problem, please either reopen this issue or file a new one. Please include a testcase which illustrates the problem if at all possible. Henry