Skip to content

Commit 7d3f022

Browse files
authored
Fix Bazel 7 related protobuf build failures (#1620)
Related to #1482, #1618, and #1619. Results from the investigation documented at: - #1619 (comment) Updates `_import_paths()` in `scala_proto_aspect.bzl` to handle differences `ProtoInfo.proto_source_root` and `ProtoInfo.direct_sources` values between Bazel 6 and Bazel 7. Without this change, `_import_paths()` emits incorrect values under Bazel 7, causing targets containing generated `.proto` inputs to fail, e.g. `//test/proto3:test_generated_proto`. See also: - Fix paths for sibling repository setup and generated .proto files bazelbuild/bazel@6c6c196 - The docstring for `ProtoInfo.proto_source_root` in the Bazel sources: https://github.com./bazelbuild/bazel/blob/7.3.2/src/main/starlark/builtins_bzl/common/proto/proto_info.bzl#L155-L172 - Remove incompatible_generated_protos_in_virtual_imports bazelbuild/bazel@3efaa32 - Comment from: Generated Protos are no longer considered as virtual_imports in Bazel 7 bazelbuild/bazel#21075 (comment) --- I cherrypicked this commit into #1618. While it fixed the `//test/proto3` build failure, it does _not_ fix the hanging scalapb_workers from the ProtoScalaPBRule aspect. I'll have to investiate further whether than hang is related to Bazel, rules_proto, com_google_protobuf, or some mixture thereof. Still, progress!
1 parent 3da60a8 commit 7d3f022

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

scala_proto/private/scala_proto_aspect.bzl

+14-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ load(
1919
)
2020
load("@bazel_skylib//lib:dicts.bzl", "dicts")
2121

22-
def _import_paths(proto):
22+
def _import_paths(proto, ctx):
23+
# Under Bazel 7.x, direct_sources from generated protos may still contain
24+
# ctx.bin_dir.path, even when proto_source_root does not. proto_source_root
25+
# may also be relative to ctx.bin_dir.path, or it may contain it. So we try
26+
# removing ctx.bin_dir.path from everything.
27+
bin_dir = ctx.bin_dir.path + "/"
2328
source_root = proto.proto_source_root
24-
if "." == source_root:
25-
return [src.path for src in proto.direct_sources]
26-
else:
27-
offset = len(source_root) + 1 # + '/'
28-
return [src.path[offset:] for src in proto.direct_sources]
29+
source_root += "/" if source_root != "." else ""
30+
source_root = source_root.removeprefix(bin_dir)
31+
32+
return [
33+
ds.path.removeprefix(bin_dir).removeprefix(source_root)
34+
for ds in proto.direct_sources
35+
]
2936

3037
def _code_should_be_generated(ctx, toolchain):
3138
# This feels rather hacky and odd, but we can't compare the labels to ignore a target easily
@@ -56,7 +63,7 @@ def _pack_sources(ctx, src_jars):
5663
)
5764

5865
def _generate_sources(ctx, toolchain, proto):
59-
sources = _import_paths(proto)
66+
sources = _import_paths(proto, ctx)
6067
descriptors = proto.transitive_descriptor_sets
6168
outputs = {
6269
k: ctx.actions.declare_file("%s_%s_scalapb.srcjar" % (ctx.label.name, k))

0 commit comments

Comments
 (0)