Skip to content

Commit

Permalink
Ensure filtering for stdlib excludes sysconfig data (microsoft#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig authored Jul 24, 2023
1 parent 739f7d7 commit fe9b898
Showing 1 changed file with 27 additions and 3 deletions.
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

0 comments on commit fe9b898

Please sign in to comment.