Unverified Commit dcf4607f authored by Sam Poder's avatar Sam Poder Committed by GitHub

Improve root detection to handle worktrees, more workspaces & stray lockfiles (#96159)

Watching too many files can hurt performance, and impacts how much work
Turbopack is doing. See for example the conversation around
https://github.com/vercel/next.js/pull/94597.

This PR handles the following four cases:

- Git worktrees: we can include a guard to prevent exiting a worktree
(by looking at the `.git` file).
- Git repository boundaries: we can include a guard to prevent us from
exiting a checked-out repository and watching the repository’s parent
directory.
    - We will log a warning if this happens.
- Empty / bogus `package.lock` files & `package-lock.json` files (and
equivalents using other package managers): if we spot a
`package-lock.json` file but it has no packages or a `package-lock.json`
w/o an associated `package.json` file. We can ignore them. This handles
the case of accidentally running `npm install` in any directory.
- Home directory detection: we can detect if we’re going to start
watching your home dir and rollback up if this is the case.
    - We will log a warning if this happens.
- If your Next config is in your home dir, we will still run your app
there as Next config is the furthest we can rollback
- NPM, Yarn, Lerna & Bun workspaces: based on the `workspaces` field in
the `package.json` or `lerna.json`

I would recommend using split-view to review. I also cleaned up the
`warnDuplicatedLockFiles()` function because it was a bit unnecessary to
repeat the full message.

Closes https://github.com/vercel/next.js/issues/92978
parent 625dccbc
This diff is collapsed.
This diff is collapsed.
......@@ -33,6 +33,7 @@ import { flushTelemetry } from '../telemetry/flush-telemetry'
import {
findRootDirAndLockFiles,
warnDuplicatedLockFiles,
warnRootBoundary,
} from '../lib/find-root'
import { setHttpClientAndAgentOptions } from './setup-http-agent-env'
import { pathHasPrefix } from '../shared/lib/router/utils/path-has-prefix'
......@@ -1158,11 +1159,10 @@ function assignDefaultsAndValidate(
const turbopackRoot = result?.turbopack?.root
let repoRoot = process.env.NEXT_PRIVATE_OUTPUT_TRACE_ROOT
let lockFiles: string[] | undefined = undefined
let rootDirResult: ReturnType<typeof findRootDirAndLockFiles> | undefined
if (!repoRoot) {
const rootDirResult = findRootDirAndLockFiles(dir)
rootDirResult = findRootDirAndLockFiles(dir)
repoRoot = rootDirResult.rootDir
lockFiles = rootDirResult.lockFiles
}
;(result as NextConfigComplete).repoRoot = repoRoot
......@@ -1176,8 +1176,11 @@ function assignDefaultsAndValidate(
let rootDir = tracingRoot || turbopackRoot
if (!rootDir) {
rootDir = repoRoot
if (lockFiles && !silent) {
warnDuplicatedLockFiles(lockFiles)
if (rootDirResult && !silent) {
if (rootDirResult.boundary) {
warnRootBoundary(rootDirResult.boundary)
}
warnDuplicatedLockFiles(rootDirResult.lockFiles)
}
}
if (!rootDir) {
......
......@@ -9,10 +9,15 @@ describe('multiple-lockfiles - has-output-file-tracing-root', () => {
'next.config.js': `module.exports = { outputFileTracingRoot: __dirname }`,
// Write a package-lock.json file to the parent directory to simulate
// multiple lockfiles.
'../package.json': JSON.stringify({
name: 'parent-workspace',
version: '1.0.0',
}),
'../package-lock.json': JSON.stringify({
name: 'parent-workspace',
version: '1.0.0',
lockfileVersion: 3,
packages: { '': { name: 'parent-workspace', version: '1.0.0' } },
}),
},
// So that ../package-lock.json doesn't leave the isolated testDir
......
......@@ -9,10 +9,15 @@ describe('multiple-lockfiles - has-turbo-root', () => {
'next.config.js': `module.exports = { turbopack: { root: __dirname } }`,
// Write a package-lock.json file to the parent directory to simulate
// multiple lockfiles.
'../package.json': JSON.stringify({
name: 'parent-workspace',
version: '1.0.0',
}),
'../package-lock.json': JSON.stringify({
name: 'parent-workspace',
version: '1.0.0',
lockfileVersion: 3,
packages: { '': { name: 'parent-workspace', version: '1.0.0' } },
}),
},
// So that ../package-lock.json doesn't leave the isolated testDir
......
......@@ -8,10 +8,15 @@ describe('multiple-lockfiles', () => {
app: new FileRef(join(__dirname, 'app')),
// Write a package-lock.json file to the parent directory to simulate
// multiple lockfiles.
'../package.json': JSON.stringify({
name: 'parent-workspace',
version: '1.0.0',
}),
'../package-lock.json': JSON.stringify({
name: 'parent-workspace',
version: '1.0.0',
lockfileVersion: 3,
packages: { '': { name: 'parent-workspace', version: '1.0.0' } },
}),
},
// So that ../package-lock.json doesn't leave the isolated testDir
......
......@@ -19,10 +19,15 @@ describe('adapter-root', () => {
overrideFiles: setEnvVar
? undefined
: {
'../package.json': JSON.stringify({
name: 'parent-workspace',
version: '1.0.0',
}),
'../package-lock.json': JSON.stringify({
name: 'parent-workspace',
version: '1.0.0',
lockfileVersion: 3,
packages: { '': { name: 'parent-workspace', version: '1.0.0' } },
}),
},
})
......
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