Skip to content

Commit

Permalink
Fix hung config (#101)
Browse files Browse the repository at this point in the history
* Config hang

* Fix config hang
  • Loading branch information
gatesn authored Aug 21, 2017
1 parent c02829a commit 6a11cb8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pyls/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,32 @@ def find_parents(root, path, names):
"""Find files matching the given names relative to the given path.
Args:
path (str): The path to start searching up from
names (List[str]): The file/directory names to look for
path (str): The file path to start searching up from.
names (List[str]): The file/directory names to look for.
root (str): The directory at which to stop recursing upwards.
Note:
The path MUST be within the root.
"""
if not root:
return []

if not os.path.commonprefix((root, path)):
log.warning("Path %s not in %s", path, root)
return []

curdir = os.path.dirname(path)
# Split the relative by directory, generate all the parent directories, then check each of them.
# This avoids running a loop that has different base-cases for unix/windows
# e.g. /a/b and /a/b/c/d/e.py -> ['/a/b', 'c', 'd']
dirs = [root] + os.path.relpath(os.path.dirname(path), root).split(os.path.sep)

while curdir != os.path.dirname(root) and curdir != '/':
existing = list(filter(os.path.exists, [os.path.join(curdir, n) for n in names]))
# Search each of /a/b/c, /a/b, /a
while dirs:
search_dir = os.path.join(*dirs)
existing = list(filter(os.path.exists, [os.path.join(search_dir, n) for n in names]))
if existing:
return existing
curdir = os.path.dirname(curdir)
dirs.pop()

# Otherwise nothing
return []
11 changes: 11 additions & 0 deletions test/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2017 Palantir Technologies, Inc.
from pyls.config import find_parents


def test_find_parents(tmpdir):
# Create a workspace in tmp
subsubdir = tmpdir.ensure_dir("subdir", "subsubdir")
path = subsubdir.ensure("path.py")
test_cfg = tmpdir.ensure("test.cfg")

assert find_parents(tmpdir.strpath, path.strpath, ["test.cfg"]) == [test_cfg.strpath]

0 comments on commit 6a11cb8

Please sign in to comment.