autonomousapps / autonomousapps/dependency-analysis-gradle-plugin

Plugin fails because of use of `afterEvaluate`

Open
#630 10 comments 3 reactions 0 assignees View on GitHub
enhancement more information needed
Dominant language
Kotlin
Stars
2.2k
Forks
158
Avg merge
16h 25m
Merged PRs (30d)
46

Description

**Build scan link**

https://ge.micronaut.io/s/slhg65invj4t2

**Plugin version**

1.0.0-rc-06

**Gradle version**

7.4.1

**Describe the bug**

Applying the plugin to the root project fails with:

```
Caused by: org.gradle.api.InvalidUserCodeException: Cannot run Project.afterEvaluate(Action) when the project is already evaluated.
at org.gradle.api.internal.project.DefaultProject.failAfterProjectIsEvaluated(DefaultProject.java:1067)
at org.gradle.api.internal.project.DefaultProject.afterEvaluate(DefaultProject.java:1048)
at com.autonomousapps.subplugin.ProjectPlugin.configureJavaLibProject(ProjectPlugin.kt:328)
at com.autonomousapps.subplugin.ProjectPlugin.access$configureJavaLibProject(ProjectPlugin.kt:46)
at com.autonomousapps.subplugin.ProjectPlugin$apply$$inlined$run$lambda$4.execute(ProjectPlugin.kt:108)
at com.autonomousapps.subplugin.ProjectPlugin$apply$$inlined$run$lambda$4.execute(ProjectPlugin.kt:46)
at org.gradle.api.internal.plugins.DefaultPluginManager$2.execute(DefaultPluginManager.java:258)
at org.gradle.api.internal.plugins.DefaultPluginManager$2.execute(DefaultPluginManager.java:255)
```

Steps to reproduce the behavior:
1. `git clone https://github.com/micronaut-projects/micronaut-core.git`
2. `cd micronaut-core`
3. edit the root `build.gradle` to add the plugin:
```gradle
plugins {
id "io.micronaut.build.internal.docs"
id "io.micronaut.build.internal.dependency-updates"
id "io.micronaut.build.internal.version-catalog-updates"
id "io.micronaut.build.internal.convention-quality"
id 'com.autonomousapps.dependency-analysis' version '1.0.0-rc06'
}
```
4. run `./gradlew buildHealth`

**Expected behavior**

Build shouldn't fail.

**Additional context**

I'm investigating the use of this plugin (which I wish was a core feature of Gradle, I wanted this for a looooooong time) for integration in our [Micronaut Build Plugin](https://github.com/micronaut-projects/micronaut-build/). This project provides convention plugins which are applied on more than 40 distinct Micronaut projects (including Micronaut Core which I used in this bug report). Our convention plugins require the use of `evaluationDependsOn`, which triggers evaluation of subprojects before the root project is evaluated.

I am aware that there's a `dependency.analysis.autoapply=false` flag that I can use which would _not_ apply the plugin to subprojects automatically. However, there are a number of issues:

- I'm missing documentation about how to use the plugin in this case. What plugin should be applied to the subprojects which we want to analyze (for us it would be applied to our custom "Micronaut module" convention plugin, which is derived from `java-library`)
- this requires setting this flag in individual project's `gradle.properties` file, which, in a multi-repo context like ours, is a bit cumbersome

I looked into the sources and I've found that internally it does `ProjectPlugin(this).apply()`, but the `ProjectPlugin` type is internal. Would it make sense to expose a plugin which can be _indidivually_ applied to a single project instead? That's basically what `ProjectPlugin` does, it seems. In general I'm not super fond of plugins which automagically apply plugins on subprojects. Like here, it can easily break if the expectations aren't met. In a typical Micronaut project we have a root plugin which performs some aggregation (javadocs, code quality, ...), different module types plugins (a Micronaut Library, a Micronaut Component) and a BOM type which requires `evaluationDependsOn` so that we can automatically determine dependencies based on the application of a plugin or not.

I'm happy to discuss options and/or provide PRs, I just need a bit more guidance on how I can _individually_ run the plugin on a project to make progress: unfortunately I couldn't find answers in the wiki.

Thanks!

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure in micronaut-core by applying the plugin in the root build.gradle and running ./gradlew buildHealth. Start at ProjectPlugin.kt:328, where configureJavaLibProject calls afterEvaluate, and trace how evaluationDependsOn changes project timing. Done means the reported build no longer fails and the individual-project usage or required configuration is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
build-system
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.