From 7d3f022143d5edd15ce07b68b16ca8727c8a358a Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Tue, 8 Oct 2024 03:57:00 -0400 Subject: [PATCH] Fix Bazel 7 related protobuf build failures (#1620) Related to #1482, #1618, and #1619. Results from the investigation documented at: - https://github.com/bazelbuild/rules_scala/pull/1619#issuecomment-2394853772 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 https://github.com/bazelbuild/bazel/commit/6c6c196ceffe14c9cf29960ef4049469df73d991 - 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 https://github.com/bazelbuild/bazel/commit/3efaa325a1e142831744e8a021c4d25e1c6869f2 - Comment from: Generated Protos are no longer considered as virtual_imports in Bazel 7 https://github.com/bazelbuild/bazel/issues/21075#issuecomment-1914040169 --- 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! --- scala_proto/private/scala_proto_aspect.bzl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/scala_proto/private/scala_proto_aspect.bzl b/scala_proto/private/scala_proto_aspect.bzl index 005fe705f..8784c08bd 100644 --- a/scala_proto/private/scala_proto_aspect.bzl +++ b/scala_proto/private/scala_proto_aspect.bzl @@ -19,13 +19,20 @@ load( ) load("@bazel_skylib//lib:dicts.bzl", "dicts") -def _import_paths(proto): +def _import_paths(proto, ctx): + # Under Bazel 7.x, direct_sources from generated protos may still contain + # ctx.bin_dir.path, even when proto_source_root does not. proto_source_root + # may also be relative to ctx.bin_dir.path, or it may contain it. So we try + # removing ctx.bin_dir.path from everything. + bin_dir = ctx.bin_dir.path + "/" source_root = proto.proto_source_root - if "." == source_root: - return [src.path for src in proto.direct_sources] - else: - offset = len(source_root) + 1 # + '/' - return [src.path[offset:] for src in proto.direct_sources] + source_root += "/" if source_root != "." else "" + source_root = source_root.removeprefix(bin_dir) + + return [ + ds.path.removeprefix(bin_dir).removeprefix(source_root) + for ds in proto.direct_sources + ] def _code_should_be_generated(ctx, toolchain): # 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): ) def _generate_sources(ctx, toolchain, proto): - sources = _import_paths(proto) + sources = _import_paths(proto, ctx) descriptors = proto.transitive_descriptor_sets outputs = { k: ctx.actions.declare_file("%s_%s_scalapb.srcjar" % (ctx.label.name, k))