Skip to content

Commit 5928f5f

Browse files
authored
fix: prevent showing cmd on Windows and add handling for ruff execution with pythonw (#108)
* Prevent showing cmd on Windows and add handling for ruff execution with pythonw * Handle CREATE_NO_WINDOW subprocess flag value (only available on Windows)
1 parent a0a33a6 commit 5928f5f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ __pycache__/
44
*.swp
55
tags
66
/build/
7+
8+
.spyproject/

pylsp_ruff/plugin.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
from subprocess import PIPE, Popen
1111
from typing import Dict, Generator, List, Optional
1212

13+
if sys.platform == "win32":
14+
from subprocess import CREATE_NO_WINDOW
15+
else:
16+
# CREATE_NO_WINDOW flag only available on Windows.
17+
# Set constant as default `Popen` `creationflag` kwarg value (`0`)
18+
CREATE_NO_WINDOW = 0
19+
1320
if sys.version_info >= (3, 11):
1421
import tomllib
1522
else:
@@ -502,7 +509,7 @@ def find_executable(executable) -> List[str]:
502509
# try the python module
503510
if cmd is None:
504511
if importlib.util.find_spec("ruff") is not None:
505-
cmd = [sys.executable, "-m", "ruff"]
512+
cmd = [sys.executable.replace("pythonw", "python"), "-m", "ruff"]
506513

507514
# try system's ruff executable
508515
if cmd is None:
@@ -557,7 +564,7 @@ def run_ruff(
557564
cmd = [*find_executable(executable), str(subcommand), *arguments]
558565

559566
log.debug(f"Calling {cmd} on '{document_path}'")
560-
p = Popen(cmd, stdin=PIPE, stdout=PIPE)
567+
p = Popen(cmd, stdin=PIPE, stdout=PIPE, creationflags=CREATE_NO_WINDOW)
561568
(stdout, _) = p.communicate(document_source.encode())
562569

563570
if p.returncode != 0:

0 commit comments

Comments
 (0)