Skip to content

Commit 856099c

Browse files
authored
Add python version to cache key (#187)
Closes: #182
1 parent e3017a7 commit 856099c

File tree

8 files changed

+167
-3
lines changed

8 files changed

+167
-3
lines changed

.github/workflows/test-cache.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,16 @@ jobs:
214214
- name: Remove cache dependency glob file
215215
run: rm -f ~/uv-cache.glob
216216
shell: bash
217+
218+
test-no-python-version:
219+
runs-on: ubuntu-latest
220+
steps:
221+
- uses: actions/checkout@v4
222+
- name: Fake pyproject.toml at root
223+
run: cp __tests__/fixtures/old-python-constraint-project/pyproject.toml pyproject.toml
224+
- name: Setup with cache
225+
uses: ./
226+
with:
227+
enable-cache: true
228+
- run: uv sync
229+
working-directory: __tests__/fixtures/old-python-constraint-project

__tests__/fixtures/old-python-constraint-project/README.md

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[project]
2+
name = "old-python-constraint-project"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.8,<=3.9"
7+
dependencies = [
8+
"ruff>=0.6.2",
9+
]
10+
11+
[build-system]
12+
requires = ["hatchling"]
13+
build-backend = "hatchling.build"
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/old-python-constraint-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.

dist/save-cache/index.js

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/setup/index.js

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache/restore-cache.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import {
44
cacheDependencyGlob,
55
cacheLocalPath,
66
cacheSuffix,
7+
pythonVersion as pythonVersionInput,
78
} from "../utils/inputs";
89
import { getArch, getPlatform } from "../utils/platforms";
910
import { hashFiles } from "../hash/hash-files";
11+
import * as exec from "@actions/exec";
1012

1113
export const STATE_CACHE_KEY = "cache-key";
1214
export const STATE_CACHE_MATCHED_KEY = "cache-matched-key";
@@ -49,7 +51,39 @@ async function computeKeys(version: string): Promise<string> {
4951
cacheDependencyPathHash += "no-dependency-glob";
5052
}
5153
const suffix = cacheSuffix ? `-${cacheSuffix}` : "";
52-
return `setup-uv-${CACHE_VERSION}-${getArch()}-${getPlatform()}-${version}${cacheDependencyPathHash}${suffix}`;
54+
const pythonVersion = await getPythonVersion();
55+
return `setup-uv-${CACHE_VERSION}-${getArch()}-${getPlatform()}-${version}-${pythonVersion}${cacheDependencyPathHash}${suffix}`;
56+
}
57+
58+
async function getPythonVersion(): Promise<string> {
59+
if (pythonVersionInput !== "") {
60+
return pythonVersionInput;
61+
}
62+
63+
let output = "";
64+
const options: exec.ExecOptions = {
65+
silent: !core.isDebug(),
66+
listeners: {
67+
stdout: (data: Buffer) => {
68+
output += data.toString();
69+
},
70+
},
71+
};
72+
73+
try {
74+
const execArgs = ["python", "find"];
75+
await exec.exec("uv", execArgs, options);
76+
const pythonPath = output.trim();
77+
78+
output = "";
79+
await exec.exec(pythonPath, ["--version"], options);
80+
// output is like "Python 3.8.10"
81+
return output.split(" ")[1].trim();
82+
} catch (error) {
83+
const err = error as Error;
84+
core.debug(`Failed to get python version from uv. Error: ${err.message}`);
85+
return "unknown";
86+
}
5387
}
5488

5589
function handleMatchResult(

0 commit comments

Comments
 (0)