gradle / playframework

Gradle Play Support
https://gradle.github.io/playframework/
Apache License 2.0
47 stars 41 forks source link

It seems that the task compileScala is recompiling ALL scala files everytime #129

Closed stephane303 closed 4 years ago

stephane303 commented 4 years ago

Whenever I change any file from the the view folder (containing scala), it takes a very long time for this task to complete, as if everything in the view folder was recompiled.

I haven't found an option to log what is recompiled, so I am not 100% sure.

Any help would be really welcome as it is very painful to work with the Play Framework at the moment.

will-sargent-eero commented 4 years ago

Take a look at https://github.com/lightbend/scala-sculpt and https://www.iteratorshq.com/blog/scala-compiler-phases-with-pictures/

will-sargent-eero commented 4 years ago

Also https://www.jetbrains.com/help/idea/dsm-analysis.html -- you want to see what dependencies you have between classes.

stephane303 commented 4 years ago

Thank you for helping ... After further analysis I have oberved that whenever ANY view file is modified, ALL the generated scala files (build\src\play\twirl\views\html*) are rewritten ... not only the one I modified, this is probably the cause of my problem.

I am using the very simple starter kit https://github.com/playframework/play-samples/tree/2.6.x/play-java-starter-example

For example if I change a single file app\views\index.scala.html

gradlew -i build --build-cache
Task :compilePlayTwirlTemplates
Caching disabled for task ':compilePlayTwirlTemplates' because:
  Caching has not been enabled for the task
Task ':compilePlayTwirlTemplates' is not up-to-date because:
  Input property 'source' file C:\Users\stef\play2.6\app\views\index.scala.html has changed.
:compilePlayTwirlTemplates (Thread[Execution worker for ':' Thread 7,5,main]) completed. Took 4.38 secs.
:compileScala (Thread[Execution worker for ':' Thread 7,5,main]) started.

and then check the generated scala files:

src\play\twirl\views\html>ls -lta
total 36
drwxr-xr-x 1 stef 197609     0 Jan 25 14:06 .
-rw-r--r-- 1 stef 197609 11897 Jan 25 14:06 tutorial.template.scala
-rw-r--r-- 1 stef 197609  3486 Jan 25 14:06 main.template.scala
-rw-r--r-- 1 stef 197609  6256 Jan 25 14:06 index.template.scala
-rw-r--r-- 1 stef 197609  7545 Jan 25 14:06 explore.template.scala
-rw-r--r-- 1 stef 197609  2806 Jan 25 14:06 commonSidebar.template.scala
drwxr-xr-x 1 stef 197609     0 Jan 25 12:49 ..

They ALL have been modified (checking the timestamp). So it seems that the task compilePlayTwirlTemplates has a problem. Could it be configured ?

This is my build.gradle file:

plugins {
    id 'org.gradle.playframework' version '0.9'
    id 'idea'
}

repositories {
    jcenter()
    maven {
        name "lightbend-maven-release"
        url "https://repo.lightbend.com/lightbend/maven-releases"
    }
    ivy {
        name "lightbend-ivy-release"
        url "https://repo.lightbend.com/lightbend/ivy-releases"
        layout "ivy"
    }
}

play {
    injectedRoutesGenerator = true
}

dependencies {
    implementation "commons-lang:commons-lang:2.6"
    implementation "com.typesafe.play:play-guice_2.12:2.6.15"
    implementation "ch.qos.logback:logback-classic:1.2.3"
    testImplementation "org.scalatestplus.play:scalatestplus-play_2.12:3.1.2"
    implementation "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"
    implementation "org.glassfish.jaxb:jaxb-runtime:2.3.2"
}
wsargent commented 4 years ago

Also see https://www.scala-lang.org/blog/2018/06/04/scalac-profiling.html

stephane303 commented 4 years ago

I guess my last comment was not clear enough. To summarize I suspect the task compilePlayTwirlTemplates to be the cause of the problem, please reread my comment, thank you.

spingel commented 4 years ago

The problem is that the generated files have a comment that contains absolute paths and the current timestamp. The timestamp changes every time the file re-generated and the absolute path breaks distributed caching.

I added the following snippet to my build.gradle to remove the lines from the generated files that contain absolute paths and date stamps:

compilePlayTwirlTemplates {
    doLast {
        fileTree("${buildDir}/src/play/twirl").each { file ->
            file.text = file.text.replaceAll("(?m)^                  (SOURCE|DATE): .*", "")
        }
    }
}

With that work around in place caching and rebuilding works as expected.

This is similar to https://github.com/gradle/playframework/issues/109 .

stephane303 commented 4 years ago

@spingel Fantastic ! My compile time went from 4 minutes to a few seconds !

mkurz commented 3 years ago

This should get fixed with Play 2.8.8: https://github.com/playframework/playframework/pull/10707 https://github.com/playframework/twirl/pull/378

Jonatha1983 commented 2 years ago

Just updating that it is not resolve in 2.8.8

Jonatha1983 commented 2 years ago

Hi

Extra update about this - so @spingel is right that the twirl and the route task are generating files with dates and paths but even if I am doing the suggested trick and I am only modifying or adding a new unused Java file the scala compiler compile all Java and Scala files:

 Task :compileScala
Build cache key for task ':compileScala' is 54b3d7b547777fa256441d360afdf9db
Task ':compileScala' is not up-to-date because:
  Input property 'source' file /Users/jonathangafner/IdeaProjects/plyplugintest/app/controllers/hello/A.java has been added.
Starting process 'Gradle Worker Daemon 11'. Working directory: /Users/jonathangafner/.gradle/workers Command: /Users/jonathangafner/Library/Java/JavaVirtualMachines/corretto-1.8.0_342/Contents/Home/bin/java -Djava.security.manager=worker.org.gradle.process.internal.worker.child.BootstrapSecurityManager -Xmx5120m -Dfile.encoding=UTF-8 -Duser.country=IL -Duser.language=en -Duser.variant -cp /Users/jonathangafner/.gradle/caches/7.5/workerMain/gradle-worker.jar worker.org.gradle.process.internal.worker.GradleWorkerMain 'Gradle Worker Daemon 11'
Successfully started process 'Gradle Worker Daemon 11'
Started Gradle worker daemon (0.693 secs) with fork options DaemonForkOptions{executable=/Users/jonathangafner/Library/Java/JavaVirtualMachines/corretto-1.8.0_342/Contents/Home/bin/java, minHeapSize=null, maxHeapSize=5120m, jvmArgs=[], keepAliveMode=SESSION}.
Compiling with Zinc Scala compiler.
Prepared Zinc Scala inputs: 0.501 secs
compiling 17 Scala sources and 4 Java source....
Jonatha1983 commented 2 years ago

Seems related to a regression in gradle 7.5: https://github.com/gradle/gradle/issues/20854