-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
Expected Behavior
No error should be shown in the IDE kotlin script editor.
Current Behavior
Workaround
You can install the Gradle Libs Error Suppressor IntelliJ Plugin or add the @Suppress("DSL_SCOPE_VIOLATION")
annotation as follows:
Context
This was reported on the Kotlin IntelliJ plugin bug tracker first:
It is in fact a Gradle issue.
Version catalog accessors are available as a Project
extension, e.g. libs
, outside of the scope of the plugins {}
block. This is exactly what the IDE correctly reports.
It works in the Groovy DSL because there the version catalogs are explicitly made available via ScriptTarget.getPluginsBlockPermits()
. This was not done for the Kotlin DSL.
This was not spotted when adding support for version catalog plugin aliases in the plugins {}
block because the template class used for the evaluation of the Kotlin DSL plugins block does not restrict access to the Project
in CompiledKotlinBuildscriptAndPluginsBlock
(not using the PluginDependenciesSpecScope
and has @ImplicitReceiver(Project::class)
).
This is asymetrical with CompiledKotlinSettingsPluginManagementBlock
that correctly applies the limitation using PluginDependenciesSpecScope
which is annotated with @GradleDsl
but still it has @ImplicitReceiver(Settings::class)
. This is an old mistake. This should be fixed. IOW version catalogs in the Kotlin DSL plugins block work by accident.
Once fixed it will break usage of version catalog plugin aliases from the Project
extension in the plugins {}
block for the Kotlin DSL. In order to make it work again, version catalog accessors should be made available in the plugins {}
block.
There are of course complications because, although the Groovy DSL applies limitations so that only <catalogName>.plugins.*
is allowed, there's no such limitation on the Kotlin DSL. The Groovy DSL limitations capture the original intent for the feature but adding the same limitation on the Kotlin DSL to a stable feature is a breaking change. That can only happen on a major Gradle version. It could be introduced behind a feature flag in a minor version though.
Steps to Reproduce
- clone https://github.com/eskatos/gradle-kotlin-dsl-version-catalog-plugins-issue
- open in IDE
- open the
build.gradle.kts
file