fix: accept --cache-dir as a CLI setting (#17428)

Add `cache-dir` to `BARE_SETTING_FLAGS` as a text setting, so
`--cache-dir <dir>` and `--cache-dir=<dir>` are pulled out of argv before
clap sees them and recorded in the CLI layer's setting values, the same path
`--config.cache-dir=<dir>` takes. Config loading then sets `cacheDir` and
records it as an explicit setting.

No command declares its own `--cache-dir` option and no global option uses
the name, so claiming it on every command line is safe.

Fixes pnpm/pnpm#17422
This commit is contained in:
samiur shahin authored and GitHub committed 2026-10-27 12:39:36 +01:00
1 parent 3f4e9fcf13
commit 4201601a4a
5 files changed
+39 -2

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"pacquet": patch
---
pnpm now accepts the `--cache-dir <dir>` flag, as in `pnpm install --cache-dir /tmp/pnpm-cache` [#17422](https://github.com/pnpm/pnpm/issues/17422).
@@ -18,6 +18,22 @@ fn extract_accepts_the_on_fail_settings_as_bare_flags() {
assert_eq!(config.runtime_on_fail, Some(RuntimeOnFail::Warn));
}
#[test]
fn extract_accepts_cache_dir_as_a_bare_flag() {
let (overrides, remaining) =
ConfigOverrides::extract(argv(["pacquet", "install", "--cache-dir", "/tmp/pnpm-cache"]));
assert_eq!(remaining, argv(["pacquet", "install"]));
assert_eq!(
overrides
.cli_layer(None)
.setting_values
.get("cache-dir")
.map(String::as_str),
Some("/tmp/pnpm-cache"),
);
}
#[test]
fn extract_leaves_other_bare_flags_for_clap() {
let (_, remaining) =
@@ -154,9 +154,10 @@ pub(super) enum SettingArity {
/// setting the invoked command declares as its own option is left for
/// clap; a setting that collides with a *global* option would be claimed
/// on every command line and so must not appear here at all.
pub(super) const BARE_SETTING_FLAGS: [(&str, SettingArity); 46] = [
pub(super) const BARE_SETTING_FLAGS: [(&str, SettingArity); 47] = [
("allow-unused-patches", SettingArity::Boolean),
("auto-dedupe", SettingArity::Boolean),
("cache-dir", SettingArity::Text),
("child-concurrency", SettingArity::Parsed(is_i32)),
("dangerously-allow-all-builds", SettingArity::Boolean),
("engine-strict", SettingArity::Boolean),
+2 -1
View File
@@ -12,7 +12,8 @@ use std::{fs, process::Command};
/// Every setting flag, the value it is given, and what `pnpm config get`
/// reports back for it.
const SETTING_FLAGS: [(&[&str], &str, &str); 17] = [
const SETTING_FLAGS: [(&[&str], &str, &str); 18] = [
(&["--cache-dir", "/custom/cache"], "cache-dir", "/custom/cache"),
(&["--package-import-method", "hardlink"], "package-import-method", "hardlink"),
(&["--hoist-pattern=eslint"], "hoist-pattern", "[\n \"eslint\"\n]"),
(&["--public-hoist-pattern=@types/*"], "public-hoist-pattern", "[\n \"@types/*\"\n]"),
@@ -39,6 +39,20 @@ fn cli_setting_values_win_over_the_workspace_yaml() {
assert_eq!(config.workspace_dir.as_deref(), Some(workspace.path()));
}
#[test]
fn cache_dir_cli_setting_overrides_the_default() {
let project = tempdir().expect("project tempdir");
let cache_dir = project.path().join("pnpm-cache");
let cache_dir_str = cache_dir.to_str().expect("utf-8 tempdir path");
let config = config_with_cli_settings(&[("cache-dir", cache_dir_str)])
.current::<HostNoHome>(project.path())
.expect("loads");
assert_eq!(config.cache_dir, cache_dir);
assert_eq!(config.explicit_settings.get("cacheDir"), Some(&cache_dir_str.into()));
}
#[test]
fn empty_cli_node_options_override_the_workspace_yaml() {
let project = tempdir().expect("project tempdir");