File tree Expand file tree Collapse file tree 5 files changed +48
-4
lines changed
example/golden/testdata/strip_import_prefix/module_lib/util/nested/prefix Expand file tree Collapse file tree 5 files changed +48
-4
lines changed Original file line number Diff line number Diff line change 1+ # gazelle:proto_strip_import_prefix /module_lib/util/nested
Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 1+ syntax = "proto3" ;
2+
3+ package prefix.test ;
4+
5+ message Data {
6+ int64 id = 1 ;
7+ }
Original file line number Diff line number Diff 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 : `
6970proto_py_library(
7071 name = "test_py_library",
7172 srcs = ["test_pb2.py"],
72- imports = ["../.."],
73+ imports = ["../../.. "],
7374)
7475` ,
7576 },
Original file line number Diff line number Diff line change 11package rules_python
22
33import (
4+ "path/filepath"
45 "strings"
56
67 "github.com/bazelbuild/bazel-gazelle/config"
@@ -71,17 +72,23 @@ func (s *PyLibrary) Visibility() []string {
7172func (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 }
You can’t perform that action at this time.
0 commit comments