Avoid creating node_modules/.cache/ when --cache is not enabled (#18124)

Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
Chiawen Chenandfisker Cheung authored and GitHub committed 2025-11-17 03:05:45 +08:00
1 parent 0da474ed1d
commit 0273e33fc6
3 files changed
+16 -2

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
#### Avoid creating `node_modules/.cache/` directory when `--cache` is not enabled (#18124 by @chiawendt)
Run `prettier .` without `--cache` no longer creates an empty `node_modules/.cache/` directory.
+1 -2
View File
@@ -8,8 +8,7 @@ import { isJson, statSafe } from "./utils.js";
* Find default cache file (`./node_modules/.cache/prettier/.prettier-cache`) using https://github.com/sindresorhus/find-cache-directory
*/
function findDefaultCacheFile() {
const cacheDir =
findCacheDirectory({ name: "prettier", create: true }) || os.tmpdir();
const cacheDir = findCacheDirectory({ name: "prettier" }) ?? os.tmpdir();
const cacheFilePath = path.join(cacheDir, ".prettier-cache");
return cacheFilePath;
}
+12
View File
@@ -57,6 +57,18 @@ describe("--cache option", () => {
});
afterAll(clean);
it("creates cache file with --cache", async () => {
await runCliWithoutGitignore(dir, ["--cache", "a.js"]);
await expect(fs.stat(defaultCacheFile)).resolves.not.toThrow();
});
it("doesn't create cache directory without --cache", async () => {
await runCliWithoutGitignore(dir, ["a.js"]);
await expect(
fs.stat(path.join(dir, "node_modules/.cache")),
).rejects.toHaveProperty("code", "ENOENT");
});
it("throw error when cache-strategy is invalid", async () => {
const { stderr } = await runCliWithoutGitignore(dir, [
"--cache",