Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python version to cache key #187

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/test-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,16 @@ jobs:
- name: Remove cache dependency glob file
run: rm -f ~/uv-cache.glob
shell: bash

test-no-python-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Fake pyproject.toml at root
run: cp __tests__/fixtures/old-python-constraint-project/pyproject.toml pyproject.toml
- name: Setup with cache
uses: ./
with:
enable-cache: true
- run: uv sync
working-directory: __tests__/fixtures/old-python-constraint-project
Empty file.
13 changes: 13 additions & 0 deletions __tests__/fixtures/old-python-constraint-project/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[project]
name = "old-python-constraint-project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.8,<=3.9"
dependencies = [
"ruff>=0.6.2",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello() -> str:
return "Hello from uv-project!"
38 changes: 38 additions & 0 deletions __tests__/fixtures/old-python-constraint-project/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 33 additions & 1 deletion dist/save-cache/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 33 additions & 1 deletion dist/setup/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion src/cache/restore-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
cacheDependencyGlob,
cacheLocalPath,
cacheSuffix,
pythonVersion as pythonVersionInput,
} from "../utils/inputs";
import { getArch, getPlatform } from "../utils/platforms";
import { hashFiles } from "../hash/hash-files";
import * as exec from "@actions/exec";

export const STATE_CACHE_KEY = "cache-key";
export const STATE_CACHE_MATCHED_KEY = "cache-matched-key";
Expand Down Expand Up @@ -49,7 +51,39 @@ async function computeKeys(version: string): Promise<string> {
cacheDependencyPathHash += "no-dependency-glob";
}
const suffix = cacheSuffix ? `-${cacheSuffix}` : "";
return `setup-uv-${CACHE_VERSION}-${getArch()}-${getPlatform()}-${version}${cacheDependencyPathHash}${suffix}`;
const pythonVersion = await getPythonVersion();
return `setup-uv-${CACHE_VERSION}-${getArch()}-${getPlatform()}-${version}-${pythonVersion}${cacheDependencyPathHash}${suffix}`;
}

async function getPythonVersion(): Promise<string> {
if (pythonVersionInput !== "") {
return pythonVersionInput;
}

let output = "";
const options: exec.ExecOptions = {
silent: !core.isDebug(),
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
},
},
};

try {
const execArgs = ["python", "find"];
await exec.exec("uv", execArgs, options);
const pythonPath = output.trim();

output = "";
await exec.exec(pythonPath, ["--version"], options);
// output is like "Python 3.8.10"
return output.split(" ")[1].trim();
} catch (error) {
const err = error as Error;
core.debug(`Failed to get python version from uv. Error: ${err.message}`);
return "unknown";
}
}

function handleMatchResult(
Expand Down
Loading