fix(coverage): exclude scripts with invalid URLs from raw coverage output (#28210)

Fixes https://github.com/denoland/deno/issues/28206.

Basically if you execute a script with `node:vm`, this produces a
"script" with the file name `evalmachine.<anonymous>`, which ends up
producing coverage like

```json
{
  "scriptId": "319",
  "url": "evalmachine.<anonymous>",
  "functions": [
    {
      "functionName": "",
      "ranges": [{ "startOffset": 0, "endOffset": 18, "count": 1 }],
      "isBlockCoverage": true
    }
  ]
}
```

We assume that the `url` field here (the specifier of the script) is a
valid URL, and so we error out when processing that coverage.

There are two potential fixes: either don't write the coverage files for
those scripts, or ignore the errors when we process the data. I went
with the former here.
This commit is contained in:
Nathan Whitaker 2025-02-20 14:23:43 -08:00 committed by GitHub
parent b20f98ccf7
commit e5de22b0b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 39 additions and 5 deletions

View file

@ -74,14 +74,15 @@ impl crate::worker::CoverageCollector for CoverageCollector {
let script_coverages = self.take_precise_coverage().await?.result;
for script_coverage in script_coverages {
// Filter out internal and http/https JS files and eval'd scripts
// from being included in coverage reports
if script_coverage.url.starts_with("ext:")
// Filter out internal and http/https JS files, eval'd scripts,
// and scripts with invalid urls from being included in coverage reports
if script_coverage.url.is_empty()
|| script_coverage.url.starts_with("ext:")
|| script_coverage.url.starts_with("[ext:")
|| script_coverage.url.starts_with("http:")
|| script_coverage.url.starts_with("https:")
|| script_coverage.url.starts_with("node:")
|| script_coverage.url.is_empty()
|| Url::parse(&script_coverage.url).is_err()
{
continue;
}