Skip to content

V8 source map cache is not used on Windows #300

@clemyan

Description

@clemyan
  • Version: v16.0.0, v15.14.0
  • Platform: Windows 10 64-bit

Summary

Somewhere along the node v15 line, V8 coverage data output by node changed format on Windows. Specifically, file URLs within the source-map-cache changed from

file://C:\some\path\main.js

to

file:///C:/some/path/main.js

(Note the extra forward slash in front)

I haven't bisected further but v15.0.0 is using backslashes and v15.14.0 is using forward slashes.

This makes _getSourceMap unable to correctly get cached source maps (since it searches using backslashes but keys in the sourceMapCache use forward slashes).

Symptoms

This directly affects use cases where the source maps are virtual (e.g. ts-node), causing c8 to report incorrect locations for uncovered code. Worse-case scenario is when the reported range is outside of the original source size (e.g. position 1000 in generated code is uncovered but original source is only 500 bytes), in which case the uncovered branch/function/line is silently discarded.

This also affects cases where source maps are not virtual (i.e. exist in actual fs), but in an unexpected way. v8-to-instanbul is also affected by a similar bug, and would throw when given a source map with file URL as source paths. However, since c8 cannot find the source map, no source maps are passed to v8-to-instanbul, avoiding the buggy code path.

Reproduction

Run the following file with c8 and ts-node

export default () => {
	console.log('uncovered')
}

// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Node v14.16.1 & v15.0.0 result:

image

Node v15.14.0 & v16.0.0 result:

image

Fix

This can be fixed by normalizing file URLs in the source-map-cache to (and always querying the sourceMapCache using) forward slashes, with url.fileURLToPath and url.pathToFileURL.

This, however, breaks some test due to the v8-to-instanbul bug noted above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions