fix issues in discovering ruff in pip build environments (#13881)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz (push) Blocked by required conditions
CI / Fuzz the parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->
Changes in this PR https://github.com/astral-sh/ruff/pull/13591 did not
allow correct discovery in pip build environments.

```python
# both of these variables are tuple[str, str] (length is 2)
first, second = os.path.split(paths[0]), os.path.split(paths[1])

# so these length checks are guaranteed to fail even for build environment folders
if (
    len(first) >= 3
    and len(second) >= 3 
    ...
)
```

~~Here we instead use `pathlib`, and we check all `pip-build-env-` paths
for the folder that is expected to contain the `ruff` executable.~~

Here we update the logic to more properly split out the path components
that we use for `pip-build-env-` inspection.

## Test Plan

I've checked this manually against a workflow that was failing, I'm not
sure what to do for real tests. The same issues apply as with the
previous PR.

---------

Co-authored-by: Jonathan Surany <jsurany@bloomberg.net>
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
This commit is contained in:
jsurany 2024-10-29 11:50:29 -04:00 committed by GitHub
parent 8d98aea6c4
commit 60a2dc53e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -35,22 +35,40 @@ def find_ruff_bin() -> str:
# Search for pip-specific build environments.
#
# Expect to find ruff in <prefix>/pip-build-env-<rand>/overlay/bin/ruff
# Expect to find a "normal" folder at <prefix>/pip-build-env-<rand>/normal
#
# See: https://github.com/pypa/pip/blob/102d8187a1f5a4cd5de7a549fd8a9af34e89a54f/src/pip/_internal/build_env.py#L87
paths = os.environ.get("PATH", "").split(os.pathsep)
if len(paths) >= 2:
first, second = os.path.split(paths[0]), os.path.split(paths[1])
# Search for both an `overlay` and `normal` folder within a `pip-build-env-{random}` folder. (The final segment
# of the path is the `bin` directory.)
def get_last_three_path_parts(path: str) -> list[str]:
"""Return a list of up to the last three parts of a path."""
parts = []
while len(parts) < 3:
head, tail = os.path.split(path)
if tail or head != path:
parts.append(tail)
path = head
else:
parts.append(path)
break
return parts
maybe_overlay = get_last_three_path_parts(paths[0])
maybe_normal = get_last_three_path_parts(paths[1])
if (
len(first) >= 3
and len(second) >= 3
and first[-3].startswith("pip-build-env-")
and first[-2] == "overlay"
and second[-3].startswith("pip-build-env-")
and second[-2] == "normal"
len(maybe_normal) >= 3
and maybe_normal[-1].startswith("pip-build-env-")
and maybe_normal[-2] == "normal"
and len(maybe_overlay) >= 3
and maybe_overlay[-1].startswith("pip-build-env-")
and maybe_overlay[-2] == "overlay"
):
# The overlay must contain the ruff binary.
candidate = os.path.join(first, ruff_exe)
candidate = os.path.join(paths[0], ruff_exe)
if os.path.isfile(candidate):
return candidate