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