<#20591 Black writes to shared `$XDG_CACHE_DIR/bla...
# github-notifications
c
#20591 Black writes to shared `$XDG_CACHE_DIR/black` cache directory Issue created by huonw Describe the bug Black has its own global caching mechanism, to avoid reformatting files that have already been formatted. • https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#black-cache-dirhttps://black.readthedocs.io/en/stable/usage_and_configuration/file_collection_and_discovery.html#ignoring-unmodified-files Pants doesn't currently seem to consider this, it seems like we should either: 1. explicitly support Black writing to a global one, by passing through the
XDG_CACHE_DIR
and/or
BLACK_CACHE_DIR
env vars automatically(?) 2. writing it to a Pants-managed named/append-only cache 3. disabling it entirely, and just rely on Pants' process level caching Reproducer:
Copy code
cd $(mktemp -d)

default_cache_dir=~/Library/Caches/black # or ~/.cache/black, or something else

# WARNING: this is mutates your disk! Check before running
clear_global_cache() {
    echo "clearing '$default_cache_dir'"
    rm -rf "$default_cache_dir"
}

cat > pants.toml <<EOF
[GLOBAL]
pants_version = "2.19.0"

backend_packages = [
  "pants.backend.python",
  "pants.backend.python.lint.black",
]

[python]
interpreter_constraints = ["==3.*"]
EOF

echo 'python_sources(name="src")' > BUILD

echo 'a' > file.py

# This bug relates to when black is actually executed, so let's get the Pants caches out of the way
export PANTS_PANTSD=false PANTS_LOCAL_CACHE=false

# BUG 1: writes to cache with default settings
clear_global_cache
pants fmt ::
tree "$default_cache_dir"

# BUG 2: writes to cache even with (attempted) override
tmp_cache_dir="temp-cache"
clear_global_cache
BLACK_CACHE_DIR="$tmp_cache_dir" pants fmt ::

tree "$default_cache_dir"
tree "$tmp_cache_dir"
This sets up pants to run black in two configurations, starting with an empty global/default black cache each time. In both configurations, that global cache is written to: 1. default settings 2. with an explicit override via
BLACK_CACHE_DIR
Pants version 2.19.0 OS macOS Additional info N/A pantsbuild/pants