diff --git a/changelog_unreleased/cli/18124.md b/changelog_unreleased/cli/18124.md new file mode 100644 index 000000000..2ef6af5bc --- /dev/null +++ b/changelog_unreleased/cli/18124.md @@ -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. diff --git a/src/cli/find-cache-file.js b/src/cli/find-cache-file.js index 7e58613d9..7de9eee46 100644 --- a/src/cli/find-cache-file.js +++ b/src/cli/find-cache-file.js @@ -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; } diff --git a/tests/integration/__tests__/cache.js b/tests/integration/__tests__/cache.js index 6eced1075..a466fb09d 100644 --- a/tests/integration/__tests__/cache.js +++ b/tests/integration/__tests__/cache.js @@ -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",