Skip to content

Commit 535dc26

Browse files
authored
Respect UV_CACHE_DIR and cache-dir (#612)
Fixes: #583
1 parent f610be5 commit 535dc26

File tree

13 files changed

+1277
-96
lines changed

13 files changed

+1277
-96
lines changed

.github/workflows/test.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ jobs:
599599
- name: Setup with cache
600600
uses: ./
601601
with:
602+
enable-cache: true
602603
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-cache-local
603604
- run: |
604605
if [ "$UV_CACHE_DIR" != "${{ matrix.inputs.expected-cache-dir }}" ]; then
@@ -607,6 +608,25 @@ jobs:
607608
fi
608609
shell: bash
609610
611+
test-cache-local-cache-disabled:
612+
runs-on: ubuntu-latest
613+
steps:
614+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
615+
with:
616+
persist-credentials: false
617+
- name: Setup without cache
618+
uses: ./
619+
with:
620+
enable-cache: false
621+
622+
- name: Check UV_CACHE_DIR is not set
623+
run: |
624+
if [ -n "$UV_CACHE_DIR" ]; then
625+
echo "UV_CACHE_DIR should not be set when cache is disabled: $UV_CACHE_DIR"
626+
exit 1
627+
fi
628+
shell: bash
629+
610630
test-setup-cache-local:
611631
runs-on: selfhosted-ubuntu-arm64
612632
steps:
@@ -791,6 +811,31 @@ jobs:
791811
- run: uv run long-running.py &
792812
working-directory: __tests__/fixtures/uv-project
793813

814+
test-cache-dir-from-file:
815+
runs-on: ubuntu-latest
816+
steps:
817+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
818+
with:
819+
persist-credentials: false
820+
- name: Verify uv cache dir is not populated
821+
run: |
822+
if [ -f "/tmp/pyproject-toml-defined-cache-path/CACHEDIR.TAG" ]; then
823+
echo "Cache dir should not exist"
824+
exit 1
825+
fi
826+
- name: Setup uv
827+
uses: ./
828+
with:
829+
working-directory: __tests__/fixtures/cache-dir-defined-project
830+
- run: uv sync
831+
working-directory: __tests__/fixtures/cache-dir-defined-project
832+
- name: Verify uv cache dir is populated
833+
run: |
834+
if [ ! -f "/tmp/pyproject-toml-defined-cache-path/CACHEDIR.TAG" ]; then
835+
echo "Cache dir should exist"
836+
exit 1
837+
fi
838+
794839
all-tests-passed:
795840
runs-on: ubuntu-latest
796841
needs:
@@ -811,6 +856,7 @@ jobs:
811856
- test-activate-environment
812857
- test-musl
813858
- test-cache-local
859+
- test-cache-local-cache-disabled
814860
- test-setup-cache
815861
- test-restore-cache
816862
- test-setup-cache-requirements-txt
@@ -831,6 +877,7 @@ jobs:
831877
- test-absolute-path
832878
- test-relative-path
833879
- test-cache-prune-force
880+
- test-cache-dir-from-file
834881
if: always()
835882
steps:
836883
- name: All tests passed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,18 @@ By default, the cache will be saved.
316316

317317
### Local cache path
318318

319-
This action controls where uv stores its cache on the runner's filesystem by setting `UV_CACHE_DIR`.
319+
If caching is enabled, this action controls where uv stores its cache on the runner's filesystem
320+
by setting `UV_CACHE_DIR`.
321+
320322
It defaults to `setup-uv-cache` in the `TMP` dir, `D:\a\_temp\uv-tool-dir` on Windows and
321323
`/tmp/setup-uv-cache` on Linux/macOS. You can change the default by specifying the path with the
322324
`cache-local-path` input.
323325

326+
> [!NOTE]
327+
> If the environment variable `UV_CACHE_DIR` is already set this action will not override it.
328+
> If you configured [cache-dir](https://docs.astral.sh/uv/reference/settings/#cache-dir) in your
329+
> config file then it is also respected and this action will not set `UV_CACHE_DIR`.
330+
324331
```yaml
325332
- name: Define a custom uv cache path
326333
uses: astral-sh/setup-uv@v6

__tests__/fixtures/cache-dir-defined-project/README.md

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[project]
2+
name = "uv-project"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = [
8+
"ruff>=0.6.2",
9+
]
10+
11+
[build-system]
12+
requires = ["hatchling"]
13+
build-backend = "hatchling.build"
14+
15+
[tool.uv]
16+
cache-dir = "/tmp/pyproject-toml-defined-cache-path"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def hello() -> str:
2+
return "Hello from uv-project!"

__tests__/fixtures/cache-dir-defined-project/uv.lock

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)