Skip to content

Commit 4da55f5

Browse files
authored
Update the strip prefix logic for python libraries (#361)
* Fix imports directive for py_library when using stripPrefix Signed-off-by: Vihang Mehta <[email protected]> * Tests Signed-off-by: Vihang Mehta <[email protected]> --------- Signed-off-by: Vihang Mehta <[email protected]>
1 parent e0b5d46 commit 4da55f5

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:proto_strip_import_prefix /module_lib/util/nested
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@build_stack_rules_proto//rules/py:proto_py_library.bzl", "proto_py_library")
3+
load("@build_stack_rules_proto//rules:proto_compile.bzl", "proto_compile")
4+
5+
# gazelle:proto_strip_import_prefix /module_lib/util/nested
6+
7+
proto_library(
8+
name = "prefix_test_proto",
9+
srcs = ["test.proto"],
10+
strip_import_prefix = "/module_lib/util/nested",
11+
visibility = ["//visibility:public"],
12+
)
13+
14+
proto_compile(
15+
name = "prefix_test_python_compile",
16+
output_mappings = ["test_pb2.py=/prefix/test_pb2.py"],
17+
outputs = ["test_pb2.py"],
18+
plugins = ["@build_stack_rules_proto//plugin/builtin:python"],
19+
proto = "prefix_test_proto",
20+
)
21+
22+
proto_py_library(
23+
name = "prefix_test_py_library",
24+
srcs = ["test_pb2.py"],
25+
imports = [".."],
26+
visibility = ["//visibility:public"],
27+
deps = ["@com_google_protobuf//:protobuf_python"],
28+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package prefix.test;
4+
5+
message Data {
6+
int64 id = 1;
7+
}

pkg/rule/rules_python/proto_py_library_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ proto_py_library(
6464
Outputs: []string{"test_pb2.py"},
6565
},
6666
},
67+
Rel: "com/foo/baz/qux/v1",
6768
},
6869
want: `
6970
proto_py_library(
7071
name = "test_py_library",
7172
srcs = ["test_pb2.py"],
72-
imports = ["../.."],
73+
imports = ["../../.."],
7374
)
7475
`,
7576
},

pkg/rule/rules_python/py_library.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rules_python
22

33
import (
4+
"path/filepath"
45
"strings"
56

67
"github.com/bazelbuild/bazel-gazelle/config"
@@ -71,17 +72,23 @@ func (s *PyLibrary) Visibility() []string {
7172
func (s *PyLibrary) ImportsAttr() (imps []string) {
7273
// if we have a strip_import_prefix on the proto_library, the python search
7374
// path should include the directory N parents above the current package,
74-
// where N is the number of segments in an absolute strip_import_prefix
75+
// where N is the number of segments needed to ascend to the prefix from
76+
// the dir for the current rule.
7577
if s.Config.Library.StripImportPrefix() == "" {
7678
return
7779
}
7880
prefix := s.Config.Library.StripImportPrefix()
7981
if !strings.HasPrefix(prefix, "/") {
8082
return // deal with relative-imports at another time
8183
}
84+
8285
prefix = strings.TrimPrefix(prefix, "/")
83-
prefix = strings.TrimSuffix(prefix, "/")
84-
parts := strings.Split(prefix, "/")
86+
rel, err := filepath.Rel(prefix, s.Config.Rel)
87+
if err != nil {
88+
return // the prefix doesn't prefix the current path, shouldn't happen
89+
}
90+
91+
parts := strings.Split(rel, "/")
8592
for i := 0; i < len(parts); i++ {
8693
parts[i] = ".."
8794
}

0 commit comments

Comments
 (0)