Skip to content

Commit

Permalink
fix(gatsby): Better compile error handling (#35038)
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts authored Mar 3, 2022
1 parent 2f361cc commit c621381
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/gatsby/src/bootstrap/get-config-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ export async function getConfigFile(
configPath = path.join(`${siteDirectory}/${COMPILED_CACHE_DIR}`, configName)
configFilePath = require.resolve(configPath)
configModule = require(configFilePath)
} catch (err) {
if (!(err.code === `MODULE_NOT_FOUND`)) {
// If it's not the MODULE_NOT_FOUND error (which can happen if we're looking for JS files)
// It means it's an error with the compiled file
} catch (outerError) {
// Not all plugins will have a compiled file, so the err.message can look like this:
// "Cannot find module '<root>/node_modules/gatsby-source-filesystem/.cache/compiled/gatsby-config'"
// But the compiled file can also have an error like this:
// "Cannot find module 'foobar'"
// So this is trying to differentiate between an error we're fine ignoring and an error that we should throw
const isModuleNotFoundError = outerError.code === `MODULE_NOT_FOUND`
const isThisFileRequireError =
outerError?.requireStack?.[0]?.includes(`get-config-file`) ?? true

if (!(isModuleNotFoundError && isThisFileRequireError)) {
report.panic({
id: `11902`,
error: err,
error: outerError,
context: {
configName,
message: err.message,
message: outerError.message,
},
})
}
Expand All @@ -51,27 +58,27 @@ export async function getConfigFile(
try {
configFilePath = require.resolve(configPath)
configModule = require(configFilePath)
} catch (err) {
} catch (innerError) {
// Only then hard fail
const nearMatch = await fs.readdir(siteDirectory).then(files =>
files.find(file => {
const fileName = file.split(siteDirectory).pop()
return isNearMatch(fileName, configName, distance)
})
)
if (!testRequireError(configPath, err)) {
if (!testRequireError(configPath, innerError)) {
report.panic({
id: `10123`,
error: err,
error: innerError,
context: {
configName,
message: err.message,
message: innerError.message,
},
})
} else if (nearMatch) {
report.panic({
id: `10124`,
error: err,
error: innerError,
context: {
configName,
nearMatch,
Expand Down

0 comments on commit c621381

Please sign in to comment.