Skip to content

Fix cache-hit output when cache missed #1404

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
merged 5 commits into from
Jul 31, 2024

Conversation

fchimpan
Copy link
Contributor

@fchimpan fchimpan commented May 22, 2024

Description

fix #1334

According to the README, if the cache was not found, the output should be 'false'. However, it actually returned an empty string. This seems to be due to the behavior of GitHub Actions to return an empty string if the output is not defined. Please see these conversations.

I would like to change the code to return 'false' when the cache is not found, per the README.

Motivation and Context

In PR #1263, a README correction is suggested. This is a great initiative, thank you. However, to avoid confusion, how about changing the implementation instead?

How Has This Been Tested?

The issue was caused by not defining an output for cache-hit when the cache was not found, and then returning. Therefore, I will set the output before returning.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (add or update README or docs)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@fchimpan fchimpan requested a review from a team as a code owner May 22, 2024 03:03
@fchimpan
Copy link
Contributor Author

fchimpan commented May 27, 2024

Hi, @bethanyj28
I would like to request a reviewer for this pull request. If you are not the reviewer, could you please recommend a suitable reviewer?

Thank you very much!

@fchimpan
Copy link
Contributor Author

@joshmgross
I would like to request a reviewer for this pull request. If you are not the reviewer, could you please recommend a suitable reviewer?

Thank you very much!

