Unverified Commit a030ddc7 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Run AI upgrades with the latest official canary (#98870)

### Why?

`next@canary` can reuse a cached `canary.33` even after `canary.35` is
published, causing `next upgrade --ai` to run outdated upgrade logic.

### How?

Resolve the latest official canary from public npm, then reuse the
running CLI if it matches or launch and verify that exact version. Keep
pnpm installation errors visible so failed launches are diagnosable.

The lookup does not use package-manager registry, proxy, or CA settings:
public npm must be reachable, and installation can fail if a configured
mirror lacks the selected version.

<!-- NEXT_JS_LLM -->
parent 55b20af2
......@@ -8,12 +8,6 @@ const tools = dirname(fileURLToPath(import.meta.url))
const args = process.argv.slice(2)
const executable = join(tools, 'next/node_modules/next/dist/bin/next')
const assessment = join(tools, 'security/assessment.mjs')
const assessmentConfig = join(tools, 'security/assessment.json')
if (existsSync(assessmentConfig)) {
const { source } = JSON.parse(readFileSync(assessmentConfig, 'utf8'))
if (source) process.env.__NEXT_VERSION = source
}
appendFileSync(
join(tools, 'invocations.jsonl'),
JSON.stringify({
......@@ -46,7 +40,10 @@ if (args[0] === 'build' && existsSync(assessment)) {
if (existsSync(assessment)) await import(pathToFileURL(assessment).href)
if (args[0] === 'upgrade') {
process.env.__NEXT_UPGRADE_USE_CURRENT_CLI = '1'
const { version } = JSON.parse(
readFileSync(join(tools, 'next/node_modules/next/package.json'), 'utf8')
)
process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION = version
}
process.argv = [process.execPath, executable, ...args]
......
......@@ -2,6 +2,7 @@ import { spawn } from 'child_process'
import { cp, mkdir, mkdtemp, readFile, rm, writeFile } from 'fs/promises'
import { tmpdir } from 'os'
import { dirname, join } from 'path'
import { valid } from 'next/dist/compiled/semver'
import * as Log from '../build/output/log'
import createSpinner from '../build/spinner'
import { findDir } from '../lib/find-pages-dir'
......@@ -143,6 +144,31 @@ async function resolveAIUpgradeType(
: 'security'
}
async function resolveCanaryVersion(): Promise<string> {
try {
const response = await fetch('https://registry.npmjs.org/next/canary', {
signal: AbortSignal.timeout(10_000),
cache: 'no-store',
redirect: 'error',
})
if (!response.ok) {
throw new Error(`HTTP ${response.status}`)
}
const { version } = await response.json()
if (typeof version !== 'string' || valid(version) !== version) {
throw new Error('Invalid canary version')
}
return version
} catch (error) {
throw new Error('Could not fetch the latest Next.js canary from npm.', {
cause: error,
})
}
}
export async function spawnNextUpgrade(
directory: string | undefined,
options: NextUpgradeOptions
......@@ -151,33 +177,48 @@ export async function spawnNextUpgrade(
if (options.ai) {
try {
// A delegated canary uses itself. Local runs and evals use their invoked build.
const useCurrentCli = process.env.__NEXT_UPGRADE_USE_CURRENT_CLI === '1'
const expectedVersion = process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION
delete process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION
delete process.env.__NEXT_UPGRADE_USE_CURRENT_CLI
if (!useCurrentCli) {
Log.info(dim('Preparing upgrade...'))
const [command, ...runnerArgs] = getNpxCommand(baseDir).split(' ')
const aiArgument =
typeof options.ai === 'string' ? `--ai=${options.ai}` : '--ai'
const args = [
...runnerArgs,
'next@canary',
'upgrade',
baseDir,
aiArgument,
]
if (options.verbose) {
args.push('--verbose')
if (expectedVersion !== undefined) {
// Delegated upgrades and evals use their pinned CLI without another lookup.
if (process.env.__NEXT_VERSION !== expectedVersion) {
throw new Error(
`Expected Next.js ${expectedVersion} for the upgrade, but launched ${process.env.__NEXT_VERSION}.`
)
}
} else {
Log.info(dim('Preparing upgrade...'))
const canaryVersion = await resolveCanaryVersion()
if (process.env.__NEXT_VERSION !== canaryVersion) {
const [command, ...runnerArgs] = getNpxCommand(baseDir).split(' ')
const aiArgument =
typeof options.ai === 'string' ? `--ai=${options.ai}` : '--ai'
const args = [
...runnerArgs,
`next@${canaryVersion}`,
'upgrade',
baseDir,
aiArgument,
]
if (options.verbose) {
args.push('--verbose')
}
process.exitCode = await runChildProcess(command, args, {
cwd: baseDir,
stdio: 'inherit',
env: { ...process.env, __NEXT_UPGRADE_USE_CURRENT_CLI: '1' },
})
return
process.exitCode = await runChildProcess(command, args, {
cwd: baseDir,
stdio: 'inherit',
env: {
...process.env,
__NEXT_UPGRADE_EXPECTED_CLI_VERSION: canaryVersion,
// Older canaries recognize only this recursion guard.
__NEXT_UPGRADE_USE_CURRENT_CLI: '1',
},
})
return
}
}
// A workspace root must not launch an upgrade for an unspecified app.
......
......@@ -5,7 +5,7 @@ export function getNpxCommand(baseDir: string) {
const pkgManager = getPkgManager(baseDir)
let command = 'npx --yes'
if (pkgManager === 'pnpm') {
command = 'pnpm --silent dlx'
command = 'pnpm --loglevel=error dlx'
} else if (pkgManager === 'yarn') {
try {
execSync('yarn dlx --help', { stdio: 'ignore' })
......
......@@ -75,6 +75,7 @@ jest.mock('next/dist/telemetry/agent-name', () => ({
const createSpinner = require('next/dist/build/spinner').default as jest.Mock
const crossSpawn = require('next/dist/compiled/cross-spawn') as jest.Mock
const cliVersion: string = require('next/package.json').version
const restoreDescriptors: Array<() => void> = []
function normalizedBootstrapCalls(): string[][] {
......@@ -116,11 +117,16 @@ function overrideTTY(target: NodeJS.ReadStream | NodeJS.WriteStream): void {
describe('agentic upgrade prompts', () => {
const originalPath = process.env.PATH
const originalUseCurrentCli = process.env.__NEXT_UPGRADE_USE_CURRENT_CLI
const originalExpectedCliVersion =
process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION
const originalFetch = global.fetch
const originalExitCode = process.exitCode
beforeEach(() => {
jest.resetAllMocks()
process.env.__NEXT_UPGRADE_USE_CURRENT_CLI = '1'
process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION = cliVersion
global.fetch = jest.fn()
process.exitCode = undefined
jest.mocked(getProjectDir).mockReturnValue('/workspace/app')
......@@ -167,11 +173,170 @@ describe('agentic upgrade prompts', () => {
}
process.exitCode = originalExitCode
global.fetch = originalFetch
if (originalExpectedCliVersion === undefined) {
delete process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION
} else {
process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION =
originalExpectedCliVersion
}
while (restoreDescriptors.length > 0) {
restoreDescriptors.pop()?.()
}
})
it('uses the pinned CLI without looking up canary again', async () => {
jest.mocked(prepareUpgrade).mockResolvedValue({
status: 'unaffected',
reason: 'Already current.',
})
await spawnNextUpgrade('/workspace/app', {
revision: 'latest',
verbose: false,
ai: 'security',
})
expect(global.fetch).toHaveBeenCalledTimes(0)
expect(crossSpawn).toHaveBeenCalledTimes(0)
expect(prepareUpgrade).toHaveBeenCalledTimes(1)
expect(process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION).toBeUndefined()
expect(process.env.__NEXT_UPGRADE_USE_CURRENT_CLI).toBeUndefined()
})
it('rejects a worker running a different CLI version', async () => {
process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION = '0.0.0'
await spawnNextUpgrade('/workspace/app', {
revision: 'latest',
verbose: false,
ai: 'security',
})
expect(Log.error).toHaveBeenCalledWith(
'Could not prepare the upgrade:',
`Expected Next.js 0.0.0 for the upgrade, but launched ${cliVersion}.`
)
expect(process.exitCode).toBe(1)
expect(global.fetch).toHaveBeenCalledTimes(0)
expect(prepareUpgrade).toHaveBeenCalledTimes(0)
})
it.each([undefined, '1'])(
'reuses the current canary after checking npm with legacy guard %s',
async (legacyGuard) => {
delete process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION
if (legacyGuard === undefined) {
delete process.env.__NEXT_UPGRADE_USE_CURRENT_CLI
} else {
process.env.__NEXT_UPGRADE_USE_CURRENT_CLI = legacyGuard
}
jest
.mocked(global.fetch)
.mockResolvedValue(
new Response(JSON.stringify({ version: cliVersion }))
)
jest.mocked(prepareUpgrade).mockResolvedValue({
status: 'unaffected',
reason: 'Already current.',
})
await spawnNextUpgrade('/workspace/app', {
revision: 'latest',
verbose: false,
ai: 'security',
})
expect(global.fetch).toHaveBeenCalledTimes(1)
expect(global.fetch).toHaveBeenCalledWith(
'https://registry.npmjs.org/next/canary',
expect.objectContaining({
signal: expect.any(AbortSignal),
cache: 'no-store',
})
)
expect(crossSpawn).toHaveBeenCalledTimes(0)
expect(prepareUpgrade).toHaveBeenCalledTimes(1)
}
)
it.each([true, 'security', 'latest', 'future'])(
'delegates %s to the exact canary and preserves its failure status',
async (ai) => {
delete process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION
const version = '99.0.0-canary.35'
jest
.mocked(global.fetch)
.mockResolvedValue(new Response(JSON.stringify({ version })))
crossSpawn.mockImplementation(() => {
const child = new EventEmitter()
process.nextTick(() => child.emit('close', 42, null))
return child
})
await spawnNextUpgrade('/workspace/app', {
revision: 'latest',
verbose: true,
ai,
})
expect(global.fetch).toHaveBeenCalledTimes(1)
expect(crossSpawn).toHaveBeenCalledTimes(1)
expect(crossSpawn).toHaveBeenCalledWith(
'npx',
[
`next@${version}`,
'upgrade',
'/workspace/app',
ai === true ? '--ai' : `--ai=${ai}`,
'--verbose',
],
expect.objectContaining({
cwd: '/workspace/app',
stdio: 'inherit',
env: expect.objectContaining({
__NEXT_UPGRADE_EXPECTED_CLI_VERSION: version,
__NEXT_UPGRADE_USE_CURRENT_CLI: '1',
}),
})
)
expect(process.exitCode).toBe(42)
expect(prepareUpgrade).toHaveBeenCalledTimes(0)
}
)
it.each([
['HTTP error', () => Promise.resolve(new Response('', { status: 503 }))],
['network error', () => Promise.reject(new TypeError('fetch failed'))],
[
'timeout',
() => Promise.reject(new DOMException('Timed out', 'TimeoutError')),
],
['invalid JSON', () => Promise.resolve(new Response('invalid'))],
['missing version', () => Promise.resolve(new Response('{}'))],
[
'invalid version',
() => Promise.resolve(new Response('{"version":"canary"}')),
],
] as const)('stops before upgrading on %s', async (_name, response) => {
delete process.env.__NEXT_UPGRADE_EXPECTED_CLI_VERSION
jest.mocked(global.fetch).mockImplementation(response)
await spawnNextUpgrade('/workspace/app', {
revision: 'latest',
verbose: false,
ai: 'security',
})
expect(Log.error).toHaveBeenCalledWith(
'Could not prepare the upgrade:',
'Could not fetch the latest Next.js canary from npm.'
)
expect(process.exitCode).toBe(1)
expect(crossSpawn).toHaveBeenCalledTimes(0)
expect(prepareUpgrade).toHaveBeenCalledTimes(0)
})
it('uses the planned multiple-agent question and choices', async () => {
delete process.env.__NEXT_UPGRADE_USE_CURRENT_CLI
process.env.PATH = '/agents'
......
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