Search code examples
thriftbazel

bazel error while running build file, wrong protocol, is it?


facing following error when running the build file through bazel

ERROR: /home/mywork1/...../X/service/tests/BUILD:47:11: in deps attribute of java_binary rule //javatests/.../X/service/tests:XHandler-Test:
'//java/..../X/service:thrift-server' does not have mandatory provider 'link_params' or 'java_common.provider'
and java_binary rule '//java/..../X/service:thrift-server' is misplaced here
(expected cc_binary, cc_library, genrule, genproto, java_import, java_library, java_proto_library, java_lite_proto_library, proto_library, sh_binary or sh_library).

The BUILD file looks like this:

package(default_visibility = ["//visibility:public"])

java_library(
    name = "X-thrift-s",
    srcs = glob(["*.java"]),
    deps = [
        "//thrift/services/X:XService",
    "//java/com/.../services/X/service:thrift-server",
    ]
)

java_import(
    name = "X-thrift-p",
    jars = [
        "//external:thrift-jar",
        "//external:opencsv-jar",
        "//java/lib:cspj-jar",
    ]
)

load("//tools/bzl:genthrift.bzl","thrift_java_library")

thrift_java_library(
 name = "XService",
 srcs = [
     "XService.thrift"        
 ],
)


java_binary(
   name = "XHandler-Test",
   srcs = glob(["*.java"]),
   main_class = "com.......service.tests.XHandlerTest",
   deps = [
       "//java/....:X-thrift",
       "//java/com.....:thrift-server",
       "//java/com.....:XService",
   ],  
)

Solution

  • In order to behave as a Java library, a rule needs to provide certain information. Evidently, thrift_java_libary does not. There is a blog post on how to implement this if you're a rule writer.

    However, assuming you're just using the Thrift rules and don't want to modify them: are the Thrift rules generating a .jar file as output? If so, you could do:

    thrift_java_library(
     name = "XService",
     srcs = [
         "XService.thrift"        
     ],
    )
    
    java_import(
        name = "XServiceWrapper",
        jars = ["XService.jar"], # Replace this with the actual name of the thrift_library's output file
    )
    
    java_binary(
        ...
        deps = [":XServiceWrapper", ...],
    )
    

    What you're doing is manually wrapping the output in a rule that's compatible with java_binary.