Unverified Commit 826a237b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update default tracing root handling (#45864)

This updates the default tracing root used when one isn't manually
provided to be the directory of the closest lockfile. Previously it
defaulted to the directory of the project being built which is incorrect
in monorepos as any top-level dependencies wouldn't be traced correctly.
Our tests were previously passing without this handling due to the env
variable being manually set which this removes.

x-ref: https://github.com/vercel/next.js/discussions/39432
x-ref: https://github.com/vercel/vercel/pull/9402
x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1675821643786319)

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
parent d83a3c8e
import { existsSync } from 'fs' import { existsSync } from 'fs'
import { basename, extname, join, relative, isAbsolute, resolve } from 'path' import {
basename,
extname,
join,
relative,
isAbsolute,
resolve,
dirname,
} from 'path'
import { pathToFileURL } from 'url' import { pathToFileURL } from 'url'
import { Agent as HttpAgent } from 'http' import { Agent as HttpAgent } from 'http'
import { Agent as HttpsAgent } from 'https' import { Agent as HttpsAgent } from 'https'
...@@ -667,6 +675,28 @@ function assignDefaults( ...@@ -667,6 +675,28 @@ function assignDefaults(
} }
} }
// use the closest lockfile as tracing root
if (!result.experimental?.outputFileTracingRoot) {
const lockFiles: string[] = [
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml',
]
const foundLockfile = findUp.sync(lockFiles, { cwd: dir })
if (foundLockfile) {
if (!result.experimental) {
result.experimental = {}
}
if (!defaultConfig.experimental) {
defaultConfig.experimental = {}
}
result.experimental.outputFileTracingRoot = dirname(foundLockfile)
defaultConfig.experimental.outputFileTracingRoot =
result.experimental.outputFileTracingRoot
}
}
if (result.output === 'standalone' && !result.outputFileTracing) { if (result.output === 'standalone' && !result.outputFileTracing) {
if (!silent) { if (!silent) {
Log.warn( Log.warn(
......
...@@ -186,7 +186,6 @@ export function runNextCommand(argv, options = {}) { ...@@ -186,7 +186,6 @@ export function runNextCommand(argv, options = {}) {
...process.env, ...process.env,
NODE_ENV: '', NODE_ENV: '',
__NEXT_TEST_MODE: 'true', __NEXT_TEST_MODE: 'true',
NEXT_PRIVATE_OUTPUT_TRACE_ROOT: path.join(__dirname, '../../'),
...options.env, ...options.env,
} }
......
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