Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: address bugs with source remapping on Windows #301

Merged
merged 3 commits into from May 2, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 23 additions & 6 deletions lib/report.js
@@ -1,10 +1,10 @@
const Exclude = require('test-exclude')
const furi = require('furi')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove this from the package.json as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can indeed

const libCoverage = require('istanbul-lib-coverage')
const libReport = require('istanbul-lib-report')
const reports = require('istanbul-reports')
const { readdirSync, readFileSync, statSync } = require('fs')
const { isAbsolute, resolve, extname } = require('path')
const { pathToFileURL, fileURLToPath } = require('url')
const getSourceMapFromFile = require('./source-map-from-file')
// TODO: switch back to @c88/v8-coverage once patch is landed.
const v8toIstanbul = require('v8-to-istanbul')
Expand Down Expand Up @@ -139,8 +139,8 @@ class Report {
*/
_getSourceMap (v8ScriptCov) {
const sources = {}
if (this.sourceMapCache[`file://${v8ScriptCov.url}`]) {
const sourceMapAndLineLengths = this.sourceMapCache[`file://${v8ScriptCov.url}`]
const sourceMapAndLineLengths = this.sourceMapCache[pathToFileURL(v8ScriptCov.url).href]
if (sourceMapAndLineLengths) {
// See: https://github.com/nodejs/node/pull/34305
if (!sourceMapAndLineLengths.data) return
sources.sourceMap = {
Expand Down Expand Up @@ -173,7 +173,7 @@ class Report {
for (const v8ProcessCov of this._loadReports()) {
if (this._isCoverageObject(v8ProcessCov)) {
if (v8ProcessCov['source-map-cache']) {
Object.assign(this.sourceMapCache, v8ProcessCov['source-map-cache'])
Object.assign(this.sourceMapCache, this._normalizeSourceMapCache(v8ProcessCov['source-map-cache']))
}
v8ProcessCovs.push(this._normalizeProcessCov(v8ProcessCov, fileIndex))
}
Expand All @@ -194,7 +194,7 @@ class Report {
const stat = statSync(fullPath)
const sourceMap = getSourceMapFromFile(fullPath)
if (sourceMap) {
this.sourceMapCache[`file://${fullPath}`] = { data: sourceMap }
this.sourceMapCache[pathToFileURL(fullPath)] = { data: sourceMap }
}
emptyReports.push({
scriptId: 0,
Expand Down Expand Up @@ -275,7 +275,7 @@ class Report {
}
if (/^file:\/\//.test(v8ScriptCov.url)) {
try {
v8ScriptCov.url = furi.toSysPath(v8ScriptCov.url)
v8ScriptCov.url = fileURLToPath(v8ScriptCov.url)
fileIndex.add(v8ScriptCov.url)
} catch (err) {
debuglog(`${err.stack}`)
Expand All @@ -290,6 +290,23 @@ class Report {
}
return { result }
}

/**
* Normalizes a V8 source map cache.
*
* This function normalizes file URLs to a system-independent format.
*
* @param v8SourceMapCache V8 source map cache to normalize.
* @return {v8SourceMapCache} Normalized V8 source map cache.
* @private
*/
_normalizeSourceMapCache (v8SourceMapCache) {
const cache = {}
for (const fileURL of Object.keys(v8SourceMapCache)) {
cache[pathToFileURL(fileURLToPath(fileURL)).href] = v8SourceMapCache[fileURL]
}
return cache
}
}

module.exports = function (opts) {
Expand Down