diff --git a/pylsp/plugins/symbols.py b/pylsp/plugins/symbols.py index 3b57fc11..939dcda6 100644 --- a/pylsp/plugins/symbols.py +++ b/pylsp/plugins/symbols.py @@ -2,7 +2,7 @@ # Copyright 2021- Python Language Server Contributors. import logging -import os +from pathlib import Path from pylsp import hookimpl from pylsp.lsp import SymbolKind @@ -91,14 +91,7 @@ def pylsp_document_symbols(config, document): else: continue - try: - docismodule = os.path.samefile(document.path, d.module_path) - except (TypeError, FileNotFoundError): - # Python 2 on Windows has no .samefile, but then these are - # strings for sure - docismodule = document.path == d.module_path - - if _include_def(d) and docismodule: + if _include_def(d) and Path(document.path) == d.module_path: tuple_range = _tuple_range(d) if tuple_range in exclude: continue diff --git a/test/plugins/test_symbols.py b/test/plugins/test_symbols.py index a25f5621..40e3e1e3 100644 --- a/test/plugins/test_symbols.py +++ b/test/plugins/test_symbols.py @@ -80,6 +80,16 @@ def test_symbols_all_scopes(config, workspace): helper_check_symbols_all_scope(symbols) +def test_symbols_non_existing_file(config, workspace, tmpdir): + path = tmpdir.join("foo.py") + # Check pre-condition: file must not exist + assert not path.check(exists=1) + + doc = Document(uris.from_fs_path(str(path)), workspace, DOC) + symbols = pylsp_document_symbols(config, doc) + helper_check_symbols_all_scope(symbols) + + @pytest.mark.skipif(PY2 or not LINUX or not CI, reason="tested on linux and python 3 only") def test_symbols_all_scopes_with_jedi_environment(workspace): doc = Document(DOC_URI, workspace, DOC)