Skip to content
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

Add support for legacy python.linting.cwd setting #251

Merged
merged 3 commits into from
Jan 19, 2023
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
2 changes: 1 addition & 1 deletion bundled/tool/lsp_jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ def __init__(self):
self._lock = threading.Lock()
self._thread_pool = ThreadPoolExecutor(10)

@atexit.register
def stop_all_processes(self):
"""Send exit command to all processes and shutdown transport."""
for i in self._rpc.values():
Expand Down Expand Up @@ -175,6 +174,7 @@ def get_json_rpc(self, workspace: str) -> JsonRpc:


_process_manager = ProcessManager()
atexit.register(_process_manager.stop_all_processes)


def _get_json_rpc(workspace: str) -> Union[JsonRpc, None]:
Expand Down
9 changes: 5 additions & 4 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ def _create_workspace_edits(
return lsp.WorkspaceEdit(
document_changes=[
lsp.TextDocumentEdit(
text_document=lsp.VersionedTextDocumentIdentifier(
text_document=lsp.OptionalVersionedTextDocumentIdentifier(
uri=document.uri,
version=0 if document.version is None else document.version,
version=document.version,
),
edits=results,
)
Expand Down Expand Up @@ -390,6 +390,7 @@ def _update_workspace_settings(settings):
if not settings:
key = os.getcwd()
WORKSPACE_SETTINGS[key] = {
"cwd": key,
"workspaceFS": key,
"workspace": uris.from_fs_path(key),
"path": [],
Expand Down Expand Up @@ -461,7 +462,7 @@ def _run_tool_on_document(
settings = copy.deepcopy(_get_settings_by_document(document))

code_workspace = settings["workspaceFS"]
cwd = settings["workspaceFS"]
cwd = settings["cwd"]

use_path = False
use_rpc = False
Expand Down Expand Up @@ -543,7 +544,7 @@ def _run_tool_on_document(
def _run_tool(extra_args: Sequence[str], settings: Dict[str, Any]) -> utils.RunResult:
"""Runs tool."""
code_workspace = settings["workspaceFS"]
cwd = settings["workspaceFS"]
cwd = settings["cwd"]

use_path = False
use_rpc = False
Expand Down
24 changes: 23 additions & 1 deletion src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import { ConfigurationChangeEvent, WorkspaceFolder } from 'vscode';
import { traceLog } from './log/logging';
import { getInterpreterDetails } from './python';
import { getConfiguration, getWorkspaceFolders } from './vscodeapi';

Expand All @@ -14,6 +15,7 @@ const DEFAULT_SEVERITY: Record<string, string> = {
info: 'Information',
};
export interface ISettings {
cwd: string;
workspace: string;
args: string[];
severity: Record<string, string>;
Expand Down Expand Up @@ -48,7 +50,13 @@ function getArgs(namespace: string, workspace: WorkspaceFolder): string[] {
}

const legacyConfig = getConfiguration('python', workspace.uri);
return legacyConfig.get<string[]>('linting.pylintArgs', []);
const legacyArgs = legacyConfig.get<string[]>('linting.pylintArgs', []);
if (legacyArgs.length > 0) {
traceLog('Using legacy Pylint args from `python.linting.pylintArgs`');
return legacyArgs;
}

return [];
}

function getPath(namespace: string, workspace: WorkspaceFolder): string[] {
Expand All @@ -62,11 +70,24 @@ function getPath(namespace: string, workspace: WorkspaceFolder): string[] {
const legacyConfig = getConfiguration('python', workspace.uri);
const legacyPath = legacyConfig.get<string>('linting.pylintPath', '');
if (legacyPath.length > 0 && legacyPath !== 'pylint') {
traceLog('Using legacy Pylint path from `python.linting.pylintPath`');
return [legacyPath];
}
return [];
}

function getCwd(namespace: string, workspace: WorkspaceFolder): string {
const legacyConfig = getConfiguration('python', workspace.uri);
const legacyCwd = legacyConfig.get<string>('linting.cwd');

if (legacyCwd) {
traceLog('Using cwd from `python.linting.cwd`.');
return resolveWorkspace(workspace, legacyCwd);
}

return workspace.uri.fsPath;
}

export function getInterpreterFromSetting(namespace: string) {
const config = getConfiguration(namespace);
return config.get<string[]>('interpreter');
Expand All @@ -90,6 +111,7 @@ export async function getWorkspaceSettings(
const args = getArgs(namespace, workspace).map((s) => resolveWorkspace(workspace, s));
const path = getPath(namespace, workspace).map((s) => resolveWorkspace(workspace, s));
const workspaceSetting = {
cwd: getCwd(namespace, workspace),
workspace: workspace.uri.toString(),
args,
severity: config.get<Record<string, string>>('severity', DEFAULT_SEVERITY),
Expand Down
9 changes: 5 additions & 4 deletions src/test/python_tests/lsp_test_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def as_uri(path: str) -> str:
@contextlib.contextmanager
def python_file(contents: str, root: pathlib.Path, ext: str = ".py"):
"""Creates a temporary python file."""
basename = (
"".join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(9)) + ext
)
fullpath = root / basename
try:
basename = (
"".join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(9)) + ext
)
fullpath = root / basename
fullpath.write_text(contents)
yield fullpath
finally:
Expand Down Expand Up @@ -63,5 +63,6 @@ def get_initialization_options():

setting["workspace"] = as_uri(str(PROJECT_ROOT))
setting["interpreter"] = []
setting["cwd"] = str(PROJECT_ROOT)

return {"settings": [setting]}