Skip to content

Commit

Permalink
Ensure filtering for stdlib excludes sysconfig data (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig authored Aug 1, 2023
1 parent aef008f commit 6f63801
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
11 changes: 6 additions & 5 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def _get_global_defaults():

def _update_workspace_settings(settings):
if not settings:
key = GLOBAL_SETTINGS.get("cwd", os.getcwd())
key = utils.normalize_path(GLOBAL_SETTINGS.get("cwd", os.getcwd()))
WORKSPACE_SETTINGS[key] = {
"cwd": key,
"workspaceFS": key,
Expand All @@ -413,7 +413,7 @@ def _update_workspace_settings(settings):
return

for setting in settings:
key = uris.to_fs_path(setting["workspace"])
key = utils.normalize_path(uris.to_fs_path(setting["workspace"]))
WORKSPACE_SETTINGS[key] = {
**setting,
"workspaceFS": key,
Expand All @@ -427,8 +427,9 @@ def _get_document_key(document: workspace.Document):

# Find workspace settings for the given file.
while document_workspace != document_workspace.parent:
if str(document_workspace) in workspaces:
return str(document_workspace)
norm_path = utils.normalize_path(document_workspace)
if norm_path in workspaces:
return norm_path
document_workspace = document_workspace.parent

return None
Expand All @@ -441,7 +442,7 @@ def _get_settings_by_document(document: workspace.Document | None):
key = _get_document_key(document)
if key is None:
# This is either a non-workspace file or there is no workspace.
key = os.fspath(pathlib.Path(document.path).parent)
key = utils.normalize_path(pathlib.Path(document.path).parent)
return {
"cwd": key,
"workspaceFS": key,
Expand Down
58 changes: 45 additions & 13 deletions bundled/tool/lsp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import io
import os
import os.path
import pathlib
import runpy
import site
import subprocess
import sys
import sysconfig
import threading
from typing import Any, Callable, List, Sequence, Tuple, Union

Expand All @@ -19,36 +21,66 @@
CWD_LOCK = threading.Lock()


def as_list(content: Union[Any, List[Any], Tuple[Any]]) -> Union[List[Any], Tuple[Any]]:
def as_list(content: Union[Any, List[Any], Tuple[Any]]) -> List[Any]:
"""Ensures we always get a list"""
if isinstance(content, (list, tuple)):
return content
return list(content)
return [content]


_site_paths = tuple(
[
os.path.normcase(os.path.normpath(p))
for p in (as_list(site.getsitepackages()) + as_list(site.getusersitepackages()))
def _get_sys_config_paths() -> List[str]:
"""Returns paths from sysconfig.get_paths()."""
return [
path
for group, path in sysconfig.get_paths().items()
if group not in ["data", "platdata", "scripts"]
]


def _get_extensions_dir() -> List[str]:
"""This is the extensions folder under ~/.vscode or ~/.vscode-server."""

# The path here is calculated relative to the tool
# this is because users can launch VS Code with custom
# extensions folder using the --extensions-dir argument
path = pathlib.Path(__file__).parent.parent.parent.parent
# ^ bundled ^ extensions
# tool <extension>
if path.name == "extensions":
return [os.fspath(path)]
return []


_stdlib_paths = set(
str(pathlib.Path(p).resolve())
for p in (
as_list(site.getsitepackages())
+ as_list(site.getusersitepackages())
+ _get_sys_config_paths()
+ _get_extensions_dir()
)
)


def is_same_path(file_path1, file_path2) -> bool:
def is_same_path(file_path1: str, file_path2: str) -> bool:
"""Returns true if two paths are the same."""
return os.path.normcase(os.path.normpath(file_path1)) == os.path.normcase(
os.path.normpath(file_path2)
)
return pathlib.Path(file_path1) == pathlib.Path(file_path2)


def normalize_path(file_path: str) -> str:
"""Returns normalized path."""
return str(pathlib.Path(file_path).resolve())


def is_current_interpreter(executable) -> bool:
"""Returns true if the executable path is same as the current interpreter."""
return is_same_path(executable, sys.executable)


def is_stdlib_file(file_path) -> bool:
"""Return True if the file belongs to standard library."""
return os.path.normcase(os.path.normpath(file_path)).startswith(_site_paths)
def is_stdlib_file(file_path: str) -> bool:
"""Return True if the file belongs to the standard library."""
normalized_path = str(pathlib.Path(file_path).resolve())
return any(normalized_path.startswith(path) for path in _stdlib_paths)


class RunResult:
Expand Down

0 comments on commit 6f63801

Please sign in to comment.