Repair cached Python directory junctions on Windows (#1122)

With `cache-python: true`, a Windows runner can report a cache hit while
extracting the cache incorrectly. When `@actions/cache`'s tar extracts
uv's minor-version junction before the patch directory it points to, it
creates a file symlink. Resolving that link later raises `[WinError 5]
Access is denied`.

This PR fixes things up after restoring the Python cache, recreating the
junction if resolving the link fails with access denied and its recorded
target is a matching, accessible CPython installation in the same
directory.

This is a temporary workaround until
https://github.com/actions/toolkit/issues/2562 gets resolved and
`@actions/cache` itself becomes resilient to these situations.

I tested this by restoring the same archive on two fresh `windows-2022`
runners. The original run passed because resolving the minor-version
link raised the expected WinError 5. With the patch, `uv sync --offline
--managed-python --no-python-downloads` created a virtual environment
whose `home` in `pyvenv.cfg` resolved to the cached CPython 3.10.22
installation. [Original
run](https://github.com/astral-sh/ruff/actions/runs/41469344610/job/125358623949#step:9:1)
· [Patched
run](https://github.com/astral-sh/ruff/actions/runs/41469344610/job/125358623835#step:10:1).
This commit is contained in:
Zsolt Dollenstein
2026-11-03 19:11:16 +01:00
committed by GitHub
parent a1b1886df2
commit fce2a640fd
7 changed files with 224 additions and 69 deletions
+37
View File
@@ -1,3 +1,4 @@
import { fileURLToPath } from "node:url";
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { createSetupInputs } from "../helpers/setup-inputs";
@@ -48,6 +49,42 @@ beforeEach(() => {
});
describe("restoreCache", () => {
it.each([
["win32", false],
["linux", true],
] as const)(
"keeps the matched Python cache key on %s when the install directory cannot be read",
async (platform, pythonCacheHit) => {
const inputs = createSetupInputs({
cachePython: true,
// A file at the install directory path makes readdir fail.
pythonDir: fileURLToPath(import.meta.url),
restoreCache: true,
});
mockRestoreCache
.mockReturnValueOnce("uv-key")
.mockReturnValueOnce("python-key");
const originalPlatform = process.platform;
Object.defineProperty(process, "platform", { value: platform });
try {
await restoreCache(inputs);
} finally {
Object.defineProperty(process, "platform", { value: originalPlatform });
}
expect(mockSetOutput).toHaveBeenCalledWith("cache-hit", true);
expect(mockSetOutput).toHaveBeenCalledWith(
"python-cache-hit",
pythonCacheHit,
);
expect(mockSaveState).toHaveBeenCalledWith(
"python-cache-matched-key",
"python-key",
);
},
);
it("encodes Python version ranges before adding them to the cache key", async () => {
await restoreCache(createSetupInputs(), ">3.10.11,<3.11");