-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: include lock files in dependency hash for cache invalidation #8818
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
Open
dcabib
wants to merge
1
commit into
aws:develop
Choose a base branch
from
dcabib:fix/issue-8242-include-lockfiles-in-cache-hash
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,27 @@ | ||
| """Utility Class for Getting Function or Layer Manifest Dependency Hashes""" | ||
|
|
||
| import hashlib | ||
| import pathlib | ||
| from typing import Any, Optional | ||
|
|
||
| from samcli.lib.build.workflow_config import get_workflow_config | ||
| from samcli.lib.utils.hash import file_checksum | ||
|
|
||
|
|
||
| # Mapping of dependency managers to their lock file names | ||
| LOCK_FILE_MAPPING = { | ||
| "npm": "package-lock.json", | ||
| "npm-esbuild": "package-lock.json", | ||
| "bundler": "Gemfile.lock", | ||
| "gradle": "gradle.lockfile", | ||
| "cli-package": "packages.lock.json", | ||
| "modules": "go.sum", | ||
| "cargo": "Cargo.lock", | ||
| "uv": "uv.lock", | ||
| "poetry": "poetry.lock", | ||
| } | ||
|
|
||
|
|
||
| # TODO Expand this class to hash specific sections of the manifest | ||
| class DependencyHashGenerator: | ||
| _code_uri: str | ||
|
|
@@ -50,15 +65,17 @@ def __init__( | |
| self._hash = None | ||
|
|
||
| def _calculate_dependency_hash(self) -> Optional[str]: | ||
| """Calculate the manifest file hash | ||
| """Calculate the manifest file hash, including lock file if applicable | ||
|
|
||
| Returns | ||
| ------- | ||
| Optional[str] | ||
| Returns manifest hash. If manifest does not exist or not supported, None will be returned. | ||
| Returns combined hash of manifest and lock file (if present). | ||
| If manifest does not exist or not supported, None will be returned. | ||
| """ | ||
| if self._manifest_path_override: | ||
| manifest_file = self._manifest_path_override | ||
| config = None | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we not wanting to add the config to the hash if there is a different manifest path? Can the lock file not still be resolved relative to the override path's directory? |
||
| else: | ||
| config = get_workflow_config(self._runtime, self._code_dir, self._base_dir) | ||
| manifest_file = config.manifest_name | ||
|
|
@@ -70,7 +87,24 @@ def _calculate_dependency_hash(self) -> Optional[str]: | |
| if not manifest_path.is_file(): | ||
| return None | ||
|
|
||
| return file_checksum(str(manifest_path), hash_generator=self._hash_generator) | ||
| manifest_hash = file_checksum(str(manifest_path), hash_generator=self._hash_generator) | ||
|
|
||
| # Check if there's a lock file for this dependency manager | ||
| if config and config.dependency_manager in LOCK_FILE_MAPPING: | ||
| lock_file_name = LOCK_FILE_MAPPING[config.dependency_manager] | ||
| lock_file_path = pathlib.Path(self._code_dir, lock_file_name).resolve() | ||
|
|
||
| # If lock file exists, combine hashes | ||
| if lock_file_path.is_file(): | ||
| lock_file_hash = file_checksum(str(lock_file_path), hash_generator=self._hash_generator) | ||
|
|
||
| # Combine both hashes into a single hash | ||
| combined = f"{manifest_hash}:{lock_file_hash}" | ||
| hasher = self._hash_generator() if self._hash_generator else hashlib.md5() | ||
| hasher.update(combined.encode("utf-8")) | ||
| return hasher.hexdigest() | ||
|
|
||
| return manifest_hash | ||
|
|
||
| @property | ||
| def hash(self) -> Optional[str]: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we not need to add yarn.lock and pnpm-lock.yaml?