feat: add --cache-dir command-line option#5201
Open
valter-silva-au wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds a
--cache-dircommand-line option that sets the directory where Black readsand writes its cache. It takes precedence over the
BLACK_CACHE_DIRenvironmentvariable, mirroring how
--workersrelates toBLACK_NUM_WORKERS.Resolution precedence (highest first):
--cache-dir→BLACK_CACHE_DIR→XDG_CACHE_HOME→ platform-specific user cache directory.This is implemented by reassigning the module-level
black.cache.CACHE_DIRconstantonce in
main()before files are dispatched — the same global the cache read/writehelpers (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
envknob at the hook level, soBLACK_CACHE_DIRcan't beset 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-cacheflagalready serves that purpose.
Checklist - did you ...
CHANGES.mdif necessary?