Unverified Commit 2a9e2f18 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update dev process exit handling (#42367)

Follow-up to https://github.com/vercel/next.js/pull/42255

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 6bb8407b
......@@ -341,6 +341,8 @@ export default async function build(
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
isCustomServer: null,
turboFlag: false,
pagesDir: !!pagesDir,
appDir: !!appDir,
})
)
......
......@@ -36,6 +36,8 @@ const handleSessionStop = async () => {
cliCommand: 'dev',
turboFlag: isTurboSession,
durationMilliseconds: Date.now() - sessionStarted,
pagesDir: !!traceGlobals.get('pagesDir'),
appDir: !!traceGlobals.get('appDir'),
})
)
await telemetry.flush()
......@@ -329,6 +331,8 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit
const telemetry = new Telemetry({
distDir,
})
setGlobal('appDir', appDir)
setGlobal('pagesDir', pagesDir)
setGlobal('telemetry', telemetry)
telemetry.record(
......@@ -342,6 +346,8 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
isCustomServer: false,
turboFlag: true,
pagesDir: !!pagesDir,
appDir: !!appDir,
})
)
const turboJson = findUp.sync('turbo.json', { cwd: dir })
......
......@@ -183,6 +183,8 @@ export default async function exportApp(
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
isCustomServer: null,
turboFlag: false,
pagesDir: null,
appDir: null,
})
)
}
......
......@@ -729,9 +729,13 @@ export default class DevServer extends Server {
hasNowJson: !!(await findUp('now.json', { cwd: this.dir })),
isCustomServer: this.isCustomServer,
turboFlag: false,
pagesDir: !!this.pagesDir,
appDir: !!this.appDir,
})
)
// This is required by the tracing subsystem.
setGlobal('appDir', this.appDir)
setGlobal('pagesDir', this.pagesDir)
setGlobal('telemetry', telemetry)
process.on('unhandledRejection', (reason) => {
......
......@@ -6,6 +6,8 @@ export type EventCliSessionStopped = {
nodeVersion: string
turboFlag?: boolean | null
durationMilliseconds?: number | null
pagesDir?: boolean
appDir?: boolean
}
export function eventCliSession(
......@@ -26,6 +28,8 @@ export function eventCliSession(
turboFlag: !!event.turboFlag,
}
: {}),
pagesDir: event.pagesDir,
appDir: event.appDir,
}
return [{ eventName: EVENT_VERSION, payload }]
}
......@@ -30,6 +30,8 @@ type EventCliSessionStarted = {
reactStrictMode: boolean
webpackVersion: number | null
turboFlag: boolean
appDir: boolean | null
pagesDir: boolean | null
}
function hasBabelConfig(dir: string): boolean {
......@@ -113,6 +115,8 @@ export function eventCliSession(
reactStrictMode: !!nextConfig?.reactStrictMode,
webpackVersion: event.webpackVersion || null,
turboFlag: event.turboFlag || false,
appDir: event.appDir,
pagesDir: event.pagesDir,
}
return [{ eventName: EVENT_VERSION, payload }]
}
......@@ -369,85 +369,124 @@ describe('Telemetry CLI', () => {
expect(stderr).toMatch(/isSrcDir.*?true/)
})
const setupAppDir = async () => {
await fs.writeFile(
path.join(__dirname, '../next.config.js'),
'module.exports = { experimental: { appDir: true } }'
)
await fs.mkdir(path.join(__dirname, '../app'))
await fs.writeFile(
path.join(__dirname, '../app/page.js'),
'export default function Page() { return "hello world" }'
)
return async function teardownAppDir() {
await fs.remove(path.join(__dirname, '../app'))
await fs.remove(path.join(__dirname, '../next.config.js'))
}
}
it('detects --turbo correctly for `next dev`', async () => {
let port = await findPort()
let stderr = ''
const handleStderr = (msg) => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
turbo: true,
})
await waitFor(1000)
const teardown = await setupAppDir()
if (app) {
await killApp(app)
try {
const handleStderr = (msg) => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
turbo: true,
})
await waitFor(1000)
if (app) {
await killApp(app)
}
const event1 = /NEXT_CLI_SESSION_STARTED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
expect(event1).toMatch(/"pagesDir": true/)
expect(event1).toMatch(/"turboFlag": true/)
} finally {
await teardown()
}
const event1 = /NEXT_CLI_SESSION_STARTED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
expect(event1).toMatch(/"turboFlag": true/)
})
it('detects --turbo correctly for `next dev` stopped', async () => {
let port = await findPort()
let stderr = ''
const handleStderr = (msg) => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
turbo: true,
})
const teardown = await setupAppDir()
if (app) {
await killApp(app)
}
await check(() => stderr, /NEXT_CLI_SESSION_STOPPED/)
try {
const handleStderr = (msg) => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
turbo: true,
})
const event1 = /NEXT_CLI_SESSION_STOPPED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
if (app) {
await killApp(app)
}
await check(() => stderr, /NEXT_CLI_SESSION_STOPPED/)
expect(event1).toMatch(/"turboFlag": true/)
const event1 = /NEXT_CLI_SESSION_STOPPED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
expect(event1).toMatch(/"pagesDir": true/)
expect(event1).toMatch(/"turboFlag": true/)
} finally {
await teardown()
}
})
it('detects correctly for `next dev` stopped (no turbo)', async () => {
let port = await findPort()
let stderr = ''
const handleStderr = (msg) => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
})
const teardown = await setupAppDir()
try {
const handleStderr = (msg) => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
})
await check(() => stderr, /NEXT_CLI_SESSION_STARTED/)
await check(() => stderr, /NEXT_CLI_SESSION_STARTED/)
if (app) {
await killApp(app)
}
await check(() => stderr, /NEXT_CLI_SESSION_STOPPED/)
if (app) {
await killApp(app)
}
await check(() => stderr, /NEXT_CLI_SESSION_STOPPED/)
const event1 = /NEXT_CLI_SESSION_STOPPED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
const event1 = /NEXT_CLI_SESSION_STOPPED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()
expect(event1).toMatch(/"turboFlag": false/)
expect(event1).toMatch(/"turboFlag": false/)
expect(event1).toMatch(/"pagesDir": true/)
expect(event1).toMatch(/"appDir": true/)
} finally {
await teardown()
}
})
it('detect reportWebVitals correctly for `next build`', async () => {
......@@ -567,6 +606,8 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/"trailingSlashEnabled": false/)
expect(event1).toMatch(/"reactStrictMode": false/)
expect(event1).toMatch(/"turboFlag": false/)
expect(event1).toMatch(/"pagesDir": true/)
expect(event1).toMatch(/"appDir": false/)
await fs.rename(
path.join(appDir, 'next.config.i18n-images'),
......
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