How do I get IntelliJ to recognize gradle generated sources dir?

I have a bug to report regarding the idea plug-in.

Go here:
https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html
Search the page for: generatedSourceDirs and you wont find it in the properties box towards the bottom. Also it doesn’t seem to work. Suspect it got left out somehow.

1 Like

generatedSourceDirs was missing from the DSL guide. If you take a look at the Groovydoc, it’s there.

Have you tried adding your generated source to the main sourceSet along with marking the directory as generated?

e.g.,

sourceSets.main.java.srcDir new File(buildDir, "generated-src/java")

If you are going to add them to the main sourceSet, then would you even need the idea plug-in? I’ll give it a whirl and report back.

Well it appears I was correct. The icon indicates that “generated-src/java” is being picked up as a regular source set. That’s a blue square icon. When you mark it as generated, there’s an asterisk. Also shows as regular source, rather than generated, in the module settings. Something would appear to be amiss.

This worked for me (Gradle 3.5, IDEA 2017.1.2, and the code generator happens to be javacc, but this last certainly doesn’t mater):

apply plugin: "idea"
...
sourceSets.main.java.srcDir new File(buildDir, 'generated/javacc')
idea {
    module {
        // Marks the already(!) added srcDir as "generated"
        generatedSourceDirs += file('build/generated/javacc')
    }
}

If I don’t use generatedSourceDirs then the directory will be still marked as source directory in IDEA, but it won’t be flagged as “generated sources root”. So you need to use both.

BTW, from the command line the build works even without adding the directory to the sourceSet manually, which is strange. If Gradle knew that there are sources there (or else the build should have failed), why is it not automatically in the sourceSet? Maybe just some glitch specific to the JavaCC plugin, I don’t know…

1 Like

I just stumbled on this issue and the @ddekany suggested fix worked. However, I’m wondering if this is a bug on the Gradle/IDEA integration and if there is a bug open or should be opened?

Hello. Did you find a solution? We are experiencing a similar problem.
All I found was this issue in IDEA: https://youtrack.jetbrains.com/issue/IDEA-152581

@SButterfly for me the problem still happens and the solution suggested by @ddekany above fixes it for the moment. The issue that you’ve found on IDEA but tracker is the one that I’m facing, but is was open 4 years ago, seems that nobody on Jetbraings is caring is fix it.

Edit: the issue is actually marked as fixed however seems that isn’t.