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: