Error for project.tasks.getting delegation in .kt file, but not in build.gradle.kts

The Kotlin function below compiles fine when in build.gradle.kts and can be used in that file.

The same function in a normal .kt file, however, outputs a compile error for the line with the comment // ERROR HERE.

Is it possible to fix this? If so, how?

// Imports only in .kt file, not in build.gradle.kts
import org.gradle.api.Task
import org.gradle.kotlin.dsl.getting

val Task.isTestCompile: Boolean
    get() {
        val testClasses by project.tasks.getting // ERROR HERE

        return testClasses.taskDependencies.getDependencies(testClasses).contains(this)

The command-line gradlew error is:

Missing 'getValue(Nothing?, KProperty<*>)' method on delegate of type 'ExistingDomainObjectDelegate<out Task!>'

In IntelliJ 2019.3 Beta, the error is:

Type 'ExistingDomainObjectDelegate<out Task!>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate

Have you found the root cause of this? I have the same problem with sourceSets after upgrading to Kotlin 1.3.70

I just replaced the error line with the following less elegant line:

val testClasses = project.tasks.getByName("testClasses")

Adding {} also worked for me:

    val testClasses: Task  by getting {}