Skip to content

Commit f6b10a8

Browse files
gnattishnessccordoba12
authored andcommitted
Source roots also checks for pyproject.toml, defaults to root_uri (#652)
1 parent fa486ef commit f6b10a8

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

pyls/workspace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def show_message(self, message, msg_type=lsp.MessageType.Info):
8888

8989
def source_roots(self, document_path):
9090
"""Return the source roots for the given document."""
91-
files = _utils.find_parents(self._root_path, document_path, ['setup.py']) or []
92-
return [os.path.dirname(setup_py) for setup_py in files]
91+
files = _utils.find_parents(self._root_path, document_path, ['setup.py', 'pyproject.toml']) or []
92+
return list(set((os.path.dirname(project_file) for project_file in files))) or [self._root_path]
9393

9494
def _create_document(self, doc_uri, source=None, version=None):
9595
path = uris.to_fs_path(doc_uri)

test/test_workspace.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright 2017 Palantir Technologies, Inc.
22
import os
3-
import os.path as osp
43
import sys
4+
5+
import pytest
6+
57
from pyls import uris
68

79
PY2 = sys.version_info.major == 2
@@ -16,7 +18,7 @@
1618

1719

1820
def path_as_uri(path):
19-
return pathlib.Path(osp.abspath(path)).as_uri()
21+
return pathlib.Path(os.path.abspath(path)).as_uri()
2022

2123

2224
def test_local(pyls):
@@ -49,21 +51,32 @@ def test_rm_document(pyls):
4951
assert pyls.workspace.get_document(DOC_URI)._source is None
5052

5153

52-
def test_non_root_project(pyls):
54+
@pytest.mark.parametrize('metafiles', [('setup.py',), ('pyproject.toml',), ('setup.py', 'pyproject.toml')])
55+
def test_non_root_project(pyls, metafiles):
5356
repo_root = os.path.join(pyls.workspace.root_path, 'repo-root')
5457
os.mkdir(repo_root)
5558
project_root = os.path.join(repo_root, 'project-root')
5659
os.mkdir(project_root)
5760

58-
with open(os.path.join(project_root, 'setup.py'), 'w+') as f:
59-
f.write('# setup.py')
61+
for metafile in metafiles:
62+
with open(os.path.join(project_root, metafile), 'w+') as f:
63+
f.write('# ' + metafile)
6064

6165
test_uri = uris.from_fs_path(os.path.join(project_root, 'hello/test.py'))
6266
pyls.workspace.put_document(test_uri, 'assert True')
6367
test_doc = pyls.workspace.get_document(test_uri)
6468
assert project_root in test_doc.sys_path()
6569

6670

71+
def test_root_project_with_no_setup_py(pyls):
72+
"""Default to workspace root."""
73+
workspace_root = pyls.workspace.root_path
74+
test_uri = uris.from_fs_path(os.path.join(workspace_root, 'hello/test.py'))
75+
pyls.workspace.put_document(test_uri, 'assert True')
76+
test_doc = pyls.workspace.get_document(test_uri)
77+
assert workspace_root in test_doc.sys_path()
78+
79+
6780
def test_multiple_workspaces(tmpdir, pyls):
6881
workspace1_dir = tmpdir.mkdir('workspace1')
6982
workspace2_dir = tmpdir.mkdir('workspace2')

0 commit comments

Comments
 (0)