Skip to content

Commit

Permalink
fix: partial fix for #21997 & #22004, throw the originalError
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Jun 1, 2022
1 parent 4b4e992 commit cec6ce1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/server/lib/plugins/child/run_require_async_child.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ function run (ipc, file, projectRoot) {
// 3a. Yes: Use bundleRequire
// 3b. No: Continue through to `await import(configFile)`
// 4. Use node's dynamic import to import the configFile
let originalError

try {
return require(file)
} catch (err) {
originalError = err
if (!err.stack.includes('[ERR_REQUIRE_ESM]') && !err.stack.includes('SyntaxError: Cannot use import statement outside a module')) {
throw err
}
Expand All @@ -122,8 +124,15 @@ function run (ipc, file, projectRoot) {
debug(`User doesn't have esbuild. Going to use native node imports.`)

// We cannot replace the initial `require` with `await import` because
// Certain modules cannot be dynamically imported
return await import(file)
// Certain modules cannot be dynamically imported. If this throws, however, we want
// to show the original error that was thrown, because that's ultimately the source of the problem
try {
return await import(file)
} catch (e) {
// If we aren't able to import the file at all, throw the original error, since that has more accurate information
// of what failed to begin with
throw originalError
}
}

throw err
Expand Down

0 comments on commit cec6ce1

Please sign in to comment.