Skip to content

Commit

Permalink
Fix --pip-log re-use.
Browse files Browse the repository at this point in the history
Previously, when only 1 resolve target was specified, a `--pip-log`
would be re-used which would append the current Pip log outout with the
prior output. For a normal PEX build, this was potentially confusing,
but for a `pex3 lock` command it could lead to errors since the log is
used to generate the lock.

Fixes pex-tool#2568
  • Loading branch information
jsirois committed Oct 23, 2024
1 parent d94fa6a commit 5c41a32
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pex/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hashlib
import itertools
import os
import shutil
import zipfile
from abc import abstractmethod
from collections import OrderedDict, defaultdict
Expand Down Expand Up @@ -85,9 +86,7 @@ def create(
):
# type: (...) -> PipLogManager
log_by_target = {} # type: Dict[Target, str]
if log and len(targets) == 1:
log_by_target[targets[0]] = log
elif log:
if log:
log_dir = safe_mkdtemp(prefix="pex-pip-log.")
log_by_target.update(
(target, os.path.join(log_dir, "pip.{target}.log".format(target=target.id)))
Expand All @@ -113,7 +112,12 @@ def _target_id(target):
def finalize_log(self):
# type: () -> None
target_count = len(self._log_by_target)
if target_count <= 1:
if target_count == 0:
return

if self.log and target_count == 1:
log = next(iter(self._log_by_target.values()))
shutil.move(log, self.log)
return

with safe_open(self.log, "a") as out_fp:
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/resolve/test_issue_2568.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2024 Pex project contributors.
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import absolute_import

import re
from textwrap import dedent

from pex import targets
from testing.cli import run_pex3
from testing.pytest.tmp import Tempdir


def test_independent_logs_for_independent_runs(tmpdir):
# type: (Tempdir) -> None

lock = tmpdir.join("lock.json")
log = tmpdir.join("pip.log")
run_pex3(
"lock", "sync", "--lock", lock, "--pip-log", log, "ansicolors==1.1.8", "cowsay==6.0"
).assert_success()
target = str(targets.current().platform.tag)
run_pex3("lock", "sync", "--lock", lock, "--pip-log", log, "ansicolors==1.1.8").assert_success(
expected_error_re=r".*{footer}$".format(
footer=re.escape(
dedent(
"""\
Updates for lock generated by {target}:
Deleted cowsay 6
Updates to lock input requirements:
Deleted 'cowsay==6.0'
"""
).format(target=target)
)
),
re_flags=re.DOTALL,
)
run_pex3("lock", "sync", "--lock", lock, "--pip-log", log, "ansicolors==1.1.8").assert_success(
expected_error_re=r".*No updates for lock generated by {target}\.$".format(
target=re.escape(target)
),
re_flags=re.DOTALL,
)

0 comments on commit 5c41a32

Please sign in to comment.