Skip to content

Commit

Permalink
support for system ruff executable
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJJ committed Sep 12, 2024
1 parent 790e76c commit 4bf5086
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions pylsp_ruff/plugin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import enum
import importlib.util
import json
import logging
import re
import shutil
import sys
from functools import lru_cache
from pathlib import PurePath
from subprocess import PIPE, Popen
from typing import Dict, Generator, List, Optional
Expand Down Expand Up @@ -116,7 +119,6 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
Document to apply ruff on.
"""

log.debug(f"textDocument/formatting: {document}")
outcome = yield
result = outcome.get_result()
Expand Down Expand Up @@ -178,7 +180,6 @@ def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
List of dicts containing the diagnostics.
"""

with workspace.report_progress("lint: ruff"):
settings = load_settings(workspace, document.path)
checks = run_ruff_check(document=document, settings=settings)
Expand Down Expand Up @@ -487,6 +488,31 @@ def run_ruff_format(
)


@lru_cache
def find_executable(executable):
cmd = None
# use the explicit executable configuration
if executable is not None:
exe_path = shutil.which(executable)
if exe_path is not None:
cmd = [executable]
else:
raise RuntimeError(f"configured ruff executable not found: {executable!r}")

# try the python module
if cmd is None:
if importlib.util.find_spec("ruff") is not None:
cmd = [sys.executable, "-m", "ruff"]

# try system's ruff executable
if cmd is None:
system_exe = shutil.which("ruff")
if system_exe is not None:
cmd = [system_exe]

return cmd


def run_ruff(
settings: PluginSettings,
document_path: str,
Expand Down Expand Up @@ -522,23 +548,11 @@ def run_ruff(

arguments = subcommand.build_args(document_path, settings, fix, extra_arguments)

p = None
if executable is not None:
log.debug(f"Calling {executable} with args: {arguments} on '{document_path}'")
try:
cmd = [executable, str(subcommand)]
cmd.extend(arguments)
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
except Exception:
log.error(f"Can't execute ruff with given executable '{executable}'.")
if p is None:
log.debug(
f"Calling ruff via '{sys.executable} -m ruff'"
f" with args: {arguments} on '{document_path}'"
)
cmd = [sys.executable, "-m", "ruff", str(subcommand)]
cmd.extend(arguments)
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
cmd = [*find_executable(executable), str(subcommand)]
cmd.extend(arguments)

log.debug(f"Calling {cmd} on '{document_path}'")
p = Popen(cmd, stdin=PIPE, stdout=PIPE)
(stdout, stderr) = p.communicate(document_source.encode())

if p.returncode != 0:
Expand Down

0 comments on commit 4bf5086

Please sign in to comment.