Skip to content

fix(nextjs): retry loading the config with TURBOPACK when Next rejects a Turbopack-only option - #699

Merged
abhis3 merged 7 commits into
firebase:mainfrom
npateras:nextjs-load-config-turbopack
Dec 4, 2026
Merged

abhis3 merged 7 commits into
firebase:mainfrom
npateras:nextjs-load-config-turbopack

Conversation

@npateras

@npateras npateras commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Next 16 builds with Turbopack by default. next build sets TURBOPACK=auto before it loads the
config (parseBundlerArgs in packages/next/src/lib/bundler.ts), and loadConfig rejects
Turbopack-only options when TURBOPACK is unset:

`experimental.turbopackRustReactCompiler` is only supported with Turbopack. Please remove the option or run Next.js with Turbopack in next.config.ts.

The adapter calls loadConfig(PHASE_PRODUCTION_BUILD, root) itself before runBuild(), and again
in validateNextConfigOverride, without that variable. So an app that sets one of these options
(turbopackRustReactCompiler, durableUseCacheEntries, useCacheStaticRootParamTracking,
cssChunking: "graph") builds locally and fails every App Hosting build. Apps work around it today
with turbopackRustReactCompiler: Boolean(process.env.TURBOPACK).

loadConfig still loads the config the way it does today. If Next rejects it as "only supported
with Turbopack" and TURBOPACK was unset, it loads once more with TURBOPACK=auto, the value
next build uses. It removes the variable again in a finally, so it never reaches runBuild(),
where next build --webpack would exit with "Multiple bundler flags set". Configs that load today
never see the variable, so webpack apps can't regress, and only Next 16+ throws this error. Thanks
to @abhis3 for suggesting this approach in review.

next-rspack stays as it is: withRspack calls process.exit(1) instead of throwing when
TURBOPACK is unset, so there is nothing to retry.

Testing

  • loadConfig unit tests in utils.spec.ts use a stand-in next package whose config loader
    reports the TURBOPACK value it ran with. They cover a config Next accepts (loaded without
    TURBOPACK), the Turbopack-only error (retried with auto, unset afterwards), any other error
    (rethrown without a retry), and the Turbopack-only error when TURBOPACK was already set
    (rethrown, variable left alone).
  • By hand against a real Next 16.4.3 install:
    • reactCompiler + experimental.turbopackRustReactCompiler: throws the error above → loads
    • experimental.cssChunking: "strict" (webpack-only): loads → loads, without TURBOPACK
  • npm run build, npm run lint (no errors) and npm run test:unit in adapter-nextjs pass. I
    didn't run test:functional (the starter is on Next 15.5, where this path is a no-op).

@google-cla

google-cla Bot commented Oct 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces logic to automatically set process.env.TURBOPACK to 'auto' when loading the configuration for Next.js 16+ projects that build with Turbopack, cleaning it up afterward to prevent conflicts. It also adds corresponding unit tests. Feedback was provided to make the buildsWithTurbopack helper more robust by using semVer.coerce to handle non-strict semver strings, safely handling potentially null scripts in package.json, and ensuring only string values are evaluated.

@abhis3 abhis3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the pr!

@npateras npateras changed the title fix(nextjs): set TURBOPACK while loading the config of a Next 16 Turbopack build fix(nextjs): retry loading the config with TURBOPACK when Next rejects a Turbopack-only option Oct 28, 2026
@npateras

Copy link
Copy Markdown
Contributor Author

Hi @abhis3, this is up to date with main and the checks are green, but GitHub still shows it as blocked. Could you approve it again or merge it when you get a chance?

@abhis3
abhis3 merged commit 1ca76b4 into firebase:main Dec 4, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants