Fix import issue when a shorter base path matches before a shorter one

This commit is contained in:
Stavros Korokithakis 2020-12-09 16:49:18 +02:00 committed by GitHub
parent 7248258672
commit aae76c6388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -56,7 +56,9 @@ def match_syspath(filename):
If `filename` is not under any directory in `sys.path`, raises `ValueError`.
"""
absolute_filename = str(pathlib.Path(filename).expanduser().resolve())
for root_path in sys.path:
# We sort the paths in reverse order of length so a longer path matches
# before a shorter one.
for root_path in sorted(sys.path, key=len, reverse=True):
root_path = pathlib.Path(root_path).expanduser().resolve()
if absolute_filename.startswith(str(root_path)):
return root_path