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({
distDir,
pageKeys,
bundler,
tracingRoot,
repoRoot,
outputFileTracingRoot,
adapterPath,
appPageKeys,
staticPages,
......@@ -485,13 +486,18 @@ export async function handleBuildComplete({
hasInstrumentationHook,
functionsConfigManifest,
}: {
/** The folder containing the next.config.js file */
dir: string
appType: 'app' | 'pages' | 'hybrid'
/** The .next folder */
distDir: string
buildId: string
configOutDir: 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
hasStatic404: boolean
hasStatic500: boolean
......@@ -580,7 +586,8 @@ export async function handleBuildComplete({
distDir,
requiredServerFiles,
dir,
tracingRoot,
repoRoot,
outputFileTracingRoot,
bundler,
hasInstrumentationHook,
config,
......@@ -595,7 +602,7 @@ export async function handleBuildComplete({
const { entryHash } = await loadNFT(
assets,
assetsHashes,
tracingRoot,
repoRoot,
`${entryFilePath}.nft.json`
)
Object.assign(
......@@ -611,7 +618,7 @@ export async function handleBuildComplete({
type === 'app' ? appPagesSharedNodeAssetsHashes : {}
)
if (entryHash) {
assetsHashes[path.relative(tracingRoot, entryFilePath)] = entryHash
assetsHashes[path.relative(repoRoot, entryFilePath)] = entryHash
}
return { assets, assetsHashes, entryHash }
}
......@@ -684,7 +691,7 @@ export async function handleBuildComplete({
const originalPath = path.join(distDir, file)
const fileOutputPath = path.relative(
config.distDir,
path.join(path.relative(tracingRoot, distDir), file)
path.join(path.relative(repoRoot, distDir), file)
)
output.assets[fileOutputPath] = originalPath
}
......@@ -1026,7 +1033,7 @@ export async function handleBuildComplete({
await pushAsset(
existingOutput.assets,
existingOutput.assetsHashes,
path.relative(tracingRoot, pageFile),
path.relative(repoRoot, pageFile),
pageFile,
bundler
)
......@@ -2112,7 +2119,7 @@ export async function handleBuildComplete({
buildId,
nextVersion,
projectDir: dir,
repoRoot: tracingRoot,
repoRoot: repoRoot,
})
} catch (err) {
Log.error(`Failed to run onBuildComplete from ${adapterMod.name}`)
......@@ -2125,7 +2132,8 @@ async function getSharedNodeAssets({
dir,
bundler,
distDir,
tracingRoot,
repoRoot,
outputFileTracingRoot,
requiredServerFiles,
hasInstrumentationHook,
config,
......@@ -2133,7 +2141,8 @@ async function getSharedNodeAssets({
dir: string
bundler: Bundler
distDir: string
tracingRoot: string
repoRoot: string
outputFileTracingRoot: string
requiredServerFiles: string[]
hasInstrumentationHook: boolean
config: NextConfigComplete
......@@ -2167,14 +2176,14 @@ async function getSharedNodeAssets({
}
for (const dependencyPath of currentDependencies) {
const rootRelativeFilePath = path.relative(tracingRoot, dependencyPath)
const rootRelativeFilePath = path.relative(repoRoot, dependencyPath)
if (type === 'pages') {
await pushAsset(
pagesSharedNodeAssets,
pagesSharedNodeAssetsHashes,
rootRelativeFilePath,
path.join(tracingRoot, rootRelativeFilePath),
path.join(repoRoot, rootRelativeFilePath),
bundler
)
} else {
......@@ -2182,7 +2191,7 @@ async function getSharedNodeAssets({
appPagesSharedNodeAssets,
appPagesSharedNodeAssetsHashes,
rootRelativeFilePath,
path.join(tracingRoot, rootRelativeFilePath),
path.join(repoRoot, rootRelativeFilePath),
bundler
)
}
......@@ -2198,7 +2207,7 @@ async function getSharedNodeAssets({
await pushAsset(
sharedNodeAssets,
sharedNodeAssetsHashes,
path.relative(tracingRoot, setupNodeStubPath),
path.relative(repoRoot, setupNodeStubPath),
require.resolve('next/dist/build/adapter/setup-node-env.external'),
bundler
)
......@@ -2226,7 +2235,10 @@ async function getSharedNodeAssets({
'**/next/dist/server/web/sandbox/**/*',
'**/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
const necessaryNodeDependencies = [
......@@ -2266,19 +2278,26 @@ async function getSharedNodeAssets({
const { fileList, esmFileList } = await nodeFileTrace(
necessaryNodeDependencies,
{
base: tracingRoot,
base: outputFileTracingRoot,
ignore: sharedIgnoreFn,
moduleSyncCatchall: true,
}
)
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(
sharedNodeAssets,
sharedNodeAssetsHashes,
rootRelativeFilePath,
path.join(tracingRoot, rootRelativeFilePath),
path.relative(repoRoot, absoluteFilePath),
absoluteFilePath,
bundler
)
}
......@@ -2288,12 +2307,12 @@ async function getSharedNodeAssets({
const { entryHash: instrumentationEntryHash } = await loadNFT(
sharedNodeAssets,
sharedNodeAssetsHashes,
tracingRoot,
repoRoot,
path.join(distDir, 'server', 'instrumentation.js.nft.json')
)
const fileOutputPath = path.relative(
tracingRoot,
repoRoot,
path.join(distDir, 'server', 'instrumentation.js')
)
await pushAsset(
......@@ -2310,7 +2329,7 @@ async function getSharedNodeAssets({
for (const file of requiredServerFiles) {
// add to shared node assets
const filePath = path.join(dir, file)
const fileOutputPath = path.relative(tracingRoot, filePath)
const fileOutputPath = path.relative(repoRoot, filePath)
await pushAsset(
sharedNodeAssets,
sharedNodeAssetsHashes,
......@@ -2350,7 +2369,7 @@ async function pushAsset(
async function loadNFT(
assets: Record<string, string>,
assetsHashes: Record<string, string>,
tracingRoot: string,
repoRoot: string,
traceFilePath: string
): Promise<{ entryHash?: string }> {
const { files, fileHashes, entryHash } = (await JSON.parse(
......@@ -2366,7 +2385,7 @@ async function loadNFT(
const relativeFile = files[i]
const contentHash = fileHashes?.[i]
const tracedFilePath = path.join(traceFileDir, relativeFile)
const fileOutputPath = path.relative(tracingRoot, tracedFilePath)
const fileOutputPath = path.relative(repoRoot, tracedFilePath)
assets[fileOutputPath] = tracedFilePath
if (contentHash) {
assetsHashes[fileOutputPath] = contentHash
......
......@@ -4195,7 +4195,8 @@ export default async function build(
staticPages,
serverPropsPages,
nextVersion: process.env.__NEXT_VERSION as string,
tracingRoot: outputFileTracingRoot,
repoRoot: config.repoRoot,
outputFileTracingRoot,
hasNodeMiddleware,
hasInstrumentationHook,
adapterPath,
......
......@@ -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
// traverse into the output directory.
distDirRoot: string
// The repository root, regardless of overwritten outputFileTracingRoot or turbopack.root.
repoRoot: string
}
export type I18NDomains = readonly DomainLocale[]
......@@ -1960,7 +1962,7 @@ export const defaultConfig = Object.freeze({
staticPageGenerationTimeout: 60,
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
modularizeImports: undefined,
outputFileTracingRoot: process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT || '',
outputFileTracingRoot: '',
allowedDevOrigins: undefined,
enablePrerenderSourceMaps: true,
cacheComponents: false,
......
......@@ -1173,6 +1173,15 @@ function assignDefaultsAndValidate(
const tracingRoot = result?.outputFileTracingRoot
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 (tracingRoot && turbopackRoot && tracingRoot !== turbopackRoot) {
Log.warn(
......@@ -1180,22 +1189,18 @@ function assignDefaultsAndValidate(
`Using \`outputFileTracingRoot\` value: ${tracingRoot}.`
)
}
let rootDir = tracingRoot || turbopackRoot
if (!rootDir) {
const { rootDir: foundRootDir, lockFiles } = findRootDirAndLockFiles(dir)
rootDir = foundRootDir
if (!silent) {
rootDir = repoRoot
if (lockFiles && !silent) {
warnDuplicatedLockFiles(lockFiles)
}
}
if (!rootDir) {
throw new Error(
'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
result.outputFileTracingRoot = rootDir
dset(result, ['turbopack', 'root'], rootDir)
......@@ -2120,7 +2125,7 @@ export default async function loadConfig(
{ ...clonedDefaultConfig, configFileName },
silent,
phase
) as NextConfigComplete
)
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