Skip to content

Commit

Permalink
feat: throw error when signal kill child process (#39)
Browse files Browse the repository at this point in the history
close pnpm/pnpm#5525
Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
await-ovo committed Nov 30, 2022
1 parent 9393912 commit baa049a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 12 deletions.
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()
})
})

0 comments on commit baa049a

Please sign in to comment.