From c5bccf4d1bb0b4be25f6398ec2d735e5c3efdea0 Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Mon, 7 Nov 2022 14:20:10 -0800 Subject: [PATCH] fix(next/dev): bubble up unhandled exception from --turbo (#42594) This PR fixes `--turbo` can shallow some of unhandled exception, trying to catch and proceed as much as it can. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) --- packages/next/cli/next-dev.ts | 116 ++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 48 deletions(-) diff --git a/packages/next/cli/next-dev.ts b/packages/next/cli/next-dev.ts index fa079ccfd35536d..f00f4b9d8806ec2 100755 --- a/packages/next/cli/next-dev.ts +++ b/packages/next/cli/next-dev.ts @@ -165,6 +165,8 @@ const nextDev: cliCommand = (argv) => { const { setGlobal } = require('../trace') as typeof import('../trace') const { Telemetry } = require('../telemetry/storage') as typeof import('../telemetry/storage') + const findUp = + require('next/dist/compiled/find-up') as typeof import('next/dist/compiled/find-up') // To regenerate the TURBOPACK gradient require('gradient-string')('blue', 'red')('>>> TURBOPACK') const isTTY = process.stdout.isTTY @@ -182,59 +184,75 @@ const nextDev: cliCommand = (argv) => { let babelrc = await getBabelConfigFile(dir) if (babelrc) babelrc = path.basename(babelrc) - const rawNextConfig = interopDefault( - await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, undefined, true) - ) as NextConfig - - const checkUnsupportedCustomConfig = ( - configKey = '', - parentUserConfig: any, - parentDefaultConfig: any - ): boolean => { - // these should not error - if ( - configKey === 'serverComponentsExternalPackages' || - configKey === 'appDir' || - configKey === 'transpilePackages' || - configKey === 'reactStrictMode' || - configKey === 'swcMinify' || - configKey === 'configFileName' - ) { - return false - } - let userValue = parentUserConfig[configKey] - let defaultValue = parentDefaultConfig[configKey] + let hasNonDefaultConfig + let postcssFile + let tailwindFile + let rawNextConfig: NextConfig + + try { + rawNextConfig = interopDefault( + await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, undefined, true) + ) as NextConfig + + const checkUnsupportedCustomConfig = ( + configKey = '', + parentUserConfig: any, + parentDefaultConfig: any + ): boolean => { + try { + // these should not error + if ( + configKey === 'serverComponentsExternalPackages' || + configKey === 'appDir' || + configKey === 'transpilePackages' || + configKey === 'reactStrictMode' || + configKey === 'swcMinify' || + configKey === 'configFileName' + ) { + return false + } + let userValue = parentUserConfig?.[configKey] + let defaultValue = parentDefaultConfig?.[configKey] - if (typeof defaultValue !== 'object') { - return defaultValue !== userValue + if (typeof defaultValue !== 'object') { + return defaultValue !== userValue + } + return Object.keys(userValue || {}).some((key: string) => { + return checkUnsupportedCustomConfig(key, userValue, defaultValue) + }) + } catch (e) { + console.error( + `Unexpected error occurred while checking ${configKey}`, + e + ) + return false + } } - return Object.keys(userValue || {}).some((key: string) => { - return checkUnsupportedCustomConfig(key, userValue, defaultValue) - }) - } - const hasNonDefaultConfig = Object.keys(rawNextConfig).some((key) => - checkUnsupportedCustomConfig(key, rawNextConfig, defaultConfig) - ) - - const findUp = - require('next/dist/compiled/find-up') as typeof import('next/dist/compiled/find-up') - const packagePath = findUp.sync('package.json', { cwd: dir }) - let hasSideCar = false - - if (packagePath) { - const pkgData = require(packagePath) - hasSideCar = Object.values( - (pkgData.scripts || {}) as Record - ).some( - (script) => script.includes('tailwind') || script.includes('postcss') + hasNonDefaultConfig = Object.keys(rawNextConfig).some((key) => + checkUnsupportedCustomConfig(key, rawNextConfig, defaultConfig) ) - } - let postcssFile = !hasSideCar && (await findConfigPath(dir, 'postcss')) - let tailwindFile = !hasSideCar && (await findConfigPath(dir, 'tailwind')) - if (postcssFile) postcssFile = path.basename(postcssFile) - if (tailwindFile) tailwindFile = path.basename(tailwindFile) + const packagePath = findUp.sync('package.json', { cwd: dir }) + let hasSideCar = false + + if (packagePath) { + const pkgData = require(packagePath) + hasSideCar = Object.values( + (pkgData.scripts || {}) as Record + ).some( + (script) => + script.includes('tailwind') || script.includes('postcss') + ) + } + postcssFile = !hasSideCar && (await findConfigPath(dir, 'postcss')) + tailwindFile = !hasSideCar && (await findConfigPath(dir, 'tailwind')) + + if (postcssFile) postcssFile = path.basename(postcssFile) + if (tailwindFile) tailwindFile = path.basename(tailwindFile) + } catch (e) { + console.error('Unexpected error occurred while checking config', e) + } const hasWarningOrError = tailwindFile || postcssFile || babelrc || hasNonDefaultConfig @@ -381,6 +399,8 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit console.error(err) } ) + }).catch((err) => { + console.error(err) }) } else { startServer(devServerOptions)