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 4 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
76 changes: 42 additions & 34 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,40 @@ 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
// add a counter to infer importer count, once counter = 0, remove the importee prop
err.importee = dep
err.importerCount = (err.importerCount || 0) + 1
throw err
}
return moduleGraph.urlToModuleMap.get(dep)?.ssrModule
}

const ssrDynamicImport = (dep: string) => {
Expand Down Expand Up @@ -212,7 +220,8 @@ async function instantiateModule(
server.config.logger.error(
colors.red(
`Error when evaluating SSR module ${url}:` +
(e.importee ? ` failed to import "${e.importee}"\n` : '\n'),
(e.importee ? ` failed to import "${e.importee}"` : '') +
`\n|- ${e.stack}\n`,
),
{
timestamp: true,
Expand All @@ -221,7 +230,13 @@ async function instantiateModule(
},
)

delete e.importee
bluwy marked this conversation as resolved.
Show resolved Hide resolved
e.importerCount--

if (!e.importerCount) {
delete e.importee
delete e.importerCount
}

throw e
}

Expand Down Expand Up @@ -262,15 +277,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