Strip the longest matching import path, not the first match

This commit is contained in:
Reid 'arrdem' McKenzie 2021-08-12 14:07:40 -06:00
parent bdf31076a7
commit 2f7295ba9e

View file

@ -26,16 +26,21 @@ def _store_path(path, ctx, imports):
else: else:
# Main workspace, for example 'mypackage/main.py' # Main workspace, for example 'mypackage/main.py'
# stored_path = ctx.workspace_name + "/" + path stored_path = ctx.workspace_name + "/" + path
stored_path = path
matching_prefix = None matching_prefixes = []
for i in imports: for i in imports:
if stored_path.startswith(i): if stored_path.startswith(i):
stored_path = stored_path[len(i):] matching_prefixes.append(i)
matching_prefix = i
break
# Find the longest prefix match
matching_prefixes = sorted(matching_prefixes, key=len, reverse=True)
if matching_prefixes:
# Strip the longest matching prefix
stored_path = stored_path[len(matching_prefixes[0]):]
# Strip any trailing /
stored_path = stored_path.lstrip("/") stored_path = stored_path.lstrip("/")
return stored_path return stored_path