Skip to content

Commit 4180991

Browse files
authored
allow cache-local-path w/o enable-cache (#707)
Fixes: #705
1 parent 0439606 commit 4180991

8 files changed

Lines changed: 173 additions & 60 deletions

File tree

.github/workflows/test.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,26 @@ jobs:
649649
fi
650650
shell: bash
651651

652+
test-cache-local-cache-disabled-but-explicit-path:
653+
runs-on: ubuntu-latest
654+
steps:
655+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
656+
with:
657+
persist-credentials: false
658+
- name: Setup without cache
659+
uses: ./
660+
with:
661+
enable-cache: false
662+
cache-local-path: /tmp/uv-cache-disabled
663+
664+
- name: Check UV_CACHE_DIR is set
665+
run: |
666+
if [ "$UV_CACHE_DIR" != "/tmp/uv-cache-disabled" ]; then
667+
echo "UV_CACHE_DIR should be set when cache is disabled but explicit path is provided"
668+
exit 1
669+
fi
670+
shell: bash
671+
652672
test-setup-cache-local:
653673
runs-on: selfhosted-ubuntu-arm64
654674
steps:
@@ -984,6 +1004,7 @@ jobs:
9841004
- test-musl
9851005
- test-cache-local
9861006
- test-cache-local-cache-disabled
1007+
- test-cache-local-cache-disabled-but-explicit-path
9871008
- test-setup-cache
9881009
- test-restore-cache
9891010
- test-setup-cache-requirements-txt

dist/save-cache/index.js

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

dist/setup/index.js

Lines changed: 41 additions & 19 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
@@ -134,6 +134,10 @@ It defaults to `setup-uv-cache` in the `TMP` dir, `D:\a\_temp\setup-uv-cache` on
134134
> If you configured [cache-dir](https://docs.astral.sh/uv/reference/settings/#cache-dir) in your
135135
> config file then it is also respected and this action will not set `UV_CACHE_DIR`.
136136

137+
> [!NOTE]
138+
> If caching is disabled, you can still use `cache-local-path` so this action sets `UV_CACHE_DIR`
139+
> to your desired path.
140+
137141
```yaml
138142
- name: Define a custom uv cache path
139143
uses: astral-sh/setup-uv@v7

src/cache/restore-cache.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ export async function restoreCache(): Promise<void> {
3232
core.info(
3333
`Trying to restore uv cache from GitHub Actions cache with key: ${cacheKey}`,
3434
);
35-
const cachePaths = [cacheLocalPath];
35+
if (cacheLocalPath === undefined) {
36+
throw new Error(
37+
"cache-local-path is not set. Cannot restore cache without a valid cache path.",
38+
);
39+
}
40+
const cachePaths = [cacheLocalPath.path];
3641
if (cachePython) {
3742
cachePaths.push(pythonDir);
3843
}

src/save-cache.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,18 @@ async function saveCache(): Promise<void> {
5959
await pruneCache();
6060
}
6161

62-
let actualCachePath = cacheLocalPath;
63-
if (process.env.UV_CACHE_DIR && process.env.UV_CACHE_DIR !== cacheLocalPath) {
62+
if (cacheLocalPath === undefined) {
63+
throw new Error(
64+
"cache-local-path is not set. Cannot save cache without a valid cache path.",
65+
);
66+
}
67+
let actualCachePath = cacheLocalPath.path;
68+
if (
69+
process.env.UV_CACHE_DIR &&
70+
process.env.UV_CACHE_DIR !== cacheLocalPath.path
71+
) {
6472
core.warning(
65-
`The environment variable UV_CACHE_DIR has been changed to "${process.env.UV_CACHE_DIR}", by an action or step running after astral-sh/setup-uv. This can lead to unexpected behavior. If you expected this to happen set the cache-local-path input to "${process.env.UV_CACHE_DIR}" instead of "${cacheLocalPath}".`,
73+
`The environment variable UV_CACHE_DIR has been changed to "${process.env.UV_CACHE_DIR}", by an action or step running after astral-sh/setup-uv. This can lead to unexpected behavior. If you expected this to happen set the cache-local-path input to "${process.env.UV_CACHE_DIR}" instead of "${cacheLocalPath.path}".`,
6674
);
6775
actualCachePath = process.env.UV_CACHE_DIR;
6876
}

src/setup-uv.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
resolveVersion,
99
tryGetFromToolCache,
1010
} from "./download/download-version";
11-
import { getConfigValueFromTomlFile } from "./utils/config-file";
1211
import { STATE_UV_PATH, STATE_UV_VERSION } from "./utils/constants";
1312
import {
1413
activateEnvironment as activateEnvironmentInput,
1514
addProblemMatchers,
15+
CacheLocalSource,
1616
cacheLocalPath,
1717
checkSum,
1818
enableCache,
@@ -252,16 +252,15 @@ async function activateEnvironment(): Promise<void> {
252252
}
253253

254254
function setCacheDir(): void {
255-
if (enableCache) {
256-
const cacheDirFromConfig = getConfigValueFromTomlFile("", "cache-dir");
257-
if (cacheDirFromConfig !== undefined) {
255+
if (cacheLocalPath !== undefined) {
256+
if (cacheLocalPath.source === CacheLocalSource.Config) {
258257
core.info(
259258
"Using cache-dir from uv config file, not modifying UV_CACHE_DIR",
260259
);
261260
return;
262261
}
263-
core.exportVariable("UV_CACHE_DIR", cacheLocalPath);
264-
core.info(`Set UV_CACHE_DIR to ${cacheLocalPath}`);
262+
core.exportVariable("UV_CACHE_DIR", cacheLocalPath.path);
263+
core.info(`Set UV_CACHE_DIR to ${cacheLocalPath.path}`);
265264
}
266265
}
267266

0 commit comments

Comments
 (0)