Locate ruff executable in 'bin' directory as installed by 'pip install --target'. (#11450)

Fixes #11246

## Summary

This change adds an intermediate additional search path for
`find_ruff_bin`.

I would have added this path as the last one, except that the last one
is the one reported to the user, so I made this one second to last.

## Test Plan

It's shown to work with this command:

```
 ~ @ pip-run git+https://github.com/jaraco/ruff@feature/honor-install-target-bin -- -m ruff --version
ruff 0.4.4
```

I tried running the same command on Windows, which should work in
theory, but building ruff from source on Windows is complicated. Even
after installing Rust, ruff fails to build when `libmimalloc-sys` fails
to build because `gcc` isn't installed (and the error message points to
a [broken
anchor](https://github.com/rust-lang/cc-rs#compile-time-requirements)).
I was really hoping Rust would get us away from the Windows as
second-class-citizen model :(.
This commit is contained in:
Jason R. Coombs 2024-05-16 12:07:22 -04:00 committed by GitHub
parent f67c02c837
commit 42b655b24f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,9 +8,9 @@ def find_ruff_bin() -> str:
ruff_exe = "ruff" + sysconfig.get_config_var("EXE")
path = os.path.join(sysconfig.get_path("scripts"), ruff_exe)
if os.path.isfile(path):
return path
scripts_path = os.path.join(sysconfig.get_path("scripts"), ruff_exe)
if os.path.isfile(scripts_path):
return scripts_path
if sys.version_info >= (3, 10):
user_scheme = sysconfig.get_preferred_scheme("user")
@ -21,11 +21,19 @@ def find_ruff_bin() -> str:
else:
user_scheme = "posix_user"
path = os.path.join(sysconfig.get_path("scripts", scheme=user_scheme), ruff_exe)
if os.path.isfile(path):
return path
user_path = os.path.join(
sysconfig.get_path("scripts", scheme=user_scheme), ruff_exe
)
if os.path.isfile(user_path):
return user_path
raise FileNotFoundError(path)
# Search in `bin` adjacent to package root (as created by `pip install --target`).
pkg_root = os.path.dirname(os.path.dirname(__file__))
target_path = os.path.join(pkg_root, "bin", ruff_exe)
if os.path.isfile(target_path):
return target_path
raise FileNotFoundError(scripts_path)
if __name__ == "__main__":