From fe9b8985c0c09bf5952b4ed5de68c30c74aed365 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 24 Jul 2023 13:57:03 -0700 Subject: [PATCH] Ensure filtering for stdlib excludes sysconfig data (#307) --- bundled/tool/lsp_utils.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/bundled/tool/lsp_utils.py b/bundled/tool/lsp_utils.py index b642c04..6ca9f1e 100644 --- a/bundled/tool/lsp_utils.py +++ b/bundled/tool/lsp_utils.py @@ -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 + 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() ) ) @@ -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