Skip to content
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
15 changes: 13 additions & 2 deletions bundled/tool/lsp_jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import contextlib
import io
import json
import os
import pathlib
import subprocess
import threading
Expand Down Expand Up @@ -135,13 +136,23 @@ def stop_all_processes(self):
i.send_data({"id": str(uuid.uuid4()), "method": "exit"})
self._thread_pool.shutdown(wait=False)

def start_process(self, workspace: str, args: Sequence[str], cwd: str) -> None:
def start_process(
self,
workspace: str,
args: Sequence[str],
cwd: str,
env: Optional[Dict[str, str]] = None,
) -> None:
"""Starts a process and establishes JSON-RPC communication over stdio."""
_env = os.environ.copy()
if env is not None:
_env.update(env)
proc = subprocess.Popen(
args,
cwd=cwd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
env=_env,
)
self._processes[workspace] = proc
self._rpc[workspace] = create_json_rpc(proc.stdout, proc.stdin)
Expand Down Expand Up @@ -212,7 +223,7 @@ def run_over_json_rpc(
"""Uses JSON-RPC to execute a command."""
rpc: Union[JsonRpc, None] = get_or_start_json_rpc(workspace, interpreter, cwd)
if not rpc:
raise Exception("Failed to run over JSON-RPC.")
raise ConnectionError("Failed to run over JSON-RPC.")

msg_id = str(uuid.uuid4())
msg = {
Expand Down
14 changes: 14 additions & 0 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,20 @@ def _get_settings_by_document(document: TextDocument | None):
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot generated:
-706

Prior feedback unresolved — shadow-set pattern is structurally fragile (all four reviewers agree). The comment asserting keys equal workspaceFS values doesn't prevent breakage if normalization ever diverges (trailing slash, case, slash direction on Windows). The Skeptic demonstrates a concrete scenario: if normalize_path produces backslashes but workspaceFS stores forward slashes, the in workspaces check silently fails on every ancestor and the function falls through to returning an arbitrary workspace's settings — a silent wrong-answer bug.

Replace the shadow-set + key-lookup with direct iteration:

def _get_settings_by_path(file_path: pathlib.Path):
    while file_path != file_path.parent:
        str_file_path = utils.normalize_path(file_path)
        for _key, settings in WORKSPACE_SETTINGS.items():
            if settings["workspaceFS"] == str_file_path:
                return settings
        file_path = file_path.parent

    setting_values = list(WORKSPACE_SETTINGS.values())
    if not setting_values:
        return {}
    return setting_values[0]


def _get_settings_by_path(file_path: pathlib.Path):
while file_path != file_path.parent:
str_file_path = utils.normalize_path(file_path)
for _key, settings in WORKSPACE_SETTINGS.items():
if settings["workspaceFS"] == str_file_path:
return settings
file_path = file_path.parent

setting_values = list(WORKSPACE_SETTINGS.values())
if not setting_values:
return {}
return setting_values[0]


# *****************************************************
# Internal execution APIs.
# *****************************************************
Expand Down
5 changes: 3 additions & 2 deletions src/common/envFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import { traceInfo, traceWarn } from './logging';
import { getConfiguration } from './vscodeapi';

function expandTilde(value: string): string {
const home = process.env.HOME || process.env.USERPROFILE || '';
export function expandTilde(value: string): string {
const home = process.env.HOME || process.env.USERPROFILE;
if (!home) return value;

Check warning on line 13 in src/common/envFile.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests (windows-latest)

Expected { after 'if' condition

Check warning on line 13 in src/common/envFile.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests (windows-latest)

Expected { after 'if' condition

Check warning on line 13 in src/common/envFile.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests (ubuntu-latest)

Expected { after 'if' condition

Check warning on line 13 in src/common/envFile.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests (ubuntu-latest)

Expected { after 'if' condition

Check warning on line 13 in src/common/envFile.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected { after 'if' condition

Check warning on line 13 in src/common/envFile.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected { after 'if' condition
if (value === '~') {
return home;
}
Expand Down
8 changes: 1 addition & 7 deletions src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { traceLog, traceWarn } from './logging';
import { getInterpreterDetails } from './python';
import { getConfiguration, getWorkspaceFolders } from './vscodeapi';
import { getInterpreterFromSetting } from './utilities';
import { expandTilde } from './envFile';

/* eslint-disable @typescript-eslint/naming-convention */
const DEFAULT_SEVERITY: Record<string, string> = {
Expand Down Expand Up @@ -149,13 +150,6 @@ export async function getWorkspaceSettings(
return workspaceSetting;
}

function expandTilde(value: string): string {
const home = process.env.HOME || process.env.USERPROFILE || '';
if (value === '~') return home;
if (value.startsWith('~/') || value.startsWith('~\\')) return home + value.slice(1);
return value;
}

function getGlobalValue<T>(config: WorkspaceConfiguration, key: string, defaultValue: T): T {
const inspect = config.inspect<T>(key);
return inspect?.globalValue ?? inspect?.defaultValue ?? defaultValue;
Expand Down
Loading