Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dev session stopped handling #44112

Merged
merged 3 commits into from Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 45 additions & 28 deletions packages/next/cli/next-dev.ts
Expand Up @@ -8,40 +8,64 @@ import { startedDevelopmentServer } from '../build/output'
import { cliCommand } from '../lib/commands'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { CONFIG_FILES } from '../shared/lib/constants'
import { CONFIG_FILES, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
import path from 'path'
import type { NextConfig } from '../types'
import type { NextConfigComplete } from '../server/config-shared'
import { traceGlobals } from '../trace/shared'
import cluster from 'cluster'
import { Telemetry } from '../telemetry/storage'
import loadConfig from '../server/config'
import { findPagesDir } from '../lib/find-pages-dir'

let isTurboSession = false
let sessionStopHandled = false
let sessionStarted = Date.now()
let dir: string

const handleSessionStop = async () => {
if (sessionStopHandled) return
sessionStopHandled = true

const { eventCliSession } =
require('../telemetry/events/session-stopped') as typeof import('../telemetry/events/session-stopped')
const telemetry = traceGlobals.get('telemetry') as InstanceType<
typeof import('../telemetry/storage').Telemetry
>
if (!telemetry) {
process.exit(0)
}
try {
const { eventCliSession } =
require('../telemetry/events/session-stopped') as typeof import('../telemetry/events/session-stopped')

telemetry.record(
eventCliSession({
cliCommand: 'dev',
turboFlag: isTurboSession,
durationMilliseconds: Date.now() - sessionStarted,
pagesDir: !!traceGlobals.get('pagesDir'),
appDir: !!traceGlobals.get('appDir'),
})
)
await telemetry.flush()
const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, dir)

let telemetry =
(traceGlobals.get('telemetry') as InstanceType<
typeof import('../telemetry/storage').Telemetry
>) ||
new Telemetry({
distDir: path.join(dir, config.distDir),
})

let appDir: boolean = !!traceGlobals.get('pagesDir')
let pagesDir: boolean = !!traceGlobals.get('appDir')

if (
typeof traceGlobals.get('pagesDir') === 'undefined' ||
typeof traceGlobals.get('appDir') === 'undefined'
) {
const pagesResult = await findPagesDir(dir, !!config.experimental.appDir)
appDir = !!pagesResult.appDir
pagesDir = !!pagesResult.pagesDir
}

telemetry.record(
eventCliSession({
cliCommand: 'dev',
turboFlag: isTurboSession,
durationMilliseconds: Date.now() - sessionStarted,
pagesDir,
appDir,
})
)
await telemetry.flush()
} catch (err) {
console.error(err)
}
process.exit(0)
}

Expand Down Expand Up @@ -95,7 +119,7 @@ const nextDev: cliCommand = async (argv) => {
process.exit(0)
}

const dir = getProjectDir(args._[0])
dir = getProjectDir(args._[0])

// Check if pages dir exists and warn if not
if (!existsSync(dir)) {
Expand Down Expand Up @@ -147,10 +171,6 @@ const nextDev: cliCommand = async (argv) => {
require('../build/webpack-config') as typeof import('../build/webpack-config')
const { defaultConfig } =
require('../server/config-shared') as typeof import('../server/config-shared')
const { default: loadConfig } =
require('../server/config') as typeof import('../server/config')
const { PHASE_DEVELOPMENT_SERVER } =
require('../shared/lib/constants') as typeof import('../shared/lib/constants')
const chalk =
require('next/dist/compiled/chalk') as typeof import('next/dist/compiled/chalk')
const { interopDefault } =
Expand Down Expand Up @@ -353,11 +373,8 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit
require('../build/swc') as typeof import('../build/swc')
const { eventCliSession } =
require('../telemetry/events/version') as typeof import('../telemetry/events/version')
const { findPagesDir } =
require('../lib/find-pages-dir') as typeof import('../lib/find-pages-dir')
const { setGlobal } = require('../trace') as typeof import('../trace')
const { Telemetry } =
require('../telemetry/storage') as typeof import('../telemetry/storage')
require('../telemetry/storage') as typeof import('../telemetry/storage')
const findUp =
require('next/dist/compiled/find-up') as typeof import('next/dist/compiled/find-up')

Expand Down
10 changes: 6 additions & 4 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -732,6 +732,12 @@ export default class DevServer extends Server {
}

const telemetry = new Telemetry({ distDir: this.distDir })

// This is required by the tracing subsystem.
setGlobal('appDir', this.appDir)
setGlobal('pagesDir', this.pagesDir)
setGlobal('telemetry', telemetry)

const isSrcDir = relative(
this.dir,
this.pagesDir || this.appDir || ''
Expand All @@ -748,10 +754,6 @@ export default class DevServer extends Server {
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) => {
this.logErrorWithOriginalStack(reason, 'unhandledRejection').catch(
Expand Down
6 changes: 4 additions & 2 deletions test/integration/telemetry/test/index.test.js
Expand Up @@ -467,8 +467,10 @@ describe('Telemetry CLI', () => {
turbo: true,
})

await check(() => stderr, /NEXT_CLI_SESSION_STARTED/)

if (app) {
await killApp(app)
await app.kill('SIGTERM')
}
await check(() => stderr, /NEXT_CLI_SESSION_STOPPED/)

Expand Down Expand Up @@ -503,7 +505,7 @@ describe('Telemetry CLI', () => {
await check(() => stderr, /NEXT_CLI_SESSION_STARTED/)

if (app) {
await killApp(app)
await app.kill('SIGTERM')
}
await check(() => stderr, /NEXT_CLI_SESSION_STOPPED/)

Expand Down