sqldelight: Database interface occasionally not resolved

Our project has one module (named sqldelight) that depends on SqlDelight directly, and all SqlDelight code (sq files, Kotlin classes referenced in sql files, etc…) as well as some helpers that wrap the generated Database for certain things is in that module. There is another module (models) that has an api dependency on our sqldelight module, and any other module that needs access to Database depends on models.

For the past few months on our CI machines, the :sqldelight:compileReleaseKotlin occasionally fails with every file in that module that references Database saying that it is an unresolved reference. Running it again usually works. We originally though it was a build cache issue, but we’ve disabled that and it still happens.

Lately it has also been happening in Android Studio. Database is highlighted in red with the error Unresolved reference: Database but compilation works, and the generated interface is in the build directory. The only way to fix it is to restart and invalidate caches.

I’ve noticed that it tends to happen more often when switching between our dev branch and a long lived feature branch that we have (around 50k lines difference between them and many modules renamed, created, and removed).

What’s the best way to further debug this?

Build Environment SQLDelight version: 1.4.4 OS: Ubuntu 20.04 Gradle version: 6.7.1 Kotlin version: 1.4.20 AGP Version (if applicable): 4.1.1

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 23 (11 by maintainers)

Most upvoted comments

this should be fixed on master now that we dont reuse an outputdirectory for different variants

@RamenSea If you’re referring to this block:

sqldelight {
  Database { // This will be the name of the generated database class.
    packageName = "com.example"
  }
}

then Database can be whatever name you want. Maybe you just need to rename database to Database here? If not, please create a separate issue with the details.

I think I finally found something that might cause this. This is hitting us every few builds. I just tested the gradle 7 milestone and here a warning pops up:

Execution optimizations have been disabled for task ':database:generateJvmMainDatabaseSchema' to ensure correctness due to the following reasons:
  - Gradle detected a problem with the following location: '/home/paul/repos/yazio-shared/database/src/commonMain/sqldelight/databases'. Reason: Task ':database:generateJvmMainDatabaseInterface' uses this output of task ':database:generateJvmMainDatabaseSchema' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.0-milestone-3/userguide/validation_problems.html#implicit_dependency for more details about this problem.

I usually notice when I try to run my app from Android Studio. Then I try a clean and build. If that doesn’t help I try running something along the lines of gradle clean assembleDebug, and if that also fails then I use Android Studio’s invalidate caches and restart option.

Lately there’s been an additional build failure:

> Task :sqldelight:compileDevReleaseKotlin FAILED

e: ~/myproject/sqldelight/build/generated/sqldelight/code/MyDatabase/co/my/models/MyDatabase.kt: (35, 25): Unresolved reference: sqldelight

e: ~/myproject/sqldelight/build/generated/sqldelight/code/MyDatabase/co/my/models/MyDatabase.kt: (36, 25): Unresolved reference: sqldelight

e: ~/myproject/sqldelight/build/generated/sqldelight/code/MyDatabase/co/my/models/MyDatabase.kt: (145, 37): Unresolved reference: schema

e: ~/myproject/sqldelight/build/generated/sqldelight/code/MyDatabase/co/my/models/MyDatabase.kt: (168, 47): Unresolved reference: newInstance

Line 35 and 36 are imports, line 145 is from:

val Schema: SqlDriver.Schema
      get() = MyDatabase::class.schema

and line 168 is from:

operator fun invoke(
  driver: SqlDriver,
  ...
): MyDatabase = MyDatabase::class.newInstance(driver, ...)