Skip to content

Commit a626a9f

Browse files
committed
fix: allow setting --max-arg-length=Infinity
1 parent 25e2fbb commit a626a9f

9 files changed

Lines changed: 67 additions & 38 deletions

File tree

‎.changeset/honest-views-drum.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+
It's now possible to set `--max-arg-length=Infinity` to effectively disable chunking of tasks based on the number of staged files.

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ By default changes made by tasks are automatically staged and added to the commi
164164

165165
#### `--max-arg-length [number]`
166166

167-
long commands (a lot of files) are automatically split into multiple chunks when it detects the current shell cannot handle them. Use this flag to override the maximum length of the generated command string.
167+
The list of staged files are appended to configured tasks as arguments, and the resulting string might be too long for the current shell, especially on Windows. _Lint-staged_ tries to avoid this by splitting the list of staged files into chunks resulting in the commands being run multiple times. This behavior affects all tasks, including functions. Use the `--max-arg-length` to override the platform-specific default value if you want to avoid this. Setting `--max-arg-length=Infinity` will disable chunking completely.
168168

169169
#### `--no-stash`
170170

‎lib/chunkFiles.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export const chunkFiles = ({ files, baseDir, maxArgLength = null, relative = fal
4646
}
4747
})
4848

49-
if (!maxArgLength) {
50-
debugLog('Skip chunking files because of undefined maxArgLength')
49+
if (!maxArgLength || maxArgLength === Infinity) {
50+
debugLog(`Skip chunking files because of maxArgLength is ${maxArgLength}`)
5151
return [normalizedFiles] // wrap in an array to return a single chunk
5252
}
5353

‎lib/cli.js‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,7 @@ export const parseCliOptions = (argv) => {
168168
}
169169

170170
const maxArgLength =
171-
values['max-arg-length'] !== undefined ? parseInt(values['max-arg-length'], 10) : undefined
172-
173-
if (Number.isNaN(maxArgLength)) {
174-
throw new TypeError(`Option '--mar-arg-length' takes a numeric argument`, {
175-
cause: { '--max-arg-length': values['max-arg-length'] },
176-
})
177-
}
171+
values['max-arg-length'] !== undefined ? parseFloat(values['max-arg-length']) : undefined
178172

179173
return {
180174
allowEmpty: values['allow-empty'] ?? false,

‎lib/index.js‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ import { getVersion } from './version.js'
2828
const debugLog = createDebug('lint-staged')
2929

3030
/**
31-
* Get the maximum length of a command-line argument string based on current platform
31+
* Get the maximum length of a command-line argument string based on current platform,
32+
* roughly half of the real maximum length
3233
*
3334
* https://serverfault.com/questions/69430/what-is-the-maximum-length-of-a-command-line-in-mac-os-x
3435
* https://support.microsoft.com/en-us/help/830473/command-prompt-cmd-exe-command-line-string-limitation
3536
* https://unix.stackexchange.com/a/120652
3637
*/
3738
const getMaxArgLength = () => {
3839
switch (process.platform) {
39-
case 'darwin':
40-
return 262144
41-
case 'win32':
42-
return 8191
43-
default:
44-
return 131072
40+
case 'darwin': // 262_144
41+
return 131_072
42+
case 'win32': // 8_191
43+
return 4_096
44+
default: // 131_072
45+
return 65_536
4546
}
4647
}
4748

@@ -91,7 +92,7 @@ const lintStaged = async (
9192
hideAll = false,
9293
hideUnstaged = false,
9394
hidePartiallyStaged = !(hideAll || hideUnstaged),
94-
maxArgLength = getMaxArgLength() / 2,
95+
maxArgLength = getMaxArgLength(),
9596
quiet = false,
9697
relative = false,
9798
// Stashing should be disabled by default when the `diff` option is used

‎lib/validateOptions.js‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { InvalidOptionsError } from './symbols.js'
88

99
const debugLog = createDebug('lint-staged:validateOptions')
1010

11+
/** @type {(value: number) => boolean} */
12+
const isValidIntegerValue = (value) => Number.isInteger(value) || value === Infinity
13+
1114
/**
1215
* Validate lint-staged options, either from the Node.js API or the command line flags.
1316
* @param {*} options
@@ -17,26 +20,37 @@ const debugLog = createDebug('lint-staged:validateOptions')
1720
export const validateOptions = async (options = {}, logger) => {
1821
debugLog('Validating options...')
1922

23+
const { concurrent, cwd, maxArgLength } = options
24+
2025
if (
21-
options.concurrent !== undefined &&
22-
typeof options.concurrent !== 'boolean' &&
23-
(!Number.isInteger(options.concurrent) || Number.isNaN(options.concurrent))
26+
concurrent !== undefined &&
27+
typeof concurrent !== 'boolean' &&
28+
(!isValidIntegerValue(concurrent) || concurrent < 1)
2429
) {
25-
logger.error(invalidOption('concurrent', `${options.concurrent}`, 'Must be boolean or integer'))
30+
logger.error(
31+
invalidOption('concurrent', `${concurrent}`, 'Must be boolean, positive integer or Infinite')
32+
)
2633
throw InvalidOptionsError
2734
}
2835

2936
/** Ensure the passed cwd option exists; it might also be relative */
30-
if (typeof options.cwd === 'string') {
37+
if (typeof cwd === 'string') {
3138
try {
32-
const resolved = path.resolve(options.cwd)
39+
const resolved = path.resolve(cwd)
3340
await fs.access(resolved, constants.F_OK)
3441
} catch (error) {
3542
debugLog('Failed to validate options: %o', options)
36-
logger.error(invalidOption('cwd', options.cwd, error.message))
43+
logger.error(invalidOption('cwd', cwd, error.message))
3744
throw InvalidOptionsError
3845
}
3946
}
4047

48+
if (maxArgLength !== undefined && (!isValidIntegerValue(maxArgLength) || maxArgLength < 1)) {
49+
logger.error(
50+
invalidOption('maxArgLength', `${maxArgLength}`, 'Must be positive integer, or Infinite')
51+
)
52+
throw InvalidOptionsError
53+
}
54+
4155
debugLog('Validated options: %o', options)
4256
}

‎test/unit/cli.spec.js‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ suite('cli', () => {
8484
)
8585
})
8686

87-
it('should throw when using max-arg-length with non-numeric value', ({ expect }) => {
88-
expect(() => parseCliOptions(['--max-arg-length=test'])).toThrow(
89-
`Option '--mar-arg-length' takes a numeric argument`
90-
)
91-
})
92-
9387
it('should parse max-arg-length option as integer', ({ expect }) => {
9488
const options = parseCliOptions(['--max-arg-length=100'])
9589
expect(options.maxArgLength).toBe(100)

‎test/unit/index3.spec.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ describe('lintStaged', () => {
114114
})
115115

116116
it.for([
117-
['darwin', 262144 / 2],
118-
['win32', 8191 / 2],
119-
['others', 131072 / 2],
117+
['darwin', 131_072],
118+
['win32', 4_096],
119+
['others', 65_536],
120120
])(
121121
'should use default max arg length of $1 on $2',
122122
async ([platform, maxArgLength], { expect }) => {

‎test/unit/validateOptions.spec.js‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ describe('validateOptions', () => {
3636
}
3737
)
3838

39-
it.for(['foo', 0.5, NaN])('Should reject invalid value: $0', async (concurrent, { expect }) => {
40-
const logger = makeConsoleMock()
41-
await expect(validateOptions({ concurrent }, logger)).rejects.toThrow(InvalidOptionsError)
42-
})
39+
it.for([-1, 'foo', 0.5, NaN])(
40+
'Should reject invalid value: $0',
41+
async (concurrent, { expect }) => {
42+
const logger = makeConsoleMock()
43+
await expect(validateOptions({ concurrent }, logger)).rejects.toThrow(InvalidOptionsError)
44+
}
45+
)
4346
})
4447

4548
describe('cwd', () => {
@@ -99,4 +102,22 @@ describe('validateOptions', () => {
99102
`)
100103
})
101104
})
105+
106+
describe('maxArgLength', () => {
107+
it.for([1, 8191, 131072, 262144, Infinity])(
108+
'Should accept valid value: $0',
109+
async (maxArgLength, { expect }) => {
110+
const logger = makeConsoleMock()
111+
await expect(validateOptions({ maxArgLength }, logger)).resolves.toBeUndefined()
112+
}
113+
)
114+
115+
it.for([-Infinity, -1, 0, 0.5, NaN])(
116+
'Should reject invalid value: $0',
117+
async (maxArgLength, { expect }) => {
118+
const logger = makeConsoleMock()
119+
await expect(validateOptions({ maxArgLength }, logger)).rejects.toThrow(InvalidOptionsError)
120+
}
121+
)
122+
})
102123
})

0 commit comments

Comments
 (0)