From 3a2fff57020b3d5c672645c679d4693430d135cf Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 4 Apr 2025 16:19:06 -0700 Subject: [PATCH 1/2] Revert "Turbopack build: Replace process.env.TURBOPACK usage (#77783)" This reverts commit 1d685d4b5d73b797213a006f6d5ef24a309614f2. --- packages/next/next-runtime.webpack-config.js | 1 - packages/next/src/build/index.ts | 1 + packages/next/src/cli/next-start.ts | 10 ++++++++++ packages/next/src/server/next-server.ts | 14 ++++++++++++++ packages/next/src/server/next.ts | 3 +++ .../app-dir/turbopack-build-marker/app/layout.tsx | 8 ++++++++ .../app-dir/turbopack-build-marker/app/page.tsx | 3 +++ .../app-dir/turbopack-build-marker/next.config.js | 6 ++++++ .../turbopack-build-marker.test.ts | 15 +++++++++++++++ 9 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 test/production/app-dir/turbopack-build-marker/app/layout.tsx create mode 100644 test/production/app-dir/turbopack-build-marker/app/page.tsx create mode 100644 test/production/app-dir/turbopack-build-marker/next.config.js create mode 100644 test/production/app-dir/turbopack-build-marker/turbopack-build-marker.test.ts diff --git a/packages/next/next-runtime.webpack-config.js b/packages/next/next-runtime.webpack-config.js index 59c129caa0981..c868ada85b295 100644 --- a/packages/next/next-runtime.webpack-config.js +++ b/packages/next/next-runtime.webpack-config.js @@ -229,7 +229,6 @@ module.exports = ({ dev, turbo, bundleType, experimental, ...rest }) => { experimental ? true : false ), 'process.env.NEXT_RUNTIME': JSON.stringify('nodejs'), - 'process.turbopack': JSON.stringify(turbo), 'process.env.TURBOPACK': JSON.stringify(turbo), }), !!process.env.ANALYZE && diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index c0317add32e3d..cda100c05d66c 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -2275,6 +2275,7 @@ export default async function build( // @ts-expect-error internal field TODO: fix this, should use a separate mechanism to pass the info. isExperimentalCompile: isCompileMode, + isTurbopackBuild: isTurbopack, }, }, appDir: dir, diff --git a/packages/next/src/cli/next-start.ts b/packages/next/src/cli/next-start.ts index c127edbdce1d2..ee606b1c1980c 100755 --- a/packages/next/src/cli/next-start.ts +++ b/packages/next/src/cli/next-start.ts @@ -33,6 +33,16 @@ const nextStart = async (options: NextStartOptions, directory?: string) => { printAndExit(getReservedPortExplanation(port), 1) } + const isTurbopack = Boolean( + options.turbo || + options.turbopack || + // TODO: Used for Testing in Next.js CI. Rename to something better like `NEXT_TEST_TURBOPACK`. + process.env.TURBOPACK + ) + if (isTurbopack) { + process.env.TURBOPACK = '1' + } + await startServer({ dir, isDev: false, diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index eb98fd4bc565e..3838afc4277c2 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -192,6 +192,20 @@ export default class NextNodeServer extends BaseServer< this.isDev = isDev this.sriEnabled = Boolean(options.conf.experimental?.sri?.algorithm) + // @ts-expect-error internal field not publicly exposed + const isTurbopackBuild = this.nextConfig.experimental?.isTurbopackBuild + if (!isDev && typeof isTurbopackBuild !== 'undefined') { + if (process.env.TURBOPACK && !isTurbopackBuild) { + throw new Error( + `Invariant: --turbopack is set but the build used Webpack` + ) + } else if (!process.env.TURBOPACK && isTurbopackBuild) { + throw new Error( + `Invariant: --turbopack is not set but the build used Turbopack. Add --turbopack to "next start".` + ) + } + } + /** * This sets environment variable to be used at the time of SSR by head.tsx. * Using this from process.env allows targeting SSR by calling diff --git a/packages/next/src/server/next.ts b/packages/next/src/server/next.ts index 8ff153b83a972..62df59fa569ad 100644 --- a/packages/next/src/server/next.ts +++ b/packages/next/src/server/next.ts @@ -240,6 +240,9 @@ export class NextServer implements NextWrapperServer { // @ts-expect-error internal field config.experimental.isExperimentalCompile = serializedConfig.experimental.isExperimentalCompile + // @ts-expect-error internal field + config.experimental.isTurbopackBuild = + serializedConfig.experimental.isTurbopackBuild } catch (_) { // if distDir is customized we don't know until we // load the config so fallback to loading the config diff --git a/test/production/app-dir/turbopack-build-marker/app/layout.tsx b/test/production/app-dir/turbopack-build-marker/app/layout.tsx new file mode 100644 index 0000000000000..888614deda3ba --- /dev/null +++ b/test/production/app-dir/turbopack-build-marker/app/layout.tsx @@ -0,0 +1,8 @@ +import { ReactNode } from 'react' +export default function Root({ children }: { children: ReactNode }) { + return ( + + {children} + + ) +} diff --git a/test/production/app-dir/turbopack-build-marker/app/page.tsx b/test/production/app-dir/turbopack-build-marker/app/page.tsx new file mode 100644 index 0000000000000..ff7159d9149fe --- /dev/null +++ b/test/production/app-dir/turbopack-build-marker/app/page.tsx @@ -0,0 +1,3 @@ +export default function Page() { + return

hello world

+} diff --git a/test/production/app-dir/turbopack-build-marker/next.config.js b/test/production/app-dir/turbopack-build-marker/next.config.js new file mode 100644 index 0000000000000..807126e4cf0bf --- /dev/null +++ b/test/production/app-dir/turbopack-build-marker/next.config.js @@ -0,0 +1,6 @@ +/** + * @type {import('next').NextConfig} + */ +const nextConfig = {} + +module.exports = nextConfig diff --git a/test/production/app-dir/turbopack-build-marker/turbopack-build-marker.test.ts b/test/production/app-dir/turbopack-build-marker/turbopack-build-marker.test.ts new file mode 100644 index 0000000000000..9c5f265498426 --- /dev/null +++ b/test/production/app-dir/turbopack-build-marker/turbopack-build-marker.test.ts @@ -0,0 +1,15 @@ +import { nextTestSetup } from 'e2e-utils' +describe('turbopack-build-marker', () => { + const { next } = nextTestSetup({ + files: __dirname, + }) + + it('should have Turbopack build marker', async () => { + const requiredServerFilesManifest = await next.readJSON( + '.next/required-server-files.json' + ) + expect( + requiredServerFilesManifest.config.experimental.isTurbopackBuild + ).toBe(!!process.env.TURBOPACK) + }) +}) From 49b0554f79584adae26dcaea8f7f9e805abf20a1 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Fri, 4 Apr 2025 16:29:37 -0700 Subject: [PATCH 2/2] Remove error --- packages/next/src/server/next-server.ts | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index 3838afc4277c2..eb98fd4bc565e 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -192,20 +192,6 @@ export default class NextNodeServer extends BaseServer< this.isDev = isDev this.sriEnabled = Boolean(options.conf.experimental?.sri?.algorithm) - // @ts-expect-error internal field not publicly exposed - const isTurbopackBuild = this.nextConfig.experimental?.isTurbopackBuild - if (!isDev && typeof isTurbopackBuild !== 'undefined') { - if (process.env.TURBOPACK && !isTurbopackBuild) { - throw new Error( - `Invariant: --turbopack is set but the build used Webpack` - ) - } else if (!process.env.TURBOPACK && isTurbopackBuild) { - throw new Error( - `Invariant: --turbopack is not set but the build used Turbopack. Add --turbopack to "next start".` - ) - } - } - /** * This sets environment variable to be used at the time of SSR by head.tsx. * Using this from process.env allows targeting SSR by calling