| |
| |
| |
| |
| @@ -18,6 +18,7 @@ import { |
| } from '../lib/handle-package' |
| import { runTransform } from './transform' |
| import { onCancel, TRANSFORMER_INQUIRER_CHOICES } from '../lib/utils' |
| +import { refreshAgentRulesBlock } from '../lib/agents-md' |
| import { BadInput } from './shared' |
| |
| type PackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun' |
| @@ -476,6 +477,16 @@ export async function runUpgrade( |
| console.log(`${pc.green('✔')} Codemods have been applied successfully.`) |
| } |
| |
| + try { |
| + if (refreshAgentRulesBlock(cwd) === 'refreshed') { |
| + console.log( |
| + `${pc.green('✔')} Refreshed the managed agent-rules block in AGENTS.md / CLAUDE.md to match the upgraded Next.js.` |
| + ) |
| + } |
| + } catch { |
| + // The block refresh is best-effort — never fail the upgrade over it. |
| + } |
| + |
| warnDependenciesOutOfRange(appPackageJson, versionMapping) |
| |
| endMessage(targetNextVersion) |
| |
| |
| |
| |
| @@ -15,6 +15,54 @@ interface NextjsVersionResult { |
| error?: string |
| } |
| |
| +const AGENT_RULES_START_MARKER = '<!-- BEGIN:nextjs-agent-rules -->' |
| + |
| +/** |
| + * After an upgrade, refresh the managed agent-rules block in |
| + * AGENTS.md / CLAUDE.md so its content matches the Next.js version |
| + * that is now installed. |
| + * |
| + * Delegates to the installed package's own generator |
| + * (`next/dist/server/lib/generate-agent-files`), so the block text is |
| + * always the one shipped with that version — this codemod never |
| + * carries its own copy. Returns `'refreshed'` when a file was |
| + * rewritten, `'current'` when the block was already up to date, and |
| + * `'skipped'` when there is nothing to do: the project never adopted |
| + * the managed block, or the installed Next.js predates the generator |
| + * (< 16.3). |
| + */ |
| +export function refreshAgentRulesBlock( |
| + cwd: string |
| +): 'refreshed' | 'current' | 'skipped' { |
| + const hostsBlock = ['AGENTS.md', 'CLAUDE.md'].some((file) => { |
| + try { |
| + return fs |
| + .readFileSync(path.join(cwd, file), 'utf-8') |
| + .includes(AGENT_RULES_START_MARKER) |
| + } catch { |
| + return false |
| + } |
| + }) |
| + if (!hostsBlock) return 'skipped' |
| + |
| + let writeAgentFiles: (dir: string) => { agentsMd: string; claudeMd: string } |
| + try { |
| + const generatorPath = require.resolve( |
| + 'next/dist/server/lib/generate-agent-files', |
| + { paths: [cwd] } |
| + ) |
| + writeAgentFiles = require(generatorPath).writeAgentFiles |
| + if (typeof writeAgentFiles !== 'function') return 'skipped' |
| + } catch { |
| + return 'skipped' |
| + } |
| + |
| + const result = writeAgentFiles(cwd) |
| + return result.agentsMd === 'updated' || result.claudeMd === 'updated' |
| + ? 'refreshed' |
| + : 'current' |
| +} |
| + |
| export function getNextjsVersion(cwd: string): NextjsVersionResult { |
| try { |
| const nextPkgPath = require.resolve('next/package.json', { paths: [cwd] }) |
| |
| |
| |
| |
| @@ -7,7 +7,7 @@ import { experimentalSchema } from '../config-schema' |
| import { detectAgent } from '../../telemetry/detect-agent' |
| import { bundlerName, getBundlerFromEnv } from '../../lib/bundler' |
| import { |
| - hasAgentRulesInstalled, |
| + hasCurrentAgentRules, |
| writeAgentFiles, |
| type AgentFilesResult, |
| } from './generate-agent-files' |
| @@ -117,17 +117,18 @@ export function logExperimentalInfo({ |
| |
| /** |
| * When `next dev` detects an AI coding agent but the managed |
| - * agent-rules block is missing from AGENTS.md / CLAUDE.md, |
| - * auto-generate the files so the agent has access to version-matched |
| - * docs. Returns the write result when files were generated, or `null` |
| - * when no action was needed. |
| + * agent-rules block is missing from AGENTS.md / CLAUDE.md — or an |
| + * outdated version of it is installed — auto-generate or refresh the |
| + * files so the agent has access to version-matched docs. Returns the |
| + * write result when files were touched, or `null` when no action was |
| + * needed. |
| * |
| * Callers gate this on `config.agentRules !== false` — opt-out is |
| * declarative in next.config, not inside this function. |
| */ |
| export function ensureAgentRulesForDev(dir: string): AgentFilesResult | null { |
| if (detectAgent() |
| - if (hasAgentRulesInstalled(dir)) return null |
| + if (hasCurrentAgentRules(dir)) return null |
| |
| return writeAgentFiles(dir) |
| } |
| |
| |
| |
| |
| @@ -41,16 +41,33 @@ export interface AgentFilesResult { |
| } |
| |
| /** |
| - * Returns true when `AGENTS.md` or `CLAUDE.md` at `dir` contains the |
| - * managed agent-rules marker. |
| + * Returns the managed block (markers included) found in `content`, or |
| + * `null` when the markers are absent or malformed. |
| */ |
| -export function hasAgentRulesInstalled(dir: string): boolean { |
| - const agentsContent = tryReadFile(path.join(dir, 'AGENTS.md')) |
| - if (agentsContent?.includes(AGENT_RULES_START_MARKER)) return true |
| - |
| - const claudeContent = tryReadFile(path.join(dir, 'CLAUDE.md')) |
| - if (claudeContent?.includes(AGENT_RULES_START_MARKER)) return true |
| +function extractAgentRulesBlock(content: string): string | null { |
| + const start = content.indexOf(AGENT_RULES_START_MARKER) |
| + if (start === -1) return null |
| + const end = content.indexOf(AGENT_RULES_END_MARKER, start) |
| + if (end === -1) return null |
| + return content.slice(start, end + AGENT_RULES_END_MARKER.length) |
| +} |
| |
| +/** |
| + * Returns true when `AGENTS.md` or `CLAUDE.md` at `dir` already |
| + * contains the current agent-rules block. A block from an earlier |
| + * Next.js version (older wording, legacy markers) returns false so |
| + * callers know to upsert the current one over it. |
| + */ |
| +export function hasCurrentAgentRules(dir: string): boolean { |
| + const block = buildAgentRulesBlock() |
| + for (const file of ['AGENTS.md', 'CLAUDE.md']) { |
| + const content = tryReadFile(path.join(dir, file)) |
| + if (!content) continue |
| + const installed = extractAgentRulesBlock(content) |
| + if (installed !== null && normalizeEol(installed, '\n') === block) { |
| + return true |
| + } |
| + } |
| return false |
| } |
| |
| @@ -58,6 +75,8 @@ export function hasAgentRulesInstalled(dir: string): boolean { |
| * Write the agent-rules block into `projectDir`, respecting whichever |
| * file the user already uses: |
| * |
| + * - A file already hosting the managed block → upsert into it, so |
| + * upgrades rewrite the block in place instead of adding a copy. |
| * - `AGENTS.md` exists → upsert into it, leave `CLAUDE.md` alone. |
| * - `CLAUDE.md` exists (but not `AGENTS.md`) → upsert into it. |
| * - Neither exists → create both (`AGENTS.md` + `CLAUDE.md` with |
| @@ -74,7 +93,14 @@ export function writeAgentFiles(projectDir: string): AgentFilesResult { |
| const agentsMdExists = fs.existsSync(agentsMdPath) |
| const claudeMdExists = fs.existsSync(claudeMdPath) |
| |
| - if (agentsMdExists) { |
| + const claudeMdHostsBlock = |
| + claudeMdExists && |
| + (tryReadFile(claudeMdPath)?.includes(AGENT_RULES_START_MARKER) ?? false) |
| + const agentsMdHostsBlock = |
| + agentsMdExists && |
| + (tryReadFile(agentsMdPath)?.includes(AGENT_RULES_START_MARKER) ?? false) |
| + |
| + if (agentsMdExists && (agentsMdHostsBlock || !claudeMdHostsBlock)) { |
| return { |
| agentsMd: upsertFile(agentsMdPath, block), |
| claudeMd: 'skipped', |
|
|