-
Notifications
You must be signed in to change notification settings - Fork 10
fix: demote benign cache reserve-race to info instead of warning #75
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
Merged
+271
−154
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { describe, it, expect } from "vite-plus/test"; | ||
| import { readFileSync } from "node:fs"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| // The shipped action is the bundled dist/index.mjs. Several bundled deps | ||
| // (notably @actions/cache) branch on `error.name === SomeError.name` — e.g. | ||
| // ReserveCacheError, which decides whether a benign cache reserve race is | ||
| // logged via core.info or core.warning. If the bundler mangles the class | ||
| // binding, `SomeError.name` becomes a mangled identifier and never equals the | ||
| // instance's preserved `this.name` literal, so benign errors are wrongly | ||
| // surfaced as warning annotations in CI. | ||
| // | ||
| // pack.minify in vite.config.ts is configured with mangle.keepNames so these | ||
| // classes stay named. This test guards against a regression to plain | ||
| // `minify: true`, which would emit anonymous `class extends Error` bindings. | ||
| const distPath = fileURLToPath(new URL("../dist/index.mjs", import.meta.url)); | ||
| const dist = readFileSync(distPath, "utf8"); | ||
|
|
||
| describe("bundled dist preserves @actions/cache error class names", () => { | ||
| for (const className of ["ReserveCacheError", "ValidationError", "FinalizeCacheError"]) { | ||
| it(`keeps the ${className} class name so error.name comparisons work`, () => { | ||
| // A named class declaration or named class expression keeps Class.name. | ||
| // Minification without keepNames would drop the name to a mangled binding. | ||
| const namedClass = new RegExp(`class ${className} extends Error`); | ||
| expect(dist).toMatch(namedClass); | ||
| }); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { describe, it, expect, beforeEach, vi } from "vite-plus/test"; | ||
|
|
||
| // Mock external shells before importing the SUT so the module's in-file | ||
| // references resolve to the mocked versions. | ||
| vi.mock("@actions/cache", () => ({ | ||
| saveCache: vi.fn(), | ||
| })); | ||
| vi.mock("@actions/core", () => ({ | ||
| getState: vi.fn(), | ||
| info: vi.fn(), | ||
| warning: vi.fn(), | ||
| })); | ||
|
|
||
| import { saveCache as saveCacheAction } from "@actions/cache"; | ||
| import { getState, info, warning } from "@actions/core"; | ||
| import { saveCache } from "./cache-save.js"; | ||
| import { State } from "./types.js"; | ||
|
|
||
| const mockedSaveCacheAction = vi.mocked(saveCacheAction); | ||
| const mockedGetState = vi.mocked(getState); | ||
| const mockedInfo = vi.mocked(info); | ||
| const mockedWarning = vi.mocked(warning); | ||
|
|
||
| // Drive getState to return values for the save path: | ||
| // a primary key, distinct matched key (so we don't short-circuit on a hit), | ||
| // and non-empty cache paths. | ||
| function stubSaveState(): void { | ||
| mockedGetState.mockImplementation((name: string) => { | ||
| switch (name) { | ||
| case State.CachePrimaryKey: | ||
| return "vite-plus-Linux-x64-pnpm-abc123"; | ||
| case State.CacheMatchedKey: | ||
| return ""; | ||
| case State.CachePaths: | ||
| return JSON.stringify(["/home/runner/.cache/pnpm"]); | ||
| default: | ||
| return ""; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| describe("saveCache", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("does not warn when the cache key was already reserved by a concurrent matrix job", async () => { | ||
| // In a build matrix, jobs that share a cache key (e.g. same OS/arch/lockfile | ||
| // across Node versions) race to save it. The losers get cacheId === -1 from | ||
| // @actions/cache. That is expected and benign, not warning-worthy. | ||
| stubSaveState(); | ||
| mockedSaveCacheAction.mockResolvedValue(-1); | ||
|
|
||
| await saveCache(); | ||
|
|
||
| expect(mockedWarning).not.toHaveBeenCalled(); | ||
| expect(mockedInfo).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("logs a success message when the cache is saved", async () => { | ||
| stubSaveState(); | ||
| mockedSaveCacheAction.mockResolvedValue(42); | ||
|
|
||
| await saveCache(); | ||
|
|
||
| expect(mockedWarning).not.toHaveBeenCalled(); | ||
| expect(mockedInfo).toHaveBeenCalledWith( | ||
| expect.stringContaining("vite-plus-Linux-x64-pnpm-abc123"), | ||
| ); | ||
| }); | ||
|
|
||
| it("warns when saveCache throws an unexpected error", async () => { | ||
| stubSaveState(); | ||
| mockedSaveCacheAction.mockRejectedValue(new Error("boom")); | ||
|
|
||
| await saveCache(); | ||
|
|
||
| expect(mockedWarning).toHaveBeenCalledWith(expect.stringContaining("boom")); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL