Unverified Commit 22f8122a authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Keep dev and build running during upgrade prompts (#99702)

parent b4a4c055
......@@ -86,8 +86,12 @@ class NextRootCommand extends Command {
const commandName = event.name()
const defaultEnv = commandName === 'dev' ? 'development' : 'production'
const standardEnv = ['production', 'development', 'test']
// `next build` reruns itself as a child to show the upgrade menu. The
// parent already warned, and it sets NODE_ENV for --debug-prerender.
const isUpgradeBuildChild =
process.env.NEXT_PRIVATE_UPGRADE_BUILD_CHILD === '1'
if (process.env.NODE_ENV) {
if (process.env.NODE_ENV && !isUpgradeBuildChild) {
const isNotStandard = !standardEnv.includes(process.env.NODE_ENV)
const shouldWarnCommands =
process.env.NODE_ENV === 'development'
......@@ -110,6 +114,7 @@ class NextRootCommand extends Command {
}
if (
!isUpgradeBuildChild &&
process.platform === 'darwin' &&
process.arch === 'x64' &&
os.cpus().some((cpu) => cpu.model.includes('Apple'))
......
import type { NudgeKind } from '../lib/upgrade/nudge'
import {
getRouteCacheKey,
ROUTE_CACHE_DIRECTORY,
......@@ -131,7 +130,7 @@ import { PAGE_TYPES } from '../lib/page-types'
import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable'
import * as Log from './output/log'
import createSpinner from './spinner'
import createSpinner, { finishSpinner } from './spinner'
import { trace, flushAllTraces, setGlobal, type Span } from '../trace'
import { writeAnalyzeSnapshot } from './analyze/snapshot'
import { writeRouteBundleStats } from './route-bundle-stats'
......@@ -1126,9 +1125,8 @@ export default async function build(
experimentalBuildMode: 'default' | 'compile' | 'generate' | 'generate-env',
traceUploadUrl: string | undefined,
debugBuildPathsPatterns: string[] | undefined,
enabledFeatures: Record<string, unknown> = {},
allowHumanUpgrade = false
): Promise<{ policy: NudgeKind; nudgeId: string | null } | 'interrupt' | void> {
enabledFeatures: Record<string, unknown> = {}
): Promise<void> {
const isCompileMode = experimentalBuildMode === 'compile'
const isGenerateMode = experimentalBuildMode === 'generate'
NextBuildContext.isCompileMode = isCompileMode
......@@ -1267,36 +1265,18 @@ export default async function build(
const { nudgeUpgrade, getUpgradeContext } =
require('../lib/upgrade/nudge') as typeof import('../lib/upgrade/nudge')
const upgradeContext = getUpgradeContext(config)
if (allowHumanUpgrade) {
// TODO: Do not block the build while prompting for an upgrade.
// Preserve all logs for display after the prompt and stop the build before Update.
let nudgeId: string | null = null
const action = await nudgeUpgrade(
if (
process.env.NEXT_PRIVATE_UPGRADE_BUILD_CHILD === '1' &&
process.connected
) {
// The CLI shows the menu; keep building instead of waiting for it.
process.send!({
nextUpgradeContext: upgradeContext,
dir,
upgradeContext,
'build',
new AbortController().signal,
null,
{
telemetry,
onNudgeId(id) {
nudgeId = id
},
}
).catch((error) => {
Log.warn(`Could not offer the upgrade: ${String(error)}`)
telemetryDisabled: process.env.NEXT_TELEMETRY_DISABLED,
})
if (action === 'update' && upgradeContext.experimental.agentUpgrade) {
return {
policy: upgradeContext.experimental.agentUpgrade,
nudgeId,
}
}
if (action === 'interrupt') {
return 'interrupt' as const
}
} else {
// Agent checks retain their parallel behavior; humans decide before building.
// No menu (e.g. an agent): nudge in the background.
pendingUpgradeNudge = nudgeUpgrade(
dir,
upgradeContext,
......@@ -2948,13 +2928,10 @@ export default async function build(
return returnValue
})
if (postCompileSpinner) {
const collectingPageDataEnd = process.hrtime(collectingPageDataStart)
postCompileSpinner.setText(
`Collecting page data using ${numberOfWorkers} worker${numberOfWorkers > 1 ? 's' : ''} in ${hrtimeDurationToString(collectingPageDataEnd)}`
)
postCompileSpinner.stopAndPersist()
}
finishSpinner(
postCompileSpinner,
`Collecting page data using ${numberOfWorkers} worker${numberOfWorkers > 1 ? 's' : ''} in ${hrtimeDurationToString(process.hrtime(collectingPageDataStart))}`
)
traceMemoryUsage('Finished collecting page data', nextBuildSpan)
if (customAppGetInitialProps) {
......@@ -4688,14 +4665,11 @@ export default async function build(
await buildTracesPromise
if (buildTracesSpinner) {
if (buildTracesStart) {
const buildTracesEnd = process.hrtime(buildTracesStart)
buildTracesSpinner.setText(
`Collecting build traces in ${hrtimeDurationToString(buildTracesEnd)}`
)
}
buildTracesSpinner.stopAndPersist()
if (buildTracesStart) {
finishSpinner(
buildTracesSpinner,
`Collecting build traces in ${hrtimeDurationToString(process.hrtime(buildTracesStart))}`
)
buildTracesSpinner = undefined
}
......@@ -4825,15 +4799,10 @@ export default async function build(
})
}
if (postBuildSpinner) {
const finalizingPageOptimizationEnd = process.hrtime(
finalizingPageOptimizationStart
)
postBuildSpinner.setText(
`Finalizing page optimization in ${hrtimeDurationToString(finalizingPageOptimizationEnd)}`
)
postBuildSpinner.stopAndPersist()
}
finishSpinner(
postBuildSpinner,
`Finalizing page optimization in ${hrtimeDurationToString(process.hrtime(finalizingPageOptimizationStart))}`
)
console.log()
if (debugOutput) {
......
......@@ -87,3 +87,19 @@ export default function createSpinner(
return spinner
}
// Ends a step with its finished line, e.g. "✓ Collecting page data in 1s".
// Without a terminal there is no spinner to turn into that line, so it is
// skipped. It is still printed when the CLI holds this process's output for
// the upgrade menu, since that output ends up on a terminal.
export function finishSpinner(
spinner: ReturnType<typeof createSpinner>,
text: string
) {
if (spinner) {
spinner.setText(text)
spinner.stopAndPersist()
} else if (process.env.NEXT_PRIVATE_PROMPT_OUTPUT === '1') {
Log.event(text)
}
}
......@@ -5,7 +5,12 @@ import { existsSync } from 'fs'
import { italic } from '../lib/picocolors'
import build from '../build'
import { warn } from '../build/output/log'
import { printAndExit } from '../server/lib/utils'
import {
blockOnOutputWrites,
getNodeDebugType,
getParsedNodeOptions,
printAndExit,
} from '../server/lib/utils'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { warnMissingReactDependencies } from '../lib/warn-missing-react-dependencies'
......@@ -13,6 +18,19 @@ import { enableMemoryDebuggingMode } from '../lib/memory/startup'
import { disableMemoryDebuggingMode } from '../lib/memory/shutdown'
import { Bundler, parseBundlerArgs } from '../lib/bundler'
import { parseBuildPathsInput } from '../lib/resolve-build-paths'
import {
closedUpgradeMenu,
createPromptOutput,
drainPromptOutput,
flushUpgradeTelemetry,
getPromptOutputEnv,
reassertRawMode,
showUpgradeMenu,
} from '../lib/upgrade/prompt-output'
import type { UpgradeContext } from '../lib/upgrade/nudge'
import { fork } from 'child_process'
import { once } from 'events'
import os from 'os'
export type NextBuildOptions = {
analyze?: boolean
......@@ -37,6 +55,15 @@ export type NextBuildOptions = {
const nextBuild = async (options: NextBuildOptions, directory?: string) => {
process.title = `next-build (v${process.env.__NEXT_VERSION})`
// To show the upgrade menu without pausing the build, run the build in a
// child and keep the menu here.
if (process.env.NEXT_PRIVATE_UPGRADE_BUILD_CHILD === '1') {
blockOnOutputWrites()
} else if (await shouldBuildInChild()) {
return buildInChild()
}
const onTerminate = () => {
saveCpuProfile()
process.exit(143)
......@@ -45,10 +72,6 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => {
saveCpuProfile()
process.exit(130)
}
const onHangup = () => {
saveCpuProfile()
process.exit(129)
}
process.on('SIGTERM', onTerminate)
process.on('SIGINT', onInterrupt)
......@@ -128,14 +151,6 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => {
}).filter(([_, value]) => value !== undefined && value !== false)
)
const { shouldPromptForUpgrade, runUpgrade } = await import(
'../lib/upgrade/nudge.js'
)
const humanUpgrade = await shouldPromptForUpgrade()
if (humanUpgrade) {
process.on('SIGHUP', onHangup)
}
return build(
dir,
analyze || experimentalAnalyze,
......@@ -148,20 +163,8 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => {
experimentalBuildMode,
traceUploadUrl,
debugBuildPathsPatterns,
enabledFeatures,
humanUpgrade
enabledFeatures
)
.then(async (action) => {
if (action === 'interrupt') {
process.exit(130)
}
if (action) {
process.off('SIGTERM', onTerminate)
process.off('SIGINT', onInterrupt)
process.off('SIGHUP', onHangup)
process.exit(await runUpgrade(dir, action.policy, action.nudgeId))
}
})
.catch((err) => {
if (experimentalDebugMemoryUsage) {
disableMemoryDebuggingMode()
......@@ -189,4 +192,123 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => {
})
}
// Debugger and profiler runs stay in one process and skip the menu. Otherwise
// the debugger would attach to the wrong process, or there would be two
// profiles.
async function shouldBuildInChild() {
const nodeOptions = getParsedNodeOptions()
if (
process.env.NEXT_CPU_PROF ||
getNodeDebugType(nodeOptions) ||
['inspect-wait', 'cpu-prof', 'heap-prof', 'prof'].some(
(flag) => nodeOptions[flag]
)
) {
return false
}
const { shouldPromptForUpgrade } = await import('../lib/upgrade/nudge.js')
return shouldPromptForUpgrade()
}
// Always exits by itself; the caller would exit with 0 if this returned.
async function buildInChild(): Promise<never> {
// Rerun the same command as a child, with its output piped to us.
const child = fork(process.argv[1], process.argv.slice(2), {
stdio: ['inherit', 'pipe', 'pipe', 'ipc'],
env: {
...process.env,
...getPromptOutputEnv(),
NEXT_PRIVATE_UPGRADE_BUILD_CHILD: '1',
},
})
const exited = once(child, 'exit') as Promise<
[number | null, NodeJS.Signals | null]
>
const output = createPromptOutput()
output.attach(child)
// Don't leave the build running if we exit first.
process.on('exit', () => child.kill())
// Pass signals to the build and close the menu. Remember the signal: the
// build may already be done, and then its exit code says nothing about it.
const controller = new AbortController()
const aborted = once(controller.signal, 'abort')
const signals = ['SIGINT', 'SIGTERM', 'SIGHUP'] as const
let received: NodeJS.Signals | undefined
function onSignal(signal: NodeJS.Signals) {
received ??= signal
controller.abort()
child.kill(signal)
}
for (const signal of signals) {
process.on(signal, onSignal)
}
// The child asks for the menu once it has loaded the config.
let menu: Promise<void> | undefined
child.on(
'message',
(message: {
nextUpgradeContext?: UpgradeContext
dir?: string
telemetryDisabled?: string
}) => {
const { nextUpgradeContext: context, dir } = message ?? {}
if (!context || !dir || menu) {
return
}
menu = (async () => {
const result = await showUpgradeMenu(output, {
dir,
context,
command: 'build',
signal: controller.signal,
initialAssessment: null,
telemetryDisabled: message.telemetryDisabled,
})
if (result === 'interrupt') {
// Ctrl+C in the menu
onSignal('SIGINT')
} else if (result) {
// Upgrade: show the result of a build that already finished, or stop
// the build and drop its output.
const finished = child.exitCode !== null || child.signalCode !== null
if (finished) {
output.release()
} else {
output.discard()
}
child.kill('SIGTERM')
await exited
// Stopped while the build was ending. Exit below with the signal.
if (received) {
return
}
// From here Ctrl+C should stop the upgrade, so stop catching it.
for (const signal of signals) {
process.off(signal, onSignal)
}
const { runUpgrade } = await import('../lib/upgrade/nudge.js')
const exitCode = await runUpgrade(dir, result.policy, result.nudgeId)
await flushUpgradeTelemetry()
// A build that failed still fails the command.
process.exit(exitCode || (finished ? (child.exitCode ?? 1) : 0))
}
})()
}
)
// If the build finishes first, keep the menu up until the user answers.
const [code, signal] = await exited
reassertRawMode()
await drainPromptOutput(child)
// After a signal, don't wait on a network check the menu may still be making.
await Promise.race([menu, aborted.then(() => closedUpgradeMenu(menu))])
await flushUpgradeTelemetry()
const stoppedBy = received ?? signal
process.exit(stoppedBy ? 128 + os.constants.signals[stoppedBy] : code!)
}
export { nextBuild, saveCpuProfile }
......@@ -30,7 +30,17 @@ import uploadTrace from '../trace/upload-trace'
import { initialEnv } from '@next/env'
import { fork } from 'child_process'
import type { ChildProcess } from 'child_process'
import type { UpgradeContext } from '../lib/upgrade/nudge'
import type { UpgradeContext, UpgradeReminder } from '../lib/upgrade/nudge'
import {
closedUpgradeMenu,
createPromptOutput,
drainPromptOutput,
flushUpgradeTelemetry,
getPromptOutputEnv,
reassertRawMode,
showUpgradeMenu,
type UpgradeMenuResult,
} from '../lib/upgrade/prompt-output'
import {
getReservedPortExplanation,
isPortIsReserved,
......@@ -75,6 +85,9 @@ let isTurbopack: boolean
let traceUploadUrl: string
let sessionStopHandled = false
let upgradeController: AbortController | null = null
let upgradeMenu: Promise<UpgradeMenuResult> | undefined
// Holds the dev server's output while the upgrade menu is open.
const promptOutput = createPromptOutput()
let upgradeOffered = false
let upgradeInProgress = false
let interruption: NodeJS.Signals | null = null
......@@ -123,11 +136,15 @@ const handleSessionStop = async (
}
sessionStopHandled = true
const interruptedUpgrade = upgradeInProgress
// Close the menu and let it print what it held.
upgradeController?.abort()
await closedUpgradeMenu(upgradeMenu)
// Capture the child's exit code if it has already exited and caused the
// session stop (via the 'exit' event), otherwise assume success (0).
const exitCode = child?.exitCode || 0
// session stop (via the 'exit' event), otherwise assume success (0). A
// worker that exited to restart is not a failure.
const exitCode =
child?.exitCode === RESTART_EXIT_CODE ? 0 : child?.exitCode || 0
if (
signal != null &&
......@@ -143,6 +160,7 @@ const handleSessionStop = async (
}
await once(child, 'exit').catch(() => {})
if (exitTimeout) clearTimeout(exitTimeout)
await drainPromptOutput(child)
}
sessionSpan.stop()
......@@ -207,6 +225,7 @@ const handleSessionStop = async (
process.stdout.write('\x1B[?25h')
process.stdout.write('\n')
if (exit) {
await flushUpgradeTelemetry()
process.exit(
interruption && (interruption === 'SIGHUP' || interruptedUpgrade)
? 128 + os.constants.signals[interruption]
......@@ -252,15 +271,15 @@ const nextDev = async (
dir = getProjectDir(process.env.NEXT_PRIVATE_DEV_DIR || directory)
warnMissingReactDependencies(dir)
const { shouldPromptForUpgrade, runUpgrade, nudgeUpgrade } = await import(
const { shouldPromptForUpgrade, runUpgrade } = await import(
'../lib/upgrade/nudge.js'
)
const humanUpgrade = await shouldPromptForUpgrade()
const allowedUpgradeRetries = new Set<string>()
async function offerUpgrade(
worker: ChildProcess,
context: UpgradeContext,
initialAssessment: Parameters<typeof nudgeUpgrade>[4]
initialAssessment: Promise<UpgradeReminder | null> | null,
telemetryDisabled: string | undefined
) {
process.on('SIGHUP', onHangup)
upgradeOffered = true
......@@ -268,60 +287,42 @@ const nextDev = async (
const controller = new AbortController()
upgradeController = controller
// Correlate the parent's rendered menu with the upgrade launched after Update.
const telemetry = new Telemetry({
distDir: path.join(dir, context.distDir),
skipNotify: true,
upgradeMenu = showUpgradeMenu(promptOutput, {
dir,
context,
command: 'dev',
signal: controller.signal,
initialAssessment,
telemetryDisabled,
})
const result = await upgradeMenu
upgradeController = null
let nudgeId: string | null = null
let action
try {
action = await nudgeUpgrade(
dir,
context,
'dev',
controller.signal,
initialAssessment,
{
telemetry,
onNudgeId(id) {
nudgeId = id
},
}
)
} catch (error) {
Log.warn(`Could not offer the upgrade: ${String(error)}`)
} finally {
upgradeController = null
}
if (controller.signal.aborted || sessionStopHandled) {
upgradeInProgress = false
process.off('SIGHUP', onHangup)
// Dev is already stopping (e.g. the server crashed).
if (sessionStopHandled) {
return
}
if (action === 'interrupt') {
if (result === 'interrupt') {
onInterrupt()
return
}
if (action === 'update' && context.experimental.agentUpgrade) {
if (result) {
// The server is stopping, so its output doesn't matter.
promptOutput.discard()
await handleSessionStop('SIGTERM', false)
if (interruption) {
await flushUpgradeTelemetry()
process.exit(128 + os.constants.signals[interruption])
}
process.off('SIGINT', onInterrupt)
process.off('SIGTERM', onTerminate)
process.off('SIGHUP', onHangup)
process.exit(
await runUpgrade(dir, context.experimental.agentUpgrade, nudgeId)
)
const exitCode = await runUpgrade(dir, result.policy, result.nudgeId)
await flushUpgradeTelemetry()
process.exit(exitCode)
}
upgradeInProgress = false
process.off('SIGHUP', onHangup)
if (worker.connected) {
worker.send({ nextUpgradeContinue: true })
}
}
// Check if pages dir exists and warn if not
......@@ -503,11 +504,13 @@ const nextDev = async (
const { nodeOptions: formattedNodeOptions, execArgv } =
formatNodeOptions(nodeOptions)
// Pipe output through us so it can be held while the menu is open.
child = fork(startServerPath, {
stdio: 'inherit',
stdio: humanUpgrade ? ['inherit', 'pipe', 'pipe', 'ipc'] : 'inherit',
execArgv,
env: {
...defaultEnv,
...(humanUpgrade ? getPromptOutputEnv() : undefined),
...(isTurbopack ? { TURBOPACK: process.env.TURBOPACK } : undefined),
__NEXT_DEV_SERVER: '1',
NEXT_PRIVATE_START_TIME: process.env.NEXT_PRIVATE_START_TIME,
......@@ -542,6 +545,9 @@ const nextDev = async (
: undefined),
},
})
if (humanUpgrade) {
promptOutput.attach(child)
}
child.on('message', (msg: any) => {
if (msg && typeof msg === 'object') {
......@@ -557,13 +563,15 @@ const nextDev = async (
msg.nextUpgradeAssessment !== undefined
? Promise.resolve(msg.nextUpgradeAssessment)
: null
void offerUpgrade(child!, context, initialAssessment).catch(
async (error) => {
console.error(error)
await handleSessionStop('SIGTERM', false)
process.exit(1)
}
)
void offerUpgrade(
context,
initialAssessment,
msg.telemetryDisabled
).catch(async (error) => {
console.error(error)
await handleSessionStop('SIGTERM', false)
process.exit(1)
})
} else if (msg.nextWorkerReady) {
child?.send({ nextWorkerOptions: startServerOptions })
} else if (msg.nextServerReady && !resolved) {
......@@ -583,8 +591,12 @@ const nextDev = async (
}
})
child.on('exit', async (code, signal) => {
upgradeController?.abort()
// A restart replaces `child`, so keep this one.
const worker = child
worker.on('exit', async (code, signal) => {
reassertRawMode()
// Read the rest of its output first. A restart keeps the menu open.
await drainPromptOutput(worker)
if (sessionStopHandled) {
return
}
......@@ -654,6 +666,8 @@ const nextDev = async (
await startServer(devServerOptions)
}
// Its warnings would land on the menu's screen and be lost.
await upgradeMenu
await preflight(reboot)
} catch (err) {
console.error(err)
......
......@@ -23,6 +23,13 @@ const enabled =
!env.NO_COLOR &&
(env.FORCE_COLOR || (stdout?.isTTY && !env.CI && env.TERM !== 'dumb'))
export const isColorSupported = Boolean(enabled)
// Whether stdout ends up on a terminal: directly, or through the CLI, which
// pipes a child's output while the upgrade menu is open.
export const stdoutIsTerminal =
Boolean(stdout?.isTTY) || env.NEXT_PRIVATE_PROMPT_OUTPUT === '1'
const replaceClose = (
str: string,
close: string,
......
......@@ -145,7 +145,7 @@ export type UpgradeContext = Pick<
experimental: { agentUpgrade: NudgeKind | false }
}
type UpgradeReminder = {
export type UpgradeReminder = {
policy: NudgeKind
installedVersion: string
} & (
......@@ -185,6 +185,16 @@ export async function assessUpgrade(
) {
return null
}
if (isTerminalForcedForTesting()) {
// Offer a fixed reminder without querying advisories or past dismissals.
return {
policy,
installedVersion,
kind: 'security',
reference: null,
targetVersion: installedVersion,
}
}
if (stopBefore === 'security') {
return null
}
......@@ -407,9 +417,14 @@ async function getUpgradePreferences(directory: string) {
return { key, preferences: new Conf({ projectName: 'nextjs' }) }
}
// The terminal test needs a menu in CI and under agents, without the network.
function isTerminalForcedForTesting(): boolean {
return process.env.__NEXT_AGENT_UPGRADE_FORCE_TERMINAL_FOR_TESTING === '1'
}
function canPromptForUpgrade(): boolean {
return (
!isCI &&
(!isCI || isTerminalForcedForTesting()) &&
Boolean(process.stdin.isTTY && process.stdout.isTTY) &&
process.env.TERM !== 'dumb'
)
......@@ -510,7 +525,10 @@ export async function runUpgrade(
}
export async function shouldPromptForUpgrade(): Promise<boolean> {
return canPromptForUpgrade() && !(await getAgentName())
return (
canPromptForUpgrade() &&
(isTerminalForcedForTesting() || !(await getAgentName()))
)
}
export async function nudgeUpgrade(
......@@ -547,7 +565,7 @@ export async function nudgeUpgrade(
}
// An agent's stopped command sends policy and nudge together before synchronous exit.
const agent = await getAgentName()
const agent = isTerminalForcedForTesting() ? null : await getAgentName()
if (!agent) {
telemetry?.record(policyEvent)
}
......@@ -572,7 +590,7 @@ export async function nudgeUpgrade(
}
}
const reminder = await (
stopBefore === null && initialAssessment
stopBefore === null && initialAssessment && !isTerminalForcedForTesting()
? initialAssessment
: assessUpgrade(
directory,
......@@ -632,11 +650,9 @@ export async function nudgeUpgrade(
onShown
)
if (shown && telemetry) {
if (!signal.aborted) {
telemetry.record(eventAgentUpgradeNudgeDecision({ nudgeId, action }))
}
await telemetry.flush()
// The caller flushes after it gives the terminal back.
if (shown && telemetry && !signal.aborted) {
telemetry.record(eventAgentUpgradeNudgeDecision({ nudgeId, action }))
}
return action
}
......
import path from 'path'
import * as Log from '../../build/output/log'
import { Telemetry } from '../../telemetry/storage'
import { once } from 'events'
import { setTimeout as sleep } from 'timers/promises'
import { isColorSupported } from '../picocolors'
import type { ChildProcess } from 'child_process'
import type { NudgeKind, UpgradeContext, UpgradeReminder } from './nudge'
type PromptOutput = ReturnType<typeof createPromptOutput>
export type UpgradeMenuResult =
| { policy: NudgeKind; nudgeId: string | null }
| 'interrupt'
| null
// Keep at most this much while the menu is open, so a menu left open doesn't
// grow memory without bound.
const MAX_HELD_BYTES = 10 * 1024 * 1024
// All output of the child (and anything it starts) comes through here, so it
// can be held while the upgrade menu is on screen.
export function createPromptOutput() {
// print: show now. hold: keep for later. drop: throw away.
let mode: 'print' | 'hold' | 'drop' = 'print'
// One list for stdout and stderr keeps their order.
let held: Array<[NodeJS.WriteStream, Buffer]> = []
let heldBytes = 0
let droppedBytes = 0
function forward(destination: NodeJS.WriteStream) {
return (chunk: Buffer) => {
if (mode === 'print') {
destination.write(chunk)
} else if (mode === 'hold') {
held.push([destination, chunk])
heldBytes += chunk.length
// Over the limit: drop the oldest output.
while (heldBytes > MAX_HELD_BYTES && held.length > 1) {
const [, oldest] = held.shift()!
heldBytes -= oldest.length
droppedBytes += oldest.length
}
}
}
}
return {
attach(child: ChildProcess) {
child.stdout!.on('data', forward(process.stdout))
child.stderr!.on('data', forward(process.stderr))
},
// The menu opened.
hold() {
mode = 'hold'
},
// The menu closed: show what was held.
release() {
mode = 'print'
if (droppedBytes > 0) {
Log.warn(
`${droppedBytes} bytes of earlier output were dropped while the upgrade menu was open.`
)
}
for (const [destination, chunk] of held) {
destination.write(chunk)
}
held = []
},
// Upgrade was chosen and the child is stopping, so its output doesn't
// matter.
discard() {
mode = 'drop'
held = []
},
}
}
// Read what is left in the pipes after the child exits. Don't wait long, in
// case something the child started keeps them open.
export async function drainPromptOutput(child: ChildProcess) {
// The pipes are often closed by the time 'exit' fires, and then 'close' has
// already been sent.
if (!child.stdout || (child.stdout.destroyed && child.stderr?.destroyed)) {
return
}
await Promise.race([once(child, 'close'), sleep(500)])
}
// A pipe is not a terminal. Like other tools that pipe a child's output, pass
// FORCE_COLOR on. Tell Next's own output code that the output still ends up on
// a terminal, and how wide it is.
export function getPromptOutputEnv() {
const { FORCE_COLOR, NODE_DISABLE_COLORS } = process.env
return {
FORCE_COLOR:
// An empty FORCE_COLOR means "on" to Node but "unset" to picocolors.
FORCE_COLOR ||
(isColorSupported && !NODE_DISABLE_COLORS ? '1' : FORCE_COLOR),
NEXT_PRIVATE_PROMPT_OUTPUT: '1',
NEXT_PRIVATE_TERMINAL_COLUMNS: process.stdout.columns
? String(process.stdout.columns)
: undefined,
}
}
// A child on the terminal puts it back in normal mode when it exits, which
// stops the menu from reading keys. Put raw mode back.
export function reassertRawMode() {
const stdin = process.stdin
if (stdin.isTTY && stdin.isRaw) {
// Node skips setting a mode it thinks is already set, so toggle.
stdin.setRawMode(false)
stdin.setRawMode(true)
}
}
let pendingTelemetry: Promise<unknown> | undefined
// Shows the menu while the child keeps working. Returns what to do next:
// upgrade, interrupt, or carry on (null).
export async function showUpgradeMenu(
output: PromptOutput,
options: {
dir: string
context: UpgradeContext
command: 'dev' | 'build'
signal: AbortSignal
initialAssessment: Promise<UpgradeReminder | null> | null
// The child's NEXT_TELEMETRY_DISABLED. It loaded .env; this process did
// not.
telemetryDisabled: string | undefined
}
): Promise<UpgradeMenuResult> {
const { dir, context, command, signal, initialAssessment } = options
const { nudgeUpgrade } = require('./nudge') as typeof import('./nudge')
if (options.telemetryDisabled) {
process.env.NEXT_TELEMETRY_DISABLED = options.telemetryDisabled
}
const telemetry = new Telemetry({
distDir: path.join(dir, context.distDir),
skipNotify: true,
})
let nudgeId: string | null = null
// onNudgeId runs as the menu draws, so holding starts at exactly that point.
const action = await nudgeUpgrade(
dir,
context,
command,
signal,
initialAssessment,
{
telemetry,
onNudgeId(id) {
nudgeId = id
output.hold()
},
}
).catch((error) => {
Log.warn(`Could not offer the upgrade: ${String(error)}`)
})
// The answer is already being sent. Don't wait for it here, or Ctrl+C would
// be ignored until it is done.
pendingTelemetry = telemetry.flush()
// Upgrade. The caller drops or shows what was held.
const policy = context.experimental.agentUpgrade
if (action === 'update' && policy && !signal.aborted) {
return { policy, nudgeId }
}
// Skip, Ctrl+C, or closed: show what was held.
output.release()
return action === 'interrupt' && !signal.aborted ? 'interrupt' : null
}
// The menu closes right away when cancelled, but it may still be waiting on a
// network check before showing. Don't wait on that.
export async function closedUpgradeMenu(menu: Promise<unknown> | undefined) {
await Promise.race([menu, sleep(1000, undefined, { ref: false })])
}
// Before exiting, give the menu's telemetry a moment to finish sending.
export async function flushUpgradeTelemetry() {
await Promise.race([pendingTelemetry, sleep(1000, undefined, { ref: false })])
}
import { codeFrameColumns } from '../../shared/lib/errors/code-frame'
import { stdoutIsTerminal } from '../../lib/picocolors'
import type { StackFrame } from '../../server/lib/parse-stack'
import { ignoreListAnonymousStackFramesIfSandwiched as ignoreListAnonymousStackFramesIfSandwichedGeneric } from '../../server/lib/source-maps'
......@@ -72,8 +73,7 @@ export function ignoreListAnonymousStackFramesIfSandwiched(
export function getOriginalCodeFrame(
frame: IgnorableStackFrame,
source: string | null,
colorsOrOptions: boolean | CodeFrameRenderOptions = process.stdout?.isTTY ??
false
colorsOrOptions: boolean | CodeFrameRenderOptions = stdoutIsTerminal
): string | null {
if (!source || frame.line1 == null) {
return null
......@@ -83,7 +83,7 @@ export function getOriginalCodeFrame(
typeof colorsOrOptions === 'boolean'
? { colors: colorsOrOptions, maxWidth: undefined }
: {
colors: colorsOrOptions.colors ?? process.stdout?.isTTY ?? false,
colors: colorsOrOptions.colors ?? stdoutIsTerminal,
maxWidth: colorsOrOptions.maxWidth,
}
......
......@@ -254,27 +254,17 @@ export async function initialize(opts: {
}
)
if (process.env.NEXT_PRIVATE_UPGRADE_PROMPT === '1' && process.send) {
// The parent retries if the worker assessment rejects.
const [promptAssessment] = await Promise.allSettled([assessment])
// TODO: Do not block dev startup while prompting for an upgrade.
// Preserve all logs for display after the prompt and stop dev before Update.
// The existing dev worker pauses here while its parent owns the menu.
await new Promise<void>((resolve) => {
const resume = (message: {
nextUpgradeContinue: boolean | undefined
}) => {
if (message.nextUpgradeContinue) {
process.off('message', resume)
resolve()
}
// The CLI shows the menu; keep serving instead of waiting for it.
void Promise.allSettled([assessment]).then(([promptAssessment]) => {
if (process.connected) {
process.send!({
nextUpgradeContext: upgradeContext,
telemetryDisabled: process.env.NEXT_TELEMETRY_DISABLED,
...(promptAssessment.status === 'fulfilled'
? { nextUpgradeAssessment: promptAssessment.value }
: {}),
})
}
process.on('message', resume)
process.send!({
nextUpgradeContext: upgradeContext,
...(promptAssessment.status === 'fulfilled'
? { nextUpgradeAssessment: promptAssessment.value }
: {}),
})
})
} else {
// CI skips the DevTools assessment, but agents still need the nudge.
......
......@@ -18,7 +18,11 @@ import os from 'os'
import { exec } from 'child_process'
import * as Log from '../../build/output/log'
import setupDebug from 'next/dist/compiled/debug'
import { getMemoryRestartStats, RESTART_EXIT_CODE } from './utils'
import {
blockOnOutputWrites,
getMemoryRestartStats,
RESTART_EXIT_CODE,
} from './utils'
import { formatHostname } from './format-hostname'
import { initialize } from './router-server'
import {
......@@ -628,6 +632,10 @@ export async function startServer(
}
if (process.env.NEXT_PRIVATE_WORKER && process.send) {
// Output is piped to the CLI to hold it while the upgrade menu is open.
if (process.env.NEXT_PRIVATE_PROMPT_OUTPUT === '1') {
blockOnOutputWrites()
}
process.addListener('message', async (msg: any) => {
if (
msg &&
......
......@@ -391,3 +391,12 @@ export function getMaxOldSpaceSize() {
return parseInt(size, 10)
}
// For a child whose output is piped to its parent. Without this,
// process.exit() can drop the last lines written to the pipe. Node already
// does this for terminals.
export function blockOnOutputWrites() {
for (const stream of [process.stdout, process.stderr]) {
;(stream as any)._handle?.setBlocking?.(true)
}
}
......@@ -13,7 +13,7 @@ import {
import { parseStack, type StackFrame } from './lib/parse-stack'
import type { IgnorableStackFrame } from '../next-devtools/server/shared'
import { workUnitAsyncStorage } from './app-render/work-unit-async-storage.external'
import { dim, italic } from '../lib/picocolors'
import { dim, italic, stdoutIsTerminal } from '../lib/picocolors'
type FindSourceMapPayload = (
sourceURL: string
......@@ -75,7 +75,7 @@ export function setCodeFrameRenderer(renderer: CodeFrameRenderer): void {
function getOriginalCodeFrame(
frame: IgnorableStackFrame,
source: string | null,
colors: boolean = process.stdout.isTTY
colors: boolean = stdoutIsTerminal
): string | null {
const codeFrameRenderer = (globalThis as GlobalWithCodeFrameRenderer)[
CODE_FRAME_RENDERER
......
......@@ -13,9 +13,12 @@ export function codeFrameColumns(
location: NapiCodeFrameLocation,
options: NapiCodeFrameOptions = {}
): string | undefined {
// Default to the terminal width
// Default to the terminal width. The CLI passes it on when it pipes this
// process's output for the upgrade menu.
if (options.maxWidth === undefined) {
options.maxWidth = process.stdout.columns
options.maxWidth =
process.stdout.columns ??
(Number(process.env.NEXT_PRIVATE_TERMINAL_COLUMNS) || undefined)
}
return getBindingsSync().codeFrameColumns(file, location, options)
}
......
This diff is collapsed.
import { connection } from 'next/server'
export async function GET() {
// Request-time only, so a build never prerenders this route and exits.
await connection()
// Write to the stream and exit at once: output still in flight when the
// server dies must reach the terminal.
process.stderr.write(`${'x'.repeat(200 * 1024)}\n`)
process.stderr.write('UPGRADE_TERMINAL_CRASH_END\n')
process.exit(1)
}
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { connection } from 'next/server'
export async function GET(request: Request) {
// Request-time only: the log marks a request served while the menu is open.
await connection()
const n = new URL(request.url).searchParams.get('n')
console.log(`UPGRADE_TERMINAL_LOG ${n}`)
return new Response('ok')
}
export default function Page() {
// Logged at build time when prerendered, and per request in dev.
console.log('UPGRADE_TERMINAL_ROUTE_RENDERED')
return <p>hello world</p>
}
import { connection } from 'next/server'
export async function GET() {
// Request-time only: each dev server process answers with its own pid, so
// the test can tell when dev has restarted.
await connection()
return new Response(String(process.pid))
}
import { connection } from 'next/server'
export async function GET() {
// Request-time only: reports whether the dev server got the terminal as
// its stdin.
await connection()
return Response.json({ stdin: Boolean(process.stdin.isTTY) })
}
// Mark config-load output so the terminal test can verify the menu clears it.
console.log('UPGRADE_TERMINAL_CONFIG_LOADED')
module.exports = {
experimental: { agentUpgrade: 'security' },
}
// Preloaded with --require. Logs the event names Next.js sends to its
// telemetry endpoint and answers those requests without the network.
const fs = require('fs')
const realFetch = globalThis.fetch
globalThis.fetch = async (input, init) => {
const url = input instanceof Request ? input.url : String(input)
if (!url.startsWith('https://telemetry.nextjs.org/')) {
return realFetch(input, init)
}
for (const { eventName } of JSON.parse(init.body).events) {
fs.appendFileSync(
process.env.UPGRADE_TERMINAL_TELEMETRY_LOG,
`${eventName}\n`
)
}
return new Response(null, { status: 200 })
}
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