@@ -62,7 +62,7 @@ export async function restoreImpl(
...restoreKeys
].join(", ")}`
);

core.setOutput(Outputs.CacheHit, false.toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we set this output before the if (failOnCacheMiss) case above?

That would ensure it's set even if fail-on-cache-miss is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, @joshmgross

Thanks for your response.

Indeed, since it applies even in the case of exceptions, it seems good to output before failOnCacheMiss. What do you think?
6f5b5ee

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @fchimpan!

Looks like the dist/ is out of date - could you re-run npm run build?

https://github.com/actions/cache/actions/runs/9599939755/job/26475812160?pr=1404

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @joshmgross !
8addae5
I have pushed the dist.

@fchimpan
Copy link
Contributor Author

fchimpan commented Jun 21, 2024

I tested the implementation of this PR by actually using it. As expected, it was confirmed that ‘false’ is returned when the cache is not found.

v4 test : https://github.com/fchimpan/cache-pr-1404-test/actions/runs/9606621627
This PR test : https://github.com/fchimpan/cache-pr-1404-test/actions/runs/9606621624
Please see cache-miss job's Check outputs step.

@fchimpan
Copy link
Contributor Author

Hi @joshmgross ,
I’ve made the necessary changes. Could you please review the code?

Thank you!

@joshmgross joshmgross self-assigned this Jul 31, 2024
@joshmgross joshmgross enabled auto-merge (squash) July 31, 2024 16:55
@joshmgross joshmgross merged commit 40c3b67 into actions:main Jul 31, 2024
14 checks passed
@joshmgross joshmgross mentioned this pull request Oct 3, 2024
siddhantdange pushed a commit to buildpulse/cache that referenced this pull request Oct 4, 2024
* fix: cache-hit output

* fix: Output chache hit timing

* fix: Output chache hit timing

---------

Co-authored-by: Josh Gross <joshmgross@github.com>
@v4hn
Copy link

v4hn commented Oct 7, 2024

Comment: The ternary logic which this patch removes was a useful (but -granted- undocumented and broken) API to detect whether restore-keys was used.
E.g.,

steps:
- name: Restore Cache
  id: restore
  uses: actions/cache/restore@v4
  with:
    path: ...
    key: abc-${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}
    restore-keys: |
      abc-${{ github.sha }}-${{ github.run_id }}
- name: Run if any cache was restored
  if: steps.restore.outputs.cache-hit != ''
  ...

The correct way to get the same check seems to be
if: steps.restore.outputs.cache-matched-key != ''.
Maybe it makes sense to add that use-case in the README?

@Jason3S
Copy link

Jason3S commented Oct 8, 2024

I think it would have been better to change the documentation. This was a major breaking change.

@Jason3S
Copy link

Jason3S commented Oct 8, 2024

Before landing this type of change, it would be useful to check how it is used:
https://github.com/search?q=.cache-hit+language%3AYaml+&type=code

@Ari-Kishon
Copy link

Ari-Kishon commented Oct 8, 2024

This was a major change and should have been released as such.

The change in return value cause several critical steps in our workflows to be skipped and caused them all to fail.

I understand that this behavior has technically a bug, but given how long it returned empty values this should have been considered valid behavior and taken into account.

This change could have potentially disrupted development for my whole team had I not caught it before going on vacation.

We rely heavily on actions from this organization because we perceive them be stable and professionally maintained.

@stof
Copy link

stof commented Oct 8, 2024

The readme says that chache-hit is a boolean value: https://github.com/actions/cache?tab=readme-ov-file#outputs

The string 'false' is not a boolean value (and it is a truthy value). Please revert this broken change

@v4hn
Copy link

v4hn commented Oct 8, 2024

The readme says that chache-hit is a boolean value: https://github.com/actions/cache?tab=readme-ov-file#outputs

The string 'false' is not a boolean value (and it is a truthy value). Please revert this broken change

In that sense the API was inconsistent with the docs already before.
When a restore-keys entry was found instead of the primary key, 'false' was already returned before and a falsy value was only returned in case neither key was found.

@stof
Copy link

stof commented Oct 8, 2024

Then the readme correction proposed in #1263 is still relevant, even though the description of this PR says that it is meant to fix the implementation instead of having to clarify the readme (the PR of course needs to be adapted to account for the change done here)

@nati-levi
Copy link

This broke our CI.
Should have been major release.
Its a breaking change.

@AnimMouse
Copy link

This basically broke all of my composite actions.
It's sad that it's 2024, and GitHub Actions still can't make their type system right!
true and false are booleans, not strings!

@joshmgross
Copy link
Contributor

👋 I will work on reverting this change

mmatl added a commit to ambi-robotics/cache that referenced this pull request Jul 23, 2025
* update documentation to use <action>@v4

* Update README.md and use v4 of checkout action (actions#1437)

Update examples to use latest available checkout action v4.

* Explicit use bash for Windows (actions#1377)

Co-authored-by: Josh Gross <joshmgross@github.com>

* Fix cache-hit output when cache missed (actions#1404)

* fix: cache-hit output

* fix: Output chache hit timing

* fix: Output chache hit timing

---------

Co-authored-by: Josh Gross <joshmgross@github.com>

* Clarify that the `restore-keys` input is a string in the docs  (actions#1434)

* Fix Description for restore-keys at Readme

As previously the restore-keys were defined as an ordered lists which is
wrong as per the issue description where the actual format is a
multi-line string with one key per line.

* Added a space between the sentence of restore-keys description

While at the PR review it's been identified there's a need for a space
between the sentence

	```
	An ordered multiline string listing the prefix-matched keys,that are
	used for restoring stale cache if no cache hit occurred for key.
	```

where it's written as "prefix-matched keys,that are" this commit will
address the review comment and introduce a space between
"prefix-matched keys, that are" and change the sentence to

	```
	An ordered multiline string listing the prefix-matched keys, that are
        used for restoring stale cache if no cache hit occurred for key.
	```

* Change restore-keys description at cache/restore/action.yml and cache/action.yml

* Add workflow file for publishing releases to immutable action package

This workflow file publishes new action releases to the immutable action package of the same name as this repo.

This is part of the Immutable Actions project which is not yet fully released to the public. First party actions like this one are part of our initial testing of this feature.

* Deprecate `save-always` input (actions#1452)

The `save-always` input added in v4 is not
working as intended due to
`post-if` expressions not supporting the input
context.
To avoid breaking users who have already added
this input to their workflows, it is being
deprecated now and will be removed
in the next major version (v5).
See actions#1315 for more details.

* Fix typo: depening -> depending (actions#1462)

Co-authored-by: Josh Gross <joshmgross@github.com>

* restore action's README now references v4 instead of v3 (actions#1445)

Co-authored-by: Josh Gross <joshmgross@github.com>

* Prepare `4.1.0` release (actions#1464)

* Restore original behavior of `cache-hit` output (actions#1467)

* Restore original behavior of `cache-hit` output

* Bump version to 4.1.1

* Add Bun example (actions#1456)

* Add Bun example

* Fix Bun Windows example

* Revise `isGhes` logic

* ran `npm run build`

* appease the linter

* added unit tests

* Bump braces from 3.0.2 to 3.0.3

Bumps [braces](https://github.com/micromatch/braces) from 3.0.2 to 3.0.3.
- [Changelog](https://github.com/micromatch/braces/blob/master/CHANGELOG.md)
- [Commits](micromatch/braces@3.0.2...3.0.3)

---
updated-dependencies:
- dependency-name: braces
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

* Create dependabot.yml

* Prepare release 4.1.2

* Bump actions/checkout from 3 to 4

Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@v3...v4)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump actions/stale from 3 to 9

Bumps [actions/stale](https://github.com/actions/stale) from 3 to 9.
- [Release notes](https://github.com/actions/stale/releases)
- [Changelog](https://github.com/actions/stale/blob/main/CHANGELOG.md)
- [Commits](actions/stale@v3...v9)

---
updated-dependencies:
- dependency-name: actions/stale
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump actions/setup-node from 3 to 4

Bumps [actions/setup-node](https://github.com/actions/setup-node) from 3 to 4.
- [Release notes](https://github.com/actions/setup-node/releases)
- [Commits](actions/setup-node@v3...v4)

---
updated-dependencies:
- dependency-name: actions/setup-node
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump github/codeql-action from 2 to 3

Bumps [github/codeql-action](https://github.com/github/codeql-action) from 2 to 3.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](github/codeql-action@v2...v3)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* Upgrade @actions/cache to 4.0.0

* Update RELEASES.md

* Upgrade @vercel/ncc to 0.38.3

* Upgrade @actions/core to 1.11.1 and other deps

* Add licensed output

* Add reviewed licensed packages

* Add lodash to list of reviewed licenses

* Add licensed output

* Rerun CI

* Add 3.4.0 release notes

* Correct GitHub Spelling in caching-strategies.md (actions#1526)

GitHub was spelled incorrectly 3 lines under the Understanding how to choose path section

* docs: Make the "always save prime numbers" example more clear (actions#1525)

* Update force deletion docs due a recent deprecation (actions#1500)

* fix: update force deletion docs due a recent deprecation

* fix: applied josh's suggestions

---------

Co-authored-by: Josh Gross <joshmgross@github.com>

* bump @actions/cache to v4.0.1

* Update publish-immutable-actions.yml

* bump @actions/cache to v4.0.2, prep for v4.2.2 release

* add changes

* changed

* mask whole url

* debugging

* type

* artifact changes

* update cache package to mask whole sas to the end of the line

* mask

* update

* latest test before pr

* updated cache with latest changes

* updates

* new package

* update cache with main

* Update to use the latest version of the cache package to obfuscate the SAS

* Update releases.md

* Update README.md

* fix: fix

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: todgru <todgru@gmail.com>
Co-authored-by: P. Ottlinger <ottlinger@users.noreply.github.com>
Co-authored-by: Oleg A. <t0rr@mail.ru>
Co-authored-by: Josh Gross <joshmgross@github.com>
Co-authored-by: r4mimu <52129983+fchimpan@users.noreply.github.com>
Co-authored-by: Soubhik Kumar Mitra <59209034+x612skm@users.noreply.github.com>
Co-authored-by: Bassem Dghaidi <568794+Link-@users.noreply.github.com>
Co-authored-by: Joel Ambass <Jcambass@users.noreply.github.com>
Co-authored-by: mackey0225 <masaki.asano0225@gmail.com>
Co-authored-by: Eman Resu <78693624+quatquatt@users.noreply.github.com>
Co-authored-by: Jan T. Sott <git@idleberg.com>
Co-authored-by: John Wesley Walker III <81404201+jww3@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: janco-absa <janco.bester@absa.africa>
Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com>
Co-authored-by: Alessandro Sebastiani <sebbalex@users.noreply.github.com>
Co-authored-by: Rob Herley <robherley@github.com>
Co-authored-by: Salman Chishti <salmanmkc@GitHub.com>
Co-authored-by: Ben De St Paer-Gotch <nebuk89@github.com>
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.

cache-hit undefined on cache miss?
9 participants