Gradle 2.4-20150302230031: Unable to import Gradle sources project using tooling api

Gradle project ( can not be imported using tooling api if org.gradle.tooling.BuildAction is used. It failed with the following error:

Caused by: java.lang.IllegalStateException: Cannot add Defaults rule 'org.gradle.api.plugins.HelpTasksPlugin$Rules$Rule#add(org.gradle.api.tasks.diagnostics.DependencyInsightReportTask, org.gradle.internal.service.ServiceRegistry)' for model element 'tasks.dependencyInsight' when element is in state GraphClosed.
at org.gradle.model.internal.registry.DefaultModelRegistry$1.execute(

It seems, the problem affects only Gradle 2.4-20150302230031+0000 which is currently used in the gradle master branch.
There is no such an issue if you use org.gradle.tooling.ProjectConnection#getModel(java.lang.Class) method or Gradle v 2.4-20150409092851+0000.

Consider the following code to reproduce:

public class App {
public static void main(String[] args) {
    ProjectConnection connection = GradleConnector.newConnector()
            .forProjectDirectory(new File(args[0]))

    try {
        BasicIdeaProject project = connection.action(new BuildAction<BasicIdeaProject>() {
            public BasicIdeaProject execute(BuildController controller) {
                return controller.getModel(BasicIdeaProject.class);

    } finally {

Are you saying that it works for a newer nightly but is broken for an older one?

We were stuck on an older nightly due to an issue with TeamCity. We should be bumping it up pretty soon.

@etiennestuder does this look familiar/something that’s already fixed?

I think I have seem something like this a few weeks ago. I cannot remember which nightly build it was, though. I’ve not had a problem recently.

I’ve just bumped the version used on both branches.