Skip to content

Commit

Permalink
fix: kill other running tasks on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
s-h-a-d-o-w committed Mar 13, 2022
1 parent 531275c commit 0402ef7
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 8,781 deletions.
29 changes: 26 additions & 3 deletions lib/resolveTaskFn.js
Expand Up @@ -2,14 +2,17 @@ import { redBright, dim } from 'colorette'
import execa from 'execa'
import debug from 'debug'
import { parseArgsStringToArgv } from 'string-argv'
import pidTree from 'pidtree'

import { error, info } from './figures.js'
import { getInitialState } from './state.js'
import { TaskError } from './symbols.js'

export const FAIL_CHECK_INTERVAL = 200

const debugLog = debug('lint-staged:resolveTaskFn')

const getTag = ({ code, killed, signal }) => signal || (killed && 'KILLED') || code || 'FAILED'
const getTag = ({ code, killed, signal }) => (killed && 'KILLED') || signal || code || 'FAILED'

/**
* Handle task console output.
Expand Down Expand Up @@ -101,9 +104,29 @@ export const resolveTaskFn = ({
debugLog('execaOptions:', execaOptions)

return async (ctx = getInitialState()) => {
const result = await (shell
let isExecutionDone = false
const execaChildProcess = shell
? execa.command(isFn ? command : `${command} ${files.join(' ')}`, execaOptions)
: execa(cmd, isFn ? args : args.concat(files), execaOptions))
: execa(cmd, isFn ? args : args.concat(files), execaOptions)

const interruptExecutionOnError = async () => {
if (!isExecutionDone) {
if (ctx.errors.size > 0) {
const ids = await pidTree(execaChildProcess.pid)
ids.forEach(process.kill)

// The execa process is killed separately in order
// to get the `KILLED` status.
execaChildProcess.kill()
} else {
setTimeout(interruptExecutionOnError, FAIL_CHECK_INTERVAL)
}
}
}
interruptExecutionOnError()

const result = await execaChildProcess
isExecutionDone = true

if (result.failed || result.killed || result.signal != null) {
throw makeErr(command, result, ctx)
Expand Down

0 comments on commit 0402ef7

Please sign in to comment.