Skip to content

Commit 86734bc

Browse files
Tobie Tusingttusing
authored andcommitted
add a warning when regenerating the lockfile for packages
do not include env vars when hashing packages add changie
1 parent ada5d3b commit 86734bc

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Fixes
2+
body: Fixes issue with package locking, where using environment variables would cause lockfile regeneration in different environments
3+
time: 2025-08-23T15:55:51.916929-07:00
4+
custom:
5+
Author: ttusing
6+
Issue: "11953"

core/dbt/contracts/project.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,27 @@ class GitPackage(Package):
7878
subdirectory: Optional[str] = None
7979
unrendered: Dict[str, Any] = field(default_factory=dict)
8080
name: Optional[str] = None
81+
exclude_env_vars_from_hash: Optional[bool] = field(
82+
default=None, metadata={"alias": "exclude-env-vars-from-hash"}
83+
)
8184

8285
def get_revisions(self) -> List[str]:
8386
if self.revision is None:
8487
return []
8588
else:
8689
return [str(self.revision)]
8790

91+
def to_dict_for_hash(self) -> Dict[str, Any]:
92+
"""Create a dict representation for hash calculation that can optionally exclude env vars"""
93+
data = self.to_dict()
94+
if self.exclude_env_vars_from_hash:
95+
# Use unrendered git URL if available to exclude env vars from hash
96+
if "git" in self.unrendered:
97+
data["git"] = self.unrendered["git"]
98+
# Remove the exclude-env-vars-from-hash flag itself from the hash
99+
data.pop("exclude-env-vars-from-hash", None)
100+
return data
101+
88102

89103
@dataclass
90104
class PrivatePackage(Package):

core/dbt/events/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,14 @@ def message(self) -> str:
15171517
return f"Found duplicate package in packages.yml, removing: {self.removed_package}"
15181518

15191519

1520+
class DepsLockfileRegenerating(WarnLevel):
1521+
def code(self):
1522+
return "M034"
1523+
1524+
def message(self) -> str:
1525+
return f"Package lockfile is out of sync with packages.yml. Regenerating lockfile at: {self.lock_filepath}"
1526+
1527+
15201528
class DepsScrubbedPackageName(WarnLevel):
15211529
def code(self):
15221530
return "M035"

core/dbt/task/deps.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
DepsFoundDuplicatePackage,
2222
DepsInstallInfo,
2323
DepsListSubdirectory,
24+
DepsLockfileRegenerating,
2425
DepsLockUpdating,
2526
DepsNoPackagesFound,
2627
DepsNotifyUpdatesAvailable,
@@ -50,7 +51,14 @@ def _create_sha1_hash(packages: List[PackageSpec]) -> str:
5051
Returns:
5152
str: SHA1 hash of the packages list
5253
"""
53-
package_strs = [json.dumps(package.to_dict(), sort_keys=True) for package in packages]
54+
package_strs = []
55+
for package in packages:
56+
if hasattr(package, "to_dict_for_hash"):
57+
package_dict = package.to_dict_for_hash()
58+
else:
59+
package_dict = package.to_dict()
60+
package_strs.append(json.dumps(package_dict, sort_keys=True))
61+
5462
package_strs = sorted(package_strs)
5563

5664
return sha1("\n".join(package_strs).encode("utf-8")).hexdigest()
@@ -220,6 +228,7 @@ def run(self) -> None:
220228
current_hash = _create_sha1_hash(self.project.packages.packages)
221229
previous_hash = load_yml_dict(lock_file_path).get(PACKAGE_LOCK_HASH_KEY, None)
222230
if previous_hash != current_hash:
231+
fire_event(DepsLockfileRegenerating(lock_filepath=lock_file_path))
223232
self.lock()
224233

225234
# Early return when 'dbt deps --lock'

tests/unit/contracts/test_git_package_env_var_exclusion.py

Whitespace-only changes.

0 commit comments

Comments
 (0)