Skip to content

Commit 18a9a91

Browse files
committed
✨ add extra_paths config
1 parent 160c98e commit 18a9a91

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

pylsp/plugins/pylint_lint.py

+25-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class PylintConfig(TypedDict):
5454
enabled: Optional[bool]
5555
args: Optional[list[str]]
5656
executable: Optional[str]
57+
extra_paths: Optional[list[str]]
5758

5859

5960
class PylintLinter:
@@ -218,6 +219,7 @@ def pylsp_settings() -> dict:
218219
pylint_config: PylintConfig = {
219220
"enabled": False,
220221
"args": [],
222+
"extra_paths": None,
221223
# disabled by default as it can slow down the workflow
222224
"executable": None,
223225
}
@@ -234,10 +236,13 @@ def pylsp_lint(
234236
log.debug("Got pylint settings: %s", settings)
235237
# pylint >= 2.5.0 is required for working through stdin and only
236238
# available with python3
239+
extra_paths = settings["extra_paths"] or []
237240
if settings.get("executable") and sys.version_info[0] >= 3:
238241
flag_list = build_args_stdio(settings)
239242
pylint_executable = settings["executable"] or "pylint"
240-
return pylint_lint_stdin(pylint_executable, document, flag_list)
243+
return pylint_lint_stdin(
244+
pylint_executable, document, flag_list, extra_paths
245+
)
241246
flags = _build_pylint_flags(settings)
242247
return PylintLinter.lint(document, is_saved, flags=flags)
243248

@@ -261,6 +266,7 @@ def pylint_lint_stdin(
261266
pylint_executable: str,
262267
document: Document,
263268
flags: list[str],
269+
extra_paths: list[str],
264270
) -> list[Diagnostic]:
265271
"""Run pylint linter from stdin.
266272
@@ -278,12 +284,12 @@ def pylint_lint_stdin(
278284
:return: linting diagnostics
279285
:rtype: list
280286
"""
281-
pylint_result = _run_pylint_stdio(pylint_executable, document, flags)
287+
pylint_result = _run_pylint_stdio(pylint_executable, document, flags, extra_paths)
282288
return _parse_pylint_stdio_result(document, pylint_result)
283289

284290

285291
def _run_pylint_stdio(
286-
pylint_executable: str, document: Document, flags: list[str]
292+
pylint_executable: str, document: Document, flags: list[str], extra_paths: list[str]
287293
) -> str:
288294
"""Run pylint in popen.
289295
@@ -302,14 +308,28 @@ def _run_pylint_stdio(
302308
cmd = [pylint_executable]
303309
cmd.extend(flags)
304310
cmd.extend(["--from-stdin", document.path])
305-
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
311+
workspace_root = document._workspace.root_path
312+
python_paths = [workspace_root] + [
313+
os.path.join(workspace_root, path) for path in extra_paths
314+
]
315+
p = Popen(
316+
cmd,
317+
stdin=PIPE,
318+
stdout=PIPE,
319+
stderr=PIPE,
320+
env={"PYTHONPATH": ":".join(python_paths)},
321+
) # pylint: disable=consider-using-with
306322
except IOError:
307323
log.debug("Can't execute %s. Trying with 'python -m pylint'", pylint_executable)
308324
cmd = [sys.executable, "-m", "pylint"]
309325
cmd.extend(flags)
310326
cmd.extend(["--from-stdin", document.path])
311327
p = Popen( # pylint: disable=consider-using-with
312-
cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE
328+
cmd,
329+
stdin=PIPE,
330+
stdout=PIPE,
331+
stderr=PIPE,
332+
env={"PYTHONPATH": ":".join(python_paths)},
313333
)
314334
(stdout, stderr) = p.communicate(document.source.encode())
315335
if stderr:

0 commit comments

Comments
 (0)