Skip to content

Commit 4aefe52

Browse files
authored
Support ruff discovery in pip build environments (#13591)
Resolves #13321. Contents of overlay: ```bash /private/var/folders/v0/l8q3ghks2gs5ns2_p63tyqh40000gq/T/pip-build-env-e0ukpbvo/overlay/bin: total 26M -rwxr-xr-x 1 bgabor8 staff 26M Oct 1 08:22 ruff drwxr-xr-x 3 bgabor8 staff 96 Oct 1 08:22 . drwxr-xr-x 4 bgabor8 staff 128 Oct 1 08:22 .. ``` Python executable: ```bash '/Users/bgabor8/git/github/ruff-find-bin-during-build/.venv/bin/python' ``` PATH is: ```bash ['/private/var/folders/v0/l8q3ghks2gs5ns2_p63tyqh40000gq/T/pip-build-env-e0ukpbvo/overlay/bin', '/private/var/folders/v0/l8q3ghks2gs5ns2_p63tyqh40000gq/T/pip-build-env-e0ukpbvo/normal/bin', '/Library/Frameworks/Python.framework/Versions/3.11/bin', '/Library/Frameworks/Python.framework/Versions/3.12/bin', ``` Not sure where to add tests, there does not seem to be any existing one. Can someone help me with that?
1 parent cc1f766 commit 4aefe52

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

python/ruff/__main__.py

+21
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ def find_ruff_bin() -> str:
3333
if os.path.isfile(target_path):
3434
return target_path
3535

36+
# Search for pip-specific build environments.
37+
#
38+
# See: https://github.com/pypa/pip/blob/102d8187a1f5a4cd5de7a549fd8a9af34e89a54f/src/pip/_internal/build_env.py#L87
39+
paths = os.environ.get("PATH", "").split(os.pathsep)
40+
if len(paths) >= 2:
41+
first, second = os.path.split(paths[0]), os.path.split(paths[1])
42+
# Search for both an `overlay` and `normal` folder within a `pip-build-env-{random}` folder. (The final segment
43+
# of the path is the `bin` directory.)
44+
if (
45+
len(first) >= 3
46+
and len(second) >= 3
47+
and first[-3].startswith("pip-build-env-")
48+
and first[-2] == "overlay"
49+
and second[-3].startswith("pip-build-env-")
50+
and second[-2] == "normal"
51+
):
52+
# The overlay must contain the ruff binary.
53+
candidate = os.path.join(first, ruff_exe)
54+
if os.path.isfile(candidate):
55+
return candidate
56+
3657
raise FileNotFoundError(scripts_path)
3758

3859

0 commit comments

Comments
 (0)