bazel, protobuf, and intellij

436 views
Skip to first unread message

taro...@gmail.com

unread,
Oct 3, 2017, 11:20:31 AM10/3/17
to bazel-discuss
I'm having an issue where I have a working java program, but intellij doesn't recognize the method toByteArray.

I forked https://github.com/cgrushko/proto_library on my local machine, imported the workspace into intellij and built. I then added the following java main class:

package src;

import demo.PersonOuterClass;

public class Main {
public static void main(String argv[]) {
byte[] ba = PersonOuterClass.Person.newBuilder().setEmail("dwwd").build().toByteArray();
for (byte b : ba) {
System.out.println(b);
}
}
}


and the following build rule

java_binary(
name = "Main",
main_class = "src.Main",
srcs = ["Main.java"],
deps = [":person_java_proto"]
)


The program builds and runs properly, but in intellij toByteArray() is red and intellij says that it can't resolve the method.

I suspect the issue is that generated Person extends com.google.protobuf.GeneratedMessageV3 but intellij doesn't know about GeneratedMessageV3 and that it extends a class, AbstractMessageLite, that defines the toByteArray method.

Anyone know how to fix either the bazel build target(s) or intellij so that toByteArray is resolved by the ide?

Thanks,

Tom.

taro...@gmail.com

unread,
Oct 3, 2017, 11:29:46 AM10/3/17
to bazel-discuss

taro...@gmail.com

unread,
Oct 7, 2017, 11:25:03 PM10/7/17
to bazel-discuss
I had an answer that I posted on s/o, but it isn't great, so still looking.
Message has been deleted

Tom Robinson

unread,
Oct 8, 2017, 12:54:47 AM10/8/17
to bazel-discuss
I tried the following, which seems close to working:
1. Remove . from the directories in .bazelproject
2. add the following to the directories
src/
bazel-blx/external/com_google_protobuf_java/java/core/src/main/java
3. Open project structure -> Modules and remove the package prefix from the second folder from step 2
 
Then the sources for protobuf lib resolve, but every time I sync, I have to repeat step 3.  
Is there a way to avoid having the bazel plugin add a package prefix to the source folder for the 
protobuf source folder?

Thanks,

Tom.
Reply all
Reply to author
Forward
0 new messages