GRPC example doesn't build with bazel

2,104 views
Skip to first unread message

Yukang Yang

unread,
Jun 26, 2017, 1:15:44 PM6/26/17
to grpc.io
Hi GRPC experts,

I found the proto in GRPC example doesn't build with bazel. I used GRPC from head. bazel is got from brew install bazel.

Here is what I did:

cd grpc
git pull && git submodule init && git submodule update && git submodule status
cd examples
bazel build :helloworld

I got:

ERROR: /Users/yukang/git-projects/grpc/WORKSPACE:16:1: no such package '@com_google_protobuf//': In new_local_repository rule //external:com_go
ogle_protobuf the 'build_file' attribute does not specify an existing file (/Users/yukang/git-projects/grpc/third_party/protobuf/BUILD does not
 exist) and referenced by '//external:protobuf'.
ERROR: Analysis of target '//examples:helloworld' failed; build aborted.

I have verified that /Users/yukang/git-projects/grpc/third_party/protobuf/BUILD exists.

I also copied example out of GRPC repository and tried to set up an external dependency on grpc repository.

Here is what I did:


git clone g...@bitbucket.org:xyyk/grpc-example.git
cd grpc-example
bazel build :helloworld

I got:

ERROR: error loading package '': Extension file not found. Unable to load package for '@grpc//bazel:grpc_build_system.bzl': BUILD file not found on package path.

I also saw the post at http://www.grpc.io/blog/bazel_rules_protobuf. But their example at https://github.com/pubref/grpc_greetertimer doesn't build either.

Does someone know what is wrong here?

Thanks,
Yukang

Nicolas Noble

unread,
Jun 27, 2017, 8:13:36 PM6/27/17
to Yukang Yang, grpc.io
So, first, you can't just grab a portion of the tree and expect it to work elsewhere. What's important is that it's able to find the WORKSPACE file properly. So your second test with the copy wouldn't work without a proper WORKSPACE file. Plus some of the examples are referring to files down the tree, such as, well, the grpc library itself.

Then, the error itself you're seeing seems is an old one that was fixed a while ago, so I'm not sure you're really at head here.

:~/sources/grpc (master)$ git show --oneline
c4f85b7 Merge pull request #11474 from makdharma/cares_fix

:~/sources/grpc (master)$ bazel build examples/...
INFO: Found 18 targets...
INFO: Elapsed time: 102.154s, Critical Path: 9.91s
:~/sources/grpc (master)$ ./bazel-bin/examples/greeter_server &
[1] 11025
I0628 02:05:58.983490133   11025 server_builder.cc:254]      Synchronous server. Num CQs: 4, Min pollers: 1, Max Pollers: 2, CQ timeout (msec): 10000
Server listening on 0.0.0.0:50051
:~/sources/grpc (master)$ ./bazel-bin/examples/greeter_client
Greeter received: Hello world


--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/b7f43ecb-0ebf-47d4-a0aa-20157f210881%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yukang Yang

unread,
Jun 28, 2017, 12:37:52 PM6/28/17
to grpc.io, zjh...@gmail.com, bazel-...@googlegroups.com
Hi Nicolas,

Thanks for reply!


I just git pull. Now I am at the same commit as you. But It still doesn't build, although this time it is a different error message. I am not sure if I am the only person who can get this error. Here is what I did:

$ cd GRPC_REPOSITORY
$ git show --oneline
c4f85b78f3 (HEAD -> master, origin/master, origin/HEAD) Merge pull request #11474 from makdharma/cares_fix
// same commit as yours

$ ~/bazel version
Build label: 0.5.2- (@non-git)
// brew hasn't updated bazel and 0.5.1 has a bug. I built bazel from 0.5.2 distribution package

$ ~/bazel build examples/...
external/com_google_protobuf/BUILD.bazel:367:1: in cc_binary rule @com_google_protobuf//:protoc: cycle in dependency graph:
    //examples:_auth_sample_codegen
    //external:protocol_compiler (host)
