cache icon indicating copy to clipboard operation
cache copied to clipboard

Bug: cannot update cache in Cargo project

Open otiv-willem-vanhulle opened this issue 11 months ago • 6 comments

I have made a minimal example of bug.

I have setup a Cargo workspace and want to cache the ~/.cargo and target directories. Calling save explicitly at the end of the script does not overwrite the existing cache after the cache is stored on the first time.

Deleting the previous version of the cache fixes it

Run actions/cache/save@v4
/usr/bin/tar --posix -cf cache.tzst --exclude cache.tzst -P -C /home/runner/work/cargo-ci/cargo-ci --files-from manifest.txt --use-compress-program zstdmt
Failed to save: Unable to reserve cache with key Linux-restore-build, another job may be creating this cache. More details: Cache already exists. Scope: refs/heads/main, Key: Linux-restore-build, Version: cd36736238095b30d245e37cee3eada29643d39d68cc49be99ecf89155bdc73a
Warning: Cache save failed.

The core of the message seems to be another job may be creating this cache. More details: Cache already exists.

The YAML workflow file I am using is

name: Rust test workflow
on:
  push:
    branches:
      - 'main' 
jobs:
  test-restore-build:
    name: Test restore compile and store
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4

      - name: Update APT package index
        run: sudo apt-get update
        
      - name: Install and download required APT packages
        run: sudo apt-get install -y rustup

      - name: Restore Cargo cache
        uses: actions/cache/restore@v4
        with:
          path: |
            ~/.cargo
            target
          key: ${{ runner.os }}-restore-build

      - name: Write to target
        run: cargo build

      - name: Save Cargo cache
        uses: actions/cache/save@v4
        with:
          path: |
            ~/.cargo
            target
          key: ${{ runner.os }}-restore-build

otiv-willem-vanhulle avatar Jan 28 '25 10:01 otiv-willem-vanhulle

I'm wondering if this is actually a bug or intended behavior. I would also like the possibility to overwrite an existing cache with actions/cache/save.

ia0 avatar Feb 02 '25 15:02 ia0

I'm wondering if this is actually a bug or intended behavior. I would also like the possibility to overwrite an existing cache with actions/cache/save.

It is not well documented but save will fail if any existing cache with the same key on the current branch exists. You have to delete it first manually with the gh cli tool. I created a nice script for listing caches and deleting them based on PR numbers. Let me know if you need it.

otiv-willem-vanhulle avatar Feb 02 '25 17:02 otiv-willem-vanhulle

I see, that's what I thought. I'll try something with gh cache delete in the workflow when I detect that I want to overwrite the cache.

ia0 avatar Feb 02 '25 21:02 ia0

I see, that's what I thought. I'll try something with gh cache delete in the workflow when I detect that I want to overwrite the cache.

It's best to extract the ID of the cache you want to delete and pass that to gh cache delete.

otiv-willem-vanhulle avatar Feb 03 '25 07:02 otiv-willem-vanhulle

Yes indeed. I got a solution working for me. I'm simply doing:

ID="$(gh cache list --ref=${{ github.ref }} --key=cargo-home --json=id --jq='.[].id')"
[ -z "$ID" ] || gh cache delete "$ID"

ia0 avatar Feb 03 '25 11:02 ia0

Duplicate of https://github.com/actions/cache/issues/342

langston-barrett avatar Apr 02 '25 14:04 langston-barrett