Skip to content
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

feat: throw error when signal kill child process #39

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions index.js
Expand Up @@ -12,6 +12,7 @@ const chain = require('slide').chain
const uidNumber = require('uid-number')
const umask = require('umask')
const byline = require('@pnpm/byline')
const { PnpmError } = require('@pnpm/error')
const resolveFrom = require('resolve-from')
const { PassThrough } = require('stream')
const extendPath = require('./lib/extendPath')
Expand Down Expand Up @@ -276,13 +277,15 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
proc.on('error', procError)
proc.on('close', (code, signal) => {
opts.log.silly('lifecycle', logid(pkg, stage), 'Returned: code:', code, ' signal:', signal)
let err
if (signal) {
err = new PnpmError('CHILD_PROCESS_FAILED', `Command failed with signal "${signal}"`)
process.kill(process.pid, signal)
} else if (code) {
var er = new Error(`Exit status ${code}`)
er.errno = code
err = new PnpmError('CHILD_PROCESS_FAILED', `Exit status ${code}`)
err.errno = code
}
procError(er)
procError(err)
})
byline(proc.stdout).on('data', data => {
opts.log.verbose('lifecycle', logid(pkg, stage), 'stdout', data.toString())
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -33,6 +33,7 @@
"homepage": "https://github.com/pnpm/npm-lifecycle#readme",
"dependencies": {
"@pnpm/byline": "^1.0.0",
"@pnpm/error": "^4.0.0",
"@yarnpkg/shell": "3.2.0-rc.8",
"node-gyp": "^8.4.1",
"resolve-from": "^5.0.0",
Expand Down
86 changes: 78 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/fixtures/count-to-10/package.json
Expand Up @@ -2,6 +2,7 @@
"name": "count-to-10",
"version": "1.0.0",
"scripts": {
"postinstall": "node postinstall"
"postinstall": "node postinstall",
"signal-exit": "echo 'signal-exit script' && kill -s ABRT $$"
}
}
34 changes: 34 additions & 0 deletions test/index.js
Expand Up @@ -159,3 +159,37 @@ test('makeEnv', function (t) {
t.equal('--inspect-brk --abort-on-uncaught-exception', env.NODE_OPTIONS, 'nodeOptions sets NODE_OPTIONS')
t.end()
})

test('throw error signal kills child', async function (t) {
const fixture = path.join(__dirname, 'fixtures', 'count-to-10')

const verbose = sinon.spy()
const silly = sinon.spy()

const stubProcessExit = sinon.stub(process, 'kill').callsFake(noop)

const log = {
level: 'silent',
info: noop,
warn: noop,
silly,
verbose,
pause: noop,
resume: noop,
clearProgress: noop,
showProgress: noop
}

const dir = fixture
const pkg = require(path.join(fixture, 'package.json'))

t.rejects(async () => {
await lifecycle(pkg, 'signal-exit', fixture, {
stdio: 'pipe',
log,
dir,
config: {}
})
stubProcessExit.restore()
})
})