Add resolved Python version output for activated environments

This commit is contained in:
William Woodruff
2026-09-03 15:01:47 -04:00
parent e105c8fb1d
commit 3956519c9e
9 changed files with 292 additions and 7 deletions
+5
View File
@@ -16,6 +16,7 @@ import {
getPlatform,
type Platform,
} from "./utils/platforms";
import { getResolvedPythonVersion } from "./utils/python-version";
import { resolveUvVersion } from "./version/resolve";
const sourceDir = __dirname;
@@ -101,6 +102,10 @@ async function run(): Promise<void> {
const detectedPythonVersion = await getPythonVersion(inputs);
core.setOutput("python-version", detectedPythonVersion);
core.setOutput(
"python-version-resolved",
await getResolvedPythonVersion(inputs),
);
if (inputs.enableCache) {
await restoreCache(inputs, detectedPythonVersion);
+32
View File
@@ -0,0 +1,32 @@
import { join } from "node:path";
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import type { SetupInputs } from "./inputs";
export async function getResolvedPythonVersion(
inputs: SetupInputs,
): Promise<string> {
if (!inputs.activateEnvironment) {
return "";
}
const pythonPath =
process.platform === "win32"
? join(inputs.venvPath, "Scripts", "python.exe")
: join(inputs.venvPath, "bin", "python");
try {
// @actions/exec parses the executable as a command line, so quote the path.
const { stdout } = await exec.getExecOutput(
`"${pythonPath.replace(/"/g, '\\"')}"`,
["-I", "-c", "import platform; print(platform.python_version())"],
{ silent: !core.isDebug() },
);
return stdout.trim();
} catch (error) {
core.debug(
`Failed to get the activated environment's Python version. Error: ${error instanceof Error ? error.message : String(error)}`,
);
return "";
}
}