c8 icon indicating copy to clipboard operation
c8 copied to clipboard

Surprising handling of CLI include/exclude compared to nyc

Open coreyfarrell opened this issue 5 years ago • 1 comments

  • Version: 7.1.0
  • Platform: Fedora 30 x86_64

I'm working on an experimental loader hook for nyc to provide coverage of ES modules. Initially my scripts looked like:

{
  "test": "c8 --include index.js -r none node test/index.js | tap-yaml-summary",
  "posttest": "c8 report"
}

Running c8 report in a follow-up process was done to allow the text report to display in color (piping into tap-yaml-summary would normally disable color).

I was surprised to find that the --include index.js (restricting by include) did not work. It seems include/exclude options have no effect on coverage collection, instead it only controls coverage reporting. Once I moved the --include index.js to the c8 report it produced the expected report.

I don't know if the functionality can be improved or if this could just be documented. Maybe recommend against controlling include/exclude using CLI arguments in favor of .c8rc.json?

coreyfarrell avatar Mar 17 '20 12:03 coreyfarrell

I think if I am understanding correctly, this is "works as 'currently' designed" but agree it is confusing and should be documented. When c8 enables "coverage" it does so by telling node to do so via an environment variable (see https://github.com/bcoe/c8/blob/master/bin/c8.js#L36, you could supply it from the command line as well). Node then enables coverage on v8 via (I think...) the devtools protocol. Coverage is then written to a temp dir for everything that is flexed. I don't think there is a way to signal to node/v8 that it should only write data for specific files (to my current knowledge anyway).

c8 report then acts as a filter on the raw coverage data for what makes it into a report. In a sense, with -r none the --include flag is sort of a noop/invalid. (maybe we should verify and assert/warn on that). I guess the simplest way to support the expectation in the issue is to support a post-run, pre-report culling of the blobs based on include/exclude.

Edit to add: Or a command that more explicitly indicates the desire to run in two stages, like c8 cache-coverage that explicitly culls the output post run.

@bcoe thoughts?

j03m avatar Mar 30 '20 13:03 j03m