Unverified Commit beee190d authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Fix repoRoot for adapters (#94478)

`repoRoot` shouldn't be `outputFileTracingRoot`. The user can configure
that to arbitrary values, which shouldn't change the adapter output.

The adapter now uses `NextConfigComplete.repoRoot` instead of
`outputFileTracingRoot`.

Closes NEXT-4942
parent ad97ca57
...@@ -469,7 +469,8 @@ export async function handleBuildComplete({ ...@@ -469,7 +469,8 @@ export async function handleBuildComplete({
distDir, distDir,
pageKeys, pageKeys,
bundler, bundler,
tracingRoot, repoRoot,
outputFileTracingRoot,
adapterPath, adapterPath,
appPageKeys, appPageKeys,
staticPages, staticPages,
...@@ -485,13 +486,18 @@ export async function handleBuildComplete({ ...@@ -485,13 +486,18 @@ export async function handleBuildComplete({
hasInstrumentationHook, hasInstrumentationHook,
functionsConfigManifest, functionsConfigManifest,
}: { }: {
/** The folder containing the next.config.js file */
dir: string dir: string
appType: 'app' | 'pages' | 'hybrid' appType: 'app' | 'pages' | 'hybrid'
/** The .next folder */
distDir: string distDir: string
buildId: string buildId: string
configOutDir: string configOutDir: string
adapterPath: string adapterPath: string
tracingRoot: string /** The repository root. The base for relative output paths. */
repoRoot: string
/** A normalized version of config.outputFileTracingRoot */
outputFileTracingRoot: string
nextVersion: string nextVersion: string
hasStatic404: boolean hasStatic404: boolean
hasStatic500: boolean hasStatic500: boolean
...@@ -580,7 +586,8 @@ export async function handleBuildComplete({ ...@@ -580,7 +586,8 @@ export async function handleBuildComplete({
distDir, distDir,
requiredServerFiles, requiredServerFiles,
dir, dir,
tracingRoot, repoRoot,
outputFileTracingRoot,
bundler, bundler,
hasInstrumentationHook, hasInstrumentationHook,
config, config,
...@@ -595,7 +602,7 @@ export async function handleBuildComplete({ ...@@ -595,7 +602,7 @@ export async function handleBuildComplete({
const { entryHash } = await loadNFT( const { entryHash } = await loadNFT(
assets, assets,
assetsHashes, assetsHashes,
tracingRoot, repoRoot,
`${entryFilePath}.nft.json` `${entryFilePath}.nft.json`
) )
Object.assign( Object.assign(
...@@ -611,7 +618,7 @@ export async function handleBuildComplete({ ...@@ -611,7 +618,7 @@ export async function handleBuildComplete({
type === 'app' ? appPagesSharedNodeAssetsHashes : {} type === 'app' ? appPagesSharedNodeAssetsHashes : {}
) )
if (entryHash) { if (entryHash) {
assetsHashes[path.relative(tracingRoot, entryFilePath)] = entryHash assetsHashes[path.relative(repoRoot, entryFilePath)] = entryHash
} }
return { assets, assetsHashes, entryHash } return { assets, assetsHashes, entryHash }
} }
...@@ -684,7 +691,7 @@ export async function handleBuildComplete({ ...@@ -684,7 +691,7 @@ export async function handleBuildComplete({
const originalPath = path.join(distDir, file) const originalPath = path.join(distDir, file)
const fileOutputPath = path.relative( const fileOutputPath = path.relative(
config.distDir, config.distDir,
path.join(path.relative(tracingRoot, distDir), file) path.join(path.relative(repoRoot, distDir), file)
) )
output.assets[fileOutputPath] = originalPath output.assets[fileOutputPath] = originalPath
} }
...@@ -1026,7 +1033,7 @@ export async function handleBuildComplete({ ...@@ -1026,7 +1033,7 @@ export async function handleBuildComplete({
await pushAsset( await pushAsset(
existingOutput.assets, existingOutput.assets,
existingOutput.assetsHashes, existingOutput.assetsHashes,
path.relative(tracingRoot, pageFile), path.relative(repoRoot, pageFile),
pageFile, pageFile,
bundler bundler
) )
...@@ -2112,7 +2119,7 @@ export async function handleBuildComplete({ ...@@ -2112,7 +2119,7 @@ export async function handleBuildComplete({
buildId, buildId,
nextVersion, nextVersion,
projectDir: dir, projectDir: dir,
repoRoot: tracingRoot, repoRoot: repoRoot,
}) })
} catch (err) { } catch (err) {
Log.error(`Failed to run onBuildComplete from ${adapterMod.name}`) Log.error(`Failed to run onBuildComplete from ${adapterMod.name}`)
...@@ -2125,7 +2132,8 @@ async function getSharedNodeAssets({ ...@@ -2125,7 +2132,8 @@ async function getSharedNodeAssets({
dir, dir,
bundler, bundler,
distDir, distDir,
tracingRoot, repoRoot,
outputFileTracingRoot,
requiredServerFiles, requiredServerFiles,
hasInstrumentationHook, hasInstrumentationHook,
config, config,
...@@ -2133,7 +2141,8 @@ async function getSharedNodeAssets({ ...@@ -2133,7 +2141,8 @@ async function getSharedNodeAssets({
dir: string dir: string
bundler: Bundler bundler: Bundler
distDir: string distDir: string
tracingRoot: string repoRoot: string
outputFileTracingRoot: string
requiredServerFiles: string[] requiredServerFiles: string[]
hasInstrumentationHook: boolean hasInstrumentationHook: boolean
config: NextConfigComplete config: NextConfigComplete
...@@ -2167,14 +2176,14 @@ async function getSharedNodeAssets({ ...@@ -2167,14 +2176,14 @@ async function getSharedNodeAssets({
} }
for (const dependencyPath of currentDependencies) { for (const dependencyPath of currentDependencies) {
const rootRelativeFilePath = path.relative(tracingRoot, dependencyPath) const rootRelativeFilePath = path.relative(repoRoot, dependencyPath)
if (type === 'pages') { if (type === 'pages') {
await pushAsset( await pushAsset(
pagesSharedNodeAssets, pagesSharedNodeAssets,
pagesSharedNodeAssetsHashes, pagesSharedNodeAssetsHashes,
rootRelativeFilePath, rootRelativeFilePath,
path.join(tracingRoot, rootRelativeFilePath), path.join(repoRoot, rootRelativeFilePath),
bundler bundler
) )
} else { } else {
...@@ -2182,7 +2191,7 @@ async function getSharedNodeAssets({ ...@@ -2182,7 +2191,7 @@ async function getSharedNodeAssets({
appPagesSharedNodeAssets, appPagesSharedNodeAssets,
appPagesSharedNodeAssetsHashes, appPagesSharedNodeAssetsHashes,
rootRelativeFilePath, rootRelativeFilePath,
path.join(tracingRoot, rootRelativeFilePath), path.join(repoRoot, rootRelativeFilePath),
bundler bundler
) )
} }
...@@ -2198,7 +2207,7 @@ async function getSharedNodeAssets({ ...@@ -2198,7 +2207,7 @@ async function getSharedNodeAssets({
await pushAsset( await pushAsset(
sharedNodeAssets, sharedNodeAssets,
sharedNodeAssetsHashes, sharedNodeAssetsHashes,
path.relative(tracingRoot, setupNodeStubPath), path.relative(repoRoot, setupNodeStubPath),
require.resolve('next/dist/build/adapter/setup-node-env.external'), require.resolve('next/dist/build/adapter/setup-node-env.external'),
bundler bundler
) )
...@@ -2226,7 +2235,10 @@ async function getSharedNodeAssets({ ...@@ -2226,7 +2235,10 @@ async function getSharedNodeAssets({
'**/next/dist/server/web/sandbox/**/*', '**/next/dist/server/web/sandbox/**/*',
'**/next/dist/server/post-process.js', '**/next/dist/server/post-process.js',
] ]
const sharedIgnoreFn = makeIgnoreFn(tracingRoot, sharedTraceIgnores) const sharedIgnoreFn = makeIgnoreFn(
outputFileTracingRoot,
sharedTraceIgnores
)
// These are modules that are necessary for bootstrapping node env // These are modules that are necessary for bootstrapping node env
const necessaryNodeDependencies = [ const necessaryNodeDependencies = [
...@@ -2266,19 +2278,26 @@ async function getSharedNodeAssets({ ...@@ -2266,19 +2278,26 @@ async function getSharedNodeAssets({
const { fileList, esmFileList } = await nodeFileTrace( const { fileList, esmFileList } = await nodeFileTrace(
necessaryNodeDependencies, necessaryNodeDependencies,
{ {
base: tracingRoot, base: outputFileTracingRoot,
ignore: sharedIgnoreFn, ignore: sharedIgnoreFn,
moduleSyncCatchall: true, moduleSyncCatchall: true,
} }
) )
esmFileList.forEach((item) => fileList.add(item)) esmFileList.forEach((item) => fileList.add(item))
for (const rootRelativeFilePath of fileList) { for (const tracingRootRelativeFilePath of fileList) {
// nodeFileTrace returns paths relative to `base` (outputFileTracingRoot),
// so resolve to an absolute path and re-relativize against repoRoot, which
// is the root all adapter output keys/source paths are based on.
const absoluteFilePath = path.join(
outputFileTracingRoot,
tracingRootRelativeFilePath
)
await pushAsset( await pushAsset(
sharedNodeAssets, sharedNodeAssets,
sharedNodeAssetsHashes, sharedNodeAssetsHashes,
rootRelativeFilePath, path.relative(repoRoot, absoluteFilePath),
path.join(tracingRoot, rootRelativeFilePath), absoluteFilePath,
bundler bundler
) )
} }
...@@ -2288,12 +2307,12 @@ async function getSharedNodeAssets({ ...@@ -2288,12 +2307,12 @@ async function getSharedNodeAssets({
const { entryHash: instrumentationEntryHash } = await loadNFT( const { entryHash: instrumentationEntryHash } = await loadNFT(
sharedNodeAssets, sharedNodeAssets,
sharedNodeAssetsHashes, sharedNodeAssetsHashes,
tracingRoot, repoRoot,
path.join(distDir, 'server', 'instrumentation.js.nft.json') path.join(distDir, 'server', 'instrumentation.js.nft.json')
) )
const fileOutputPath = path.relative( const fileOutputPath = path.relative(
tracingRoot, repoRoot,
path.join(distDir, 'server', 'instrumentation.js') path.join(distDir, 'server', 'instrumentation.js')
) )
await pushAsset( await pushAsset(
...@@ -2310,7 +2329,7 @@ async function getSharedNodeAssets({ ...@@ -2310,7 +2329,7 @@ async function getSharedNodeAssets({
for (const file of requiredServerFiles) { for (const file of requiredServerFiles) {
// add to shared node assets // add to shared node assets
const filePath = path.join(dir, file) const filePath = path.join(dir, file)
const fileOutputPath = path.relative(tracingRoot, filePath) const fileOutputPath = path.relative(repoRoot, filePath)
await pushAsset( await pushAsset(
sharedNodeAssets, sharedNodeAssets,
sharedNodeAssetsHashes, sharedNodeAssetsHashes,
...@@ -2350,7 +2369,7 @@ async function pushAsset( ...@@ -2350,7 +2369,7 @@ async function pushAsset(
async function loadNFT( async function loadNFT(
assets: Record<string, string>, assets: Record<string, string>,
assetsHashes: Record<string, string>, assetsHashes: Record<string, string>,
tracingRoot: string, repoRoot: string,
traceFilePath: string traceFilePath: string
): Promise<{ entryHash?: string }> { ): Promise<{ entryHash?: string }> {
const { files, fileHashes, entryHash } = (await JSON.parse( const { files, fileHashes, entryHash } = (await JSON.parse(
...@@ -2366,7 +2385,7 @@ async function loadNFT( ...@@ -2366,7 +2385,7 @@ async function loadNFT(
const relativeFile = files[i] const relativeFile = files[i]
const contentHash = fileHashes?.[i] const contentHash = fileHashes?.[i]
const tracedFilePath = path.join(traceFileDir, relativeFile) const tracedFilePath = path.join(traceFileDir, relativeFile)
const fileOutputPath = path.relative(tracingRoot, tracedFilePath) const fileOutputPath = path.relative(repoRoot, tracedFilePath)
assets[fileOutputPath] = tracedFilePath assets[fileOutputPath] = tracedFilePath
if (contentHash) { if (contentHash) {
assetsHashes[fileOutputPath] = contentHash assetsHashes[fileOutputPath] = contentHash
......
...@@ -4195,7 +4195,8 @@ export default async function build( ...@@ -4195,7 +4195,8 @@ export default async function build(
staticPages, staticPages,
serverPropsPages, serverPropsPages,
nextVersion: process.env.__NEXT_VERSION as string, nextVersion: process.env.__NEXT_VERSION as string,
tracingRoot: outputFileTracingRoot, repoRoot: config.repoRoot,
outputFileTracingRoot,
hasNodeMiddleware, hasNodeMiddleware,
hasInstrumentationHook, hasInstrumentationHook,
adapterPath, adapterPath,
......
...@@ -48,6 +48,8 @@ export type NextConfigComplete = Required<Omit<NextConfig, 'configFile'>> & { ...@@ -48,6 +48,8 @@ export type NextConfigComplete = Required<Omit<NextConfig, 'configFile'>> & {
// since development builds use `{distDir}/dev`. This is used to ensure that the bundler doesn't // since development builds use `{distDir}/dev`. This is used to ensure that the bundler doesn't
// traverse into the output directory. // traverse into the output directory.
distDirRoot: string distDirRoot: string
// The repository root, regardless of overwritten outputFileTracingRoot or turbopack.root.
repoRoot: string
} }
export type I18NDomains = readonly DomainLocale[] export type I18NDomains = readonly DomainLocale[]
...@@ -1960,7 +1962,7 @@ export const defaultConfig = Object.freeze({ ...@@ -1960,7 +1962,7 @@ export const defaultConfig = Object.freeze({
staticPageGenerationTimeout: 60, staticPageGenerationTimeout: 60,
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined, output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
modularizeImports: undefined, modularizeImports: undefined,
outputFileTracingRoot: process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT || '', outputFileTracingRoot: '',
allowedDevOrigins: undefined, allowedDevOrigins: undefined,
enablePrerenderSourceMaps: true, enablePrerenderSourceMaps: true,
cacheComponents: false, cacheComponents: false,
......
...@@ -1173,6 +1173,15 @@ function assignDefaultsAndValidate( ...@@ -1173,6 +1173,15 @@ function assignDefaultsAndValidate(
const tracingRoot = result?.outputFileTracingRoot const tracingRoot = result?.outputFileTracingRoot
const turbopackRoot = result?.turbopack?.root const turbopackRoot = result?.turbopack?.root
let repoRoot = process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT
let lockFiles: string[] | undefined = undefined
if (!repoRoot) {
const rootDirResult = findRootDirAndLockFiles(dir)
repoRoot = rootDirResult.rootDir
lockFiles = rootDirResult.lockFiles
}
;(result as NextConfigComplete).repoRoot = repoRoot
// If both provided, validate they match. If not, use outputFileTracingRoot. // If both provided, validate they match. If not, use outputFileTracingRoot.
if (tracingRoot && turbopackRoot && tracingRoot !== turbopackRoot) { if (tracingRoot && turbopackRoot && tracingRoot !== turbopackRoot) {
Log.warn( Log.warn(
...@@ -1180,22 +1189,18 @@ function assignDefaultsAndValidate( ...@@ -1180,22 +1189,18 @@ function assignDefaultsAndValidate(
`Using \`outputFileTracingRoot\` value: ${tracingRoot}.` `Using \`outputFileTracingRoot\` value: ${tracingRoot}.`
) )
} }
let rootDir = tracingRoot || turbopackRoot let rootDir = tracingRoot || turbopackRoot
if (!rootDir) { if (!rootDir) {
const { rootDir: foundRootDir, lockFiles } = findRootDirAndLockFiles(dir) rootDir = repoRoot
rootDir = foundRootDir if (lockFiles && !silent) {
if (!silent) {
warnDuplicatedLockFiles(lockFiles) warnDuplicatedLockFiles(lockFiles)
} }
} }
if (!rootDir) { if (!rootDir) {
throw new Error( throw new Error(
'Failed to find the root directory of the project. This is a bug in Next.js.' 'Failed to find the root directory of the project. This is a bug in Next.js.'
) )
} }
// Ensure both properties are set to the same value // Ensure both properties are set to the same value
result.outputFileTracingRoot = rootDir result.outputFileTracingRoot = rootDir
dset(result, ['turbopack', 'root'], rootDir) dset(result, ['turbopack', 'root'], rootDir)
...@@ -2120,7 +2125,7 @@ export default async function loadConfig( ...@@ -2120,7 +2125,7 @@ export default async function loadConfig(
{ ...clonedDefaultConfig, configFileName }, { ...clonedDefaultConfig, configFileName },
silent, silent,
phase phase
) as NextConfigComplete )
setHttpClientAndAgentOptions(completeConfig) setHttpClientAndAgentOptions(completeConfig)
......
import fs from 'fs'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
import type { NextAdapter } from 'next'
describe('adapter-root', () => {
describe.each([
{ name: 'via lockfile', env: {} },
// The Vercel CLI sets this to the repo root
{ name: 'via NEXT_PRIVATE_OUTPUT_TRACE_ROOT', setEnvVar: true },
])('$name', ({ setEnvVar }) => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
subDir: 'sub',
skipStart: true,
overrideFiles: setEnvVar
? undefined
: {
'../package-lock.json': JSON.stringify({
name: 'parent-workspace',
version: '1.0.0',
lockfileVersion: 3,
}),
},
})
it('should correctly determine repoRoot', async () => {
const expectedRepoRoot = path.dirname(next.testDir)
if (setEnvVar) {
next.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT = expectedRepoRoot
}
await next.build()
expect(next.cliOutput).not.toContain(
'We detected multiple lockfiles and selected the directory'
)
const {
outputs,
repoRoot,
projectDir,
}: Parameters<NextAdapter['onBuildComplete']>[0] = await next.readJSON(
'build-complete.json'
)
expect(projectDir).toBe(next.testDir)
expect(repoRoot).toBe(expectedRepoRoot)
const combinedRouteOutputs = [
...outputs.appPages,
...outputs.appRoutes,
...outputs.pages,
...outputs.pagesApi,
]
for (const output of combinedRouteOutputs) {
for (const [asset, source] of Object.entries(output.assets)) {
expect(asset).toStartWith('sub/')
expect(fs.existsSync(source)).toBeTrue()
}
}
})
})
})
export default function Layout({ children }) {
return (
<html lang="en">
<head />
<body>{children}</body>
</html>
)
}
export default function Page() {
return (
<>
<p>Hello</p>
</>
)
}
import fs from 'fs'
// @ts-check
/** @type {import('next').NextAdapter } */
const myAdapter = {
name: 'my-custom-adapter',
onBuildComplete: async (ctx) => {
await fs.promises.writeFile(
'build-complete.json',
JSON.stringify(ctx, null, 2)
)
},
}
export default myAdapter
/** @type {import('next').NextConfig} */
const nextConfig = {
adapterPath: require.resolve('./my-adapter.mjs'),
outputFileTracingRoot: __dirname,
turbopack: { root: __dirname },
}
module.exports = nextConfig
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