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: ignore ENOENT in injectSourcesContent #2904

Merged
merged 5 commits into from Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
60 changes: 47 additions & 13 deletions packages/vite/src/node/server/sourcemap.ts
@@ -1,20 +1,54 @@
import { promises as fs } from 'fs'
import path from 'path'
import { promises as fs } from 'fs'
import { Logger } from '../logger'
import { createDebugger } from '../utils'

const isDebug = !!process.env.DEBUG
const debug = createDebugger('vite:sourcemap', {
onlyWhenFocused: true
})

interface SourceMapLike {
sources: string[]
sourcesContent?: (string | null)[]
sourceRoot?: string
}

export async function injectSourcesContent(
map: { sources: string[]; sourcesContent?: string[]; sourceRoot?: string },
file: string
map: SourceMapLike,
file: string,
logger: Logger
): Promise<void> {
const sourceRoot = await fs.realpath(
path.resolve(path.dirname(file), map.sourceRoot || '')
)
map.sourcesContent = []
await Promise.all(
map.sources.filter(Boolean).map(async (sourcePath, i) => {
map.sourcesContent![i] = await fs.readFile(
path.resolve(sourceRoot, decodeURI(sourcePath)),
'utf-8'
)
let sourceRoot: string | undefined
try {
// The source root is undefined for virtual modules and permission errors.
sourceRoot = await fs.realpath(
path.resolve(path.dirname(file), map.sourceRoot || '')
)
} catch {}

const missingSources: string[] = []
map.sourcesContent = await Promise.all(
map.sources.map((sourcePath) => {
if (sourcePath) {
sourcePath = decodeURI(sourcePath)
if (sourceRoot) {
sourcePath = path.resolve(sourceRoot, sourcePath)
}
return fs.readFile(sourcePath, 'utf-8').catch(() => {
missingSources.push(sourcePath)
return null
})
}
return null
})
)

// Use this command…
// DEBUG="vite:sourcemap" vite build
// …to log the missing sources.
if (missingSources.length) {
logger.warnOnce(`Sourcemap for "${file}" points to missing source files`)
isDebug && debug(`Missing sources:\n ` + missingSources.join(`\n `))
}
}
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/transformRequest.ts
Expand Up @@ -147,7 +147,7 @@ export async function transformRequest(
if (map && mod.file) {
map = (typeof map === 'string' ? JSON.parse(map) : map) as SourceMap
if (map.mappings && !map.sourcesContent) {
await injectSourcesContent(map, mod.file)
await injectSourcesContent(map, mod.file, logger)
}
}

Expand Down