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