.-> @com_google_protobuf//:protoc (host)
|   @com_google_protobuf//:protoc_lib (host)
|   @com_google_protobuf//:protobuf (host)
|   @com_google_protobuf//:src/google/protobuf/util/internal/testdata/timestamp_duration.pb.h (host)
|   @com_google_protobuf//:cc_test_protos_genproto (host)
`-- @com_google_protobuf//:protoc (host)
This cycle occurred because of a configuration option.
ERROR: Analysis of target '//examples:_auth_sample_codegen' failed; build aborted.
// I checked protobuf BUILD file and I don't know how is @com_google_protobuf//:protobuf depends on a target of timestamp_duration.pb.h.

2. Problem in my own example (https://bitbucket.org/xyyk/grpc-example/overview):

I realized that more stuff is needed in WORKSPACE of my own example. Now I have went through all errors of "BUILD file not found", added missing external dependency and bind in my WORKSPACE, and fixed all of them. I no longer get any error about package or BUILD. But this time it seems bazel cannot output cc files successfully.

$ cd MY_OWN_EXAMPLE
$ ~/bazel build :helloworld

ERROR: /Users/yukang/git-projects/grpc-example/BUILD:31:1: output 'rotos/helloworld.pb.h' was not created.
ERROR: /Users/yukang/git-projects/grpc-example/BUILD:31:1: output 'rotos/helloworld.pb.cc' was not created.
ERROR: /Users/yukang/git-projects/grpc-example/BUILD:31:1: not all outputs were created or valid.
ERROR: /Users/yukang/git-projects/grpc-example/BUILD:31:1: output 'rotos/helloworld.grpc.pb.h' was not created.
ERROR: /Users/yukang/git-projects/grpc-example/BUILD:31:1: output 'rotos/helloworld.grpc.pb.cc' was not created.
Target //:helloworld failed to build

It shows output as 'rotos/.....', while the output should actually be 'protos/.....' (the first letter 'p' is missing). This looks like a bazel issue. I also tried to build with -s

$ ~/bazel clean
$ ~/bazel build :helloworld -s

Last output before the error is often this one:
>>>>> # //:_helloworld_codegen [action 'Generating rotos/helloworld.pb.h']
(cd /private/var/tmp/_bazel_yukang/daf269fd3c8ea3827a82b98ac2b45027/execroot/__main__ && \
  exec env - \
  bazel-out/host/bin/external/com_google_protobuf/protoc '--cpp_out=:bazel-out/darwin_x86_64-fastbuild/genfiles' '-Iprotos/helloworld.proto=protos/helloworld.proto' protos/helloworld.proto)

If I run the command as is, i.e. first cd into __main__, and then use exec env - bazel-out....., my terminal crashes immediately. I don't know where I can find any error log about the crash.

The interesting thing is if I modify this command by a little. I still cd into __main__,  remove 'exec env - ' and run only 'bazel-out/..../protoc ....'. It works. and I can see the output cc files in my bazel-out. This looks like a bazel issue.

To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.

Nicolas Noble

unread,
Jun 28, 2017, 6:03:55 PM6/28/17
to Yukang Yang, grpc.io, bazel-...@googlegroups.com
That last error is a known problem with protobuf; you've run make, and that leaves files around that confuses Bazel. Please clean the protobuf subdirectory using git clean -f -d -x for example.

To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

Yukang Yang

unread,
Jun 28, 2017, 8:24:28 PM6/28/17
to Nicolas Noble, grpc.io, bazel-...@googlegroups.com
Thanks, Nicolas! I tried too many to make it work and as a result, my repository was full of junk. I just deleted entire repository and cloned it again. I can successfully build GRPC's example with bazel now.

But my own example (https://bitbucket.org/xyyk/grpc-example/overview) still fails with the same error. Still looking into it.



You received this message because you are subscribed to a topic in the Google Groups "grpc.io" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grpc-io/mbGvH013Bhc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grpc-io+unsubscribe@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

Yukang Yang

unread,
Jun 30, 2017, 3:10:37 AM6/30/17
to Nicolas Noble, grpc.io, bazel-...@googlegroups.com
I finally know why my own example doesn't work. This is a BUG in grpc/bazel/generate_cc.bzl.

line 13: label_len = len(ctx.label.package) + 1

should actually be

label_len = len(ctx.label.package)
if label_len != 0:
  label_len = label_len + 1

grpc's example works even with the bug because BUILD file is in a subdirectory and its package is "example". When package is not empty string, it makes sense to add 1 because proto path is examples/protos/helloworld.proto. Plus 1 can remove the / after "examples".

However, this is not the case when package is an empty string. In my own example, BUILD file is in topmost directory and package is an empty string. there is no leading / before output path. for example, my proto path is proto/helloworld.proto. there is no need to plus 1 in this case.

I am using grpc_proto_library because of https://github.com/grpc/grpc/issues/11176. I am guessing it is the recommended way to use bazel with c++ grpc and it probably should not be limited to grpc's own use case.

Nicolas Noble

unread,
Jun 30, 2017, 12:20:47 PM6/30/17
to Yukang Yang, grpc.io, bazel-...@googlegroups.com
This may sound like a bug indeed. Could you file one on github ?

Yukang Yang

unread,
Jun 30, 2017, 10:32:09 PM6/30/17
to Nicolas Noble, grpc.io, bazel-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages