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).

Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148

3 Answers3

5

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.

Andrey
  • 15,144
  • 25
  • 91
  • 187
  • 1
    The protobuf-gradle-plugin is adding generated code to IDEA model: https://github.com/google/protobuf-gradle-plugin/blob/bf6301ac28c22c78eb930c7f249ebbb7f691b5aa/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy#L540 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. – voidzcy Dec 10 '20 at 22:39
  • @voidzcy I am using Quarkus. It generates code to a different directory. Maybe that's the reason why Idea doesn't pick it. – Sasha Shpota Dec 11 '20 at 14:38
  • We can something like this for multiple source folders - sourceSets.main.java.srcDir new File(buildDir, 'build/generated/source/proto/main/java') – Avinash Kadam Feb 14 '22 at 15:16
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.

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.

voidzcy
  • 520
  • 2
  • 7
  • `moduleA` is built automatically every time `moduleB` is built. But I also tried building `moduleA` manually - doesn't help. – Sasha Shpota Dec 10 '20 at 07:41
  • To which location does moduleA generate source files? Is this directory detected by IDE as a [**source** directory type](https://www.jetbrains.com/help/idea/content-roots.html#folder-categories)? Can you share a sample project? – Andrey Dec 10 '20 at 08:24
  • - 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. – voidzcy Dec 10 '20 at 19:08
  • Make sure to refresh your IDE imports (on the Intellij Gradle toolbar, click Reimport All Gradle Projects) after generating code in `moduleA`. – voidzcy Dec 10 '20 at 19:13
  • 1
    @voidzcy 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 answer](https://stackoverflow.com/a/65230983/2065796) – Sasha Shpota Dec 11 '20 at 14:45
  • Sure, you can open an issue on github if you have a minimal reproducible project. – voidzcy Dec 12 '20 at 08:40