mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-27 19:41:44 -04:00
* fix: handle verifyDepsBeforeRun prompt in non-TTY environments In non-interactive environments like CI, verifyDepsBeforeRun: 'prompt' would silently exit with code 0 even when node_modules were out of sync. This could cause tests to pass when they should fail. Now, pnpm throws an error in non-TTY environments, alerting users that they need to run 'pnpm install' first. Also handles Ctrl+C gracefully during the prompt - exits cleanly without showing a stack trace. Fixes #10889 Fixes #10888 * fix: improve Ctrl+C handling and fix prompt TTY guard - Replace brittle ERR_USE_AFTER_CLOSE check with generic catch for prompt cancellation (enquirer rejects with empty string on Ctrl+C, not that error) - Fix prompt test to mock isTTY=true since Jest runs in non-TTY environment - Fix test description "noTTY" → "non-TTY" Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: narrow try/catch to only wrap enquirer.prompt Avoid catching errors from install() which should propagate normally. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: zubeyralmaho <zubeyralmaho@users.noreply.github.com> Co-authored-by: Zoltan Kochan <z@kochan.io> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
81 lines
3.1 KiB
TypeScript
81 lines
3.1 KiB
TypeScript
import type { VerifyDepsBeforeRun } from '@pnpm/config'
|
|
import { checkDepsStatus, type CheckDepsStatusOptions, type WorkspaceStateSettings } from '@pnpm/deps.status'
|
|
import { PnpmError } from '@pnpm/error'
|
|
import { runPnpmCli } from '@pnpm/exec.pnpm-cli-runner'
|
|
import { globalWarn } from '@pnpm/logger'
|
|
import enquirer from 'enquirer'
|
|
|
|
export interface RunDepsStatusCheckOptions extends CheckDepsStatusOptions {
|
|
dir: string
|
|
verifyDepsBeforeRun?: VerifyDepsBeforeRun
|
|
}
|
|
|
|
export async function runDepsStatusCheck (opts: RunDepsStatusCheckOptions): Promise<void> {
|
|
// the following flags are always the default values during `pnpm run` and `pnpm exec`,
|
|
// so they may not match the workspace state after `pnpm install --production|--no-optional`
|
|
const ignoredWorkspaceStateSettings = ['dev', 'optional', 'production'] satisfies Array<keyof WorkspaceStateSettings>
|
|
opts.ignoredWorkspaceStateSettings = ignoredWorkspaceStateSettings
|
|
|
|
const { upToDate, issue, workspaceState } = await checkDepsStatus(opts)
|
|
if (upToDate) return
|
|
|
|
const command = ['install', ...createInstallArgs(workspaceState?.settings)]
|
|
const install = runPnpmCli.bind(null, command, { cwd: opts.dir })
|
|
|
|
switch (opts.verifyDepsBeforeRun) {
|
|
case 'install':
|
|
install()
|
|
break
|
|
case 'prompt': {
|
|
// In non-TTY environments (like CI), we can't prompt the user
|
|
// Exit with error to alert users that node_modules are out of sync
|
|
if (!process.stdin.isTTY) {
|
|
throw new PnpmError('VERIFY_DEPS_BEFORE_RUN', issue ?? 'Your node_modules are out of sync with your lockfile', {
|
|
hint: 'Run "pnpm install" before running scripts. The "verifyDepsBeforeRun: prompt" setting cannot prompt for confirmation in non-interactive environments.',
|
|
})
|
|
}
|
|
let confirmed: { runInstall: boolean }
|
|
try {
|
|
confirmed = await enquirer.prompt<{ runInstall: boolean }>({
|
|
type: 'confirm',
|
|
name: 'runInstall',
|
|
message: `Your "node_modules" directory is out of sync with the "pnpm-lock.yaml" file. This can lead to issues during scripts execution.
|
|
|
|
Would you like to run "pnpm ${command.join(' ')}" to update your "node_modules"?`,
|
|
initial: true,
|
|
})
|
|
} catch {
|
|
// User cancelled the prompt (e.g. Ctrl+C) — exit immediately
|
|
// so the caller doesn't proceed to run the script.
|
|
process.exit(1)
|
|
}
|
|
if (confirmed.runInstall) {
|
|
install()
|
|
}
|
|
break
|
|
}
|
|
case 'error':
|
|
throw new PnpmError('VERIFY_DEPS_BEFORE_RUN', issue ?? 'Your node_modules are out of sync with your lockfile', {
|
|
hint: 'Run "pnpm install"',
|
|
})
|
|
case 'warn':
|
|
globalWarn(`Your node_modules are out of sync with your lockfile. ${issue}`)
|
|
break
|
|
}
|
|
}
|
|
|
|
export function createInstallArgs (opts: Pick<WorkspaceStateSettings, 'dev' | 'optional' | 'production'> | undefined): string[] {
|
|
const args: string[] = []
|
|
if (!opts) return args
|
|
const { dev, optional, production } = opts
|
|
if (production && !dev) {
|
|
args.push('--production')
|
|
} else if (dev && !production) {
|
|
args.push('--dev')
|
|
}
|
|
if (!optional) {
|
|
args.push('--no-optional')
|
|
}
|
|
return args
|
|
}
|