I'm trying to use Protobuf with Google's official Gradle plugin, with Gradle, in VS Code, in a small Java application to test out the workflow.
I can't get VS Code to recognize the generated Java source code, and in the terminal, I can't get Gradle to recognize the code either when I try to use Gradle to build the application.
VS Code displays this when I try to reference the generated code:
And Gradle displays the following error when I try to do a build:
> Task :app:compileJava FAILED
/home/mattgbi/vscode-gradle-protobuf-repro/app/src/main/java/vscode/gradle/protobuf/repro/App.java:14: error: cannot find symbol
System.out.println(Person.class);
^
symbol: class Person
location: class App
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 405ms
4 actionable tasks: 1 executed, 3 up-to-date
I understand that I need to add an import in App.java
to use the Person
class. But when I try to invoke the VS Code code completion tool to do that, as described above, the code completion fails to help me do that.
However, if I manually type out the import statement, VS Code says it can't find it.:
However, at this point, Gradle is able to use the import statement (even though VS Code rendered errors) and is able to complete the build:
> ./gradlew build
> Task :app:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
BUILD SUCCESSFUL in 1s
13 actionable tasks: 10 executed, 3 up-to-date
Here is a GitHub repo with a complete reproduction of the problem: https://github.com/mattwelke/repros/tree/main/vscode-gradle-protobuf-repro
Here are the steps I completed to encounter this issue:
- Create project with
gradle init
. Choose "application" and leave all settings as default. - Follow instructions at https://github.com/google/protobuf-gradle-plugin to add the plugin to my project. Add "buildscript" part to top of
build.gradle
file, aboveplugins
part of the file. Add line insideplugins
part withid "com.google.protobuf" version "0.8.18"
, below the lineid 'application'
in that part. Add block starting withprotobuf {
to bottom of the file, so that I can have Gradle download and use its ownprotoc
binary. - Follow Java tutorial for Protobuf (https://developers.google.com/protocol-buffers/docs/javatutorial), add the code that has
message Person {
near the top of the page to my application in the new fileapp/src/main/proto/message.proto
. - Build with
./gradlew build
in VS Code terminal. - Observe source code generated into
app/build/generated/source
even though there were errors in the console sayingpackage com.google.protobuf does not exist
. - Add dependency
protobuf-java
(from https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java) tobuild.gradle
file underdependencies
section of the file, so that my applications generated Java source code would be able to reference Protobuf library. Chose version 3.21.1 because it's the latest version as of right now. Added usingimplementation
syntax. - Build application again with
./gradlew build
in VS Code terminal. Observed no errors this time in terminal. - Try to write source code in
App.java
(undersrc
) that references the generatedPerson
class. VS Code can't find it. - Try to build application again with
./gradlew build
in VS Code terminal. Gradle can't find the source code either. - Manually add import statement to
App.java
. Observe VS Code displaying errors about not being able to find the package still. - Try to build application again with
./gradlew build
in VS Code terminal. Gradle can't find the source code either. This time Gradle succeeds. Only issue remaining is how the IDE recognizes all the code (desired is that all code, including generated code, is recognized).