10000 fix: `TypeError: fs.exists is not a function` on read-only file syste… · eslint/eslint@7d5e5f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d5e5f6

Browse files
authored
fix: TypeError: fs.exists is not a function on read-only file system (#17846)
1 parent 9271d10 commit 7d5e5f6

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

lib/eslint/flat-eslint.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
// Note: Node.js 12 does not support fs/promises.
1313
const fs = require("fs").promises;
14+
const { existsSync } = require("fs");
1415
const path = require("path");
1516
const findUp = require("find-up");
1617
const { version } = require("../../package.json");
@@ -766,7 +767,7 @@ class FlatESLint {
766767
const errorCode = error && error.code;
767768

768769
// Ignore errors when no such file exists or file system is read only (and cache file does not exist)
769-
if (errorCode !== "ENOENT" && !(errorCode === "EROFS" && !(await fs.exists(cacheFilePath)))) {
770+
if (errorCode !== "ENOENT" && !(errorCode === "EROFS" && !existsSync(cacheFilePath))) {
770771
throw error;
771772
}
772773
}

tests/lib/eslint/eslint.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,6 +2711,41 @@ describe("ESLint", () => {
27112711
assert(!shell.test("-f", cacheFilePath), "the cache for eslint should have been deleted since last run did not use the cache");
27122712
});
27132713

2714+
it("should not throw an error if the cache file to be deleted does not exist on a read-only file system", async () => {
2715+
cacheFilePath = getFixturePath(".eslintcache");
2716+
doDelete(cacheFilePath);
2717+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
2718+
2719+
// Simulate a read-only file system.
2720+
sinon.stub(fs, "unlinkSync").throws(
2721+
Object.assign(new Error("read-only file system"), { code: "EROFS" })
2722+
);
2723+
2724+
const eslintOptions = {
2725+
useEslintrc: false,
2726+
2727+
// specifying cache true the cache will be created
2728+
cache: false,
2729+
cacheLocation: cacheFilePath,
2730+
overrideConfig: {
2731+
rules: {
2732+
"no-console": 0,
2733+
"no-unused-vars": 2
2734+
}
2735+
},
2736+
extensions: ["js"],
2737+
cwd: path.join(fixtureDir, "..")
2738+
};
2739+
2740+
eslint = new ESLint(eslintOptions);
2741+
2742+
const file = getFixturePath("cache/src", "test-file.js");
2743+
2744+
await eslint.lintFiles([file]);
2745+
2746+
assert(fs.unlinkSync.calledWithExactly(cacheFilePath), "Expected attempt to delete the cache was not made.");
2747+
});
2748+
27142749
it("should store in the cache a file that has lint messages and a file that doesn't have lint messages", async () => {
27152750
cacheFilePath = getFixturePath(".eslintcache");
27162751
doDelete(cacheFilePath);

tests/lib/eslint/flat-eslint.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,40 @@ describe("FlatESLint", () => {
26012601
assert(!shell.test("-f", cacheFilePath), "the cache for eslint should have been deleted since last run did not use the cache");
26022602
});
26032603

2604+
it("should not throw an error if the cache file to be deleted does not exist on a read-only file system", async () => {
2605+
cacheFilePath = getFixturePath(".eslintcache");
2606+
doDelete(cacheFilePath);
2607+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
2608+
2609+
// Simulate a read-only file system.
2610+
sinon.stub(fsp, "unlink").rejects(
2611+
Object.assign(new Error("read-only file system"), { code: "EROFS" })
2612+
);
2613+
2614+
const eslintOptions = {
2615+
overrideConfigFile: true,
2616+
2617+
// specifying cache false the cache will be deleted
2618+
cache: false,
2619+
cacheLocation: cacheFilePath,
2620+
overrideConfig: {
2621+
rules: {
2622+
"no-console": 0,
2623+
"no-unused-vars": 2
2624+
}
2625+
},
2626+
cwd: path.join(fixtureDir, "..")
2627+
};
2628+
2629+
eslint = new FlatESLint(eslintOptions);
2630+
2631+
const file = getFixturePath("cache/src", "test-file.js");
2632+
2633+
await eslint.lintFiles([file]);
2634+
2635+
assert(fsp.unlink.calledWithExactly(cacheFilePath), "Expected attempt to delete the cache was not made.");
2636+
});
2637+
26042638
it("should store in the cache a file that has lint messages and a file that doesn't have lint messages", async () => {
26052639
cacheFilePath = getFixturePath(".eslintcache");
26062640
doDelete(cacheFilePath);

0 commit comments

Comments
 (0)
0