Skip to content

Commit 33d4502

Browse files
Adrian-Baran-GYiiroj
authored andcommitted
fix: run all tasks when --continue-on-error=true
* Fixes #1686
1 parent 54ba9eb commit 33d4502

File tree

6 files changed

+67
-17
lines changed

6 files changed

+67
-17
lines changed

.changeset/cool-sites-taste.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'lint-staged': patch
3+
---
4+
5+
Fix problems with `--continue-on-error` option, where tasks might have still been killed (`SIGINT`) when one of them failed.

lib/getSpawnedTask.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export const makeErr = (command, error, ctx) => {
129129
* @param {Object} options
130130
* @param {boolean} [options.color]
131131
* @param {string} options.command — Linter task
132+
* @param {string} [options.continueOnError]
132133
* @param {string} [options.cwd]
133134
* @param {String} options.topLevelDir - Current git repo top-level path
134135
* @param {Boolean} options.isFn - Whether the linter task is a function
@@ -139,6 +140,7 @@ export const makeErr = (command, error, ctx) => {
139140
export const getSpawnedTask = ({
140141
color,
141142
command,
143+
continueOnError = false,
142144
cwd = process.cwd(),
143145
files,
144146
topLevelDir,
@@ -165,15 +167,19 @@ export const getSpawnedTask = ({
165167

166168
try {
167169
const subprocess = spawn(cmd, isFn ? args : args.concat(files), spawnOptions)
168-
quitInterruptCheck = interruptExecutionOnError(ctx, subprocess)
170+
if (!continueOnError) {
171+
quitInterruptCheck = interruptExecutionOnError(ctx, subprocess)
172+
}
169173
const result = await subprocess
170174
if (verbose) {
171175
handleOutput(command, result, ctx)
172176
}
173177
} catch (error) {
174178
throw makeErr(command, error, ctx)
175179
} finally {
176-
await quitInterruptCheck()
180+
if (quitInterruptCheck) {
181+
await quitInterruptCheck()
182+
}
177183
}
178184
}
179185
}

lib/getSpawnedTasks.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ const debugLog = createDebug('lint-staged:getSpawnedTasks')
1010
* @param {object} options
1111
* @param {boolean} [options.color]
1212
* @param {Array<string|Function>|string|Function} options.commands
13+
* @param {string} options.continueOnError
1314
* @param {string} options.cwd
1415
* @param {import('./getStagedFiles.js').StagedFile[]} options.files
1516
* @param {string} options.topLevelDir
1617
* @param {Boolean} verbose
1718
*/
18-
export const getSpawnedTasks = async ({ color, commands, cwd, files, topLevelDir, verbose }) => {
19+
export const getSpawnedTasks = async ({
20+
color,
21+
commands,
22+
continueOnError,
23+
cwd,
24+
files,
25+
topLevelDir,
26+
verbose,
27+
}) => {
1928
debugLog('Creating Listr tasks for commands %o', commands)
2029
const cmdTasks = []
2130

@@ -48,6 +57,7 @@ export const getSpawnedTasks = async ({ color, commands, cwd, files, topLevelDir
4857
const task = getSpawnedTask({
4958
color,
5059
command,
60+
continueOnError,
5161
cwd,
5262
files: filepaths,
5363
topLevelDir,

lib/runAll.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export const runAll = async (
224224
: getSpawnedTasks({
225225
color,
226226
commands: task.commands,
227+
continueOnError,
227228
cwd: groupCwd,
228229
files: task.fileList,
229230
topLevelDir,

package-lock.json

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

test/unit/runAll.spec.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,19 @@ describe('runAll', () => {
452452
})
453453

454454
it('should reject after running all tasks when continueOnError is true', async ({ expect }) => {
455-
getStagedFiles.mockImplementationOnce(async () => [{ filepath: 'foo.js', status: 'A' }])
455+
getStagedFiles.mockImplementationOnce(async () => [
456+
{
457+
filepath: 'foo.js',
458+
status: 'A',
459+
},
460+
{ filepath: 'bar.py', status: 'A' },
461+
])
462+
456463
searchConfigs.mockImplementationOnce(async () => ({
457-
'': { '*.js': 'echo "failing command"' },
464+
'': {
465+
'*.js': ['echo "success js command 1"', 'echo "success js command 2"'],
466+
'*.py': 'echo "failing py command"',
467+
},
458468
}))
459469

460470
// Mock first spawn call (git operations) to succeed
@@ -465,7 +475,15 @@ describe('runAll', () => {
465475
})
466476
)
467477

468-
// Mock second spawn call (the actual task) to fail
478+
// Mock second spawn call (`echo "success js command 1"`) to succeed
479+
spawn.mockImplementationOnce(() =>
480+
mockNanoSpawnReturnValue({
481+
output: 'Success',
482+
nodeChildProcess: { pid: 0 },
483+
})
484+
)
485+
486+
// Mock second spawn call (`echo "failing py command"`) to fail
469487
spawn.mockImplementationOnce(() =>
470488
mockNanoSpawnReturnValue(
471489
Object.assign(new SubprocessError(), {
@@ -475,11 +493,32 @@ describe('runAll', () => {
475493
)
476494
)
477495

496+
// Mock first spawn call ('success js command 2') to succeed
497+
spawn.mockImplementationOnce(() =>
498+
mockNanoSpawnReturnValue(
499+
{
500+
output: 'Success',
501+
nodeChildProcess: { pid: 0 },
502+
},
503+
1000
504+
)
505+
)
506+
478507
mockGitWorkflow.runTasks.mockImplementationOnce(async (ctx, task, { listrTasks }) => {
479508
return task.newListr(listrTasks)
480509
})
481510

482511
// With continueOnError: true, should still reject but after running all tasks
483512
await expect(runAll({ continueOnError: true })).rejects.toThrow('lint-staged failed')
513+
514+
expect(console.printHistory()).toMatch(
515+
/"data":"COMPLETED".*"title":"echo \\"success js command 1\\"/
516+
)
517+
expect(console.printHistory()).toMatch(
518+
/"data":{"error":"echo \\"failing py command\\" \[FAILED\]"}/
519+
)
520+
expect(console.printHistory()).toMatch(
521+
/"data":"COMPLETED".*"title":"echo \\"success js command 2\\"/
522+
)
484523
})
485524
})

0 commit comments

Comments
 (0)