Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDE not indexing generated source #37

Closed
davidjwiner opened this issue Jun 10, 2020 · 18 comments
Closed

IDE not indexing generated source #37

davidjwiner opened this issue Jun 10, 2020 · 18 comments
Assignees
Labels
enhancement New feature or request P2 affects usability but not blocks users
Milestone

Comments

@davidjwiner
Copy link
Contributor

Looks like that kapt has an IDE plugin to achieve this: KaptProjectResolverExtension.kt

We either need to copy it over, or simply tell users to mark generated source root themselves in the preview.

@ZacSweers
Copy link
Contributor

For now, I've manually supported this like so

sourceSets {
  main {
    java {
      srcDir(file("build/generated/ksp/src/main"))
    }
  }
  test {
    java {
      srcDir(file("build/generated/ksp/src/test"))
    }
  }
}

@ting-yuan
Copy link
Collaborator

The IDE extension, where KaptProjectResolverExtension resides, is bundled with the IntelliJ Kotlin plugin. Therefore that approach doesn't work for now.

@ting-yuan
Copy link
Collaborator

Moving this to Q4 as it depends on upstreaming, which won't happen in Q3.

@ting-yuan ting-yuan transferred this issue from android/kotlin Sep 25, 2020
@ting-yuan ting-yuan added enhancement New feature or request P1 major features or blocking bugs labels Oct 3, 2020
@ting-yuan ting-yuan added this to the 2020Q4 milestone Oct 3, 2020
@ting-yuan ting-yuan added P2 affects usability but not blocks users and removed P1 major features or blocking bugs labels Oct 17, 2020
@ting-yuan
Copy link
Collaborator

Deferring again to 2021Q1.

@ting-yuan ting-yuan modified the milestones: 2020Q4, 2021Q1 Dec 31, 2020
@SeongUgJung
Copy link

SeongUgJung commented Feb 17, 2021

buildTypes {
    getByName("debug") {
        sourceSets {
            getByName("main") {
                java.srcDir(File("build/generated/ksp/debug/kotlin"))
            }
        }
    }
    getByName("release") {
        sourceSets {
            getByName("main") {
                java.srcDir(File("build/generated/ksp/release/kotlin"))
            }
        }

    }
}

I am using above script in build.gradle.kts and working as well for now.

@ting-yuan ting-yuan modified the milestones: 1.0.0-beta, 1.0.0 Mar 22, 2021
@keyboardr
Copy link

keyboardr commented Jun 26, 2021

As a slight modification to the above, here's an approach that's a little more compact and flexible (for Android projects):

    applicationVariants.all {
        val variantName = name
        sourceSets {
            getByName("main") {
                java.srcDir(File("build/generated/ksp/$variantName/kotlin"))
            }
        }
    }

edrd-f added a commit to edrd-f/ksp that referenced this issue Aug 7, 2021
It's good for newcomers to know of this workaround while google#37 is open.
ting-yuan pushed a commit that referenced this issue Aug 11, 2021
It's good for newcomers to know of this workaround while #37 is open.
github-actions bot pushed a commit that referenced this issue Aug 11, 2021
It's good for newcomers to know of this workaround while #37 is open.

(cherry picked from commit 4aa938d)
github-actions bot pushed a commit that referenced this issue Aug 13, 2021
It's good for newcomers to know of this workaround while #37 is open.

(cherry picked from commit 4aa938d)
@gmazzo
Copy link
Contributor

gmazzo commented Nov 12, 2021

We use this logic in our tooling:

        plugins.withId("com.google.devtools.ksp") {
            the<BaseExtension>().variants.configureEach {
                addJavaSourceFoldersToModel(layout.buildDirectory
                    .dir("generated/ksp/$name/kotlin").get().asFile)
            }
        }

having:

val BaseExtension.variants: DomainObjectSet<out BaseVariant>
    get() = when (this) {
        is AppExtension -> applicationVariants
        is LibraryExtension -> libraryVariants
        is TestExtension -> applicationVariants
        else -> error("unsupported module type: $this")
    }

@naingaungluu-codigo
Copy link

naingaungluu-codigo commented Jan 28, 2022

For android folks who want to set kotlin sourceSets, we can configure it this way

android.applicationVariants.all { variant ->
    kotlin.sourceSets {
        def name = variant.name
        getByName(name) {
            kotlin.srcDir("build/generated/ksp/$name/kotlin")
        }
    }
}

