Skip to content

Commit

Permalink
fix(coverage): flaky c8 coverage caused by parallel sourcemap constru…
Browse files Browse the repository at this point in the history
…cting

- construct sourcemaps one-by-one, non parallel
- prevent overwriting already loaded sourcemaps
  • Loading branch information
AriPerkkio committed Jan 2, 2023
1 parent e641a11 commit 1475f76
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions packages/coverage-c8/src/provider.ts
Expand Up @@ -48,39 +48,56 @@ export class C8CoverageProvider implements CoverageProvider {
takeCoverage()
const report = createReport(this.ctx.config.coverage)

interface MapAndSource { map: RawSourceMap; source: string | undefined }
type SourceMapMeta = { url: string; filepath: string } & MapAndSource

// add source maps
const sourceMapMeta: Record<string, { map: RawSourceMap; source: string | undefined }> = {}
await Promise.all(Array
const sourceMapMeta: Record<SourceMapMeta['url'], MapAndSource> = {}

const entries = Array
.from(this.ctx.vitenode.fetchCache.entries())
.filter(i => !i[0].includes('/node_modules/'))
.map(async ([file, { result }]) => {
const map = result.map
if (!map)
return
.map(([file, { result }]) => {
if (!result.map)
return null

const filepath = file.split('?')[0]

const url = _url.pathToFileURL(filepath).href

let code: string | undefined
try {
code = (await fs.readFile(filepath)).toString()
}
catch { }

// Vite does not report full path in sourcemap sources
// so use an actual file path
const sources = [url]

sourceMapMeta[url] = {
return {
filepath,
url: _url.pathToFileURL(filepath).href,
map: result.map,
source: result.code,
map: {
sourcesContent: code ? [code] : undefined,
...map,
sources,
},
}
}))
})
.filter((entry, index, all) => {
if (!entry)
return false

// Filter out duplicate entries
return all.findIndex(e => e && e.url === entry.url) === index
}) as SourceMapMeta[]

await Promise.all(entries.map(async ({ url, source, map, filepath }) => {
let code: string | undefined
try {
code = (await fs.readFile(filepath)).toString()
}
catch { }

// Vite does not report full path in sourcemap sources
// so use an actual file path
const sources = [url]

sourceMapMeta[url] = {
source,
map: {
sourcesContent: code ? [code] : undefined,
...map,
sources,
},
}
}))

// This is a magic number. It corresponds to the amount of code
// that we add in packages/vite-node/src/client.ts:114 (vm.runInThisContext)
Expand Down

0 comments on commit 1475f76

Please sign in to comment.