Repository: pex-tool/pex

Issue:
A relative `--tmpdir` foils `pex3 lock create`.
Discovered this trying to upgrade Pants to Pex 2.1.86.

An absolute path works fine:
```
$ mkdir /tmp/.tmp
$ pex3 lock create --tmpdir /tmp/.tmp ansicolors
{"allow_builds": true, "allow_prereleases": false, "allow_wheels": true, "build_isolation": true, "constraints": [], "locked_resolves": [{"locked_requirements": [{"artifacts": [{"algorithm": "sha256", "hash": "00d2dde5a675579325902536738dd27e4fac1fd68f773fe36c21044eb559e187", "url": "https://files.pythonhosted.org/packages/53/18/a56e2fe47b259bb52201093a3a9d4a32014f9d85071ad07e9d60600890ca/ansicolors-1.1.8-py2.py3-none-any.whl"}], "project_name": "ansicolors", "requires_dists": [], "requires_python": null, "version": "1.1.8"}], "platform_tag": ["cp310", "cp310", "manylinux_2_35_x86_64"]}], "path_mappings": {}, "pex_version": "2.1.86", "prefer_older_binary": false, "requirements": ["ansicolors"], "requires_python": [], "resolver_version": "pip-legacy-resolver", "style": "strict", "transitive": true, "use_pep517": null}^jsirois@gill ~/dev/pantsbuild/jsirois-pex (main *) $ mkdir .tmp
```

But a relative path foils the explicit download save detection added in #1666 to fix #1665:
```
$ pex3 lock create --tmpdir .tmp ansicolors
Traceback (most recent call last):
  File "/home/jsirois/.venv/pex/bin/pex3", line 8, in <module>
    sys.exit(main())
  File "/home/jsirois/.venv/pex/lib/python3.10/site-packages/pex/cli/pex.py", line 30, in main
    result = catch(command.run)
  File "/home/jsirois/.venv/pex/lib/python3.10/site-packages/pex/result.py", line 101, in catch
    return func(*args, **kwargs)
  File "/home/jsirois/.venv/pex/lib/python3.10/site-packages/pex/cli/command.py", line 84, in run
    return subcommand_func(self)
  File "/home/jsirois/.venv/pex/lib/python3.10/site-packages/pex/cli/commands/lock.py", line 270, in _create
    create(
  File "/home/jsirois/.venv/pex/lib/python3.10/site-packages/pex/resolve/lockfile/__init__.py", line 293, in create
    create_lock_download_manager = CreateLockDownloadManager.create(
  File "/home/jsirois/.venv/pex/lib/python3.10/site-packages/pex/resolve/lockfile/__init__.py", line 208, in create
    artifact = file_artifacts_by_filename.get(f) or vcs_artifacts_by_pin[pin]
KeyError: Pin(project_name=ProjectName(project_name='ansicolors'), version=Version(version='1.1.8', parsed_version=<Version('1.1.8')>))
```


Discussion:

Resolving patch:
diff --git a/pex/pip/tool.py b/pex/pip/tool.py
--- a/pex/pip/tool.py
+++ b/pex/pip/tool.py
@@ -326,17 +326,6 @@ def __init__(
         self._lock_request = lock_request
         self._download_dir = download_dir
 
-        self._saved_re = re.compile(
-            r"Saved (?:{download_dir}){dir_sep}(?P<filename>.+)$".format(
-                download_dir="|".join(
-                    re.escape(path)
-                    for path in frozenset(
-                        (self._download_dir, os.path.realpath(self._download_dir))
-                    )
-                ),
-                dir_sep=re.escape(os.path.sep),
-            )
-        )
         self._saved = set()  # type: Set[Pin]
 
         self._resolved_requirements = []  # type: List[ResolvedRequirement]
@@ -484,10 +473,12 @@ def analyze(self, line):
                 )
             return self.Continue()
 
-        match = self._saved_re.search(line)
+        match = re.search(r"Saved (?P<file_path>.+)$", line)
         if match:
             self._saved.add(
-                Pin.canonicalize(ProjectNameAndVersion.from_filename(match.group("filename")))
+                Pin.canonicalize(
+                    ProjectNameAndVersion.from_filename(os.path.basename(match.group("file_path")))
+                )
             )
             return self.Continue()
 


Test patch:
diff --git a/tests/integration/cli/commands/test_issue_1665.py b/tests/integration/cli/commands/test_issue_1665.py
--- a/tests/integration/cli/commands/test_issue_1665.py
+++ b/tests/integration/cli/commands/test_issue_1665.py
@@ -22,7 +22,7 @@ def test_lock_black(tmpdir):
     # type: (Any) -> None
 
     lock = os.path.join(str(tmpdir), "lock")
-    run_pex3(
+    lock_create_args = (
         "lock",
         "create",
         "--resolver-version",
@@ -32,7 +32,17 @@ def test_lock_black(tmpdir):
         "black==22.3.0",
         "-o",
         lock,
-    ).assert_success()
-    result = run_pex_command(args=["--lock", lock, "-c", "black", "--", "--version"])
-    result.assert_success()
-    assert " 22.3.0 " in result.output
+    )
+
+    def assert_lock(*extra_lock_args, **extra_popen_args):
+        run_pex3(*(lock_create_args + extra_lock_args), **extra_popen_args).assert_success()
+        result = run_pex_command(args=["--lock", lock, "-c", "black", "--", "--version"])
+        result.assert_success()
+        assert " 22.3.0 " in result.output
+
+    assert_lock()
+
+    cwd = os.path.join(str(tmpdir), "cwd")
+    tmpdir = os.path.join(cwd, ".tmp")
+    os.makedirs(tmpdir)
+    assert_lock("--tmpdir", ".tmp", cwd=cwd)