From 42b655b24fa13f76b9b4e10f5aba2ff1f7f3fbbb Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 16 May 2024 12:07:22 -0400 Subject: [PATCH] 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 :(. --- python/ruff/__main__.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/python/ruff/__main__.py b/python/ruff/__main__.py index fa8c656f32c7d..9db95031bc8df 100644 --- a/python/ruff/__main__.py +++ b/python/ruff/__main__.py @@ -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__":