From d2f7d9bda011a9f029438187b1691dbbb4c1ac31 Mon Sep 17 00:00:00 2001 From: Chris Burroughs Date: Fri, 13 Sep 2024 14:21:20 -0400 Subject: [PATCH] diff with previous versions when generating builtin lockfiles By copying the prior version, we get the same Lockfile diff as one would for a "regular" lockfile. Demo: ``` $ pants run build-support/bin/generate_builtin_lockfiles.py -- semgrep Lockfile diff: semgrep.lock [semgrep] == Upgraded dependencies == semgrep 1.86.0 --> 1.87.0 zipp 3.20.1 --> 3.20.2 == !! Downgraded dependencies !! == rich 13.8.1 --> 13.5.3 == Removed dependencies == annotated-types 0.7.0 pydantic 2.8.2 pydantic-core 2.20.1 ``` fixes #21388 --- .../bin/generate_builtin_lockfiles.py | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/build-support/bin/generate_builtin_lockfiles.py b/build-support/bin/generate_builtin_lockfiles.py index 4ad37ffd4a3..7e0c533ed59 100644 --- a/build-support/bin/generate_builtin_lockfiles.py +++ b/build-support/bin/generate_builtin_lockfiles.py @@ -268,6 +268,17 @@ def generate_jvm_tool_lockfiles(tools: Sequence[JvmTool], dry_run: bool) -> None def generate(buildroot: str, tools: Sequence[Tool], args: Sequence[str], dry_run: bool) -> None: + def lockfile_inrepo_dest(lockfile_pkg, lockfile_filename): + return os.path.join( + "src", + "python", + lockfile_pkg.replace(".", os.path.sep), + lockfile_filename, + ) + + def lockfile_buildroot_filename(lockfile_name): + return os.path.join(buildroot, lockfile_name) + pants_repo_root = get_buildroot() touch(os.path.join(buildroot, "pants.toml")) backends = sorted({tool.backend for tool in tools}) @@ -287,19 +298,24 @@ def generate(buildroot: str, tools: Sequence[Tool], args: Sequence[str], dry_run logger.info("Would run: " + " ".join(args)) return + # If there is a pre-existing lockfile, seed it so we get the pretty lockfile diff + for tool in tools: + lockfile_pkg, lockfile_filename = tool.cls.default_lockfile_resource + lockfile_dest = lockfile_inrepo_dest(lockfile_pkg, lockfile_filename) + if os.path.isfile(lockfile_dest): + logger.debug(f"copying existing lockfile from {lockfile_dest}") + shutil.copy(lockfile_dest, lockfile_buildroot_filename(tool.lockfile_name)) + logger.debug("Running: " + " ".join(args)) subprocess.run(args, cwd=buildroot, check=True) # Copy the generated lockfiles from the tmp repo to the Pants repo. for tool in tools: lockfile_pkg, lockfile_filename = tool.cls.default_lockfile_resource - lockfile_dest = os.path.join( - "src", - "python", - lockfile_pkg.replace(".", os.path.sep), - lockfile_filename, + shutil.copy( + lockfile_buildroot_filename(tool.lockfile_name), + lockfile_inrepo_dest(lockfile_pkg, lockfile_filename), ) - shutil.copy(os.path.join(buildroot, tool.lockfile_name), lockfile_dest) def main() -> None: