Open runningcode opened 6 years ago
Can you provide some code snippet examples?
This build.gradle.kts
from the linked github project has two examples:
https://github.com/runningcode/TripletBug/blob/master/build.gradle.kts.
For example writing this code fails to compile inside the buildscript block, but works fine outside the buildscript block.
ext["kotlin_version"] = "1.2.50"
The function notUsableInBuildscript()
in that build.gradle.kts
file doesn't compile when called inside the buildscript block but compiles elsewhere in the buildscript.
Can you use a fully qualified version of the method and have it work?
I think that this is failing because imports can only be satisfied after the buildscript
block has been executed.
Functionally, what you'd need is a special set of imports specific to the buildscript
block. Or maybe @bamboo and @eskatos can figure out some dark magic that will make this possible.
A fully qualified version of the function notUsableInBuildscript()
works.
However, I can't get a fully qualified version of the extension property ext
to work.
I tried: org.gradle.kotlin.dsl.ext["kotlin_version"] = "1.2.50"
I just tried kotlin dsl for gradle. First thing that got me wondering was the maven-block inside the buildscript block. I tried to use the default java.net.URI class to set the url, but had to rewrite my script, because "java" is an extension? I know there is a workaround, but this is something that beginners have to fight with and its not a great experience.
Moving to 7.0 as it's a potentially breaking change for scripts.
It's already mentioned in the title, but I want to emphasize that the same issue exists in the plugins block (as demonstrated here: https://github.com/BjoernPetersen/gradle-import-bug-demo).
Any update on this?
It works in Groovy, so what kind of black magic is used there, that cannot be used in Kotlin DSL?
@herrbert74 Groovy is a dynamic language. Kotlin is not.
I'm facing the same issue. When is it going to be solved?
same issue here. it breaks the build system on kotlin dsl. Dependencies like kotlin have both library dependencies and plugin dependencies, so we must maintain two kotlin version values in separate files. One in buildSrc folder and another in root build.gradle.kts.
one way to mitigate the issue is the following (build.gradle.kts
):
import com.something.something.Versions
plugins {
// buildSrc import issue: https://github.com/gradle/gradle/issues/9270
val versions = com.something.something.Versions
kotlin("jvm") version versions.kotlin
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Versions.kotlin}")
}
In this case Versions
is a kotlin object in the package com.something.something
in the buildSrc
folder
package com.something.something
object Versions {
const val kotlin = "1.7.10" //https://github.com/JetBrains/kotlin/releases/latest
}
plugins { // buildSrc import issue: https://github.com/gradle/gradle/issues/9270 val versions = com.something.something.Versions kotlin("jvm") version versions.kotlin }
I'm not happy that this fixes it, lol.
So, just to make sure I understand this, the issue is you can't use an import
to pull Versions
(or whatever reference from buildSrc
) into the script and reference it in the plugins
block?
Instead you must use the fully-qualified reference to the definition in buildSrc
, in the plugins
block.
It's "by design" guys. https://github.com/gradle/kotlin-dsl-samples/issues/158#issuecomment-358374735
Imported functions and properties cannot be used inside of a
buildscript { }
block in abuild.gradle.kts
file. Extension functions also cannot be used.If I define a function
foo()
inside buildSrc in the root source package, then function foo is usable in buildscript blocks inbuild.gradle.kts
files.However, if I move the function
foo()
in to packagebar
such that at the top of mybuild.gradle.kts
file, I must now writeimport bar.foo
, then this function cannot be used inside the builscript block of the build file, but it can be used in other parts of the file.Expected behavior,
I expect to be able to use imported function declared in other packages inside
buildscript
blocks ofbuild.gradle.kts
files.Context
This means that code and functions must be duplicated in order to be used inside buildscript blocks.
Steps to Reproduce (for bugs)
Sample repro project: https://github.com/runningcode/TripletBug/ using kotlin-dsl 0.17.5 with gradle 4.8