Unverified Commit 698220d1 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Fix runtime error in `writeConfigurationDefaults` (#76106)

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.
parent 4b8bf686
...@@ -136,6 +136,9 @@ export function getRequiredConfiguration( ...@@ -136,6 +136,9 @@ export function getRequiredConfiguration(
return res return res
} }
const localDevTestFilesExcludeAction =
'NEXT_PRIVATE_LOCAL_DEV_TEST_FILES_EXCLUDE'
export async function writeConfigurationDefaults( export async function writeConfigurationDefaults(
ts: typeof import('typescript'), ts: typeof import('typescript'),
tsConfigPath: string, tsConfigPath: string,
...@@ -328,9 +331,7 @@ export async function writeConfigurationDefaults( ...@@ -328,9 +331,7 @@ export async function writeConfigurationDefaults(
} }
if (hasUpdates) { if (hasUpdates) {
requiredActions.push( requiredActions.push(localDevTestFilesExcludeAction)
'Local development only: Excluded test files from coverage'
)
} }
} }
...@@ -338,17 +339,6 @@ export async function writeConfigurationDefaults( ...@@ -338,17 +339,6 @@ export async function writeConfigurationDefaults(
return 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( await fs.writeFile(
tsConfigPath, tsConfigPath,
CommentJson.stringify(userTsConfig, null, 2) + os.EOL CommentJson.stringify(userTsConfig, null, 2) + os.EOL
...@@ -386,14 +376,20 @@ export async function writeConfigurationDefaults( ...@@ -386,14 +376,20 @@ export async function writeConfigurationDefaults(
Log.info('') Log.info('')
} }
if (requiredActions.length) { const requiredActionsToBeLogged = process.env.NEXT_PRIVATE_LOCAL_DEV
? requiredActions.filter(
(action) => action !== localDevTestFilesExcludeAction
)
: requiredActions
if (requiredActionsToBeLogged.length) {
Log.info( Log.info(
`The following ${white('mandatory changes')} were made to your ${cyan( `The following ${white('mandatory changes')} were made to your ${cyan(
'tsconfig.json' 'tsconfig.json'
)}:\n` )}:\n`
) )
requiredActions.forEach((action) => Log.info(`\t- ${action}`)) requiredActionsToBeLogged.forEach((action) => Log.info(`\t- ${action}`))
Log.info('') Log.info('')
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment