My CustomTest.java has this import:

com.google.protobuf.Timestamp

I'm using java_test_suite to run tests in my BUILD file like so:

java_test_suite(
    name = "all-tests",
    srcs = glob(["src/test/java/**/*.java"]),
    runner = "junit5",
    test_suffixes = ["Test.java"],
    runtime_deps = JUNIT5_DEPS,
    deps = [
        ":mylib",
        "@com_google_protobuf//:timestamp_proto",
        artifact("org.junit.jupiter:junit-jupiter-api"),
        artifact("org.junit.jupiter:junit-jupiter-params"),
    ] + deps,
)

However when I run tests on it using:

bazel test //:all-tests

I'm getting this error:

src/test/java/com/x/CustomTest.java:75: error: [strict] Using type com.google.protobuf.Timestamp from an indirect dependency (TOOL_INFO: "@com_google_protobuf//:timestamp_proto wrapped in java_proto_library"). See command below **
private static Timestamp timestampFromMilli(long milli) {
               ^
 ** Please add the following dependencies: 
  @com_google_protobuf//:timestamp_proto to //:src/test/java/com/x/CustomTest 
 ** You can use the following buildozer command: 
buildozer 'add deps @com_google_protobuf//:timestamp_proto' //:src/test/java/com/x/CustomTest 

What do I need to do exactly? I tried using the buildozer command but all I got was:

rule 'src/test/java/com/x/CustomTest' not found

Where do I need to add this @com_google_protobuf//:timestamp_proto?

1

There are 1 best solutions below

0
On

Looking at protobuf's build files, it looks like timestamp_proto is a plain proto_library:

https://github.com/protocolbuffers/protobuf/blob/main/BUILD.bazel#L70-L74

https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/BUILD.bazel#L64-L68

and so per the advice here:

https://github.com/protocolbuffers/protobuf/blob/main/BUILD.bazel#L19-L25

you might just need to use java_proto_library to make the java version of the proto:

java_proto_library(
  name = "timestamp_java_proto",
  deps = ["@com_google_protobuf//:timestamp_proto"],
)

and then use that in the deps of your java_test_suite instead of the timestamp_proto.

Just a guess, but the error message is not very helpful maybe because there happens to be a Timestamp java class in the deps of the plain proto library, and Strict deps is finding that one in the test's indirect dependencies. Might be worth filing a bug about it on https://github.com/bazelbuild/bazel/issues