Skip to content

Commit 1af1a4a

Browse files
committed
add shutils in case -m fails
1 parent 7193b90 commit 1af1a4a

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

pylsp_ruff/plugin.py

+46-21
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import json
33
import logging
44
import re
5-
import sys
65
import shutil
6+
import sys
7+
from functools import cache
78
from pathlib import PurePath
89
from subprocess import PIPE, Popen
910
from typing import Dict, Generator, List, Optional
@@ -117,7 +118,6 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
117118
Document to apply ruff on.
118119
119120
"""
120-
121121
log.debug(f"textDocument/formatting: {document}")
122122
outcome = yield
123123
result = outcome.get_result()
@@ -179,7 +179,6 @@ def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
179179
List of dicts containing the diagnostics.
180180
181181
"""
182-
183182
with workspace.report_progress("lint: ruff"):
184183
settings = load_settings(workspace, document.path)
185184
checks = run_ruff_check(document=document, settings=settings)
@@ -488,6 +487,24 @@ def run_ruff_format(
488487
)
489488

490489

490+
@cache
491+
def find_executable(executable):
492+
if executable is not None:
493+
cmd = [executable]
494+
try:
495+
Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()
496+
except Exception:
497+
log.error(f"Can't execute ruff with given executable '{executable}'.")
498+
finally:
499+
return cmd
500+
else:
501+
cmd = [sys.executable, "-m", "ruff"]
502+
(_, stderr) = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()
503+
if "No module named ruff" in stderr.decode():
504+
cmd = [shutil.which("ruff")]
505+
return cmd
506+
507+
491508
def run_ruff(
492509
settings: PluginSettings,
493510
document_path: str,
@@ -519,27 +536,35 @@ def run_ruff(
519536
String containing the result in json format.
520537
521538
"""
522-
executable = settings.executable or shutil.which("ruff")
539+
executable = settings.executable
523540

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

526-
p = None
527-
if executable is not None:
528-
log.debug(f"Calling {executable} with args: {arguments} on '{document_path}'")
529-
try:
530-
cmd = [executable, str(subcommand)]
531-
cmd.extend(arguments)
532-
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
533-
except Exception:
534-
log.error(f"Can't execute ruff with given executable '{executable}'.")
535-
if p is None:
536-
log.debug(
537-
f"Calling ruff via '{sys.executable} -m ruff'"
538-
f" with args: {arguments} on '{document_path}'"
539-
)
540-
cmd = [sys.executable, "-m", "ruff", str(subcommand)]
541-
cmd.extend(arguments)
542-
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
543+
# p = None
544+
# if executable is not None:
545+
# log.debug(f"Calling {executable} with args: {arguments} on '{document_path}'")
546+
# try:
547+
# cmd = [executable, str(subcommand)]
548+
# cmd.extend(arguments)
549+
# p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
550+
# (stdout, stderr) = p.communicate(document_source.encode())
551+
# except Exception:
552+
# log.error(f"Can't execute ruff with given executable '{executable}'.")
553+
# else:
554+
# cmd = [sys.executable, "-m", "ruff", str(subcommand)]
555+
# cmd.extend(arguments)
556+
# p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
557+
# (stdout, stderr) = p.communicate(document_source.encode())
558+
# if "No module named ruff" in stderr.decode():
559+
# cmd = [shutil.which("ruff"), str(subcommand)]
560+
#
561+
# p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
562+
# (stdout, stderr) = p.communicate(document_source.encode())
563+
cmd = find_executable(executable)
564+
cmd.extend([str(subcommand), *arguments])
565+
566+
log.debug(f"Calling {cmd} on '{document_path}'")
567+
p = Popen(cmd, stdin=PIPE, stdout=PIPE)
543568
(stdout, stderr) = p.communicate(document_source.encode())
544569

545570
if p.returncode != 0:

0 commit comments

Comments
 (0)