Skip to content

Commit 36b7b57

Browse files
soujasKclaude
andauthored
fix: only report a saved cache when saving succeeded (#1110)
## Summary The post step logs `uv cache saved with key: ...` (and `Python cache saved with key: ...`) even when the save failed. The log in #769 shows it right after `Warning: Failed to save: ...`: ``` Warning: Failed to save: "C:\Program failed with error: The process 'C:\Program Files\Git\usr\bin\tar.exe' failed with exit code 2 uv cache saved with key: setup-uv-2-x86_64-pc-windows-msvc-windows-11-3.12.12-pruned-... ``` `@actions/cache`'s `saveCache` doesn't throw for these failures. It logs them and returns `-1`, and `saveCacheToKey` ignored the return value. This now checks for `-1` before logging, which is the same check the `actions/cache` save step uses (`if (cacheId != -1) core.info("Cache saved with key: ...")`). `@actions/cache` already logs the reason, so nothing else is logged on failure, and the exit code stays the same. This only changes the log line. It doesn't address the underlying tar/gzip failure in #769. One limitation: with the legacy (v1) cache service, an upload that fails after the cache was reserved still returns the reserved id, so that one case is still reported as saved. That's on the `@actions/cache` side and affects `actions/cache` the same way. Related to #769 ## Test plan - Added `__tests__/cache/save-cache.test.ts`. The `-1` case fails before the change and passes after it. - `npm run all` on Ubuntu (WSL) with Node 24.19.0 / npm 11.17.0: build, Biome (no fixes), packaging, and all 161 tests across 16 suites pass. The regenerated `dist/save-cache/index.cjs` only differs in the changed lines. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 56618ca commit 36b7b57

3 files changed

Lines changed: 95 additions & 4 deletions

File tree

‎__tests__/cache/save-cache.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { mkdtempSync, rmSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import {
5+
afterAll,
6+
beforeEach,
7+
describe,
8+
expect,
9+
it,
10+
jest,
11+
} from "@jest/globals";
12+
import { CacheLocalSource, type SetupInputs } from "../../src/utils/inputs";
13+
import { createSetupInputs } from "../helpers/setup-inputs";
14+
15+
const mockSaveCache = jest.fn<() => Promise<number>>();
16+
const mockInfo = jest.fn();
17+
const mockLoadInputs = jest.fn<() => SetupInputs>(() =>
18+
createSetupInputs({ enableCache: false }),
19+
);
20+
const state: Record<string, string> = {};
21+
22+
jest.unstable_mockModule("@actions/cache", () => ({
23+
saveCache: mockSaveCache,
24+
}));
25+
26+
jest.unstable_mockModule("@actions/core", () => ({
27+
getState: jest.fn((name: string) => state[name] ?? ""),
28+
setFailed: jest.fn(),
29+
}));
30+
31+
jest.unstable_mockModule("../../src/utils/inputs", () => ({
32+
loadInputs: mockLoadInputs,
33+
}));
34+
35+
jest.unstable_mockModule("../../src/utils/logging", () => ({
36+
info: mockInfo,
37+
warning: jest.fn(),
38+
}));
39+
40+
jest.spyOn(process, "exit").mockImplementation((() => undefined) as never);
41+
42+
const { run } = await import("../../src/save-cache");
43+
44+
const cacheDir = mkdtempSync(join(tmpdir(), "setup-uv-save-cache-"));
45+
const cacheKey = "setup-uv-2-x86_64-unknown-linux-gnu-ubuntu-24.04-3.12-hash";
46+
47+
function infoMessages(): string[] {
48+
return mockInfo.mock.calls.map(([msg]) => msg as string);
49+
}
50+
51+
beforeEach(() => {
52+
delete process.env.UV_CACHE_DIR;
53+
state["cache-key"] = cacheKey;
54+
state["cache-matched-key"] = "";
55+
mockLoadInputs.mockReturnValue(
56+
createSetupInputs({
57+
cacheLocalPath: { path: cacheDir, source: CacheLocalSource.Input },
58+
}),
59+
);
60+
});
61+
62+
afterAll(() => {
63+
rmSync(cacheDir, { force: true, recursive: true });
64+
});
65+
66+
describe("saveCache", () => {
67+
it("reports the saved key when the cache is saved", async () => {
68+
mockSaveCache.mockResolvedValue(42);
69+
70+
await run();
71+
72+
expect(mockSaveCache).toHaveBeenCalledWith([cacheDir], cacheKey);
73+
expect(infoMessages()).toContain(`uv cache saved with key: ${cacheKey}`);
74+
});
75+
76+
it("does not report the cache as saved when saving fails", async () => {
77+
// @actions/cache logs the failure itself and returns -1
78+
mockSaveCache.mockResolvedValue(-1);
79+
80+
await run();
81+
82+
expect(mockSaveCache).toHaveBeenCalledWith([cacheDir], cacheKey);
83+
expect(infoMessages()).not.toContain(
84+
`uv cache saved with key: ${cacheKey}`,
85+
);
86+
});
87+
});

‎dist/save-cache/index.cjs‎

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/save-cache.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ async function saveCacheToKey(
159159
}
160160

161161
log.info(`Including ${cacheName} path: ${cachePath}`);
162-
await cache.saveCache([cachePath], cacheKey);
163-
log.info(`${cacheName} saved with key: ${cacheKey}`);
162+
const cacheId = await cache.saveCache([cachePath], cacheKey);
163+
if (cacheId !== -1) {
164+
log.info(`${cacheName} saved with key: ${cacheKey}`);
165+
}
164166
}
165167

166168
run();

0 commit comments

Comments
 (0)