zf8848 / protobuf

Automatically exported from code.google.com/p/protobuf
Other
0 stars 0 forks source link

too strict check on .proto file path in Descriptors.java #577

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. prepare files
$ cat a/aa.proto
message A {
    optional string name = 1;
}

$ cat b/bb.proto
import "aa.proto";

message B {
    optional string name = 1;
    optional A a = 2;
}

$ cat A.java
import com.google.protobuf.*;

public class A {
    public static void main(String[] args) {
        Bb.B b = Bb.B.newBuilder().build();
        System.out.println(TextFormat.printToUnicodeString(b));
    }
}

2. compile files

$ protoc a/aa.proto --java_out .
$ protoc -Ia -I. b/bb.proto --java_out .
$ javac -cp 'protobuf-java-2.5.0.jar' *.java

3. run

java -cp 'protobuf-java-2.5.0.jar:.' A
Exception in thread "main" java.lang.ExceptionInInitializerError
    at Bb$B.internalGetFieldAccessorTable(Bb.java:129)
    at com.google.protobuf.GeneratedMessage.getAllFieldsMutable(GeneratedMessage.java:105)
    at com.google.protobuf.GeneratedMessage.getAllFields(GeneratedMessage.java:153)
    at com.google.protobuf.TextFormat$Printer.print(TextFormat.java:272)
    at com.google.protobuf.TextFormat$Printer.access$400(TextFormat.java:248)
    at com.google.protobuf.TextFormat.printToUnicodeString(TextFormat.java:146)
    at A.main(A.java:6)
Caused by: java.lang.IllegalArgumentException: Invalid embedded descriptor for 
"b/bb.proto".
    at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:301)
    at Bb.<clinit>(Bb.java:703)
    ... 7 more
Caused by: com.google.protobuf.Descriptors$DescriptorValidationException: 
b/bb.proto: Dependencies passed to FileDescriptor.buildFrom() don't match those 
listed in the FileDescriptorProto.
    at com.google.protobuf.Descriptors$FileDescriptor.buildFrom(Descriptors.java:246)
    at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:299)
    ... 8 more

What is the expected output? What do you see instead?

I expect A.java will run successfully without exception thrown.  The cause is 
strict check at 
http://code.google.com/p/protobuf/source/browse/trunk/java/src/main/java/com/goo
gle/protobuf/Descriptors.java?spec=svn514&r=425#245

Possible solutions:
(1) change protoc's code, not embed the file path of .proto files in command 
line into generated Java source code, only embed the base file name.

or
(2) change Descriptors.java to only compare the base file name.

What version of the product are you using? On what operating system?

2.5.0, Mac OS X.

Please provide any additional information below.

It's hard to ask all .proto files to be put in same directory, currently my 
workaround is to compile under each sub directory:
$ cd a; protoc --java_out .. aa.proto
$ cd ../b; protoc --java_out .. -I../a -I. bb.proto

This works but looks not intuitive.

Original issue reported on code.google.com by li...@yahoo-inc.com on 15 Nov 2013 at 6:11

GoogleCodeExporter commented 9 years ago
The commands you used to compile the protos are faulty. You should use:
$ protoc -Ia a/aa.proto --java_out .
$ protoc -Ia -Ib b/bb.proto --java_out .
Let me know if you still experience problems after compiling protos like the 
above.

Original comment by xiaof...@google.com on 15 Nov 2013 at 7:14

GoogleCodeExporter commented 9 years ago
Oh, I thought it should be "protoc -Ia aa.proto --java_out ." but it reports 
"aa.proto: No such file or directory".

The command usage is a little weird, but it does work, thanks!

Original comment by li...@yahoo-inc.com on 16 Nov 2013 at 4:05