Repository navigation
Contribute the Grails BOM platform lazily so defaultDependencies keep firing - #16416
Conversation
… firing The Grails Gradle plugin injected the selected Grails BOM eagerly into every declarable configuration from afterEvaluate. A platform counts as a declared dependency, so Gradle's defaultDependencies hooks - which only fire on an otherwise empty configuration - never ran for configurations other plugins populate on demand. The jacoco plugin's jacocoAgent/jacocoAnt resolved to the BOM alone and 'test' failed with "Expected configuration ':jacocoAgent' to contain exactly one file"; the plugin's own 'profile' configuration lost its default profile the same way. The platform is now contributed through Configuration.withDependencies, which Gradle runs after defaultDependencies when a configuration's dependencies are first observed. The two eager readers of declared dependencies are adapted: the auto-applied BOM is registered explicitly with bom-property-overrides (its auto-detection scans declared platforms in afterEvaluate), and the Micronaut enforcedPlatform validation accepts the Micronaut variant selected for automatic application. Fixes #16335
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 8.0.x #16416 +/- ##
==================================================
- Coverage 57.8656% 57.8590% -0.0066%
- Complexity 22870 22871 +1
==================================================
Files 2137 2137
Lines 104677 104689 +12
Branches 18811 18815 +4
==================================================
Hits 60572 60572
- Misses 35742 35755 +13
+ Partials 8363 8362 -1
🚀 New features to boost your workflow:
|
This comment has been minimized.
This comment has been minimized.
matrei
left a comment
There was a problem hiding this comment.
Thanks @jdaugherty. I reviewed head 5cefc3f2c5: the fix in 4839d8130d plus the merge of 8.0.x, which moves both builds to Gradle 9.8.0. The merge doesn't change any code this PR touches, so the review applies to 9.8.0 as it stands. Contributing the platform through withDependencies is the right fix: it covers jacocoAgent/jacocoAnt, our own profile configuration and any third-party configuration populated through defaultDependencies, not only jacoco. I have one small code change to ask for, and the rest is questions and nits.
Run locally:
| What | Result |
|---|---|
grails-gradle on 9.8.0: DO_NOT_CACHE_TESTS=1 ./gradlew :grails-gradle-plugins:test --tests '*Bom*' --tests '*CliAutoDiscoverySpec' --rerun |
8 specs, 29 tests, 0 failures (BomPlatformFunctionalSpec 5/5, BomCliMultiprojectRaceFunctionalSpec 1/1, CliAutoDiscoverySpec 8/8 including the new --warning-mode=fail feature) |
The BOM fixtures again with --warning-mode=all |
no deprecation from the plugin. The only warnings are older project.plugins.findPlugin(...) calls inside doLast in the fixtures, which this PR doesn't touch |
Scratch build on Gradle 9.7.1 and 9.8.0, platform added via configureEach + withDependencies from afterEvaluate (java-library + maven-publish) |
same result on both versions, details below |
The new --warning-mode=fail feature only configures tasks and doesn't resolve anything, so it never runs the withDependencies action. That's why I also ran the BOM fixtures, which do resolve, with --warning-mode=all.
What the scratch build showed:
- Publication is unaffected.
generatePomFileForMavenPublicationstill writes the BOM as a<dependencyManagement>import, andmodule.jsonstill lists it withorg.gradle.category: platformfor bothapiElementsandruntimeElements. That confirms the claim in the description. - Observing a configuration before the
afterEvaluatedoes not lose the platform. When the build script readscompileClasspath.incoming.dependenciesat configuration time, thewithDependenciesaction registered later still runs when the configuration is resolved. - A late-created configuration can break the build. See point 1.
1. canBeDeclared is checked before the configuration's role is final
GrailsGradlePlugin.groovy:600: the configureEach action checks configuration.canBeDeclared when the configuration is added to the container. For configurations.create(name) { ... }, Gradle adds the configuration before it runs the configure closure. So if a plugin or build creates a configuration after the Grails afterEvaluate has run and turns off canBeDeclared in the closure, the action is attached while the configuration is still declarable. Resolving the configuration then fails:
afterEvaluate { // registered after the Grails plugin's afterEvaluate
configurations.create('late') { canBeDeclared = false; canBeConsumed = false; canBeResolved = true }
}
// resolving 'late':
// > Dependencies can not be declared against the `late` configuration.Before this PR, configurations created after the afterEvaluate were never touched, so this is new. Setting the role in the create closure is still supported: 9.8.0 prints no deprecation warning for it. configurations.register(...) and resolvable(...) / consumable(...) / dependencyScope(...) are fine, because their role is set before configureEach runs (checked with register('reg') { canBeDeclared = false }). The case is narrow, but the failure is a hard error in someone else's configuration, and the fix is one line: check again inside the action, where the role is final:
configuration.withDependencies { DependencySet dependencies ->
if (!configuration.canBeDeclared || declaresGrailsBom(dependencies)) {
return
}
...
}With that check added, the scratch build resolves late without error on 9.8.0. A variant of the bom-platform-default-dependencies fixture that creates such a configuration in a later afterEvaluate would cover it.
2. Should jacocoAgent / jacocoAnt receive the Grails BOM at all?
With this change, the jacoco configurations get their defaults back and also receive the Grails BOM (the new test asserts JACOCO_AGENT_RESOLVED=[…lazy-bom…, org.jacoco.agent…]). isExcludedFromBomPlatform already excludes checkstyle, codenarc, pmd, spotbugs and spotbugsPlugins. Commit 5e89656e46 gives the reason: tool classpaths should not receive platform() constraints, because these take part in conflict resolution and can upgrade the tools' transitive dependencies. jacocoAnt is the same kind of classpath. org.jacoco.core depends on org.ow2.asm:asm*, and grails-bom manages ASM. Today that is probably harmless, but it is the case the exclusion list exists for. Should jacocoAgent and jacocoAnt be added to the list? The lazy contribution is still needed for profile and for third-party configurations. For jacoco, the test would then check that the defaults resolve without the BOM.
3. Upgrade notes: the BOM is no longer visible as a declared dependency
The fixture changes show a behaviour change that user builds can run into as well. configurations.implementation.dependencies / allDependencies no longer contain the Grails BOM until something resolves or observes the configuration. The three existing fixtures had to switch to incoming.dependencies because of this. The same applies to any third-party plugin that scans declared platforms in afterEvaluate, which is exactly what bom-property-overrides did and why it now needs the explicit overrides.bom(...) registration. I'd add a sentence to the upgrading80x.adoc paragraph, for example: The platform is therefore not visible in a configuration's declared dependencies / allDependencies; build logic that looks for it should inspect incoming.dependencies or the resolution result instead.
Nits
bom-platform-default-dependencies/build.gradle:64:JACOCO_AGENT_HAS_BOMis printed but never asserted. It is already implied byJACOCO_AGENT_RESOLVED, so either assert it or drop the line.- The
autoDetect = falsebranch of the newoverrides.bom(...)registration (GrailsGradlePlugin.groovy:621) has no test.BomCliMultiprojectRaceFunctionalSpeccovers theautoDetect = truepath with an auto-applied BOM. The case where a property override is not applied becauseautoDetect = falseis not covered. gradleDependencies.adoc: "when a configuration's dependencies are first resolved" is slightly narrower than the code comment's "observed" (publication,incoming.dependencies). For users, "resolved" is probably the more useful word, so I'm fine either way.
Verified as correct
effectiveGrailsBom()is a straight extraction.validateMicronautBomruns afterapplyGrailsBom'safterEvaluate, so the "more than one Grails BOM" failure is still reported from the same place.- The new Micronaut acceptance clause only applies when
implementationdeclares no Grails BOM by hand. So a hand-declared non-enforcedplatform(grails-micronaut-bom)still fails, and so doesgrails.bom = nullwithout a hand-declared BOM (covered by the new negative test). ENFORCED_PLATFORM_BOMShas the same three names as the removed localvalidMicronautBomsset.category.nameis equivalent to the oldtoString()comparison.- The
overrides.bom(...)call runs in anafterEvaluateregistered beforeBomPropertyOverridesPluginis applied, so it lands before the plugin readsextension.boms. When a BOM is also declared by hand at a different version, both coordinates are processed. That matches the old behaviour, where the injected copy on sibling configurations was auto-detected too. - The strict override constraints from
BomManagedVersions.applyToare dependency constraints, not dependencies, so they do not suppressdefaultDependencieseither.
Recheck late-created configuration roles before adding dependencies and exclude JaCoCo tool configurations from the application BOM. Cover automatic and explicit BOM property-override registration, and document lazy dependency observation.
|
@matrei, addressed the requested change and the remaining feedback in 0a40d94.
Verification on Gradle 9.8.0 / JDK 21:
There are no inline review threads on this PR to resolve, so this response covers the feedback in the review body. |
matrei
left a comment
There was a problem hiding this comment.
Thanks @jdaugherty. Second round, at head 0a40d94fa1. All points from my previous review are addressed.
Run locally on Gradle 9.8.0:
| What | Result |
|---|---|
DO_NOT_CACHE_TESTS=1 ./gradlew :grails-gradle-plugins:test --tests '*Bom*' --tests '*CliAutoDiscoverySpec' --rerun |
8 specs, 33 tests, 0 failures (BomPlatformFunctionalSpec 9/9) |
The same BomPlatformFunctionalSpec run with the new canBeDeclared check removed from the withDependencies action |
only "late-created configurations receive the platform only if their final role allows dependencies" fails, with Dependencies can not be declared against the `lateResolvable` configuration |
- Late configuration roles: the check inside
withDependenciesis the fix I had in mind. The new feature fails without it and passes with it, as shown above. ThelateDeclarablecompanion also covers the other side: a configuration created late with a declarable role still gets both its defaults and the platform. - JaCoCo:
jacocoAgent/jacocoAntare now inisExcludedFromBomPlatform. The fixture asserts that the defaults resolve without the BOM and that both*_HAS_BOMvalues arefalse.profileand the customdefaultDependenciesconfiguration still verify the lazy contribution. - Upgrade notes: the new sentence about
dependencies/allDependenciescovers the behaviour change. Both guide sections now say "observed" and mention the tool-classpath exclusions. - Nits:
JACOCO_AGENT_HAS_BOMis asserted now. TheautoDetectmatrix covers all three cases, and themanaged-lib.versionproperty in the fixture BOM makes the override observable. The newgradleDependencies.adocparagraph describes theautoDetect = falsebehaviour correctly.
Two small things, neither blocking:
GrailsGradlePlugin.groovy:597: after the edit, the comment block breaks mid-sentence ("Registering through configureEach also covers" on its own short line). Re-wrapping the last two lines would tidy it up.- FYI, and not caused by this PR: the property-override constraints still ignore the exclusion list.
BomPropertyOverridesPlugin.groovy:165applies them to every declarable configuration,jacocoAnt,checkstyleand the other tool classpaths included. Soext['asm.version'] = '…'would also force ASMstrictlyon those classpaths. The new doc sentence says the tool classpaths are excluded "so the application BOM does not change their transitive dependency versions", which is true for the platform but not for an explicit property override. That is arguably what a user who sets the property wants, andBomPropertyOverridesPluginis BOM-agnostic and has no knowledge of the Grails exclusions. So I'd leave it for a follow-up, if anyone thinks it is worth changing at all.
Approving. CI was still running when I posted: 24 checks had passed, and the only failure was the cancelled "Release - Drafter" run, which is unrelated.
Problem
GrailsGradlePlugin.applyGrailsBom()added the selected Grails BOM as aplatform()to every declarable configuration eagerly, fromafterEvaluate. A platform is a declared dependency, and Gradle'sdefaultDependencies { }hooks only fire on a configuration that holds no dependencies at all (gradle/gradle#11209). Any configuration another plugin populates on demand therefore ended up holding only the BOM:jacocoplugin'sjacocoAgent/jacocoAntnever receivedorg.jacoco:org.jacoco.agent/org.jacoco:org.jacoco.ant, so./gradlew testfailed withExpected configuration ':jacocoAgent' to contain exactly one file, however, it contains no files.profileconfiguration lost its defaultorg.apache.grails.profiles:<profile>dependency the same way (visible in the new fixture when run against the unfixed plugin:PROFILE_DEFAULT_DECLARED=false).Fix
The platform is now contributed through
Configuration.withDependenciesinstead ofDependencyHandler.add. Gradle runs a configuration's dependency actions once, when its dependencies are first observed (resolution, publication,incoming.dependencies):defaultDependenciesfirst, and only while the configuration is empty, thenwithDependencies. The defaults other plugins install therefore keep firing, and the BOM still lands on every declarable configuration - including ones created afterafterEvaluate, since the registration goes throughconfigurations.configureEach. Published POMs/module metadata are unaffected: Gradle's publishing component readsincoming.dependencies, which runs the actions.Two places read declared dependencies eagerly in
afterEvaluateand needed adapting:bom-property-overridesauto-detects BOMs by scanning declared platforms.applyGrailsBomnow registers the auto-applied BOM with thebomPropertyOverridesextension explicitly (respectingautoDetect = false), so property overrides keep working for the injected BOM.validateMicronautBomaccepted only a hand-declaredenforcedPlatformonimplementation. It now also accepts the case whereimplementationdeclares no Grails BOM by hand and the BOM selected for automatic application is a Micronaut variant (always contributed asenforcedPlatform).The effective-BOM selection (hand-declared BOM wins over
grails.bom, fail fast on more than one) moved into a sharedeffectiveGrailsBom()helper used by both callers.Tests
grails-gradle/pluginsTestKit coverage:bom-platform-default-dependenciesfixture (POM-only local repository, nothing hits the network): a Grails app applyingjacoco, a custom configuration populated throughdefaultDependencies, and a version-less dependency managed by the BOM. AssertsjacocoAgent,jacocoAnt, the custom configuration and theprofileconfiguration all keep their defaults and receive the platform, and that the lazily contributed platform still manages versions. Fails against the unfixed plugin.bom-platform-micronautfixture: the auto-applied Micronaut variant satisfies theenforcedPlatformvalidation and arrives asenforced-platform; opting out without declaring one by hand still fails the validation.bom-platform-basic/bom-platform-manual/auto-apply-bom-disabledfixtures now observe dependencies through resolvable configurations (incoming.dependencies), and the basic one asserts the platform is not declared before observation.Docs:
gradleDependencies.adocand the Grails 8 upgrade notes describe the lazy contribution and its effect ondefaultDependencies-populated configurations.Verification
cd grails-gradle && ./gradlew :grails-gradle-plugins:codeStyle- clean.cd grails-gradle && DO_NOT_CACHE_TESTS=1 ./gradlew :grails-gradle-plugins:test- 44 suites, 308 tests, 0 failures. The new default-dependencies feature fails against the unfixed plugin (JACOCO_AGENT_RESOLVED=[org.apache.grails:lazy-bom:1.0-lazy],PROFILE_DEFAULT_DECLARED=false)../gradlew ratfrom the root - 0 unknown licenses.jacocoapplied tograils-test-examples/app1withgrails { bom = 'grails-bom' }forced on (the test examples normally opt out).:grails-test-examples-app1:dependencies --configuration jacocoAgentnow listsorg.jacoco:org.jacoco.agent:0.8.14next toorg.apache.grails:grails-bom -> project ':grails-bom', and theprofileconfiguration declaresorg.apache.grails.profiles:webnext to the BOM.Fixes #16335