Unverified Commit f3a6f976 authored by Josh Story's avatar Josh Story Committed by GitHub

Preserve configured output directories during static export (#99507)

## Summary

Static export with a custom `distDir` currently overwrites that option
with `.next` during the build. Keep the configured export destination
intact and derive the intermediate build directory separately, so
adapters receive the original option while their build-directory
argument still points to the artifacts.

This removes a post-load config mutation as part of making completed
configuration immutable for adapters and other runtime consumers.
Preserve support for both relative and absolute Turbopack project output
paths.

## Verification

- Existing adapter tests pass in both production bundlers; custom
export-directory coverage passes with Turbopack.
- Existing server trace suite passes with Turbopack: six tests and five
snapshots.
- Existing Rust API suite passes its 25 main cases and 15 snapshots in
each bundler. Its RSS stabilization stress case fails locally on both
this branch and unmodified canary.

<!-- NEXT_JS_LLM -->

---------
Co-authored-by: 's avatarJamiboy Mohammad <jamiboym@gmail.com>
parent b22e5a29
...@@ -7,6 +7,7 @@ import type { ProxyMatcher } from './analysis/get-page-static-info' ...@@ -7,6 +7,7 @@ import type { ProxyMatcher } from './analysis/get-page-static-info'
import type { Rewrite } from '../lib/load-custom-routes' import type { Rewrite } from '../lib/load-custom-routes'
import path from 'node:path' import path from 'node:path'
import { needsExperimentalReact } from '../lib/needs-experimental-react' import { needsExperimentalReact } from '../lib/needs-experimental-react'
import { getBuildDistDir } from '../export/utils'
import { import {
getNextConfigEnv, getNextConfigEnv,
getNextPublicEnvironmentVariables, getNextPublicEnvironmentVariables,
...@@ -355,7 +356,9 @@ export function getDefineEnv({ ...@@ -355,7 +356,9 @@ export function getDefineEnv({
config.experimental.allowedRevalidateHeaderKeys ?? [], config.experimental.allowedRevalidateHeaderKeys ?? [],
...(isNodeServer || isEdgeServer ...(isNodeServer || isEdgeServer
? { ? {
'process.env.__NEXT_RELATIVE_DIST_DIR': config.distDir, 'process.env.__NEXT_RELATIVE_DIST_DIR': dev
? config.distDir
: getBuildDistDir(config),
'process.env.__NEXT_RELATIVE_PROJECT_DIR': path.relative( 'process.env.__NEXT_RELATIVE_PROJECT_DIR': path.relative(
process.cwd(), process.cwd(),
projectPath projectPath
......
...@@ -210,7 +210,7 @@ import { ...@@ -210,7 +210,7 @@ import {
type ConfiguredExperimentalFeature, type ConfiguredExperimentalFeature,
} from '../server/lib/app-info-log' } from '../server/lib/app-info-log'
import type { NextEnabledDirectories } from '../server/base-server' import type { NextEnabledDirectories } from '../server/base-server'
import { hasCustomExportOutput } from '../export/utils' import { getBuildDistDir, hasCustomExportOutput } from '../export/utils'
import { traceMemoryUsage } from '../lib/memory/trace' import { traceMemoryUsage } from '../lib/memory/trace'
import { generateEncryptionKeyBase64 } from '../server/app-render/encryption-utils-server' import { generateEncryptionKeyBase64 } from '../server/app-render/encryption-utils-server'
import type { DeepReadonly } from '../shared/lib/deep-readonly' import type { DeepReadonly } from '../shared/lib/deep-readonly'
...@@ -1236,12 +1236,11 @@ export default async function build( ...@@ -1236,12 +1236,11 @@ export default async function build(
bundler = finalizeBundlerFromConfig(bundler) bundler = finalizeBundlerFromConfig(bundler)
nextBuildSpan.setAttribute('bundler', getBundlerForTelemetry(bundler)) nextBuildSpan.setAttribute('bundler', getBundlerForTelemetry(bundler))
let configOutDir = 'out' const configOutDir = hasCustomExportOutput(config)
if (hasCustomExportOutput(config)) { ? config.distDir
configOutDir = config.distDir : 'out'
config.distDir = '.next' const buildDistDir = getBuildDistDir(config)
} const distDir = path.join(dir, buildDistDir)
const distDir = path.join(dir, config.distDir)
NextBuildContext.distDir = distDir NextBuildContext.distDir = distDir
setGlobal('phase', PHASE_PRODUCTION_BUILD) setGlobal('phase', PHASE_PRODUCTION_BUILD)
setGlobal('distDir', distDir) setGlobal('distDir', distDir)
...@@ -2080,6 +2079,7 @@ export default async function build( ...@@ -2080,6 +2079,7 @@ export default async function build(
version: 1, version: 1,
config: { config: {
...runtimeConfigWithoutFilePath, ...runtimeConfigWithoutFilePath,
distDir: buildDistDir,
...(ciEnvironment.hasNextSupport ...(ciEnvironment.hasNextSupport
? { ? {
compress: false, compress: false,
...@@ -2162,14 +2162,14 @@ export default async function build( ...@@ -2162,14 +2162,14 @@ export default async function build(
SERVER_FILES_MANIFEST + '.json', SERVER_FILES_MANIFEST + '.json',
] ]
.filter(nonNullable) .filter(nonNullable)
.map((file) => path.join(config.distDir, file)), .map((file) => path.join(buildDistDir, file)),
ignore: [] as string[], ignore: [] as string[],
} }
if (hasInstrumentationHook) { if (hasInstrumentationHook) {
serverFilesManifest.files.push( serverFilesManifest.files.push(
path.join( path.join(
config.distDir, buildDistDir,
SERVER_DIRECTORY, SERVER_DIRECTORY,
`${INSTRUMENTATION_HOOK_FILENAME}.js` `${INSTRUMENTATION_HOOK_FILENAME}.js`
) )
...@@ -2177,7 +2177,7 @@ export default async function build( ...@@ -2177,7 +2177,7 @@ export default async function build(
// If there are edge routes, append the edge instrumentation hook // If there are edge routes, append the edge instrumentation hook
// Turbopack generates this chunk with a hashed name and references it in middleware-manifest. // Turbopack generates this chunk with a hashed name and references it in middleware-manifest.
let edgeInstrumentationHook = path.join( let edgeInstrumentationHook = path.join(
config.distDir, buildDistDir,
SERVER_DIRECTORY, SERVER_DIRECTORY,
`edge-${INSTRUMENTATION_HOOK_FILENAME}.js` `edge-${INSTRUMENTATION_HOOK_FILENAME}.js`
) )
...@@ -2210,7 +2210,7 @@ export default async function build( ...@@ -2210,7 +2210,7 @@ export default async function build(
serverFilesManifest.files.push( serverFilesManifest.files.push(
...cssFilePaths.map((filePath) => ...cssFilePaths.map((filePath) =>
path.join(config.distDir, 'static', filePath) path.join(buildDistDir, 'static', filePath)
) )
) )
} }
...@@ -3470,7 +3470,7 @@ export default async function build( ...@@ -3470,7 +3470,7 @@ export default async function build(
} }
writeTurborepoAccessTraceResult({ writeTurborepoAccessTraceResult({
distDir: config.distDir, distDir: buildDistDir,
traces: [ traces: [
turborepoAccessTraceResult, turborepoAccessTraceResult,
...exportResult.turborepoAccessTraceResults.values(), ...exportResult.turborepoAccessTraceResults.values(),
...@@ -4934,7 +4934,7 @@ export default async function build( ...@@ -4934,7 +4934,7 @@ export default async function build(
traceUploadUrl, traceUploadUrl,
mode: 'build', mode: 'build',
projectDir: dir, projectDir: dir,
distDir: loadedConfig.distDir, distDir: getBuildDistDir(loadedConfig),
isTurboSession: bundler === Bundler.Turbopack, isTurboSession: bundler === Bundler.Turbopack,
sync: true, sync: true,
}) })
......
...@@ -653,6 +653,7 @@ function bindingToApi( ...@@ -653,6 +653,7 @@ function bindingToApi(
async function rustifyProjectOptions( async function rustifyProjectOptions(
options: ProjectOptions options: ProjectOptions
): Promise<NapiProjectOptions> { ): Promise<NapiProjectOptions> {
const projectPath = path.join(options.rootPath, options.projectPath)
const additionalRoots = Object.entries( const additionalRoots = Object.entries(
options.nextConfig.experimental.turbopackAdditionalRoots ?? {} options.nextConfig.experimental.turbopackAdditionalRoots ?? {}
).map(([key, root]) => ({ key, ...root })) ).map(([key, root]) => ({ key, ...root }))
...@@ -661,7 +662,10 @@ function bindingToApi( ...@@ -661,7 +662,10 @@ function bindingToApi(
additionalRoots, additionalRoots,
nextConfig: await serializeNextConfig( nextConfig: await serializeNextConfig(
options.nextConfig, options.nextConfig,
path.join(options.rootPath, options.projectPath) projectPath,
path.isAbsolute(options.distDir)
? path.relative(projectPath, options.distDir)
: options.distDir
), ),
env: rustifyEnv(options.env), env: rustifyEnv(options.env),
} }
...@@ -902,7 +906,8 @@ function bindingToApi( ...@@ -902,7 +906,8 @@ function bindingToApi(
async function serializeNextConfig( async function serializeNextConfig(
nextConfig: NextConfigComplete, nextConfig: NextConfigComplete,
projectPath: string projectPath: string,
distDir?: string
): Promise<string> { ): Promise<string> {
// Avoid mutating the existing `nextConfig` object. NOTE: This is only a shallow clone. // Avoid mutating the existing `nextConfig` object. NOTE: This is only a shallow clone.
let nextConfigSerializable: Record<string, any> = { ...nextConfig } let nextConfigSerializable: Record<string, any> = { ...nextConfig }
...@@ -932,7 +937,7 @@ function bindingToApi( ...@@ -932,7 +937,7 @@ function bindingToApi(
// These are relative paths, but might be backslash-separated on Windows // These are relative paths, but might be backslash-separated on Windows
nextConfigSerializable.distDir = normalizePathOnWindows( nextConfigSerializable.distDir = normalizePathOnWindows(
nextConfigSerializable.distDir distDir ?? nextConfigSerializable.distDir
) )
nextConfigSerializable.distDirRoot = normalizePathOnWindows( nextConfigSerializable.distDirRoot = normalizePathOnWindows(
nextConfigSerializable.distDirRoot nextConfigSerializable.distDirRoot
......
...@@ -8,6 +8,7 @@ import createSpinner from './spinner' ...@@ -8,6 +8,7 @@ import createSpinner from './spinner'
import { eventTypeCheckCompleted } from '../telemetry/events' import { eventTypeCheckCompleted } from '../telemetry/events'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { hrtimeDurationToString } from './duration-to-string' import { hrtimeDurationToString } from './duration-to-string'
import { getBuildDistDir } from '../export/utils'
/** /**
* TypeScript setup and type checking run in a worker so the compiler's memory * TypeScript setup and type checking run in a worker so the compiler's memory
...@@ -137,7 +138,7 @@ export async function startTypeChecking({ ...@@ -137,7 +138,7 @@ export async function startTypeChecking({
.traceAsyncFn(() => .traceAsyncFn(() =>
verifyAndRunTypeScript( verifyAndRunTypeScript(
dir, dir,
config.distDir, getBuildDistDir(config),
Boolean(config.experimental.strictRouteTypes), Boolean(config.experimental.strictRouteTypes),
!ignoreTypeScriptErrors, !ignoreTypeScriptErrors,
config.typescript.tsconfigPath, config.typescript.tsconfigPath,
......
...@@ -415,7 +415,7 @@ export async function workerMain(workerData: { ...@@ -415,7 +415,7 @@ export async function workerMain(workerData: {
> { > {
// Clone the telemetry for worker // Clone the telemetry for worker
const telemetry = new Telemetry({ const telemetry = new Telemetry({
distDir: workerData.buildContext.config!.distDir, distDir: workerData.buildContext.distDir!,
}) })
setGlobal('telemetry', telemetry) setGlobal('telemetry', telemetry)
// setup new build context from the serialized data passed from the parent // setup new build context from the serialized data passed from the parent
......
...@@ -81,7 +81,7 @@ import { ...@@ -81,7 +81,7 @@ import {
createNextApiEsmAliases, createNextApiEsmAliases,
createAppRouterApiAliases, createAppRouterApiAliases,
} from './create-compiler-aliases' } from './create-compiler-aliases'
import { hasCustomExportOutput } from '../export/utils' import { getBuildDistDir } from '../export/utils'
import { CssChunkingPlugin } from './webpack/plugins/css-chunking-plugin' import { CssChunkingPlugin } from './webpack/plugins/css-chunking-plugin'
import { import {
getBabelLoader, getBabelLoader,
...@@ -421,10 +421,8 @@ export default async function getBaseWebpackConfig( ...@@ -421,10 +421,8 @@ export default async function getBaseWebpackConfig(
const babelConfigFile = getBabelConfigFile(dir) const babelConfigFile = getBabelConfigFile(dir)
if (!dev && hasCustomExportOutput(config)) { const buildDistDir = dev ? config.distDir : getBuildDistDir(config)
config.distDir = '.next' const distDir = path.join(dir, buildDistDir)
}
const distDir = path.join(dir, config.distDir)
let useSWCLoader = !babelConfigFile || config.experimental.forceSwcTransforms let useSWCLoader = !babelConfigFile || config.experimental.forceSwcTransforms
let SWCBinaryTarget: [Feature, boolean] | undefined = undefined let SWCBinaryTarget: [Feature, boolean] | undefined = undefined
...@@ -548,12 +546,7 @@ export default async function getBaseWebpackConfig( ...@@ -548,12 +546,7 @@ export default async function getBaseWebpackConfig(
hasReactRefresh: dev && isClient, hasReactRefresh: dev && isClient,
transpilePackages: finalTranspilePackages, transpilePackages: finalTranspilePackages,
supportedBrowsers, supportedBrowsers,
swcCacheDir: path.join( swcCacheDir: path.join(distDir, 'cache', 'swc'),
dir,
config?.distDir ?? '.next',
'cache',
'swc'
),
serverReferenceHashSalt: encryptionKey, serverReferenceHashSalt: encryptionKey,
// rspack specific options // rspack specific options
...@@ -588,7 +581,7 @@ export default async function getBaseWebpackConfig( ...@@ -588,7 +581,7 @@ export default async function getBaseWebpackConfig(
jsConfig, jsConfig,
transpilePackages: finalTranspilePackages, transpilePackages: finalTranspilePackages,
supportedBrowsers, supportedBrowsers,
swcCacheDir: path.join(dir, config?.distDir ?? '.next', 'cache', 'swc'), swcCacheDir: path.join(distDir, 'cache', 'swc'),
serverReferenceHashSalt: encryptionKey, serverReferenceHashSalt: encryptionKey,
...extraOptions, ...extraOptions,
} satisfies SWCLoaderOptions, } satisfies SWCLoaderOptions,
...@@ -1984,12 +1977,7 @@ export default async function getBaseWebpackConfig( ...@@ -1984,12 +1977,7 @@ export default async function getBaseWebpackConfig(
loader: 'next-barrel-loader', loader: 'next-barrel-loader',
options: { options: {
names, names,
swcCacheDir: path.join( swcCacheDir: path.join(distDir, 'cache', 'swc'),
dir,
config?.distDir ?? '.next',
'cache',
'swc'
),
}, },
// This is part of the request value to serve as the module key. // This is part of the request value to serve as the module key.
// The barrel loader are no-op re-exported modules keyed by // The barrel loader are no-op re-exported modules keyed by
...@@ -2204,7 +2192,7 @@ export default async function getBaseWebpackConfig( ...@@ -2204,7 +2192,7 @@ export default async function getBaseWebpackConfig(
!isClient && !isClient &&
new NextTypesPlugin({ new NextTypesPlugin({
dir, dir,
distDir: config.distDir, distDir: buildDistDir,
appDir, appDir,
dev, dev,
isEdgeServer, isEdgeServer,
......
...@@ -76,6 +76,7 @@ import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic' ...@@ -76,6 +76,7 @@ import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import type { Params } from '../server/request/params' import type { Params } from '../server/request/params'
import { Bundler } from '../lib/bundler' import { Bundler } from '../lib/bundler'
import { getBuildDistDir } from './utils'
export class ExportError extends Error { export class ExportError extends Error {
code = 'NEXT_EXPORT_ERROR' code = 'NEXT_EXPORT_ERROR'
...@@ -212,7 +213,7 @@ async function exportAppImpl( ...@@ -212,7 +213,7 @@ async function exportAppImpl(
}) })
)) ))
const distDir = join(dir, nextConfig.distDir) const distDir = join(dir, getBuildDistDir(nextConfig))
const telemetry = options.buildExport ? null : new Telemetry({ distDir }) const telemetry = options.buildExport ? null : new Telemetry({ distDir })
if (telemetry) { if (telemetry) {
......
...@@ -5,10 +5,12 @@ export function hasCustomExportOutput(config: NextConfigComplete) { ...@@ -5,10 +5,12 @@ export function hasCustomExportOutput(config: NextConfigComplete) {
// ".next" (or whatever the distDir) followed by "next export" // ".next" (or whatever the distDir) followed by "next export"
// to generate "out" (or whatever the outDir). However, when // to generate "out" (or whatever the outDir). However, when
// "output: export" is configured, "next build" does both steps. // "output: export" is configured, "next build" does both steps.
// So the user-configured distDir is actually the outDir. // In this mode, a custom distDir names the export destination.
// We'll do some custom logic when meeting this condition.
// e.g.
// Will set config.distDir to .next to make sure the manifests
// are still reading from temporary .next directory.
return config.output === 'export' && config.distDir !== '.next' return config.output === 'export' && config.distDir !== '.next'
} }
export function getBuildDistDir(config: NextConfigComplete): string {
// A custom distDir is the export destination in this mode. Build artifacts
// and manifests still live in .next until they are exported.
return hasCustomExportOutput(config) ? '.next' : config.distDir
}
import fs from 'fs' import fs from 'fs'
import path from 'path'
import type { NextAdapter } from 'next' import type { NextAdapter } from 'next'
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { version as nextVersion } from 'next/package.json' import { version as nextVersion } from 'next/package.json'
...@@ -58,6 +59,9 @@ describe('adapter-config export', () => { ...@@ -58,6 +59,9 @@ describe('adapter-config export', () => {
expect(ctx.nextVersion).toBe(nextVersion) expect(ctx.nextVersion).toBe(nextVersion)
expect(ctx.projectDir).toBe(next.testDir) expect(ctx.projectDir).toBe(next.testDir)
expect(config?.basePath).toBe('/docs') expect(config?.basePath).toBe('/docs')
expect(config?.distDir).toBe('custom-out')
expect(ctx.distDir).toBe(path.join(next.testDir, '.next'))
expect(fs.existsSync(path.join(next.testDir, 'custom-out'))).toBe(true)
const combinedRouteOutputs = [ const combinedRouteOutputs = [
...outputs.appPages, ...outputs.appPages,
......
...@@ -33,4 +33,8 @@ const nextConfig = { ...@@ -33,4 +33,8 @@ const nextConfig = {
output: process.env.TEST_EXPORT ? 'export' : undefined, output: process.env.TEST_EXPORT ? 'export' : undefined,
} }
if (process.env.TEST_EXPORT) {
nextConfig.distDir = 'custom-out'
}
export default nextConfig export default nextConfig
...@@ -235,6 +235,7 @@ async function readNormalizedNFT(next, name) { ...@@ -235,6 +235,7 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/experimental/testmode/server-edge.js", "/node_modules/next/dist/experimental/testmode/server-edge.js",
"/node_modules/next/dist/experimental/testmode/server.js", "/node_modules/next/dist/experimental/testmode/server.js",
"/node_modules/next/dist/export/helpers/create-incremental-cache.js", "/node_modules/next/dist/export/helpers/create-incremental-cache.js",
"/node_modules/next/dist/export/utils.js",
"/node_modules/next/dist/lib/batcher.js", "/node_modules/next/dist/lib/batcher.js",
"/node_modules/next/dist/lib/build-custom-route.js", "/node_modules/next/dist/lib/build-custom-route.js",
"/node_modules/next/dist/lib/bundler.js", "/node_modules/next/dist/lib/bundler.js",
......
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