IntelliJ IDEA doesn't see classes generated from protobuf files in a subproject
Asked Answered
F

3

5

I have a Gradle project with modules. moduleA contains only protobuf files and produces a jar file with classes generated from the .proto files. moduleB depends on the moduleA (implementation project(':moduleA')).

moduleA
│   build.gradle
│   src
│   └───main
│       └───proto  <-- proto file defining gRPC services
moduleB
│   build.gradle
│   src            <-- code dependent on classes generated from moduleA
build.gradle

The project works well if I build/run it from Gradle.

Problem: IntelliJ IDEA doesn't see the classes generated from moduleA in sources of moduleB (imports are red).

Question: How to make IntelliJ IDEA correctly recognize classes built from .proto files?

I am using IntelliJ IDEA 2020.2.4 (Ultimate Edition).

Fugitive answered 9/12, 2020 at 15:13 Comment(0)
A
6

For the IDE to resolve classes and imports from dependant module these classes should exist and they must be located in dependant module's source directory. Looks like the classes are generated into a directory which is not recognized by IDE as a source directory. Try adding this generated directory as a Gradle source set. In moduleA's Gradel build file add:

sourceSets {
    main {
        java {
            srcDirs = ['build/generated/source/proto/main/java']
        }
    }
}

where 'build/generated/source/proto/main/java' - the directory where sources are generated.

There is a related issue for IntelliJ IDEA: IDEA-209418.

Assyria answered 10/12, 2020 at 8:31 Comment(3)
The protobuf-gradle-plugin is adding generated code to IDEA model: github.com/google/protobuf-gradle-plugin/blob/… It should work out of the box (and I tried it out by myself). The only thing is the generated code must be there already.Fellows
@Fellows I am using Quarkus. It generates code to a different directory. Maybe that's the reason why Idea doesn't pick it.Fugitive
We can something like this for multiple source folders - sourceSets.main.java.srcDir new File(buildDir, 'build/generated/source/proto/main/java')Barrage
D
1

The location of the classes generated from the .proto files is not known by Intellij out of the box. The 'com.google.protobuf' gradle plugin registers the source directories with the generated code for Intellij to see when the idea' gradle plugin is applied in the build.gradle file of moduleA.

See IntelliJ IDEA tips for com.google.protobuf gradle plugin for protobuf gradle plugin documentation.

This setup works for me with Intellij IDEA 2021.1.

Short answer: Apply the idea gradle plugin to moduleA.

Dover answered 7/7, 2021 at 7:30 Comment(0)
F
0

This is same as using proto generated code in the same module, it needs to be there first before it can be picked up by IDE.

Such dependencies cannot be resolved statically. Proto generated classes are available only after moduleA is built. You would need to build moduleA at least once (and refresh the IDE imports if necessary) to make its generated code reachable to moduleB.

Fellows answered 9/12, 2020 at 22:29 Comment(6)
moduleA is built automatically every time moduleB is built. But I also tried building moduleA manually - doesn't help.Fugitive
To which location does moduleA generate source files? Is this directory detected by IDE as a source directory type? Can you share a sample project?Assyria
- moduleA is built automatically every time moduleB is built. That's not the point. Compilation can always find the generated code at build time. But you are wanting to let the IDE pick up the generated code before building moduleB. Then you must have generated code in moduleA accessible. - Is this directory detected by IDE as a source directory type? The protobuf-gradle-plugin has integration with IDEA, the directory for generated code will be marked as module srcDirs automatically.Fellows
Make sure to refresh your IDE imports (on the Intellij Gradle toolbar, click Reimport All Gradle Projects) after generating code in moduleA.Fellows
@Fellows I will create a sample project and share it with you. Refreshing project, rebuilding it, marking the build directory as a source directory, all of these didn't help. I had to manually add the directory in Grade as it was suggested in another answerFugitive
Sure, you can open an issue on github if you have a minimal reproducible project.Fellows

© 2022 - 2024 — McMap. All rights reserved.