Skip to content

Fix unsafe error handling and cache state logic bugs#6

Merged
HiGarfield merged 4 commits intomainfrom
copilot/fix-bugs-individually
Apr 6, 2026
Merged

Fix unsafe error handling and cache state logic bugs#6
HiGarfield merged 4 commits intomainfrom
copilot/fix-bugs-individually

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 6, 2026

Three bugs identified via code review of stupidloud#39.

Changes

  • fetch.js — exact-match guard for CACHE_STATE: saveState("CACHE_STATE", "hit") was set on any cache hit, including restore-key (prefix) matches. This caused save.js to skip saving updated ccache after a build. Now only set when cacheFetchingResult === keyString (exact primary-key match).

  • save.jscacheId success check: @actions/cache.saveCache() returns -1 on failure (e.g. ReserveCacheError). The old if (cacheId) guard is truthy for -1, producing a false "Cache saved" log. Changed to cacheId > -1.

  • fetch.js / save.js — non-Error throw safety: error.message is undefined when the thrown value isn't an Error instance, yielding an opaque failure message. Both catch blocks now use:

    error instanceof Error ? error.message : String(error)

Copilot AI and others added 4 commits April 6, 2026 06:44
…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>
@HiGarfield HiGarfield marked this pull request as ready for review April 6, 2026 06:51
Copilot AI review requested due to automatic review settings April 6, 2026 06:51
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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=hit on an exact primary-key match during restore (avoid treating restore-key matches as “already up-to-date”).
  • Treat @actions/cache.saveCache() success as cacheId > -1 to avoid logging “Cache saved” when -1 is returned.
  • Make catch blocks resilient to non-Error thrown 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.

@HiGarfield HiGarfield merged commit e861afa into main Apr 6, 2026
5 checks passed
@HiGarfield HiGarfield deleted the copilot/fix-bugs-individually branch April 6, 2026 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants