JetBrains/gradle-idea-ext-plugin

afterSync task does not get triggered

hakanai opened this issue · 28 comments

I'm running IDEA 2018.2.4. In our build, we have:

gradle.taskGraph.whenReady {
  idea.project.settings.taskTriggers {
    afterSync project(':some-subproject').tasks.getByName('generateGrammarSource')
  }
}

But after syncing the Gradle project, the task does not get run.

The example on the Wiki doesn't include the gradle.taskGraph.whenReady fragment, but it turns out that if you don't do that, the task doesn't exist yet, so you can't reference it. So we wait until all tasks are loaded before setting the property, but setting the property itself doesn't appear to work.

I had similar issues and I could never properly verify it as the task triggers weren't shown in the Gradle tab. See #37 for details.

I'm able to verify that the task hasn't run, because our task generates source code from ANTLR grammars, and compiling the code fails if it hasn't been generated.

@trejkaz task graph is never created during import, so your configuration is never actually applied.
How (and when) do you create the task?
Did you try to change taskGraph.whenReady to project.afterEvaluate ?

The task is created some time later when the subproject which uses ANTLR applies the plugin. The IDEA stuff is loaded from the main project, so it's running sooner.

I tried afterEvaluate, and the block of code I pass it seems to get run, but then the task doesn't exist.

please, try this:

project(':some-subproject').afterEvaluate {
  rootProject.idea.project.settings.taskTriggers {
    afterSync project(':some-subproject').tasks.getByName('generateGrammarSource')
  }
}

If I do this:

project(':storage-core').afterEvaluate {
  rootProject.idea.project.settings.taskTriggers {
    def task = project(':storage-core').tasks.getByName('generateGrammarSource')
    System.err.println("About to add to afterSync: ${task}")
    afterSync task
  }
}

I see the output saying that the task is not null, but IDEA still doesn't execute the task.

I do not think that graphically you can add a task as a task activation, that has no group and/or has dependencies, this is, only tasks listen in the gradle view can be set as Tasks Activation. Am I right?
If this is the case, maybe that is why IntelliJ simply does not execute it. Perhaps the plugin should throw an exception or something.

Another topic related. I do not think the plugin is taking care of not inserting it twice in the workspace.xml. After each refresh it was being inserted again. Suddenly I got the same task many times in Tasks Activations window.

Wouldn't nearly every task have dependencies? That seems like an odd condition to be unable to trigger a task...

Well, turns out I can manually add the task as an After Sync task, through the UI. So it really just seems that afterSync in the API has no effect.

Mh seems that this feature broke with one of the newer 2018.2.X releases. I remember that it worked with at least 2018.2 and 2018.2.1 but with the latest 2018.2.5 it's also not working for me anylonger.

If it used to work in an earlier IDEA and broke later, should I be filing it against IDEA itself, instead of here, perhaps?

@trejkaz is this still an issues in 2018.3 ?
There is a cosmetics issue with Build Tool Output window not displayed and task not marked in the UI, but the task itself get executed. Can you confirm?

@nskvortsov it works for us on 2018.3 but its still not shown in the UI that the task has a trigger active.

thanks @guylabs , will fix task activation hint soon.

Tried this now on 2018.3:

  • Fresh clone of the repo

  • Open in IDEA

  • Import Gradle Project

  • Output looks like this:

      > Task :buildSrc:compileJava
      > Task :buildSrc:compileGroovy NO-SOURCE
      > Task :buildSrc:processResources NO-SOURCE
      > Task :buildSrc:classes
      > Task :buildSrc:jar
      > Task :buildSrc:assemble
      > Task :buildSrc:compileTestJava NO-SOURCE
      > Task :buildSrc:compileTestGroovy NO-SOURCE
      > Task :buildSrc:processTestResources NO-SOURCE
      > Task :buildSrc:testClasses UP-TO-DATE
      > Task :buildSrc:test NO-SOURCE
      > Task :buildSrc:check UP-TO-DATE
      > Task :buildSrc:build
    
      > Configure project :
      [buildinfo] Not using buildInfo properties file for this build.
      The IvyArtifactRepository.layout(String, Closure) method has been deprecated. This is scheduled to be removed in Gradle 6.0. Please use the IvyArtifactRepository.patternLayout(Action) method instead.
    
      CONFIGURE SUCCESSFUL in 35s
    
      CONFIGURE SUCCESSFUL in 0s
    
  • I don't see anything about it running the task in the output. The source files I expected the task to create haven't been created either.

  • If I manually refresh from Gradle again, I get the same result.

