Skip to content

Commit 35455c7

Browse files
authored
build: fix running eslint on Windows (electron#26014)
1 parent 10a209e commit 35455c7

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

script/lint.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,39 @@ const LINTERS = [{
9898
test: filename => filename.endsWith('.js') || filename.endsWith('.ts'),
9999
run: (opts, filenames) => {
100100
const cmd = path.join(SOURCE_ROOT, 'node_modules', '.bin', 'eslint');
101-
const args = ['--cache', '--ext', '.js,.ts', ...filenames];
101+
const args = ['--cache', '--ext', '.js,.ts'];
102102
if (opts.fix) args.unshift('--fix');
103-
spawnAndCheckExitCode(cmd, args, { cwd: SOURCE_ROOT });
103+
// Windows has a max command line length of 2047 characters, so we can't provide
104+
// all of the filenames without going over that. To work around it, run eslint
105+
// multiple times and chunk the filenames so that each run is under that limit.
106+
// Use a much higher limit on other platforms which will effectively be a no-op.
107+
const MAX_FILENAME_ARGS_LENGTH = IS_WINDOWS ? 1900 : 100 * 1024;
108+
const cmdOpts = { stdio: 'inherit', shell: IS_WINDOWS, cwd: SOURCE_ROOT };
109+
if (IS_WINDOWS) {
110+
// When running with shell spaces in filenames are problematic
111+
filenames = filenames.map(filename => `"${filename}"`);
112+
}
113+
const chunkedFilenames = filenames.reduce((chunkedFilenames, filename) => {
114+
const currentChunk = chunkedFilenames[chunkedFilenames.length - 1];
115+
const currentChunkLength = currentChunk.reduce((totalLength, _filename) => totalLength + _filename.length, 0);
116+
if (currentChunkLength + filename.length > MAX_FILENAME_ARGS_LENGTH) {
117+
chunkedFilenames.push([filename]);
118+
} else {
119+
currentChunk.push(filename);
120+
}
121+
return chunkedFilenames;
122+
}, [[]]);
123+
const allOk = chunkedFilenames.map(filenames => {
124+
const result = childProcess.spawnSync(cmd, [...args, ...filenames], cmdOpts);
125+
if (result.error) {
126+
console.error(result.error);
127+
process.exit(result.status || 1);
128+
}
129+
return result.status === 0;
130+
}).every(x => x);
131+
if (!allOk) {
132+
process.exit(1);
133+
}
104134
}
105135
}, {
106136
key: 'gn',

0 commit comments

Comments
 (0)