|
2 | 2 | import json
|
3 | 3 | import logging
|
4 | 4 | import re
|
5 |
| -import sys |
6 | 5 | import shutil
|
| 6 | +import sys |
| 7 | +from functools import cache |
7 | 8 | from pathlib import PurePath
|
8 | 9 | from subprocess import PIPE, Popen
|
9 | 10 | from typing import Dict, Generator, List, Optional
|
@@ -117,7 +118,6 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
|
117 | 118 | Document to apply ruff on.
|
118 | 119 |
|
119 | 120 | """
|
120 |
| - |
121 | 121 | log.debug(f"textDocument/formatting: {document}")
|
122 | 122 | outcome = yield
|
123 | 123 | result = outcome.get_result()
|
@@ -179,7 +179,6 @@ def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
|
179 | 179 | List of dicts containing the diagnostics.
|
180 | 180 |
|
181 | 181 | """
|
182 |
| - |
183 | 182 | with workspace.report_progress("lint: ruff"):
|
184 | 183 | settings = load_settings(workspace, document.path)
|
185 | 184 | checks = run_ruff_check(document=document, settings=settings)
|
@@ -488,6 +487,24 @@ def run_ruff_format(
|
488 | 487 | )
|
489 | 488 |
|
490 | 489 |
|
| 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 | + |
491 | 508 | def run_ruff(
|
492 | 509 | settings: PluginSettings,
|
493 | 510 | document_path: str,
|
@@ -519,27 +536,35 @@ def run_ruff(
|
519 | 536 | String containing the result in json format.
|
520 | 537 |
|
521 | 538 | """
|
522 |
| - executable = settings.executable or shutil.which("ruff") |
| 539 | + executable = settings.executable |
523 | 540 |
|
524 | 541 | arguments = subcommand.build_args(document_path, settings, fix, extra_arguments)
|
525 | 542 |
|
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) |
543 | 568 | (stdout, stderr) = p.communicate(document_source.encode())
|
544 | 569 |
|
545 | 570 | if p.returncode != 0:
|
|
0 commit comments