Files
next.js/test/e2e/cpu-profiling/cpu-profiling-build.test.ts
Luke SandbergandGitHub a7b04690e6 gitignore .next-profiles (#95256)
### What?

When profiling is enabled (`--experimental-cpu-prof` /
`--internal-trace`, or the `NEXT_CPU_PROF` / `NEXT_TURBOPACK_TRACING`
env vars), Next.js writes a `.gitignore` containing `*` into
`.next-profiles/` as it creates the directory. This keeps the profiling
output out of git and out of the way of tools that scan the project
tree.

### Why?

`.next-profiles` is a sibling of `.next`, so it's intentionally *not*
covered by the `.gitignore` create-next-app ships. The motivation was
that profiles wouldn't bloat `.next` and wouldn't be silently ignored.
But a sibling dir full of large profile/trace files turns out to be a
liability:

- **Ecosystem tools scan it.** Tailwind v4 aggressively auto-scans files
and has opened a `trace-turbopack.bin` and crashed; Turborepo <2.8.13
hashes the file and OOMs, and >=2.8.13 fails to start `dev` watchers
because reading the large trace is too slow. (workflow watchers are
likely affected too.)
- **Turbopack NFT globbing** can read/hash the file via overly broad
`fs.read` patterns.
- **Accidental commits.** Pushing a multi-GB trace would be miserable
for a customer helping us debug.

These have each gotten point bandaids (#94587, #94570), but the real fix
preserves the original motivation: a nested `.gitignore` with `*` keeps
the files off git **and** makes gitignore-respecting tools (Tailwind,
Turborepo) skip the whole subtree, which resolves the scanning failures.
Reverting to `.next/profiles` was the alternative; the gitignore
approach keeps the existing path.

### How?

- `ensureProfilesDir()` (`packages/next/src/lib/profiles-dir.ts`)
creates `.next-profiles` and writes the `.gitignore` (only if absent, so
user edits survive). Single source of truth.
- `bin/next.ts` calls a `setupProfilesDir()` helper on build/dev/start,
gated on the env vars so both the CLI flags and direct env-var usage are
covered. Skipped when `NEXT_TURBOPACK_TRACING_PATH` redirects trace
output.
- Rust keeps `create_dir_all` as a backstop for the trace dir; the
`.gitignore` is written in JS only.
- Docs switched from `NEXT_TURBOPACK_TRACING=1` to `next dev
--internal-trace`, and the stale `trace-turbopack` filename corrected to
`trace-turbopack.bin`.

<!-- NEXT_JS_LLM_PR -->
2026-07-01 15:51:33 +00:00

67 lines
2.3 KiB
TypeScript

import { nextTestSetup, isNextDeploy } from 'e2e-utils'
import { pathExists, readdir, readFile } from 'fs-extra'
import { join } from 'path'
describe('CPU Profiling - next build', () => {
const { next, isNextDev, skipped, isTurbopack } = nextTestSetup({
files: __dirname,
buildCommand: 'pnpm next build --experimental-cpu-prof',
dependencies: {},
skipStart: true,
skipDeployment: true,
})
if (skipped) return
// CPU profiling only works with local `next build`, not dev or deploy modes
if (isNextDev || isNextDeploy || skipped) {
it('skip for development/deploy mode', () => {})
return
}
beforeAll(async () => {
// Run the build with CPU profiling enabled
await next.build()
})
it('should write a .gitignore into .next-profiles', async () => {
const gitignore = join(next.testDir, '.next-profiles', '.gitignore')
expect(await pathExists(gitignore)).toBe(true)
// `*` keeps the (potentially large) profiling output out of git and away
// from gitignore-respecting tools that would otherwise scan it.
expect(await readFile(gitignore, 'utf8')).toContain('*')
})
it('should create CPU profile files after build', async () => {
const profileDir = join(next.testDir, '.next-profiles')
const profileDirExists = await pathExists(profileDir)
expect(profileDirExists).toBe(true)
const files = await readdir(profileDir)
const cpuProfiles = files.filter((f: string) => f.endsWith('.cpuprofile'))
// Main profile should always exist
expect(cpuProfiles.some((f) => f.startsWith('build-main-'))).toBe(true)
if (isTurbopack) {
// Turbopack mode generates: build-main, build-turbopack
expect(cpuProfiles.length).toBe(2)
expect(cpuProfiles.some((f) => f.startsWith('build-turbopack-'))).toBe(
true
)
} else {
// Webpack mode generates: build-main, build-webpack-client, build-webpack-server, build-webpack-edge-server
expect(cpuProfiles.length).toBe(4)
expect(
cpuProfiles.some((f) => f.startsWith('build-webpack-client-'))
).toBe(true)
expect(
cpuProfiles.some((f) => f.startsWith('build-webpack-server-'))
).toBe(true)
expect(
cpuProfiles.some((f) => f.startsWith('build-webpack-edge-server-'))
).toBe(true)
}
})
})