Skip to content

Commit 253be16

Browse files
epantkesimPod
andauthored
fix: handle unavailable require cache (backport of #20812 to v9.x) (#21065)
fix: handle unavailable require cache (#20812) * fix: handle unavailable require cache * test: ignore require cache fallback coverage * fix: avoid extra require cache coverage branch * test: isolate require cache regression coverage (cherry picked from commit b6ae5cf) Skip the require-cache regression test on Node.js versions where `vm.constants` is unavailable (< 20.12.0/21.7.0, e.g. the CI-pinned 18.18.0). That API is needed to allow dynamic `import()` calls inside the module recompiled by the test. This only affects the test harness; the production fix works on every Node.js version this branch supports. Co-authored-by: Simon Podlipsky <[email protected]>
1 parent 7b431a7 commit 253be16

2 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎lib/config/config-loader.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ async function loadConfigFile(filePath, hasUnstableNativeNodeJsTSConfigFlag) {
231231
* the require cache only if the file has been changed.
232232
*/
233233
if (importedConfigFileModificationTime.get(filePath) !== mtime) {
234-
delete require.cache[filePath];
234+
delete require.cache?.[filePath];
235235
}
236236

237237
const isTS = isFileTS(filePath);

‎tests/lib/config/config-loader.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//------------------------------------------------------------------------------
1111

1212
const assert = require("node:assert");
13+
const fs = require("node:fs");
14+
const Module = require("node:module");
15+
const os = require("node:os");
1316
const path = require("node:path");
17+
const vm = require("node:vm");
1418
const sinon = require("sinon");
1519
const {
1620
ConfigLoader,
@@ -75,6 +79,75 @@ describe("Config loaders", () => {
7579
});
7680

7781
describe("loadConfigArrayForFile()", () => {
82+
const useMainContextDefaultLoader =
83+
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- unavailable before Node.js 20.12/21.7.
84+
vm.constants?.USE_MAIN_CONTEXT_DEFAULT_LOADER;
85+
86+
(useMainContextDefaultLoader ? it : it.skip)(
87+
"should not error when require.cache is unavailable",
88+
async () => {
89+
const cwd = path.resolve(
90+
fixtureDir,
91+
"simple-valid-project-2",
92+
);
93+
const configLoaderPath = path.resolve(
94+
__dirname,
95+
"../../../lib/config/config-loader.js",
96+
);
97+
const configLoaderSource = fs.readFileSync(
98+
configLoaderPath,
99+
"utf8",
100+
);
101+
const moduleExports = { exports: {} };
102+
const requireWithoutCache =
103+
Module.createRequire(configLoaderPath);
104+
105+
requireWithoutCache.cache = void 0;
106+
107+
const compiledWrapper = vm.runInThisContext(
108+
Module.wrap(configLoaderSource),
109+
{
110+
filename: path.join(
111+
os.tmpdir(),
112+
"eslint-config-loader-without-require-cache.js",
113+
),
114+
importModuleDynamically:
115+
useMainContextDefaultLoader,
116+
},
117+
);
118+
119+
compiledWrapper.call(
120+
moduleExports.exports,
121+
moduleExports.exports,
122+
requireWithoutCache,
123+
moduleExports,
124+
configLoaderPath,
125+
path.dirname(configLoaderPath),
126+
);
127+
128+
const {
129+
[ConfigLoaderClass.name]:
130+
ConfigLoaderClassWithoutRequireCache,
131+
} = moduleExports.exports;
132+
133+
const configArray =
134+
await ConfigLoaderClassWithoutRequireCache.calculateConfigArray(
135+
path.resolve(cwd, "eslint.config.js"),
136+
cwd,
137+
{
138+
cwd,
139+
ignoreEnabled: true,
140+
warningService: new WarningService(),
141+
},
142+
);
143+
144+
assert(
145+
Array.isArray(configArray),
146+
"Expected `calculateConfigArray()` to return a config array",
147+
);
148+
},
149+
);
150+
78151
// https://github.com/eslint/eslint/issues/19025
79152
it("should lookup config file only once and create config array only once for multiple files in same directory", async () => {
80153
const cwd = path.resolve(

0 commit comments

Comments
 (0)