Skip to content

Commit 6cc8549

Browse files
authored
Merge pull request #23149 from protocolbuffers/cp-option-import-fixes
Cherry pick fixes to Java option import for 32.x
2 parents 1a7e012 + 0014173 commit 6cc8549

40 files changed

+693
-140
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""This module defines a wrapper around proto_library for Bazel versions that do not support certain attributes yet."""
2+
3+
load("//bazel:proto_library.bzl", "proto_library")
4+
5+
def protobuf_test_proto_library(**kwattrs):
6+
"""
7+
Creates a proto library, handling any attributes that are not supported by the proto_library rule.
8+
9+
Args:
10+
**kwattrs: Additional arguments to pass to the proto_library rule.
11+
"""
12+
kwargs = dict(kwattrs)
13+
14+
# TODO: Bazel 7's proto_library rule does not support option_deps, so we handle it by putting it in deps instead.
15+
if "option_deps" in kwargs and hasattr(native, "proto_library"):
16+
deps = kwargs.pop("deps", [])
17+
option_deps = kwargs.pop("option_deps")
18+
kwargs["deps"] = depset(deps + option_deps).to_list()
19+
20+
proto_library(**kwargs)

cmake/tests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ foreach(proto_file ${tests_protos})
3737
PROTOS ${proto_file}
3838
LANGUAGE cpp
3939
OUT_VAR pb_generated_files
40-
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src
40+
IMPORT_DIRS ${protobuf_SOURCE_DIR}/src ${protobuf_SOURCE_DIR}/java/core/src/main/resources
4141
)
4242
set(tests_proto_files ${tests_proto_files} ${pb_generated_files})
4343
endforeach(proto_file)

csharp/generate_protos.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ $PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf \
5050
# Note that this deliberately does *not* include old_extensions1.proto
5151
# and old_extensions2.proto, which are generated with an older version
5252
# of protoc.
53-
$PROTOC -Isrc -I. \
53+
$PROTOC -Isrc -I. -Ijava/core/src/main/resources/ \
5454
--experimental_allow_proto3_optional \
5555
--experimental_editions \
5656
--csharp_out=csharp/src/Google.Protobuf.Test.TestProtos \

csharp/src/Google.Protobuf.Test.TestProtos/UnittestLegacyFeatures.pb.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ static UnittestLegacyFeaturesReflection() {
2525
byte[] descriptorData = global::System.Convert.FromBase64String(
2626
string.Concat(
2727
"Ci5nb29nbGUvcHJvdG9idWYvdW5pdHRlc3RfbGVnYWN5X2ZlYXR1cmVzLnBy",
28-
"b3RvEhhsZWdhY3lfZmVhdHVyZXNfdW5pdHRlc3QigwEKE1Rlc3RFZGl0aW9u",
28+
"b3RvEhhsZWdhY3lfZmVhdHVyZXNfdW5pdHRlc3QijAEKE1Rlc3RFZGl0aW9u",
2929
"c01lc3NhZ2USHQoOcmVxdWlyZWRfZmllbGQYASABKAVCBaoBAggDEk0KD2Rl",
3030
"bGltaXRlZF9maWVsZBgCIAEoCzItLmxlZ2FjeV9mZWF0dXJlc191bml0dGVz",
31-
"dC5UZXN0RWRpdGlvbnNNZXNzYWdlQgWqAQIoAmIIZWRpdGlvbnNw6Ac="));
31+
"dC5UZXN0RWRpdGlvbnNNZXNzYWdlQgWqAQIoAjoHYgXKPgIoAkIYQhZVbml0",
32+
"dGVzdExlZ2FjeUZlYXR1cmVzYghlZGl0aW9uc3DpB3ojZ29vZ2xlL3Byb3Rv",
33+
"YnVmL2phdmFfZmVhdHVyZXMucHJvdG8="));
3234
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
3335
new pbr::FileDescriptor[] { },
3436
new pbr::GeneratedClrTypeInfo(null, null, new pbr::GeneratedClrTypeInfo[] {
132 Bytes
Binary file not shown.

java/core/BUILD.bazel

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ load("//bazel:cc_proto_library.bzl", "cc_proto_library")
77
load("//bazel:java_lite_proto_library.bzl", "java_lite_proto_library")
88
load("//bazel:java_proto_library.bzl", "java_proto_library")
99
load("//bazel:proto_library.bzl", "proto_library")
10+
load(
11+
"//bazel/tests:protobuf_test_proto_library.bzl",
12+
"protobuf_test_proto_library",
13+
)
1014
load("//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain")
1115
load("//build_defs:java_opts.bzl", "protobuf_java_export", "protobuf_java_library", "protobuf_versioned_java_library")
1216
load("//conformance:defs.bzl", "conformance_test")
@@ -213,7 +217,10 @@ cc_proto_library(
213217
filegroup(
214218
name = "java_features_proto_srcs",
215219
srcs = ["src/main/resources/google/protobuf/java_features.proto"],
216-
visibility = ["//pkg:__pkg__"],
220+
visibility = [
221+
"//pkg:__pkg__",
222+
"//python:__pkg__",
223+
],
217224
)
218225

219226
compile_edition_defaults(
@@ -338,9 +345,12 @@ proto_lang_toolchain(
338345
visibility = ["//visibility:public"],
339346
)
340347

341-
proto_library(
348+
protobuf_test_proto_library(
342349
name = "java_test_protos",
343350
srcs = glob(["src/test/proto/**/*.proto"]),
351+
option_deps = [
352+
"//:java_features_proto",
353+
],
344354
strip_import_prefix = "src/test/proto",
345355
deps = [
346356
"//:any_proto",
@@ -541,6 +551,7 @@ LITE_TEST_EXCLUSIONS = [
541551
"src/test/java/com/google/protobuf/FieldPresenceTest.java",
542552
"src/test/java/com/google/protobuf/ForceFieldBuildersPreRun.java",
543553
"src/test/java/com/google/protobuf/GeneratedMessageTest.java",
554+
"src/test/java/com/google/protobuf/ImportOptionTest.java",
544555
"src/test/java/com/google/protobuf/LazilyParsedMessageSetTest.java",
545556
"src/test/java/com/google/protobuf/LazyFieldTest.java",
546557
"src/test/java/com/google/protobuf/LazyStringEndToEndTest.java",
@@ -630,6 +641,7 @@ junit_tests(
630641
"src/test/java/com/google/protobuf/UnredactedDebugFormatForTestTest.java",
631642
"src/test/java/com/google/protobuf/LargeEnumTest.java",
632643
"src/test/java/com/google/protobuf/LargeEnumLiteTest.java",
644+
"src/test/java/com/google/protobuf/ImportOptionTest.java",
633645
# Excluded in core_tests
634646
"src/test/java/com/google/protobuf/DecodeUtf8Test.java",
635647
"src/test/java/com/google/protobuf/IsValidUtf8Test.java",
@@ -682,6 +694,7 @@ junit_tests(
682694
"src/test/java/com/google/protobuf/FieldPresenceTest.java",
683695
"src/test/java/com/google/protobuf/LargeEnumTest.java",
684696
"src/test/java/com/google/protobuf/LargeEnumLiteTest.java",
697+
"src/test/java/com/google/protobuf/ImportOptionTest.java",
685698
# Excluded in core_tests
686699
"src/test/java/com/google/protobuf/DecodeUtf8Test.java",
687700
"src/test/java/com/google/protobuf/IsValidUtf8Test.java",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.google.protobuf;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
5+
import com.google.protobuf.Descriptors.Descriptor;
6+
import com.google.protobuf.Descriptors.FieldDescriptor;
7+
import com.google.protobuf.Descriptors.FileDescriptor;
8+
import proto2_unittest.UnittestCustomOptions;
9+
import proto2_unittest_import_option.TestMessage;
10+
import proto2_unittest_import_option.UnittestImportOptionProto;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.junit.runners.JUnit4;
14+
15+
/** Unit test for import option. */
16+
@RunWith(JUnit4.class)
17+
public final class ImportOptionTest {
18+
19+
@Test
20+
public void testImportOption() throws Exception {
21+
// Ensure that UnittestCustomOptions is linked in and referenced.
22+
FileDescriptor unused = UnittestCustomOptions.getDescriptor();
23+
24+
FileDescriptor fileDescriptor = UnittestImportOptionProto.getDescriptor();
25+
Descriptor messageDescriptor = TestMessage.getDescriptor();
26+
FieldDescriptor fieldDescriptor = messageDescriptor.findFieldByName("field1");
27+
28+
UnknownFieldSet unknownFieldsFile = fileDescriptor.getOptions().getUnknownFields();
29+
UnknownFieldSet unknownFieldsMessage = messageDescriptor.getOptions().getUnknownFields();
30+
UnknownFieldSet unknownFieldsField = fieldDescriptor.getOptions().getUnknownFields();
31+
32+
// TODO: Currently linked in options also end up in unknown fields.
33+
// TODO: Exclude for open source tests once linked in options are treated
34+
// differently, since `option_deps` are treated as `deps` in Bazel 7.
35+
// assertThat(fileDescriptor.getOptions().getExtension(UnittestCustomOptions.fileOpt1))
36+
// .isEqualTo(1);
37+
// assertThat(messageDescriptor.getOptions().getExtension(UnittestCustomOptions.messageOpt1))
38+
// .isEqualTo(2);
39+
// assertThat(fieldDescriptor.getOptions().getExtension(UnittestCustomOptions.fieldOpt1))
40+
// .isEqualTo(3);
41+
assertThat(unknownFieldsFile.getField(7736974).getVarintList()).containsExactly(1L);
42+
assertThat(unknownFieldsMessage.getField(7739036).getVarintList()).containsExactly(2L);
43+
assertThat(unknownFieldsField.getField(7740936).getFixed64List()).containsExactly(3L);
44+
45+
// Options from import option that are not linked in should be in unknown fields.
46+
assertThat(unknownFieldsFile.getField(7736975).getVarintList()).containsExactly(1L);
47+
assertThat(unknownFieldsMessage.getField(7739037).getVarintList()).containsExactly(2L);
48+
assertThat(unknownFieldsField.getField(7740937).getFixed64List()).containsExactly(3L);
49+
50+
assertThat(unknownFieldsFile.asMap()).hasSize(2);
51+
assertThat(unknownFieldsMessage.asMap()).hasSize(2);
52+
assertThat(unknownFieldsField.asMap()).hasSize(2);
53+
}
54+
}

java/core/src/test/proto/com/google/protobuf/large_open_enum.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ edition = "2024";
1111

1212
package protobuf_test_messages.edition;
1313

14-
import "google/protobuf/java_features.proto";
14+
import option "google/protobuf/java_features.proto";
1515

1616
option java_package = "com.google.protobuf.large.openenum.edition";
1717
option features.(pb.java).large_enum = true;

java/core/src/test/proto/com/google/protobuf/test_check_utf8.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition = "2024";
1212

1313
package proto2_test_check_utf8;
1414

15-
import "google/protobuf/java_features.proto";
15+
import option "google/protobuf/java_features.proto";
1616

1717
option features.utf8_validation = NONE;
1818
option features.(pb.java).utf8_validation = VERIFY;

python/build_targets.bzl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,36 @@ def build_targets(name):
258258
)
259259

260260
internal_copy_files(
261-
name = "copied_test_dependency_proto_files",
261+
name = "copied_cpp_features_test_dependency_proto_files",
262262
srcs = [
263263
"//src/google/protobuf:cpp_features_proto_srcs",
264264
],
265265
strip_prefix = "src",
266266
)
267267

268+
internal_copy_files(
269+
name = "copied_java_features_test_dependency_proto_files",
270+
srcs = [
271+
"//java/core:java_features_proto_srcs",
272+
],
273+
strip_prefix = "java/core/src/main/resources",
274+
)
275+
276+
internal_copy_files(
277+
name = "copied_unittest_custom_options_unlinked_proto_files",
278+
srcs = [
279+
"//src/google/protobuf:unittest_custom_options_unlinked_proto_srcs",
280+
],
281+
strip_prefix = "src",
282+
)
283+
268284
internal_py_proto_library(
269285
name = "test_dependency_proto_py_pb2",
270-
srcs = [":copied_test_dependency_proto_files"],
286+
srcs = [
287+
":copied_cpp_features_test_dependency_proto_files",
288+
":copied_java_features_test_dependency_proto_files",
289+
":copied_unittest_custom_options_unlinked_proto_files",
290+
],
271291
include = ".",
272292
default_runtime = "",
273293
protoc = "//:protoc",

0 commit comments

Comments
 (0)