Skip to content

Commit f8688fc

Browse files
committed
fix: warn instead of error when no python to cache
1 parent 78cebec commit f8688fc

File tree

4 files changed

+74
-49
lines changed

4 files changed

+74
-49
lines changed

.github/workflows/test.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,29 @@ jobs:
878878
exit 1
879879
fi
880880
881+
test-cache-python-missing-managed-install-dir:
882+
runs-on: ubuntu-latest
883+
env:
884+
UV_PYTHON_INSTALL_DIR: /tmp/missing-uv-python
885+
steps:
886+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
887+
with:
888+
persist-credentials: false
889+
- name: Setup uv with cache and python cache enabled
890+
uses: ./
891+
with:
892+
enable-cache: true
893+
cache-python: true
894+
python-version: "3.12"
895+
cache-local-path: /tmp/setup-uv-cache
896+
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-cache-python-missing-managed-install-dir
897+
- name: Ensure uv cache dir exists so only python-cache behavior is tested
898+
run: uv sync
899+
working-directory: __tests__/fixtures/uv-project
900+
shell: bash
901+
- name: Ensure managed Python install dir does not exist and this does not break caching
902+
run: rm -rf "$UV_PYTHON_INSTALL_DIR"
903+
881904
test-cache-python-installs:
882905
runs-on: ubuntu-latest
883906
steps:
@@ -1029,6 +1052,7 @@ jobs:
10291052
- test-relative-path
10301053
- test-cache-prune-force
10311054
- test-cache-dir-from-file
1055+
- test-cache-python-missing-managed-install-dir
10321056
- test-cache-python-installs
10331057
- test-restore-python-installs
10341058
- test-python-install-dir

dist/save-cache/index.js

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

docs/caching.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ By default, the Python install dir (`uv python dir` / `UV_PYTHON_INSTALL_DIR`) i
199199
for the same reason that the dependency cache is pruned.
200200
If you want to cache Python installs along with your dependencies, set the `cache-python` input to `true`.
201201

202+
Note that this only caches Python versions that uv actually installs into `UV_PYTHON_INSTALL_DIR`
203+
(i.e. managed Python installs). If uv uses a system Python, there may be nothing to cache.
204+
To force managed Python installs, set `UV_PYTHON_PREFERENCE=only-managed`.
205+
202206
```yaml
203207
- name: Cache Python installs
204208
uses: astral-sh/setup-uv@v7

src/save-cache.ts

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,40 @@ async function saveCache(): Promise<void> {
5959
}
6060

6161
const actualCachePath = getUvCachePath();
62-
await saveCacheToKey(
63-
cacheKey,
64-
actualCachePath,
65-
STATE_CACHE_MATCHED_KEY,
66-
"uv cache",
67-
`Cache path ${actualCachePath} does not exist on disk. This likely indicates that there are no dependencies to cache. Consider disabling the cache input if it is not needed.`,
68-
);
62+
if (!fs.existsSync(actualCachePath)) {
63+
if (ignoreNothingToCache) {
64+
core.info(
65+
"No cacheable uv cache paths were found. Ignoring because ignore-nothing-to-cache is enabled.",
66+
);
67+
} else {
68+
throw new Error(
69+
`Cache path ${actualCachePath} does not exist on disk. This likely indicates that there are no dependencies to cache. Consider disabling the cache input if it is not needed.`,
70+
);
71+
}
72+
} else {
73+
await saveCacheToKey(
74+
cacheKey,
75+
actualCachePath,
76+
STATE_CACHE_MATCHED_KEY,
77+
"uv cache",
78+
);
79+
}
6980
}
7081

7182
if (cachePython) {
83+
if (!fs.existsSync(pythonDir)) {
84+
core.warning(
85+
`Python cache path ${pythonDir} does not exist on disk. Skipping Python cache save because no managed Python installation was found. If you want uv to install managed Python instead of using a system interpreter, set UV_PYTHON_PREFERENCE=only-managed.`,
86+
);
87+
return;
88+
}
89+
7290
const pythonCacheKey = `${cacheKey}-python`;
7391
await saveCacheToKey(
7492
pythonCacheKey,
7593
pythonDir,
7694
STATE_PYTHON_CACHE_MATCHED_KEY,
7795
"Python cache",
78-
`Python cache path ${pythonDir} does not exist on disk. This likely indicates that there are no Python installations to cache. Consider disabling the cache input if it is not needed.`,
7996
);
8097
}
8198
}
@@ -119,7 +136,6 @@ async function saveCacheToKey(
119136
cachePath: string,
120137
stateKey: string,
121138
cacheName: string,
122-
pathNotExistErrorMessage: string,
123139
): Promise<void> {
124140
const matchedKey = core.getState(stateKey);
125141

@@ -131,26 +147,8 @@ async function saveCacheToKey(
131147
}
132148

133149
core.info(`Including ${cacheName} path: ${cachePath}`);
134-
if (!fs.existsSync(cachePath) && !ignoreNothingToCache) {
135-
throw new Error(pathNotExistErrorMessage);
136-
}
137-
138-
try {
139-
await cache.saveCache([cachePath], cacheKey);
140-
core.info(`${cacheName} saved with key: ${cacheKey}`);
141-
} catch (e) {
142-
if (
143-
e instanceof Error &&
144-
e.message ===
145-
"Path Validation Error: Path(s) specified in the action for caching do(es) not exist, hence no cache is being saved."
146-
) {
147-
core.info(
148-
`No cacheable ${cacheName} paths were found. Ignoring because ignore-nothing-to-save is enabled.`,
149-
);
150-
} else {
151-
throw e;
152-
}
153-
}
150+
await cache.saveCache([cachePath], cacheKey);
151+
core.info(`${cacheName} saved with key: ${cacheKey}`);
154152
}
155153

156154
run();

0 commit comments

Comments
 (0)