Skip to content

Change Pylint run to set cwd correctly #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions pylsp/plugins/pylint_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from subprocess import Popen, PIPE
import os

from pylint.epylint import py_run
from pylsp import hookimpl, lsp

try:
Expand Down Expand Up @@ -85,20 +84,21 @@ def lint(cls, document, is_saved, flags=''):
# save.
return cls.last_diags[document.path]

# py_run will call shlex.split on its arguments, and shlex.split does
# not handle Windows paths (it will try to perform escaping). Turn
# backslashes into forward slashes first to avoid this issue.
path = document.path
if sys.platform.startswith('win'):
path = path.replace('\\', '/')

pylint_call = '{} -f json {}'.format(path, flags)
log.debug("Calling pylint with '%s'", pylint_call)
json_out, err = py_run(pylint_call, return_std=True)

# Get strings
json_out = json_out.getvalue()
err = err.getvalue()
cmd = [
'python',
'-c',
'import sys; from pylint.lint import Run; Run(sys.argv[1:])',
'-f',
'json',
document.path
] + (str(flags).split(' ') if flags else [])
log.debug("Calling pylint with '%s'", ' '.join(cmd))

with Popen(cmd, stdout=PIPE, stderr=PIPE,
cwd=document._workspace.root_path, universal_newlines=True) as process:
process.wait()
json_out = process.stdout.read()
err = process.stderr.read()

if err != '':
log.error("Error calling pylint: '%s'", err)
Expand Down
6 changes: 4 additions & 2 deletions test/plugins/test_pylint_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# Copyright 2021- Python Language Server Contributors.

import contextlib
from pathlib import Path
import os
import tempfile

from pylsp import lsp, uris
from pylsp.workspace import Document
from pylsp.workspace import Document, Workspace
from pylsp.plugins import pylint_lint

DOC_URI = uris.from_fs_path(__file__)
Expand Down Expand Up @@ -90,8 +91,9 @@ def test_lint_free_pylint(config, workspace):
# Can't use temp_document because it might give us a file that doesn't
# match pylint's naming requirements. We should be keeping this file clean
# though, so it works for a test of an empty lint.
ws = Workspace(str(Path(__file__).absolute().parents[2]), workspace._endpoint)
assert not pylint_lint.pylsp_lint(
config, workspace, Document(uris.from_fs_path(__file__), workspace), True)
config, ws, Document(uris.from_fs_path(__file__), ws), True)


def test_lint_caching(workspace):
Expand Down