-
Notifications
You must be signed in to change notification settings - Fork 108
feat(cli): locks for agents running dev and build commands #1265
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
Open
harlan-zw
wants to merge
5
commits into
main
Choose a base branch
from
feat/dev-server-lockfile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c545543
feat(cli): add lock file for dev and build commands
harlan-zw 0a8c359
fix: unexport LockInfo interface to satisfy knip
harlan-zw 6ac1f44
fix: make lockfile test platform-aware for Windows kill command
harlan-zw fabedc5
feat: add NUXT_IGNORE_LOCK env var to bypass lock check
harlan-zw e34082b
fix: address CodeRabbit review feedback
harlan-zw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs' | ||
| import { mkdir } from 'node:fs/promises' | ||
| import process from 'node:process' | ||
|
|
||
| import { dirname, join } from 'pathe' | ||
| import { isAgent } from 'std-env' | ||
|
|
||
| interface LockInfo { | ||
| pid: number | ||
| startedAt: number | ||
| command: 'dev' | 'build' | ||
| port?: number | ||
| hostname?: string | ||
| url?: string | ||
| } | ||
|
|
||
| const LOCK_FILENAME = 'nuxt.lock' | ||
|
|
||
| function isProcessAlive(pid: number): boolean { | ||
| try { | ||
| process.kill(pid, 0) | ||
| return true | ||
| } | ||
| catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function readLockFile(lockPath: string): LockInfo | undefined { | ||
| try { | ||
| const content = readFileSync(lockPath, 'utf-8') | ||
| return JSON.parse(content) as LockInfo | ||
| } | ||
| catch { | ||
| return undefined | ||
| } | ||
| } | ||
|
|
||
| function isLockEnabled(): boolean { | ||
| return isAgent && !process.env.NUXT_IGNORE_LOCK | ||
| } | ||
|
|
||
| /** | ||
| * Check if a Nuxt process is already running for this project. | ||
| * Only active when running inside an AI agent environment. | ||
| * Set NUXT_IGNORE_LOCK=1 to bypass. | ||
| * Stale lock files (from crashed processes) are automatically cleaned up. | ||
| */ | ||
| export function checkLock(buildDir: string): LockInfo | undefined { | ||
| if (!isLockEnabled()) { | ||
| return undefined | ||
| } | ||
|
|
||
| const lockPath = join(buildDir, LOCK_FILENAME) | ||
|
|
||
| if (!existsSync(lockPath)) { | ||
| return undefined | ||
| } | ||
|
|
||
| const info = readLockFile(lockPath) | ||
| if (!info) { | ||
| try { | ||
| unlinkSync(lockPath) | ||
| } | ||
| catch {} | ||
| return undefined | ||
| } | ||
|
|
||
| if (!isProcessAlive(info.pid)) { | ||
| try { | ||
| unlinkSync(lockPath) | ||
| } | ||
| catch {} | ||
| return undefined | ||
| } | ||
|
|
||
| // Don't block ourselves (fork pool scenario) | ||
| if (info.pid === process.pid) { | ||
| return undefined | ||
| } | ||
|
|
||
| return info | ||
| } | ||
|
|
||
| /** | ||
| * Write a lock file atomically. Returns a cleanup function. | ||
| * Only writes when running inside an AI agent environment. | ||
| * Uses exclusive file creation (`wx` flag) to prevent race conditions. | ||
| */ | ||
| export async function writeLock(buildDir: string, info: LockInfo): Promise<() => void> { | ||
| const noop = () => {} | ||
| if (!isLockEnabled()) { | ||
| return noop | ||
| } | ||
|
|
||
| const lockPath = join(buildDir, LOCK_FILENAME) | ||
|
|
||
| await mkdir(dirname(lockPath), { recursive: true }) | ||
|
|
||
| try { | ||
| writeFileSync(lockPath, JSON.stringify(info, null, 2), { flag: 'wx' }) | ||
| } | ||
| catch (error) { | ||
| // Lock already exists, another process won the race | ||
| if ((error as NodeJS.ErrnoException).code === 'EEXIST') { | ||
| return noop | ||
| } | ||
| throw error | ||
| } | ||
|
|
||
| let cleaned = false | ||
| const exitHandler = () => cleanup() | ||
| const signalHandlers: Array<[string, () => void]> = [] | ||
|
|
||
| function cleanup() { | ||
| if (cleaned) | ||
| return | ||
| cleaned = true | ||
| process.off('exit', exitHandler) | ||
| for (const [signal, handler] of signalHandlers) { | ||
| process.off(signal, handler) | ||
| } | ||
| try { | ||
| unlinkSync(lockPath) | ||
| } | ||
| catch {} | ||
| } | ||
|
|
||
| process.on('exit', exitHandler) | ||
| for (const signal of ['SIGTERM', 'SIGINT', 'SIGQUIT', 'SIGHUP'] as const) { | ||
| const handler = () => { | ||
| cleanup() | ||
| process.exit() | ||
| } | ||
| signalHandlers.push([signal, handler]) | ||
| process.once(signal, handler) | ||
| } | ||
|
|
||
| return cleanup | ||
| } | ||
|
|
||
| /** | ||
| * Format an error message when a Nuxt process is already running. | ||
| * Designed to be actionable for both humans and LLM agents. | ||
| */ | ||
| export function formatLockError(info: LockInfo, cwd: string): string { | ||
| const isWindows = process.platform === 'win32' | ||
| const killCmd = isWindows ? `taskkill /PID ${info.pid} /F` : `kill ${info.pid}` | ||
| const label = info.command === 'dev' ? 'dev server' : 'build' | ||
|
|
||
| const lines = [ | ||
| '', | ||
| `Another Nuxt ${label} is already running:`, | ||
| '', | ||
| ] | ||
|
|
||
| if (info.url) { | ||
| lines.push(` URL: ${info.url}`) | ||
| } | ||
| lines.push(` PID: ${info.pid}`) | ||
| lines.push(` Dir: ${cwd}`) | ||
| lines.push(` Started: ${new Date(info.startedAt).toLocaleString()}`) | ||
| lines.push('') | ||
|
|
||
| if (info.command === 'dev' && info.url) { | ||
| lines.push(`Run \`${killCmd}\` to stop it, or connect to ${info.url}`) | ||
| } | ||
| else { | ||
| lines.push(`Run \`${killCmd}\` to stop it.`) | ||
| } | ||
| lines.push(`Set NUXT_IGNORE_LOCK=1 to bypass this check.`) | ||
| lines.push('') | ||
|
|
||
| return lines.join('\n') | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.