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

Ensure filtering for stdlib excludes sysconfig data #273

Merged
merged 1 commit into from
Jul 13, 2023
Merged
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: 27 additions & 3 deletions bundled/tool/lsp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,36 @@ def as_list(content: Union[Any, List[Any], Tuple[Any]]) -> List[Any]:
return [content]


_site_paths = set(
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())
+ list(sysconfig.get_paths().values())
+ _get_sys_config_paths()
+ _get_extensions_dir()
)
)

Expand All @@ -55,7 +79,7 @@ def is_current_interpreter(executable) -> bool:
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 _site_paths)
return any(normalized_path.startswith(path) for path in _stdlib_paths)


# pylint: disable-next=too-few-public-methods
Expand Down