From b69cba361d7dcd930dac71c87582a5379af4a4cf Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Fri, 4 Dec 2026 11:03:29 -0800 Subject: [PATCH] Use running AbstractCompile destinationDirectory in PruneGeneratedFactories --- .../core/PruneGeneratedFactories.groovy | 10 +++-- .../core/PruneGeneratedFactoriesSpec.groovy | 37 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/PruneGeneratedFactories.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/PruneGeneratedFactories.groovy index 270fb5f1c05..a95f20c8a6c 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/PruneGeneratedFactories.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/PruneGeneratedFactories.groovy @@ -87,9 +87,13 @@ class PruneGeneratedFactories implements Action { @Override void execute(Task task) { - File classesDirectory = destinationDirectory != null - ? destinationDirectory.get().asFile - : ((AbstractCompile) task).destinationDirectory.get().asFile + Provider targetDirectory = task instanceof AbstractCompile + ? ((AbstractCompile) task).destinationDirectory + : destinationDirectory + File classesDirectory = targetDirectory?.getOrNull()?.asFile + if (classesDirectory == null) { + return + } File factoriesFile = new File(classesDirectory, factoriesLocation) if (!factoriesFile.isFile()) { return diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/PruneGeneratedFactoriesSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/PruneGeneratedFactoriesSpec.groovy index 869f135b7d1..689e9dc6675 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/PruneGeneratedFactoriesSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/PruneGeneratedFactoriesSpec.groovy @@ -135,6 +135,43 @@ class PruneGeneratedFactoriesSpec extends Specification { load(generated) == ['grails.core.ArtefactHandler': 'example.KeepHandler'] } + void 'forwarded action prunes the running AbstractCompile task destination rather than the source task'() { + given: 'a source compileGroovy task with its own stale factories file' + String sourceText = 'grails.core.ArtefactHandler=example.SourceGoneHandler\n' + write(generated, sourceText) + + and: 'a second GroovyCompile task with a different destination directory' + File otherClassesDirectory = new File(projectDir, 'build/classes/groovy/other') + GroovyCompile otherCompile = project.tasks.register('compileOtherGroovy', GroovyCompile) { GroovyCompile task -> + task.destinationDirectory.set(otherClassesDirectory) + }.get() + write(new File(otherClassesDirectory, 'example/OtherKeepHandler.class'), '') + File otherGenerated = new File(otherClassesDirectory, PruneGeneratedFactories.GRAILS_FACTORIES_LOCATION) + write(otherGenerated, 'grails.core.ArtefactHandler=example.OtherGoneHandler,example.OtherKeepHandler\n') + + when: 'the action registered on compileGroovy is forwarded to the other GroovyCompile task' + PruneGeneratedFactories.register(project, compileGroovy, PruneGeneratedFactories.GRAILS_FACTORIES_LOCATION, [HAND_AUTHORED]) + compileGroovy.actions.last().execute(otherCompile) + + then: 'the running task destination is pruned and the source task destination is untouched' + load(otherGenerated) == ['grails.core.ArtefactHandler': 'example.OtherKeepHandler'] + generated.exists() + generated.text == sourceText + } + + void 'two-argument constructor ignores a non-AbstractCompile task without throwing'() { + given: + Task otherTask = project.tasks.register('plainTask').get() + Action action = new PruneGeneratedFactories(PruneGeneratedFactories.GRAILS_FACTORIES_LOCATION, + [new File(projectDir, HAND_AUTHORED)]) + + when: + action.execute(otherTask) + + then: + noExceptionThrown() + } + private void prune() { Action action = new PruneGeneratedFactories(PruneGeneratedFactories.GRAILS_FACTORIES_LOCATION, [new File(projectDir, HAND_AUTHORED)])