Fix unsafe error handling and cache state logic bugs#6
Merged
HiGarfield merged 4 commits intomainfrom Apr 6, 2026
Merged
Conversation
…match When a cache is found via a restore key (prefix match) instead of the exact primary key, CACHE_STATE should NOT be set to "hit". Previously, any hit — including a partial/restore-key match — set the state, causing save.js to skip saving the updated ccache content after a build. Now CACHE_STATE is only set to "hit" when cacheFetchingResult === keyString (exact primary-key match), so ccache is correctly saved after every build even when a previous cache was restored via the fallback restore key. Agent-Logs-Url: https://github.com/HiGarfield/cachewrtbuild/sessions/d095a004-01ae-47c6-94fd-826723eff894 Co-authored-by: HiGarfield <32226909+HiGarfield@users.noreply.github.com>
@actions/cache.saveCache() returns -1 (not undefined/0) when the save fails (e.g. ReserveCacheError because the key already exists). Because -1 is truthy in JavaScript, the previous `if (cacheId)` guard incorrectly logged "Cache saved with key: ... (id: -1)" on every such failure. Use `cacheId !== -1` to correctly distinguish a successful save (positive integer ID) from a failed one (-1). Agent-Logs-Url: https://github.com/HiGarfield/cachewrtbuild/sessions/d095a004-01ae-47c6-94fd-826723eff894 Co-authored-by: HiGarfield <32226909+HiGarfield@users.noreply.github.com>
Prefer > -1 over !== -1 to guard against any negative return value, not just exactly -1. Agent-Logs-Url: https://github.com/HiGarfield/cachewrtbuild/sessions/d095a004-01ae-47c6-94fd-826723eff894 Co-authored-by: HiGarfield <32226909+HiGarfield@users.noreply.github.com>
error.message is undefined when the thrown value is not an Error object (e.g. a plain string or number). Use the pattern error instanceof Error ? error.message : String(error) in both fetch.js (core.setFailed) and save.js (core.warning) so that any thrown value produces a meaningful failure/warning message. Agent-Logs-Url: https://github.com/HiGarfield/cachewrtbuild/sessions/130dfcbb-449a-4a4b-903c-aa57ffc5280d Co-authored-by: HiGarfield <32226909+HiGarfield@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
HiGarfield
April 6, 2026 06:51
View session
There was a problem hiding this comment.
Pull request overview
This PR fixes cache correctness and error-reporting issues in this GitHub Action’s restore/save flow to prevent skipping needed cache saves and to avoid misleading logs on failed cache saves.
Changes:
- Only set
CACHE_STATE=hiton an exact primary-key match during restore (avoid treating restore-key matches as “already up-to-date”). - Treat
@actions/cache.saveCache()success ascacheId > -1to avoid logging “Cache saved” when-1is returned. - Make
catchblocks resilient to non-Errorthrown values by formatting messages safely.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| fetch.js | Restricts CACHE_STATE to exact-match restores; improves failure message formatting for non-Error throws. |
| save.js | Fixes cache save success check for -1 return; improves warning message formatting for non-Error throws. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Three bugs identified via code review of stupidloud#39.
Changes
fetch.js— exact-match guard forCACHE_STATE:saveState("CACHE_STATE", "hit")was set on any cache hit, including restore-key (prefix) matches. This causedsave.jsto skip saving updated ccache after a build. Now only set whencacheFetchingResult === keyString(exact primary-key match).save.js—cacheIdsuccess check:@actions/cache.saveCache()returns-1on failure (e.g.ReserveCacheError). The oldif (cacheId)guard is truthy for-1, producing a false "Cache saved" log. Changed tocacheId > -1.fetch.js/save.js— non-Errorthrow safety:error.messageisundefinedwhen the thrown value isn't anErrorinstance, yielding an opaque failure message. Both catch blocks now use: