Skip to content

feat: add --cache-dir command-line option#5201

Open
valter-silva-au wants to merge 1 commit into
psf:mainfrom
valter-silva-au:feat/black-cache-dir-cli-argument
Open

feat: add --cache-dir command-line option#5201
valter-silva-au wants to merge 1 commit into
psf:mainfrom
valter-silva-au:feat/black-cache-dir-cli-argument

Conversation

@valter-silva-au

Copy link
Copy Markdown

Description

Adds a --cache-dir command-line option that sets the directory where Black reads
and writes its cache. It takes precedence over the BLACK_CACHE_DIR environment
variable, mirroring how --workers relates to BLACK_NUM_WORKERS.

Resolution precedence (highest first): --cache-dirBLACK_CACHE_DIR
XDG_CACHE_HOME → platform-specific user cache directory.

This is implemented by reassigning the module-level black.cache.CACHE_DIR constant
once in main() before files are dispatched — the same global the cache read/write
helpers (and the existing test fixture) already consult at runtime. The single- and
multi-file paths both read/write the cache in the parent process, so one override
covers both.

Motivation

Closes #5174. The reporter runs Black as a pre-commit hook in a monorepo. Pre-commit
deliberately won't expose an env knob at the hook level, so BLACK_CACHE_DIR can't be
set for that invocation and the cache lands in the project root. A CLI option lets the
cache location be configured directly and committed to VCS via the hook args.

Scope is intentionally narrow: this only relocates the formatting cache. It does not
overload an empty path as a cache-disable switch — the existing --no-cache flag
already serves that purpose.

Checklist - did you ...

  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

Add a --cache-dir option to set the directory where Black reads and writes
its cache. It takes precedence over the BLACK_CACHE_DIR environment variable,
mirroring how --workers relates to BLACK_NUM_WORKERS.

This helps when an environment variable can't be set for the invocation, such
as running Black as a pre-commit hook in a monorepo where the cache should be
written outside the project root.

Closes psf#5174
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow cache dir configuration via command-line argument

1 participant