Skip to content

Commit d18bcc7

Browse files
authored
Add value of UV_PYTHON_INSTALL_DIR to path (#628)
Closes: #610
1 parent bd1f875 commit d18bcc7

File tree

7 files changed

+112
-49
lines changed

7 files changed

+112
-49
lines changed

.github/workflows/test.yml

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ jobs:
852852
persist-credentials: false
853853
- name: Verify Python install dir is not populated
854854
run: |
855-
if [ -d ~/.local/share/uv/python ]; then
855+
if [ -d /home/runner/work/_temp/uv-python-dir ]; then
856856
echo "Python install dir should not exist"
857857
exit 1
858858
fi
@@ -866,7 +866,7 @@ jobs:
866866
working-directory: __tests__/fixtures/uv-project
867867
- name: Verify Python install dir exists
868868
run: |
869-
if [ ! -d ~/.local/share/uv/python ]; then
869+
if [ ! -d /home/runner/work/_temp/uv-python-dir ]; then
870870
echo "Python install dir should exist"
871871
exit 1
872872
fi
@@ -879,7 +879,7 @@ jobs:
879879
persist-credentials: false
880880
- name: Verify Python install dir does not exist
881881
run: |
882-
if [ -d ~/.local/share/uv/python ]; then
882+
if [ -d /home/runner/work/_temp/uv-python-dir ]; then
883883
echo "Python install dir should not exist"
884884
exit 1
885885
fi
@@ -892,7 +892,7 @@ jobs:
892892
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-cache-python-installs
893893
- name: Verify Python install dir exists
894894
run: |
895-
if [ ! -d ~/.local/share/uv/python ]; then
895+
if [ ! -d /home/runner/work/_temp/uv-python-dir ]; then
896896
echo "Python install dir should exist"
897897
exit 1
898898
fi
@@ -906,6 +906,34 @@ jobs:
906906
- run: uv sync --managed-python
907907
working-directory: __tests__/fixtures/uv-project
908908

909+
test-python-install-dir:
910+
strategy:
911+
matrix:
912+
inputs:
913+
- os: ubuntu-latest
914+
expected-python-dir: "/home/runner/work/_temp/uv-python-dir"
915+
- os: windows-latest
916+
expected-python-dir: "D:\\a\\_temp\\uv-python-dir"
917+
- os: selfhosted-ubuntu-arm64
918+
expected-python-dir: "/home/ubuntu/.local/share/uv/python"
919+
runs-on: ${{ matrix.inputs.os }}
920+
steps:
921+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
922+
with:
923+
persist-credentials: false
924+
- name: Install latest version
925+
id: setup-uv
926+
uses: ./
927+
- name: Check Python dir is expected dir
928+
run: |
929+
if [ "$UV_PYTHON_INSTALL_DIR" != "${{ matrix.inputs.expected-python-dir }}" ]; then
930+
echo "Wrong UV_PYTHON_INSTALL_DIR: UV_PYTHON_INSTALL_DIR"
931+
exit 1
932+
fi
933+
shell: bash
934+
- name: Install python works
935+
run: uv python install
936+
909937
all-tests-passed:
910938
runs-on: ubuntu-latest
911939
needs:
@@ -950,6 +978,7 @@ jobs:
950978
- test-cache-dir-from-file
951979
- test-cache-python-installs
952980
- test-restore-python-installs
981+
- test-python-install-dir
953982
if: always()
954983
steps:
955984
- name: All tests passed

dist/save-cache/index.js

Lines changed: 20 additions & 17 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: 28 additions & 12 deletions
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
cacheLocalPath,
88
cachePython,
99
cacheSuffix,
10-
getUvPythonDir,
1110
pruneCache,
11+
pythonDir,
1212
pythonVersion as pythonVersionInput,
1313
restoreCache as shouldRestoreCache,
1414
workingDirectory,
@@ -34,7 +34,7 @@ export async function restoreCache(): Promise<void> {
3434
);
3535
const cachePaths = [cacheLocalPath];
3636
if (cachePython) {
37-
cachePaths.push(await getUvPythonDir());
37+
cachePaths.push(pythonDir);
3838
}
3939
try {
4040
matchedKey = await cache.restoreCache(cachePaths, cacheKey);

src/save-cache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
cacheLocalPath,
1313
cachePython,
1414
enableCache,
15-
getUvPythonDir,
1615
ignoreNothingToCache,
16+
pythonDir,
1717
pruneCache as shouldPruneCache,
1818
saveCache as shouldSaveCache,
1919
} from "./utils/inputs";
@@ -73,7 +73,6 @@ async function saveCache(): Promise<void> {
7373

7474
const cachePaths = [actualCachePath];
7575
if (cachePython) {
76-
const pythonDir = await getUvPythonDir();
7776
core.info(`Including Python cache path: ${pythonDir}`);
7877
if (!fs.existsSync(pythonDir) && !ignoreNothingToCache) {
7978
throw new Error(

src/setup-uv.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
githubToken,
2020
ignoreEmptyWorkdir,
2121
manifestFile,
22+
pythonDir,
2223
pythonVersion,
2324
toolBinDir,
2425
toolDir,
@@ -51,6 +52,7 @@ async function run(): Promise<void> {
5152
addToolBinToPath();
5253
addUvToPathAndOutput(setupResult.uvDir);
5354
setToolDir();
55+
addPythonDirToPath();
5456
setupPython();
5557
await activateEnvironment();
5658
addMatchers();
@@ -194,6 +196,17 @@ function setToolDir(): void {
194196
}
195197
}
196198

199+
function addPythonDirToPath(): void {
200+
core.exportVariable("UV_PYTHON_INSTALL_DIR", pythonDir);
201+
core.info(`Set UV_PYTHON_INSTALL_DIR to ${pythonDir}`);
202+
if (process.env.UV_NO_MODIFY_PATH !== undefined) {
203+
core.info("UV_NO_MODIFY_PATH is set, not adding python dir to path");
204+
} else {
205+
core.addPath(pythonDir);
206+
core.info(`Added ${pythonDir} to the path`);
207+
}
208+
}
209+
197210
function setupPython(): void {
198211
if (pythonVersion !== "") {
199212
core.exportVariable("UV_PYTHON", pythonVersion);

src/utils/inputs.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from "node:path";
22
import * as core from "@actions/core";
3-
import * as exec from "@actions/exec";
43
import { getConfigValueFromTomlFile } from "./config-file";
54

65
export const workingDirectory = core.getInput("working-directory");
@@ -23,6 +22,7 @@ export const ignoreEmptyWorkdir =
2322
core.getInput("ignore-empty-workdir") === "true";
2423
export const toolBinDir = getToolBinDir();
2524
export const toolDir = getToolDir();
25+
export const pythonDir = getUvPythonDir();
2626
export const githubToken = core.getInput("github-token");
2727
export const manifestFile = getManifestFile();
2828
export const addProblemMatchers =
@@ -125,23 +125,26 @@ function getCacheDirFromConfig(): string | undefined {
125125
return undefined;
126126
}
127127

128-
export async function getUvPythonDir(): Promise<string> {
128+
export function getUvPythonDir(): string {
129129
if (process.env.UV_PYTHON_INSTALL_DIR !== undefined) {
130130
core.info(
131-
`Using UV_PYTHON_INSTALL_DIR from environment: ${process.env.UV_PYTHON_INSTALL_DIR}`,
131+
`UV_PYTHON_INSTALL_DIR is already set to ${process.env.UV_PYTHON_INSTALL_DIR}`,
132132
);
133133
return process.env.UV_PYTHON_INSTALL_DIR;
134134
}
135-
core.info("Determining uv python dir using `uv python dir`...");
136-
const result = await exec.getExecOutput("uv", ["python", "dir"]);
137-
if (result.exitCode !== 0) {
138-
throw new Error(
139-
`Failed to get uv python dir: ${result.stderr || result.stdout}`,
140-
);
135+
if (process.env.RUNNER_ENVIRONMENT !== "github-hosted") {
136+
if (process.platform === "win32") {
137+
return `${process.env.APPDATA}${path.sep}uv${path.sep}python`;
138+
} else {
139+
return `${process.env.HOME}${path.sep}.local${path.sep}share${path.sep}uv${path.sep}python`;
140+
}
141141
}
142-
const dir = result.stdout.trim();
143-
core.info(`Determined uv python dir: ${dir}`);
144-
return dir;
142+
if (process.env.RUNNER_TEMP !== undefined) {
143+
return `${process.env.RUNNER_TEMP}${path.sep}uv-python-dir`;
144+
}
145+
throw Error(
146+
"Could not determine UV_PYTHON_INSTALL_DIR. Please make sure RUNNER_TEMP is set or provide the UV_PYTHON_INSTALL_DIR environment variable",
147+
);
145148
}
146149

147150
function getCacheDependencyGlob(): string {

0 commit comments

Comments
 (0)