From 4ab9e6dc14f562316f536ea19eed58f7b28c583f Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Thu, 25 Jul 2024 20:01:27 -0400 Subject: [PATCH] fix(nextjs): fix inlined workspace root in .nx-helpers (#27136) This PR fixes an issue where our `withNx` function for Next.js is compiled with inlined `workspaceRoot` (to support container environments). On Windows, we write the `.nx-helpers/with-nx.js` with the following: ```js workspaceRoot: 'C:\Users\user\projects\app', ``` The `\u` character result in a Node error: `Invalid Unicode escape sequence`. The fix is to escape `\` as `\\`, so when the file is written, the path is valid and does not cause unicode errors. ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes #18824 --- .../next/src/executors/build/lib/create-next-config-file.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/next/src/executors/build/lib/create-next-config-file.ts b/packages/next/src/executors/build/lib/create-next-config-file.ts index e4e0bf1983462..ab2d804fc9141 100644 --- a/packages/next/src/executors/build/lib/create-next-config-file.ts +++ b/packages/next/src/executors/build/lib/create-next-config-file.ts @@ -124,7 +124,11 @@ export function getWithNxContent( index: getWithNxContextDeclaration.getStart(withNxSource), text: stripIndents`function getWithNxContext() { return { - workspaceRoot: '${workspaceRoot}', + workspaceRoot: '${ + // For Windows, paths like C:\Users\foo\bar need to be written as C:\\Users\\foo\\bar, + // or else when the file is read back, the single "\" will be treated as an escape character. + workspaceRoot.replaceAll('\\', '\\\\') + }', libsDir: '${workspaceLayout().libsDir}' } }`,