Skip to content
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

Check for wrong venv path in cache #287

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions .github/workflows/test-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
- name: Setup with cache
uses: ./
with:
version: '0.11.0'
enable-cache: true
working-directory: __tests__/fixtures/rye-project
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}
Expand All @@ -38,7 +37,6 @@ jobs:
id: restore
uses: ./
with:
version: '0.11.0'
enable-cache: true
working-directory: __tests__/fixtures/rye-project
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}
Expand All @@ -65,16 +63,52 @@ jobs:
cache-local-storage-path: /tmp/rye-cache
- run: rye sync
working-directory: __tests__/fixtures/rye-project
test-restore-cache-local:
runs-on: oracle-aarch64
needs: test-setup-cache-local
steps:
- uses: actions/checkout@v4
- name: Restore with cache
id: restore
uses: ./
with:
enable-cache: true
working-directory: __tests__/fixtures/rye-project
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}
cache-local-storage-path: /tmp/rye-cache
- name: Cache was hit
run: |
if [ "$CACHE_HIT" != "true" ]; then
exit 1
fi
env:
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}
- run: rye sync
working-directory: __tests__/fixtures/rye-project

test-setup-cache-local-venv-path-mismatch:
runs-on: oracle-aarch64
steps:
- uses: actions/checkout@v4
- name: Setup with cache
uses: ./
with:
enable-cache: true
working-directory: __tests__/fixtures/rye-project
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}-path-mismatch
cache-local-storage-path: /tmp/rye-cache
- run: rye sync
working-directory: __tests__/fixtures/rye-project
alter-venv-path-in-cache-local:
runs-on: oracle-aarch64
needs: test-setup-cache-local
steps:
- uses: actions/checkout@v4
- name: Alter venv path in cache
run: |
RYE_VENV_PATH=$(find /tmp/rye-cache -type f -name 'rye-venv.json' -path '/tmp/rye-cache/${{ github.run_id }}-${{ github.run_attempt }}*')
RYE_VENV_PATH=$(find /tmp/rye-cache -type f -name 'rye-venv.json' -path '/tmp/rye-cache/${{ github.run_id }}-${{ github.run_attempt }}-path-mismatch*')
sed -i 's/"venv_path":\s*"[^"]*"/"venv_path": "something"/' "$RYE_VENV_PATH"
test-restore-cache-local:
test-restore-cache-local-venv-path-mismatch:
runs-on: oracle-aarch64
needs: alter-venv-path-in-cache-local
steps:
Expand All @@ -85,14 +119,14 @@ jobs:
with:
enable-cache: true
working-directory: __tests__/fixtures/rye-project
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}
cache-prefix: ${{ github.run_id }}-${{ github.run_attempt }}-path-mismatch
cache-local-storage-path: /tmp/rye-cache
- name: Cache was hit
- name: Cache was not hit
run: |
if [ "$CACHE_HIT" != "true" ]; then
if [ "$CACHE_HIT" == "true" ]; then
exit 1
fi
env:
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}
- run: rye sync
working-directory: __tests__/fixtures/rye-project
working-directory: __tests__/fixtures/rye-project
38 changes: 19 additions & 19 deletions dist/save-cache/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/save-cache/index.js.map

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions dist/setup/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/setup/index.js.map

Large diffs are not rendered by default.

47 changes: 27 additions & 20 deletions src/restore-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,41 @@ function handleMatchResult(
matchedKey: string | undefined,
primaryKey: string
): void {
if (matchedKey) {
core.saveState(STATE_CACHE_MATCHED_KEY, matchedKey)
core.info(
`.venv restored from${cacheLocalStoragePath ? ' local' : ''} cache with key: ${matchedKey}`
)
overwriteCachedVenvPath()
core.setOutput('cache-hit', true)
} else {
if (!matchedKey) {
core.info(
`No${cacheLocalStoragePath ? ' local' : ''} cache found for key: ${primaryKey}`
)
core.setOutput('cache-hit', false)
return
}

const venvPathMatch = doesCachedVenvPathMatchCurrentVenvPath()
if (!venvPathMatch) {
fs.rmSync(venvPath, {recursive: true})
core.setOutput('cache-hit', false)
return
}

core.saveState(STATE_CACHE_MATCHED_KEY, matchedKey)
core.info(
`.venv restored from${cacheLocalStoragePath ? ' local' : ''} cache with key: ${matchedKey}`
)
core.setOutput('cache-hit', true)
}

/**
* Overwrite the cached venv path in rye-venv.json
*
* Rye prevents unwanted behavior if people copy around .venv between projects.
* But we can be sure, that we are always working with the same project but the base path of previous runners might be different.
*/
function overwriteCachedVenvPath(): void {
function doesCachedVenvPathMatchCurrentVenvPath(): boolean {
const ryeVenvPath = `${venvPath}/rye-venv.json`
let ryeVenv = JSON.parse(fs.readFileSync(ryeVenvPath, 'utf8'))
core.debug(`venv_path in cache: ${ryeVenv.venv_path}`)
core.debug(`Overwriting cached venv_path with ${venvPath}`)
ryeVenv.venv_path = venvPath
fs.writeFileSync(ryeVenvPath, JSON.stringify(ryeVenv))
const ryeVenv = JSON.parse(fs.readFileSync(ryeVenvPath, 'utf8'))
core.info(
`Checking if the cached .venv matches the current path: ${venvPath}`
)
if (ryeVenv.venv_path != venvPath) {
core.warning(
`The .venv in the cache cannot be used because it is from another location: ${ryeVenv.venv_path}`
)
return false
}
return true
}

async function restoreCacheLocal(
Expand Down
Loading