Skip to content

Commit

Permalink
Locate ruff executable in 'bin' directory as installed by 'pip instal…
Browse files Browse the repository at this point in the history
…l --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 :(.
  • Loading branch information
jaraco authored May 16, 2024
1 parent f67c02c commit 42b655b
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions python/ruff/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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__":
Expand Down

0 comments on commit 42b655b

Please sign in to comment.