Skip to content

Commit cd8c3bc

Browse files
Ttayujhossbach
authored andcommitted
feat: Support for calling the Ruff module.
1 parent 0c0827d commit cd8c3bc

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

pylsp_ruff/ruff_lint.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
import sys
34
from pathlib import PurePath
45
from subprocess import PIPE, Popen
56

@@ -91,18 +92,19 @@ def run_ruff_lint(ruff_executable: str, document: Document, arguments: list) ->
9192
cmd = [ruff_executable]
9293
cmd.extend(arguments)
9394
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
94-
(stdout, stderr) = p.communicate(document.source.encode())
95-
96-
if stderr:
97-
log.error(f"Error running ruff: {stderr.decode()}")
95+
except Exception:
96+
log.debug(
97+
f"Can't execute {ruff_executable}. Trying with '{sys.executable} -m ruff'"
98+
)
99+
cmd = [sys.executable, "-m", "ruff"]
100+
cmd.extend(arguments)
101+
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
102+
(stdout, stderr) = p.communicate(document.source.encode())
98103

99-
return stdout.decode()
100-
except Exception as e:
101-
# Ruff doesn't yet support calling with python -m ruff,
102-
# see https://github.com/charliermarsh/ruff/issues/593
103-
log.error(f"Error running {ruff_executable}: {e}")
104+
if stderr:
105+
log.error(f"Error running ruff: {stderr.decode()}")
104106

105-
return ""
107+
return stdout.decode()
106108

107109

108110
def parse_ruff_stdout(stdout: str) -> list:

0 commit comments

Comments
 (0)