I added diagnostics:

project(':storage-core') {
  afterEvaluate {
    rootProject.idea.project.settings.taskTriggers {
      println("adding: ${tasks.getByName('generateGrammarSource')}")
      afterSync tasks.getByName('generateGrammarSource')
    }
  }
}

The println there does appear in the output, so I know it's adding it - it just isn't getting run.

Simpler check:

task('testIdea') {
  doLast {
    println('I\'m being run!')
  }
}

idea.project.settings.taskTriggers {
  println("Being added: ${tasks.getByName('testIdea')}")
  afterSync tasks.getByName('testIdea')
}

I get the "Being added" line output, but not the "I'm being run!" line.

@trejkaz thank you for the sample project!
Indeed, there is an issue with tool window not being open for a trigger-activated task, so calling println inside doFirst do not show up.
As a check, can I suggest using something like

task('testIdea') {
  doFirst {
    new File(project.rootDirectory,'test.txt').write(System.currentTimeMillis())
  }
}

I'm already writing a file into the build directory and that one isn't being created, which seems like it already shows that the task is not actually being executed.

(Plus, of course, in our real project, the task is supposed to generate source files, so it's very obvious when it hasn't run - you can't compile.)

@trejkaz I've created a pull request for your project hakanai/idea-ext-issue-43#1

Unfortunately, task failure is hidden and this should get fixed on IDEA side.

IDEA still doesn't run the task in the real project, which is why I created the sample project in the first place. The real task clearly does work, because when I run it, it creates the source files required to make the project compile, but when I just import, the files don't get created, so I can't compile.

(I'll have to wait until I'm back at work to figure out how that proposed fix changes anything at all. It just seems to be outputting the created file to a different [wrong] location.)

With the change in that PR, it seems to behave the same. In "Build: Sync" window:

> Configure project :
Being added: task ':testIdea'

CONFIGURE SUCCESSFUL in 0s

But the file does not appear.

The task works fine from the command-line:

$ cat testIdea.log
cat: testIdea.log: No such file or directory
$ ./gradlew testIdea

> Configure project :
Being added: task ':testIdea'

> Task :testIdea
I'm being run!

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
$ cat testIdea.log
Task was run

I went to the Gradle tool window to see whether the task would run from there, but it seems to be missing from there entirely, so I don't know what's going on there.

In the full project, the task does appear in the Gradle tool window, and the task does run when I run it manually, but does not get run when the project is imported. So neither work, but now there are two different bugs going on? :/

This works for me using IDEA 2019.3.4 and org.jetbrains.gradle.plugin.idea-ext:org.jetbrains.gradle.plugin.idea-ext.gradle.plugin:0.7 so I guess the issue can be closed?

@trejkaz getting back to this: is does afterSycn work for you ATM ?

Currently using: IDEA 2020.2.2

Steps:

Results: task is not run, build dir is not created and no errors come out either. When I refresh, I do see:

"Being added: task ':testIdea'"

So it definitely knows about the task, but I see no evidence of it being run.

Oh right, that was still with v0.2.

Updated build.gradle to get gradle-idea-ext:0.7 instead, and now it does work. So I guess we're good to close this.

Thank you for the followup!

Hello, I tried this out with out with Android Studio 4.2.1 and classpath "gradle.plugin.org.jetbrains.gradle.plugin.idea-ext:gradle-idea-ext:1.0 and I'm getting the buggy behavior that @hakanai was getting.

However, setting the plugin version back to 0.7 works as expected. Any reason why?