Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 4

how to cache cargo binaries?

I'd like to cache binaries that are installed via cargo (e.g. wasm-bindgen-cli).


I've tried variations of the following, but no dice. Please help :)


- name: Cache cargo bin
uses: actions/cache@v1
path: ~/.cargo/bin
key: ${{ runner.os }}-cargo-index-${{ hashFiles('~/.cargo/bin/**') }}
3 Replies
GitHub Partner
Message 2 of 4

Re: how to cache cargo binaries?

There are few notes need you pay attention to:

1.  If have created and saved a cache in job of a workflow run, when using the key you provided to search a cache in a subsequent job in the same workflow run, or in another workflow run, after finding the cache, the action will restore the cached files to the specified path directory under the GITHUB_WORKSPACE.


2. If the cahce action doesn't find any cahce with the key you provided, the action will create a new cache entry if the job completes successfully. The new cache will use the key you provided and contains the files in the specified path directory. If the specified path directory is not extsting or there is not any file in the path directory, the action still will create a new cache but the cache is empty without any file, if the job completes successfully.


3. When using the function hashFiles, the path pattern must can match at least one existing file under the GITHUB_WORKSPACE. If it doesn't match any file under the GITHUB_WORKSPACE, this function will be failed, and also may cause the step which is using this function to be failed. The path is relative to the GITHUB_WORKSPACE directory and can only include files inside of the GITHUB_WORKSPACE.


According to your comments, maybe the problem you are facing is related to the 3rd of above item, that means it is not appropriate to use the function hashFiles as part of the key in the cache action, especially when using the key to search cahce.

In addition, you can reference Patterns to match file paths to learn more details about Filter pattern.


If the problem you are facing still occurs, you can share your repository with us, so that we can check the full configurations of your workflow and analyze the root cause.

Copilot Lvl 3
Message 3 of 4

Re: how to cache cargo binaries?

Oh, so hashFiles can't look in other directories...


Maybe I could do a step of writing the version of various binaries to something like a local "version-cache.txt" and then hash that...

GitHub Partner
Message 4 of 4

Re: how to cache cargo binaries?

Yeah, all the path operations for the function hashFiles and action actions/cache are under the GITHUB_WORKSPACE directory, you should not set the path outside of GITHUB_WORKSPACE.


About the function hashFiles, I have tried the below actions:

1. Same file paths (include same filenames) Different contents, Same OS Different machines --> Different hash
2. Same file paths (include same filenames) Same content, Same OS Different machines --> Same hash
3. Same file paths (include same filenames) Same content, Different OSs Different machines --> Same hash


According my above tests, under the premise that the files already exist inside of the GITHUB_WORKSPACE directory, the hash codes are the same only both the file paths and contents are same.


If you want to share the hash code of the cached file, between the different jobs in the same workflow, you can try using artifacts to share the hash code between the jobs. Between different workflows, you still need using cache to share the hash code.