Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6ae5cf

Browse files
authored
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
1 parent 3ffb14e commit b6ae5cf

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

lib/config/config-loader.js

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

236236
const isTS = isFileTS(filePath);

tests/lib/config/config-loader.js

Lines changed: 61 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 { ConfigLoader } = require("../../../lib/config/config-loader");
1620
const { WarningService } = require("../../../lib/services/warning-service");
@@ -68,6 +72,63 @@ describe("ConfigLoader", () => {
6872
});
6973

7074
describe("loadConfigArrayForFile()", () => {
75+
it("should not error when require.cache is unavailable", async () => {
76+
const cwd = path.resolve(fixtureDir, "simple-valid-project-2");
77+
const configLoaderPath = path.resolve(
78+
__dirname,
79+
"../../../lib/config/config-loader.js",
80+
);
81+
const configLoaderSource = fs.readFileSync(
82+
configLoaderPath,
83+
"utf8",
84+
);
85+
const module = { exports: {} };
86+
const requireWithoutCache =
87+
Module.createRequire(configLoaderPath);
88+
89+
requireWithoutCache.cache = void 0;
90+
91+
const compiledWrapper = vm.runInThisContext(
92+
Module.wrap(configLoaderSource),
93+
{
94+
filename: path.join(
95+
os.tmpdir(),
96+
"eslint-config-loader-without-require-cache.js",
97+
),
98+
importModuleDynamically:
99+
vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER,
100+
},
101+
);
102+
103+
compiledWrapper.call(
104+
module.exports,
105+
module.exports,
106+
requireWithoutCache,
107+
module,
108+
configLoaderPath,
109+
path.dirname(configLoaderPath),
110+
);
111+
112+
const { ConfigLoader: ConfigLoaderWithoutRequireCache } =
113+
module.exports;
114+
115+
const configArray =
116+
await ConfigLoaderWithoutRequireCache.calculateConfigArray(
117+
path.resolve(cwd, "eslint.config.js"),
118+
cwd,
119+
{
120+
cwd,
121+
ignoreEnabled: true,
122+
warningService: new WarningService(),
123+
},
124+
);
125+
126+
assert(
127+
Array.isArray(configArray),
128+
"Expected `calculateConfigArray()` to return a config array",
129+
);
130+
});
131+
71132
// https://github.com/eslint/eslint/issues/19025
72133
it("should lookup config file only once and create config array only once for multiple files in same directory", async () => {
73134
const cwd = path.resolve(fixtureDir, "simple-valid-project-2");

0 commit comments

Comments
 (0)