Skip to content

Commit f8e55ee

Browse files
committed
fix(plugin): isolate ESLint instance per webpack config in non-threaded mode
Avoid reusing global ESLint instances across multiple webpack configurations. This fixes incorrect `cwd` and context resolution due to shared state between configurations. Threaded mode remains broken due to fundamental architectural flaws and requires a complete rewrite to be compatible. BREAKING CHANGE: threaded mode is broken and now explicitly unsupported fixes #286
1 parent 337451d commit f8e55ee

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/getESLint.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { cpus } = require('os');
33
const { Worker: JestWorker } = require('jest-worker');
44

55
// @ts-ignore
6-
const { setup, lintFiles } = require('./worker');
6+
const { setup } = require('./worker');
77
const { getESLintOptions } = require('./options');
88
const { jsonStringifyReplacerSortKeys } = require('./utils');
99
const { stringify } = require('flatted');
@@ -25,16 +25,16 @@ const cache = {};
2525
*/
2626
async function loadESLint(options) {
2727
const { eslintPath } = options;
28-
const eslint = await setup({
28+
const eslintPack = await setup({
2929
eslintPath,
3030
configType: options.configType,
3131
eslintOptions: getESLintOptions(options),
3232
});
3333

3434
return {
3535
threads: 1,
36-
lintFiles,
37-
eslint,
36+
lintFiles : eslintPack.lintFiles,
37+
eslint : eslintPack.eslint,
3838
// no-op for non-threaded
3939
cleanup: async () => {},
4040
};

src/worker.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@
44
/** @typedef {{new (arg0: ESLintOptions): ESLint; outputFixes: (arg0: LintResult[]) => any;}} ESLintClass */
55

66
Object.assign(module.exports, {
7-
lintFiles,
8-
setup,
7+
setup
98
});
109

11-
/** @type {ESLintClass} */
12-
let ESLint;
13-
14-
/** @type {ESLint} */
15-
let eslint;
16-
1710
/** @type {boolean} */
1811
let fix;
1912

@@ -25,48 +18,61 @@ let fix;
2518
*
2619
* @param {setupOptions} arg0 - setup worker
2720
*/
28-
function setup({ eslintPath, configType, eslintOptions }) {
21+
function setup({eslintPath, configType, eslintOptions}) {
2922
fix = !!(eslintOptions && eslintOptions.fix);
3023
const eslintModule = require(eslintPath || 'eslint');
3124

3225
if (eslintModule.ESLint && parseFloat(eslintModule.ESLint.version) >= 9) {
3326
return eslintModule
34-
.loadESLint({ useFlatConfig: configType === 'flat' })
27+
.loadESLint({useFlatConfig: configType === 'flat'})
3528
.then((/** @type {ESLintClass} */ classESLint) => {
36-
ESLint = classESLint;
37-
eslint = new ESLint(eslintOptions);
38-
return eslint;
29+
const ESLint = classESLint;
30+
const eslint = new ESLint(eslintOptions);
31+
const pack = {
32+
ESLint,
33+
eslint,
34+
lintFiles: lintFiles.bind(null, ESLint, eslint),
35+
};
36+
return pack;
3937
});
4038
}
4139

4240
let FlatESLint;
43-
41+
/** @type {ESLintClass} */
42+
let ESLint;
4443
if (eslintModule.LegacyESLint) {
4544
ESLint = eslintModule.LegacyESLint;
46-
({ FlatESLint } = eslintModule);
45+
({FlatESLint} = eslintModule);
4746
} else {
48-
({ ESLint } = eslintModule);
49-
47+
({ESLint} = eslintModule);
5048
if (configType === 'flat') {
5149
throw new Error(
5250
"Couldn't find FlatESLint, you might need to set eslintPath to 'eslint/use-at-your-own-risk'",
5351
);
5452
}
5553
}
56-
54+
/** @type {ESLint} */
55+
let eslint;
5756
if (configType === 'flat') {
5857
eslint = new FlatESLint(eslintOptions);
5958
} else {
6059
eslint = new ESLint(eslintOptions);
6160
}
61+
const pack = {
62+
ESLint,
63+
eslint,
64+
lintFiles: lintFiles.bind(null, ESLint, eslint),
65+
};
6266

63-
return eslint;
67+
return pack;
6468
}
6569

6670
/**
71+
* @param {ESLintClass} ESLint
72+
* @param {ESLint} eslint
6773
* @param {string | string[]} files
6874
*/
69-
async function lintFiles(files) {
75+
async function lintFiles(ESLint, eslint, files) {
7076
/** @type {LintResult[]} */
7177
const result = await eslint.lintFiles(files);
7278
// if enabled, use eslint autofixing where possible

0 commit comments

Comments
 (0)