Refactoring code out of buildSrc fails until the gradle cache is cleared

Hi! We’re in the process of moving groovy code out of buildSrc and into gradle scripts. We are finding that after doing that running gradle will fail for ANY task until the cache is cleared.

I have a trivial example showing the issue - but I cannot upload it, so here is the gist of the problem:

Let’s say I have a trivial groovy file ‘buildSrc/src/main/groovy/GreetingTask.groovy’ with contents:

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.OutputFiles
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.Input

class GreetingTask extends DefaultTask {
  @TaskAction
  def greet() { println 'hello from GreetingTask' }
}

and a trivial ‘build.gradle’ file with contents

task hello(type: GreetingTask)

Running ‘gradlew -q hello’ gives me the expected output ‘hello from GreetingTask

Now - if I refactor the groovy code into ‘scripts/GreetingTask.gradle’ with contents:

class GreetingTask extends DefaultTask {
  @TaskAction
  def greet() { println 'hello from GreetingTask' }
}

project.ext.GreetingTask = GreetingTask

and I updated my main ‘build.gradle’ file as follows:

ext.gradleScripts = file("${project.rootProject.projectDir}/scripts")
apply from: "${gradleScripts}/GreetingTask.gradle"

task hello(type: GreetingTask)

and I run ‘gradlew -q hello’ then I still get the expected output ‘hello from GreetingTask’ until I realize that I don’t need the buildSrc directory anymore and I delete it - then I get:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/me/Documents/source/buildsrc_issue/build.gradle' line: 4

* What went wrong:
A problem occurred evaluating root project 'buildsrc_issue'.
> GreetingTask

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

until I clear ‘~/.gradle/.caches’ and then it works correctly.

If I have refactored code around and removed old code - I feel like I should not have to clear the cache to pick up changes (because clearing the cache is not something we want users to get into a habit of doing and it takes a while to build after clearing the cache).

The problem here is you need to recompile the build script, however, you aren’t actually changing it (just deleting some of the classpath out from underneath it) so the build fails. You can force recompilation via the --recompile-scripts command line option.