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(ssr): show ssr module loader error stack #12651

Merged
merged 6 commits into from
Mar 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 38 additions & 34 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type SSRModule = Record<string, any>

const pendingModules = new Map<string, Promise<SSRModule>>()
const pendingImports = new Map<string, string[]>()
const importErrors = new WeakMap<Error, { importee: string }>()

export async function ssrLoadModule(
url: string,
Expand Down Expand Up @@ -131,32 +132,39 @@ async function instantiateModule(
const pendingDeps: string[] = []

const ssrImport = async (dep: string) => {
if (dep[0] !== '.' && dep[0] !== '/') {
return nodeImport(dep, mod.file!, resolveOptions)
}
// convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that
dep = unwrapId(dep)
if (!isCircular(dep) && !pendingImports.get(dep)?.some(isCircular)) {
pendingDeps.push(dep)
if (pendingDeps.length === 1) {
pendingImports.set(url, pendingDeps)
try {
if (dep[0] !== '.' && dep[0] !== '/') {
return await nodeImport(dep, mod.file!, resolveOptions)
}
const mod = await ssrLoadModule(
dep,
server,
context,
urlStack,
fixStacktrace,
)
if (pendingDeps.length === 1) {
pendingImports.delete(url)
} else {
pendingDeps.splice(pendingDeps.indexOf(dep), 1)
// convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that
dep = unwrapId(dep)
if (!isCircular(dep) && !pendingImports.get(dep)?.some(isCircular)) {
pendingDeps.push(dep)
if (pendingDeps.length === 1) {
pendingImports.set(url, pendingDeps)
}
const mod = await ssrLoadModule(
dep,
server,
context,
urlStack,
fixStacktrace,
)
if (pendingDeps.length === 1) {
pendingImports.delete(url)
} else {
pendingDeps.splice(pendingDeps.indexOf(dep), 1)
}
// return local module to avoid race condition #5470
return mod
}
// return local module to avoid race condition #5470
return mod
return moduleGraph.urlToModuleMap.get(dep)?.ssrModule
} catch (err) {
// tell external error handler which mod was imported with error
importErrors.set(err, { importee: dep })

throw err
}
return moduleGraph.urlToModuleMap.get(dep)?.ssrModule
}

const ssrDynamicImport = (dep: string) => {
Expand Down Expand Up @@ -204,6 +212,7 @@ async function instantiateModule(
)
} catch (e) {
mod.ssrError = e
const errorData = importErrors.get(e)

if (e.stack && fixStacktrace) {
ssrFixStacktrace(e, moduleGraph)
Expand All @@ -212,7 +221,10 @@ async function instantiateModule(
server.config.logger.error(
colors.red(
`Error when evaluating SSR module ${url}:` +
(e.importee ? ` failed to import "${e.importee}"\n` : '\n'),
(errorData?.importee
? ` failed to import "${errorData.importee}"`
: '') +
`\n|- ${e.stack}\n`,
),
{
timestamp: true,
Expand All @@ -221,7 +233,6 @@ async function instantiateModule(
},
)

delete e.importee
bluwy marked this conversation as resolved.
Show resolved Hide resolved
throw e
}

Expand Down Expand Up @@ -262,15 +273,8 @@ async function nodeImport(
}
}

try {
const mod = await dynamicImport(url)
return proxyESM(mod)
} catch (err) {
// tell external error handler which mod was imported with error
err.importee = id

throw err
}
const mod = await dynamicImport(url)
return proxyESM(mod)
}

// rollup-style default import interop for cjs
Expand Down