@apkelly
Copy link

apkelly commented Feb 22, 2022

This worked for us in an android library module.

androidComponents.onVariants {variant ->
    kotlin.sourceSets.findByName(variant.name)?.kotlin?.srcDirs(
        file("$buildDir/generated/ksp/${variant.name}/kotlin")
    )
}

@eugene-krivobokov
Copy link

eugene-krivobokov commented May 10, 2022

I'd like to warn about issues in previously mentioned options.

    applicationVariants.all {
        val variantName = name
        sourceSets {
            getByName("main") {
                java.srcDir(File("build/generated/ksp/$variantName/kotlin"))
            }
        }
    }

sourceSets is not variant aware.
This code adds generated code from all build variants to the main source set.
E.g. generated code for "debug" and "release" build variants will be in "main" source set.
Print out source sets to see the final state.
It goes against the idea of build variants. This is probably not what you want.

configure<BaseExtension> {
    buildVariants.configureEach {
        sourceSets {
            named(name).configure {
                java.srcDir("build/generated/ksp/$name/kotlin")
            }
        }
    }
}

This alternative has another issue.
It adds generated code as input and breaks build cache.
I'll show this on example.

First clean build:

./gradlew :module:clean  :module:kspReleaseKotlin -Dorg.gradle.caching.debug=true

Appending input file fingerprints for 'source' to build cache key: xxx - RELATIVE_PATH{/project/module/src/main/java/.../Clazz.kt' / ...}

There were only sources as input for this task.
Now, lets run again:

./gradlew  :module:kspReleaseKotlin -Dorg.gradle.caching.debug=true

Appending input file fingerprints for 'source' to build cache key: yyy - RELATIVE_PATH{/project/module/build/generated/ksp/release/kotlin/<generated>, /project/module/src/main/java/.../Clazz.kt' / ...}

Here we see generated files as an addition to original sources.
It causes a cache miss. The output is not "stable".

@phucynwa
Copy link

For android folks who want to set kotlin sourceSets, we can configure it this way

android.applicationVariants.all { variant ->
    kotlin.sourceSets {
        def name = variant.name
        getByName(name) {
            kotlin.srcDir("build/generated/ksp/$name/kotlin")
        }
    }
}

For build.gradle.kts

    android.applicationVariants.all {
        kotlin {
            sourceSets {
                getByName(name) {
                    kotlin.srcDir("build/generated/ksp/$name/kotlin")
                }
            }
        }
    }

@lukaszkalnik
Copy link

@eugene-krivobokov can you suggest correct solution for Android when using build variants? Thank you 🙏

@eugene-krivobokov
Copy link

Unfortunately, I haven't found it yet.

@leinardi
Copy link

After upgrading to Android Studio Electric Eel the following solution didn't work anymore:

android {
    libraryVariants.all {
        kotlin.sourceSets {
            getByName(name) {
                kotlin.srcDir("build/generated/ksp/${name}/kotlin")
            }
        }
    }
}

But this one seems to work fine:

android {
    kotlin {
        sourceSets {
            debug {
                kotlin.srcDir("build/generated/ksp/debug/kotlin")
            }
            release {
                kotlin.srcDir("build/generated/ksp/release/kotlin")
            }
        }
    }
}

@Jacob3075
Copy link

None of the above solutions work on Android Studio Flamingo | 2022.2.1 Canary 9
kotlin version: 1.7.20
ksp: 1.7.20-1.0.8
AGP: 8.0.0-alpha09
Gradle: 7.6

The solution that leinardi gave was working around canary 4 but I don't remember in which version it stopped.

@ting-yuan ting-yuan modified the milestones: 1.0.0, 1.0.9 Jan 6, 2023
@RavenLiao
Copy link

RavenLiao commented Jan 12, 2023

    @Suppress("UnstableApiUsage")
    sourceSets.configureEach {
        kotlin.srcDir("$buildDir/generated/ksp/$name/kotlin/")
    }

it works well on Android Studio.
it come from

@ting-yuan
Copy link
Collaborator

Fixed in e81d01f and will be available in 1.0.9. There should no longer workarounds be needed.

@eygraber
Copy link

eygraber commented Jul 7, 2023

@ting-yuan using 1.0.11 it looks like the following is still required sorry posted on the wrong issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request P2 affects usability but not blocks users
Projects
None yet
Development

No branches or pull requests