Skip to content

Commit

Permalink
Debug tracing: time devserver startup (#54537)
Browse files Browse the repository at this point in the history
This:

- Profiles dev server startup by wrapping most logic in a debug tracing Span
- Sets tracing globals for this case earlier, as they would otherwise be unavailable
- Allows Turbopack traces to be uploaded, as while currently incomplete, they are accurate


Closes WEB-1440

Co-authored-by: Jiachi Liu <[email protected]>
  • Loading branch information
wbinnssmith and huozhi authored Sep 2, 2023
1 parent c3d23c6 commit 125605c
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getProjectDir } from '../lib/get-project-dir'
import { CONFIG_FILES, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
import path from 'path'
import { NextConfigComplete } from '../server/config-shared'
import { traceGlobals } from '../trace/shared'
import { setGlobal, traceGlobals } from '../trace/shared'
import { Telemetry } from '../telemetry/storage'
import loadConfig from '../server/config'
import { findPagesDir } from '../lib/find-pages-dir'
Expand All @@ -27,6 +27,7 @@ import type { ChildProcess } from 'child_process'
import { checkIsNodeDebugging } from '../server/lib/is-node-debugging'
import { createSelfSignedCertificate } from '../lib/mkcert'
import uploadTrace from '../trace/upload-trace'
import { trace } from '../trace'

let dir: string
let config: NextConfigComplete
Expand Down Expand Up @@ -90,19 +91,13 @@ const handleSessionStop = async () => {
}

if (traceUploadUrl) {
if (isTurboSession) {
console.warn(
'Uploading traces with Turbopack is not yet supported. Skipping sending trace.'
)
} else {
uploadTrace({
traceUploadUrl,
mode: 'dev',
isTurboSession,
projectDir: dir,
distDir: config.distDir,
})
}
uploadTrace({
traceUploadUrl,
mode: 'dev',
isTurboSession,
projectDir: dir,
distDir: config.distDir,
})
}

// ensure we re-enable the terminal cursor before exiting
Expand Down Expand Up @@ -330,6 +325,10 @@ const nextDev: CliCommand = async (argv) => {
process.env.EXPERIMENTAL_TURBOPACK = '1'
}

const distDir = path.join(dir, config.distDir ?? '.next')
setGlobal('phase', PHASE_DEVELOPMENT_SERVER)
setGlobal('distDir', distDir)

if (process.env.TURBOPACK) {
isTurboSession = true

Expand All @@ -339,7 +338,6 @@ const nextDev: CliCommand = async (argv) => {
require('../build/swc') as typeof import('../build/swc')
const { eventCliSession } =
require('../telemetry/events/version') as typeof import('../telemetry/events/version')
const { setGlobal } = require('../trace') as typeof import('../trace')
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 All @@ -351,7 +349,6 @@ const nextDev: CliCommand = async (argv) => {
isDev: true,
})

const distDir = path.join(dir, rawNextConfig.distDir || '.next')
const { pagesDir, appDir } = findPagesDir(dir)
const telemetry = new Telemetry({
distDir,
Expand Down Expand Up @@ -380,15 +377,19 @@ const nextDev: CliCommand = async (argv) => {
// Turbopack need to be in control over reading the .env files and watching them.
// So we need to start with a initial env to know which env vars are coming from the user.
resetEnv()
let bindings = await loadBindings()
let server
trace('start-dev-server').traceAsyncFn(async (_) => {
let bindings = await loadBindings()

server = bindings.turbo.startDev({
...devServerOptions,
showAll: args['--show-all'] ?? false,
root: args['--root'] ?? findRootDir(dir),
})

let server = bindings.turbo.startDev({
...devServerOptions,
showAll: args['--show-all'] ?? false,
root: args['--root'] ?? findRootDir(dir),
// Start preflight after server is listening and ignore errors:
preflight(false).catch(() => {})
})
// Start preflight after server is listening and ignore errors:
preflight(false).catch(() => {})

if (!isCustomTurbopack) {
await telemetry.flush()
Expand Down Expand Up @@ -468,7 +469,9 @@ const nextDev: CliCommand = async (argv) => {
}
})

runningServer = await runDevServer(false)
await trace('start-dev-server').traceAsyncFn(async (_) => {
runningServer = await runDevServer(false)
})
}
}

Expand Down

0 comments on commit 125605c

Please sign in to comment.