Skip to content

Commit

Permalink
Update dev process exit handling (#42367)
Browse files Browse the repository at this point in the history
Follow-up to #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)
  • Loading branch information
ijjk committed Nov 2, 2022
1 parent 6bb8407 commit 2a9e2f1
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 54 deletions.
2 changes: 2 additions & 0 deletions packages/next/build/index.ts
Expand Up @@ -341,6 +341,8 @@ export default async function build(
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
isCustomServer: null,
turboFlag: false,
pagesDir: !!pagesDir,
appDir: !!appDir,
})
)

Expand Down
6 changes: 6 additions & 0 deletions packages/next/cli/next-dev.ts
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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 })
Expand Down
2 changes: 2 additions & 0 deletions packages/next/export/index.ts
Expand Up @@ -183,6 +183,8 @@ export default async function exportApp(
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
isCustomServer: null,
turboFlag: false,
pagesDir: null,
appDir: null,
})
)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -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) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/next/telemetry/events/session-stopped.ts
Expand Up @@ -6,6 +6,8 @@ export type EventCliSessionStopped = {
nodeVersion: string
turboFlag?: boolean | null
durationMilliseconds?: number | null
pagesDir?: boolean
appDir?: boolean
}

export function eventCliSession(
Expand All @@ -26,6 +28,8 @@ export function eventCliSession(
turboFlag: !!event.turboFlag,
}
: {}),
pagesDir: event.pagesDir,
appDir: event.appDir,
}
return [{ eventName: EVENT_VERSION, payload }]
}
4 changes: 4 additions & 0 deletions packages/next/telemetry/events/version.ts
Expand Up @@ -30,6 +30,8 @@ type EventCliSessionStarted = {
reactStrictMode: boolean
webpackVersion: number | null
turboFlag: boolean
appDir: boolean | null
pagesDir: boolean | null
}

function hasBabelConfig(dir: string): boolean {
Expand Down Expand Up @@ -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 }]
}
149 changes: 95 additions & 54 deletions test/integration/telemetry/test/index.test.js
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit 2a9e2f1

Please sign in to comment.