fix(core): improve no-TTY modules purge hint (#10973)

* fix(core): clarify non-interactive modules purge guidance

Add confirmModulesPurge=false workaround to the no-TTY error hint and
add a regression test for the non-interactive purge confirmation flow.

Refs #6778

* test(core): assert non-tty purge hint on error hint field

---------

Co-authored-by: zubeyralmaho <zubeyralmaho@users.noreply.github.com>
This commit is contained in:
zybo
2026-03-17 02:14:05 +03:00
committed by GitHub
parent 53c780bfc3
commit 672e58c285
3 changed files with 38 additions and 1 deletions

View File

@@ -0,0 +1,10 @@
---
"@pnpm/core": patch
"pnpm": patch
---
Improve the non-interactive modules purge error hint to include the `confirmModulesPurge=false` workaround.
When pnpm needs to recreate `node_modules` but no TTY is available, the error now suggests either setting `CI=true` or disabling the purge confirmation prompt via `confirmModulesPurge=false`.
Adds a regression test for the non-TTY flow.

View File

@@ -151,7 +151,7 @@ async function purgeModulesDirsOfImporters (
if (opts.confirmModulesPurge ?? true) {
if (!process.stdin.isTTY) {
throw new PnpmError('ABORTED_REMOVE_MODULES_DIR_NO_TTY', 'Aborted removal of modules directory due to no TTY', {
hint: 'If you are running pnpm in CI, set the CI environment variable to "true".',
hint: 'If you are running pnpm in CI, set the CI environment variable to "true", or set "confirmModulesPurge" to "false".',
})
}
const confirmed = await enquirer.prompt<{ question: boolean }>({

View File

@@ -1,5 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'
import util from 'node:util'
import { WANTED_LOCKFILE } from '@pnpm/constants'
import { addDependenciesToPackage, install } from '@pnpm/core'
@@ -102,6 +103,32 @@ test('do not fail on non-compatible store when forced during named installation'
})
})
test('fail fast with actionable hint on non-TTY when modules purge needs confirmation', async () => {
prepareEmpty()
const opts = testDefaults()
await saveModulesYaml('0.50.0', opts.storeDir)
const originalIsTTY = process.stdin.isTTY
Object.defineProperty(process.stdin, 'isTTY', { value: false, configurable: true })
let err: unknown
try {
await install({}, opts)
} catch (_err: unknown) {
err = _err
} finally {
Object.defineProperty(process.stdin, 'isTTY', { value: originalIsTTY, configurable: true })
}
expect(util.types.isNativeError(err)).toBeTruthy()
if (util.types.isNativeError(err)) {
expect('code' in err && err.code).toBe('ERR_PNPM_ABORTED_REMOVE_MODULES_DIR_NO_TTY')
expect(err.message).toContain('no TTY')
expect('hint' in err && typeof err.hint === 'string' && err.hint).toContain('confirmModulesPurge')
}
})
async function saveModulesYaml (pnpmVersion: string, storeDir: string) {
fs.mkdirSync('node_modules')
fs.writeFileSync('node_modules/.modules.yaml', `packageManager: pnpm@${pnpmVersion}\nstoreDir: ${storeDir}`)