From c9e2c75aa7d08bc745193e6dd9fd52831a2c483f Mon Sep 17 00:00:00 2001 From: Hendrik Liebau Date: Mon, 17 Feb 2025 10:18:48 +0100 Subject: [PATCH] Fix runtime error in `writeConfigurationDefaults` Follow-up for #74674 When running `pnpm next build` or `pnpm next dev` with a test app in the Next.js repo, we patch the app's `tsconfig.json` to exclude the test files from the program, because those are already covered by the root `tsconfig.json`. The code to suppress logging this action had a bug where we tried to remove the action from the list of `requiredActions` even if it wasn't added, resulting in the following runtime error: ``` Cannot read properties of undefined (reading 'includes') ``` This could happen when the `requiredActions` array was empty but the `suggestedActions` array was not. --- .../typescript/writeConfigurationDefaults.ts | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts index 1858bc174225a..cda8fcb658ad1 100644 --- a/packages/next/src/lib/typescript/writeConfigurationDefaults.ts +++ b/packages/next/src/lib/typescript/writeConfigurationDefaults.ts @@ -136,6 +136,9 @@ export function getRequiredConfiguration( return res } +const localDevTestFilesExcludeAction = + 'NEXT_PRIVATE_LOCAL_DEV_TEST_FILES_EXCLUDE' + export async function writeConfigurationDefaults( ts: typeof import('typescript'), tsConfigPath: string, @@ -328,9 +331,7 @@ export async function writeConfigurationDefaults( } if (hasUpdates) { - requiredActions.push( - 'Local development only: Excluded test files from coverage' - ) + requiredActions.push(localDevTestFilesExcludeAction) } } @@ -338,17 +339,6 @@ export async function writeConfigurationDefaults( return } - if (process.env.NEXT_PRIVATE_LOCAL_DEV) { - // remove it from the required actions if it exists - if ( - requiredActions[requiredActions.length - 1].includes( - 'Local development only' - ) - ) { - requiredActions.pop() - } - } - await fs.writeFile( tsConfigPath, CommentJson.stringify(userTsConfig, null, 2) + os.EOL @@ -386,14 +376,20 @@ export async function writeConfigurationDefaults( Log.info('') } - if (requiredActions.length) { + const requiredActionsToBeLogged = process.env.NEXT_PRIVATE_LOCAL_DEV + ? requiredActions.filter( + (action) => action !== localDevTestFilesExcludeAction + ) + : requiredActions + + if (requiredActionsToBeLogged.length) { Log.info( `The following ${white('mandatory changes')} were made to your ${cyan( 'tsconfig.json' )}:\n` ) - requiredActions.forEach((action) => Log.info(`\t- ${action}`)) + requiredActionsToBeLogged.forEach((action) => Log.info(`\t- ${action}`)) Log.info('') }