Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a config option to disallow the cache clean command #6610

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .yarn/versions/c6c3dd7a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
releases:
"@yarnpkg/cli": minor
"@yarnpkg/core": minor
"@yarnpkg/plugin-essentials": minor

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,16 @@ describe(`Commands`, () => {
expect(xfs.existsSync(`${path}/.yarn/cache`)).toEqual(false);
expect(xfs.existsSync(`${path}/.yarn/global/cache`)).toEqual(false);
}));

test(`it should follow the enableCacheClean configuration`, makeTemporaryEnv({
dependencies: {
[`no-deps`]: `1.0.0`,
},
}, {
enableCacheClean: false,
}, async ({path, run, source}) => {
await run(`install`);
await expect(run(`cache`, `clean`)).rejects.toThrowError();
}));
});
});
6 changes: 5 additions & 1 deletion packages/plugin-essentials/sources/commands/cache/clean.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {BaseCommand} from '@yarnpkg/cli';
import {Configuration, Cache, StreamReport, Hooks} from '@yarnpkg/core';
import {xfs} from '@yarnpkg/fslib';
import {Command, Option, Usage} from 'clipanion';
import {Command, Option, Usage, UsageError} from 'clipanion';

// eslint-disable-next-line arca/no-default-export
export default class CacheCleanCommand extends BaseCommand {
Expand Down Expand Up @@ -34,6 +34,10 @@ export default class CacheCleanCommand extends BaseCommand {

async execute() {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);

if (!configuration.get(`enableCacheClean`))
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.`);

const cache = await Cache.find(configuration);

const report = await StreamReport.start({
Expand Down
5 changes: 5 additions & 0 deletions packages/yarnpkg-core/sources/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ export const coreDefinitions: {[coreSettingName: string]: SettingsDefinition} =
type: SettingsType.BOOLEAN,
default: false,
},
enableCacheClean: {
description: `If false, disallows the \`cache clean\` command`,
type: SettingsType.BOOLEAN,
default: true,
},
checksumBehavior: {
description: `Enumeration defining what to do when a checksum doesn't match expectations`,
type: SettingsType.STRING,
Expand Down