From 2305b3a3641e30a1a888f06e8cd08e7a6b21a0fe Mon Sep 17 00:00:00 2001 From: "A. Reit" Date: Tue, 22 Nov 2022 16:13:18 +0100 Subject: [PATCH 1/2] Add test for symbols of a non-existing (unsaved) file Add a test for `textDocument/documentSymbols` of a non-existing (unsaved) file. The test fails as of this commit and is a regression test for issue #301 [1]. [1] https://github.com/python-lsp/python-lsp-server/issues/301 --- test/plugins/test_symbols.py | 10 ++++++++++ 1 file changed, 10 insertions(+) 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) From efc68217e17906e100cef1ed480d776cba6c83f7 Mon Sep 17 00:00:00 2001 From: "A. Reit" Date: Tue, 22 Nov 2022 15:29:13 +0100 Subject: [PATCH 2/2] Fix symbols for non-existing (unsaved) files Fix `textDocument/documentSymbols` returning an empty result for non-existing (unsaved) files: Do not use `os.path.samefile()` which accesses the file system to check if two file paths point to the same file. Just compare the file paths. (This basically reverts commit 40afab312a6b.) This fixes issue #301 [1]. [1] https://github.com/python-lsp/python-lsp-server/issues/301 --- pylsp/plugins/symbols.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) 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