Skip to content

Commit f0f0c68

Browse files
authored
add a config option to disallow the cache clean command (#6610)
## What's the problem this PR addresses? At [Datadog](https://github.com/datadog) we are using `yarn` inside a huge monorepository using the PnP linker and Zero-Installs with all the dependencies versioned through git. The issue we have is that people frequently have the tendency to think that this kind of environment works like others, and that cleaning the cache is the way to go whenever they encounter an install issue (like deleting `node_modules`). This is obviously not the case and we have to tell them to restore their packages from the git remote when that happens, which is a bit cumbersome. _see also: https://github.com/yarnpkg/berry/discussions/6518_ ## How did you fix it? This PR adds a non-breaking option inside the `.yarnrc` file to disallow running `yarn clean cache`. cc @arcanis ## Checklist <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent 2981acb commit f0f0c68

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

.yarn/versions/c6c3dd7a.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
releases:
2+
"@yarnpkg/cli": minor
3+
"@yarnpkg/core": minor
4+
"@yarnpkg/plugin-essentials": minor
5+
6+
declined:
7+
- "@yarnpkg/plugin-compat"
8+
- "@yarnpkg/plugin-constraints"
9+
- "@yarnpkg/plugin-dlx"
10+
- "@yarnpkg/plugin-exec"
11+
- "@yarnpkg/plugin-file"
12+
- "@yarnpkg/plugin-git"
13+
- "@yarnpkg/plugin-github"
14+
- "@yarnpkg/plugin-http"
15+
- "@yarnpkg/plugin-init"
16+
- "@yarnpkg/plugin-interactive-tools"
17+
- "@yarnpkg/plugin-link"
18+
- "@yarnpkg/plugin-nm"
19+
- "@yarnpkg/plugin-npm"
20+
- "@yarnpkg/plugin-npm-cli"
21+
- "@yarnpkg/plugin-pack"
22+
- "@yarnpkg/plugin-patch"
23+
- "@yarnpkg/plugin-pnp"
24+
- "@yarnpkg/plugin-pnpm"
25+
- "@yarnpkg/plugin-stage"
26+
- "@yarnpkg/plugin-typescript"
27+
- "@yarnpkg/plugin-version"
28+
- "@yarnpkg/plugin-workspace-tools"
29+
- "@yarnpkg/builder"
30+
- "@yarnpkg/doctor"
31+
- "@yarnpkg/extensions"
32+
- "@yarnpkg/nm"
33+
- "@yarnpkg/pnpify"
34+
- "@yarnpkg/sdks"

packages/acceptance-tests/pkg-tests-specs/sources/commands/cache/clean.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,16 @@ describe(`Commands`, () => {
3737
expect(xfs.existsSync(`${path}/.yarn/cache`)).toEqual(false);
3838
expect(xfs.existsSync(`${path}/.yarn/global/cache`)).toEqual(false);
3939
}));
40+
41+
test(`it should follow the enableCacheClean configuration`, makeTemporaryEnv({
42+
dependencies: {
43+
[`no-deps`]: `1.0.0`,
44+
},
45+
}, {
46+
enableCacheClean: false,
47+
}, async ({path, run, source}) => {
48+
await run(`install`);
49+
await expect(run(`cache`, `clean`)).rejects.toThrowError();
50+
}));
4051
});
4152
});

packages/plugin-essentials/sources/commands/cache/clean.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {BaseCommand} from '@yarnpkg/cli';
22
import {Configuration, Cache, StreamReport, Hooks} from '@yarnpkg/core';
33
import {xfs} from '@yarnpkg/fslib';
4-
import {Command, Option, Usage} from 'clipanion';
4+
import {Command, Option, Usage, UsageError} from 'clipanion';
55

66
// eslint-disable-next-line arca/no-default-export
77
export default class CacheCleanCommand extends BaseCommand {
@@ -34,6 +34,10 @@ export default class CacheCleanCommand extends BaseCommand {
3434

3535
async execute() {
3636
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
37+
38+
if (!configuration.get(`enableCacheClean`))
39+
throw new UsageError(`Cache cleaning is currently disabled. To enable it, set \`enableCacheClean: true\` in your configuration file. Note: Cache cleaning is typically not required and should be avoided when using Zero-Installs.`);
40+
3741
const cache = await Cache.find(configuration);
3842

3943
const report = await StreamReport.start({

packages/yarnpkg-core/sources/Configuration.ts

+5
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ export const coreDefinitions: {[coreSettingName: string]: SettingsDefinition} =
567567
type: SettingsType.BOOLEAN,
568568
default: false,
569569
},
570+
enableCacheClean: {
571+
description: `If false, disallows the \`cache clean\` command`,
572+
type: SettingsType.BOOLEAN,
573+
default: true,
574+
},
570575
checksumBehavior: {
571576
description: `Enumeration defining what to do when a checksum doesn't match expectations`,
572577
type: SettingsType.STRING,

0 commit comments

Comments
 (0)