Skip to content

configure .cache via $XDG_CACHE_DIR #1089

Description

@bukzor

Forked from #1029


a) A very convenient, and standard, way to configure tools' cache location is to set $XDG_CACHE_DIR. Many of my project's fixtures do this for other reasons already. Similarly, the most convenient, and standard, way to set "basetmp" would be $TMPDIR.

b) "something a CI system can easily pick up" is exactly these environment variables. Many systems will already support this, and those that don't will support injecting environment variables.

c) "non-containered ci" will either support ~/.cache correctly, or set $XDG_CACHE_DIR, because this is necessary for other tools that use the standard. Again, this is already handled in many of my projects because of other tools.

Activity

  1. RonnyPfannschmidt commented on Sep 29, 2015

    @RonnyPfannschmidt
    Member

    is there a good package that helps finding those paths and helps clearing unused caches

    after all if the cache folder is outside of the working directory,
    its lifetime is unrelated to the working directory

    imagine for example a unaware ci system that makes one folder per build could easily DOS the ci server due to filling up ~/.cache of the ci user

    with the current mode of operation, cache and working directory are simply co-located and share a lifetime, so data management is easy, the cache is gone when one deletes the working directory

    im not per se opposed to following XDG, but it makes lifetimes of file-system objects more tricky on our case and seems not really available on windows either
    (its my personal impression that xdg works only well on posix boxes with a typical linux/bsd setup)

  2. bukzor commented on Sep 30, 2015

    @bukzor
    Author

    Those are good points...

    I don't have a good answer. The applications I've seen don't worry about cleaning up like that.
    It would work for me if we use the current behavior when XDG_CACHE_DIR doesn't exist.

    @asottile

  3. bukzor commented on Sep 30, 2015

    @bukzor
    Author

    This one at least helps find a good platform-specific cache dir: https://pypi.python.org/pypi/appdirs/1.4.0

  4. bukzor commented on Sep 30, 2015

    @bukzor
    Author

    This one helps manage expiry for a file-based cache.
    Making one of the keys be the working directory would make it work.

    http://pythonhosted.org/pyfscache/

  5. RonnyPfannschmidt commented on Sep 30, 2015

    @RonnyPfannschmidt
    Member

    i shorty investigated appdirs and pyfscache, unfortunately both are unsuitable

    appdirs seems bug-ridden
    pyfscache is for something entirely different not fitting the use-case

  6. RonnyPfannschmidt commented on Oct 5, 2015

    @RonnyPfannschmidt
    Member

    how about introducing a ini value/env variable to select the cache backend,
    which defaults to worktree, but one can choose xdg/disabled?

    @hpk42 oppinions?

  7. asottile commented on Oct 5, 2015

    @asottile
    Member

    Personally I'd rather default to xdg instead of needing to add .cache to the gitignore in every one of my projects

  8. RonnyPfannschmidt commented on Oct 5, 2015

    @RonnyPfannschmidt
    Member

    while i agree that it is a nice default, we cant do a change like that befor 3.0

    also before we can do it propperly we need to come up with a solid way to clean things up

  9. asottile commented on Oct 5, 2015

    @asottile
    Member

    Maybe introducing the cache plugin defaulted to on should have been in 3.0 too :/

  10. RonnyPfannschmidt commented on Oct 6, 2015

    @RonnyPfannschmidt
    Member

    since its a normal feature addition, it doesnt warrant a release that large

  11. s-trooper commented on Dec 9, 2015

    @s-trooper

    i hate ".cache" folder for aesthetic reason. my projects folders are already populated by ide, git, python and others "support folders". sometimes i have more folders than actually python program files! please make ".cache" location configurable!

  12. nicoddemus commented on Dec 12, 2015

    @nicoddemus
    Member

    how about introducing a ini value/env variable to select the cache backend

    Anyone is against this? While it may not be the best option, it is simple to implement and backward compatible.

    It should support variable expansion and default to .cache for backward compatibility. This would allow users to change it like this:

    [pytest]
    cache_dir=$XDG_CACHE_DIR

    Or use an environment variable:

    PYTEST_CACHE_DIR=/some/path
    
  13. RonnyPfannschmidt commented on Dec 12, 2015

    @RonnyPfannschmidt
    Member

    I'm against the most easy variant, I want a simple solution

  14. bukzor commented on Dec 23, 2015

    @bukzor
    Author

    @RonnyPfannschmidt I've found that the function you'd need exists in pip.utils:

    https://github.com/pypa/pip/blob/develop/pip/utils/appdirs.py#L13

  15. nicoddemus commented on Dec 23, 2015

    @nicoddemus
    Member

    How about we add a configuration option that decides between the two systems? cache_location, which can be local (<rootdir>/.cache, the default) or system (using the heuristics suggested by @bukzor)? This could be added in 2.9.

    If we make a poll and people wants to change system as the default, we can announce that the default will change in 2.10, giving plenty of time for people to change to their desired setting while in 2.9 and not get caught off guard.

    BTW, I'm just throwing some ideas so we can reach some consensus, I don't mind having the .cache directory in pytest's rootdir: I configured my git-global-ignore file to always ignore it.

  16. 35 remaining items

  17. RonnyPfannschmidt commented on Feb 18, 2022

    @RonnyPfannschmidt
    Member

    @ssbarnea this is about pytest cache, which is different from byte code cache

    You need at least one cache per test invocation root, but potentially more than one for virtualenvs/python versions

    And you need to be able to cleanup potentially independently of the existing folders

  18. ssbarnea commented on Feb 18, 2022

    @ssbarnea
    Member

    I see two possible approaches here:

    • use only user-level .cache and delay addressing the cleanup for a follow-up contribution as that is not critical (if we include project root folder in hash)
    • adopt the project level .cache model similar to https://dot-config.github.io/ - basically changing default to be .cache/pytest_cache should be enough for that. At least we avoid being another software the clutters our project root directory with temp stuff.
  19. feluxe commented on Nov 18, 2023

    @feluxe

    pytest is the only tool in my python stack which still pollutes my source directories. python, poetry, pipenv, etc all use ~/.cache/... by default or allow me to configure conveniently via env var.

  20. SirzBenjie commented on Oct 5, 2025

    @SirzBenjie

    Would also very, very much like to see this, please.

  21. 0cjs commented on Apr 28, 2026

    @0cjs
    Contributor

    Using XDG_CACHE_HOME would cause confusion and pain. XDG_* directories are specifically for users' personal environment configuration (XDG stands for X Desktop Group, and for historical reasons is the term used in the freedesktop.org specifications) and this should not be shared with development projects that may write conflicting information and may want to remove information when doing clean builds and the like.

    Consider what can happen if you use XDG_CONFIG_HOME for configuration information for a particular project when testing it. Running whatever in your development checkout sets this would overwrite the user's configuration, which may well be different from the configuration used for testing. Further, if you have two different development checkouts, each would overwrite the other's information.

    Mixing different projects' build artifacts also makes clean builds more difficult since it's hard to know what to remove and removals may affect other projects. In the current situation, you simply rm -rf .pytest_cache or similar in your project dir and you know that a) you are fully clean on that, and b) you have not affected any other projects, or other copies of this project.

    I don't mind if you add this as some sort of option for people who really want it, but it should not be the default.

    pytest is the only tool in my python stack which still pollutes my source directories. python, poetry, pipenv, etc all use ~/.cache/... by default or allow me to configure conveniently via env var.

    We have .gitignore files for a reason. And env vars are a terrible configuration mechanism because, unlike a pyproject.toml or pytest.ini file, they are unobvious, easily change from session to session and terminal to terminal, and hard to set reliably in the first place. Where build artifacts go should not be random based on users' configuration, but should be defined in a config file in the project itself so that both users and the build system always know where everything is and always use a consistent set of files.

    (FWIW, I just got nailed today by the default 'use $CWD'—effectively the same thing as using an env var—when my --lf/--ff options broke because I had two cache directories; which one was used depended on the circumstances. I implemented the correct solution: add a pytest.ini file that puts the cache dir in one place for the project, regardless of the my personal environment.)

    (Also FWIW, I configure my projects to place all build products, cache dirs, etc. under a .build/ directory at the top level of the project, at least where the tools don't directly block this technique. This greatly reduces clutter (which I also hate), and also gives you an easy way to do a fully clean build: just remove that one directory.)

  22. RonnyPfannschmidt commented on Apr 29, 2026

    @RonnyPfannschmidt
    Member

    By now there are dozens of tools that map folders in there to worktrees including hatch pre-commit and others

    Having a distinction is a solved problem

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: cacherelated to the cache builtin plugintopic: configrelated to config handling, argument parsing and config filetype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions