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

Precompiled script plugin on Groovy with a package declaration #28851

Open
nbir94 opened this issue Apr 17, 2024 · 1 comment
Open

Precompiled script plugin on Groovy with a package declaration #28851

nbir94 opened this issue Apr 17, 2024 · 1 comment

Comments

@nbir94
Copy link

nbir94 commented Apr 17, 2024

Issue type

Wrong or misleading information

Problem description

According to the documentation for Precompiled script plugins, there is an opportunity to add a package declaration for a plugin implemented either on Groovy or Kotlin:

For example, the script src/main/*/java-library.gradle(.kts) has a plugin ID of java-library (assuming it has no package declaration).
Likewise, src/main/*/my/java-library.gradle(.kts) has a plugin ID of my.java-library as long as it has a package declaration of my.

By the way, it's impossible to create a plugin named java-library because it conflicts with a core plugin with the same name. But here I want to describe another problem.

When I implement a plugin with a package statement on Kotlin, it works according to the documentation. I could not succeed in doing the same on Groovy. If I add a package statement in the plugin having a .gradle extension like below:

// buildSrc/src/main/groovy/com/example/my-plugin.gradle
package com.example

I see an error from Gradle:

Execution failed for task ':buildSrc:extractPluginRequests'.
Script '/Users/enbirr/IdeaProjects/precompiled-plugins/buildSrc/src/main/groovy/com/example/my-plugin.gradle' should not contain a package statement.

Without specifying the package in the plugin, I cannot use the plugin ID containing the package as suggested in the documentation - com.example.my-plugin. I assume that in this case, com and example are treated as directories rather than packages. Thus, I can only use my-plugin as the plugin identifier in the build script.

Is there a misleading in the documentation, or do I need to do something differently?

Gradle version: 8.7
Mac OS

Context (optional)

I am implementing navigation to the plugin source from its ID in IntelliJ IDEA for statements like

// build.gradle
plugins {
    id 'my-plugin'
}

I found this issue while learning the documentation to find possible approaches for plugin implementation. I am looking for conditions that would allow me to verify that a file found for navigation is a Gradle plugin and not something else.

Page with the problem

https://docs.gradle.org/current/userguide/custom_plugins.html#sec:precompile_script_plugin

@nbir94 nbir94 added a:documentation Documentation content to-triage labels Apr 17, 2024
@ov7a
Copy link
Member

ov7a commented Apr 19, 2024

By the way, it's impossible to create a plugin named java-library because it conflicts with a core plugin with the same name. But here I want to describe another problem.

Indeed, the example should use a different name.

Is there a misleading in the documentation, or do I need to do something differently?

The docs are misleading.

The changes were made in

Additional note:
It should be explicitly said that id("kotlin-dsl") / id("groovy-gradle-plugin") should be put into buildSrc/build.gradle

Another related docs page:
https://docs.gradle.org/current/userguide/implementing_gradle_plugins_precompiled.html#sec:the_plugin_id

intellij-monorepo-bot pushed a commit to JetBrains/intellij-community that referenced this issue Apr 19, 2024
…igation

Initially, I added creation of some data:
- an additional build script, required for successful project building
- usage of a task from the plugin, to make sure that the plugin was implemented correctly.
Such data is useful for manual testing, but could be not used in these tests because they do not trigger Gradle sync. Navigation logic could work without this data, so I removed it to make tests shorter and increase readability.

Here are some hints for implementing Precompiled script plugins to avoid errors while building a Gradle project:
1) The plugin file should have `.gradle` or `.gradle.kts` extension
2) The file path should contain `/src/main/kotlin` or `/src/main/groovy` (depending on the language of a plugin)
3) If plugin ID is like `com.example.foo-plugin`, and the found file is `foo-plugin.gradle.kts`, then the file path should contain directories for packages: `/src/main/kotlin/com/example/foo-plugin.gradle.kts`

Also, there are more concise conditions below, but they require more tricky implementation, hence were not considered in the navigation logic:
4) Above mentioned `/src/**` folder should be placed either in
	- buildSrc directory
	- a module taht is a part of a composite build: such module should be included in root `settings.gradle` file with `importBuild` method
5) A build script in a module containing `/src` with Precompiled plugins should have included plugins applying support for the corresponding language
	- `groovy-gradle-plugin`  (for plugins on Groovy)
	- 'org.gradle.kotlin.kotlin-dsl' (for plugins on Kotlin)

	P. S. Regarding the 3rd condition. According to the documentation, package declaration is available for both Groovy and Kotlin plugins. I did not succeed in implementing such a plugin on Groovy, so created an issue in Gradle tracker:
	gradle/gradle#28851

GitOrigin-RevId: 994b3d7e18821534e894e6bc61b56b63da2f9ef7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants