From 2f7295ba9eb8989cbd0a39702802a0b8888038c5 Mon Sep 17 00:00:00 2001 From: Reid 'arrdem' McKenzie Date: Thu, 12 Aug 2021 14:07:40 -0600 Subject: [PATCH] Strip the longest matching import path, not the first match --- zapp/zapp.bzl | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/zapp/zapp.bzl b/zapp/zapp.bzl index 4c805c9..0403bd8 100644 --- a/zapp/zapp.bzl +++ b/zapp/zapp.bzl @@ -26,16 +26,21 @@ def _store_path(path, ctx, imports): else: # Main workspace, for example 'mypackage/main.py' - # stored_path = ctx.workspace_name + "/" + path - stored_path = path + stored_path = ctx.workspace_name + "/" + path - matching_prefix = None + matching_prefixes = [] for i in imports: if stored_path.startswith(i): - stored_path = stored_path[len(i):] - matching_prefix = i - break + matching_prefixes.append(i) + # 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("/") return stored_path