Skip to content

Commit baa049a

Browse files
await-ovozkochan
andauthored
feat: throw error when signal kill child process (#39)
close pnpm/pnpm#5525 Co-authored-by: Zoltan Kochan <[email protected]>
1 parent 9393912 commit baa049a

File tree

5 files changed

+121
-12
lines changed

5 files changed

+121
-12
lines changed

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const chain = require('slide').chain
1212
const uidNumber = require('uid-number')
1313
const umask = require('umask')
1414
const byline = require('@pnpm/byline')
15+
const { PnpmError } = require('@pnpm/error')
1516
const resolveFrom = require('resolve-from')
1617
const { PassThrough } = require('stream')
1718
const extendPath = require('./lib/extendPath')
@@ -276,13 +277,15 @@ function runCmd_ (cmd, pkg, env, wd, opts, stage, unsafe, uid, gid, cb_) {
276277
proc.on('error', procError)
277278
proc.on('close', (code, signal) => {
278279
opts.log.silly('lifecycle', logid(pkg, stage), 'Returned: code:', code, ' signal:', signal)
280+
let err
279281
if (signal) {
282+
err = new PnpmError('CHILD_PROCESS_FAILED', `Command failed with signal "${signal}"`)
280283
process.kill(process.pid, signal)
281284
} else if (code) {
282-
var er = new Error(`Exit status ${code}`)
283-
er.errno = code
285+
err = new PnpmError('CHILD_PROCESS_FAILED', `Exit status ${code}`)
286+
err.errno = code
284287
}
285-
procError(er)
288+
procError(err)
286289
})
287290
byline(proc.stdout).on('data', data => {
288291
opts.log.verbose('lifecycle', logid(pkg, stage), 'stdout', data.toString())

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"homepage": "https://github.com/pnpm/npm-lifecycle#readme",
3434
"dependencies": {
3535
"@pnpm/byline": "^1.0.0",
36+
"@pnpm/error": "^4.0.0",
3637
"@yarnpkg/shell": "3.2.0-rc.8",
3738
"node-gyp": "^8.4.1",
3839
"resolve-from": "^5.0.0",

pnpm-lock.yaml

Lines changed: 78 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/count-to-10/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "count-to-10",
33
"version": "1.0.0",
44
"scripts": {
5-
"postinstall": "node postinstall"
5+
"postinstall": "node postinstall",
6+
"signal-exit": "echo 'signal-exit script' && kill -s ABRT $$"
67
}
78
}

test/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,37 @@ test('makeEnv', function (t) {
159159
t.equal('--inspect-brk --abort-on-uncaught-exception', env.NODE_OPTIONS, 'nodeOptions sets NODE_OPTIONS')
160160
t.end()
161161
})
162+
163+
test('throw error signal kills child', async function (t) {
164+
const fixture = path.join(__dirname, 'fixtures', 'count-to-10')
165+
166+
const verbose = sinon.spy()
167+
const silly = sinon.spy()
168+
169+
const stubProcessExit = sinon.stub(process, 'kill').callsFake(noop)
170+
171+
const log = {
172+
level: 'silent',
173+
info: noop,
174+
warn: noop,
175+
silly,
176+
verbose,
177+
pause: noop,
178+
resume: noop,
179+
clearProgress: noop,
180+
showProgress: noop
181+
}
182+
183+
const dir = fixture
184+
const pkg = require(path.join(fixture, 'package.json'))
185+
186+
t.rejects(async () => {
187+
await lifecycle(pkg, 'signal-exit', fixture, {
188+
stdio: 'pipe',
189+
log,
190+
dir,
191+
config: {}
192+
})
193+
stubProcessExit.restore()
194+
})
195+
})

0 commit comments

Comments
 (0)