Has something changed recently with Kotlin 1.8.20 ...
# gradle
c
Has something changed recently with Kotlin 1.8.20 and producing output for Kotlin JS to be consumed by another module? I’ve got
browser-app
which produces the JavaScript browser app, while
server-app
is a JVM Ktor application that serves the JavaScript from browser-app to clients. I’ve created configurations in the browser-app, which are consumed by server-app by following these Gradle instructions. Recently I’ve started seeing two problems: 1. Sometimes the Javascript is not bundled into server-app which makes me think the dependency between browser-app and server-app isn’t quite right 2. I sometimes also see the error
Reason: Task ':server-app:startScripts' uses this output of task ':browser-app:jsBrowserDistribution' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
How can I troubleshoot this further?
browser-app/build.gradle.kts
Copy code
configurations.register("browserAppDevelopment") {
    isCanBeConsumed = true
    isCanBeResolved = false
}.also { configuration ->
    artifacts {
        val task = tasks.getByName("jsBrowserDevelopmentExecutableDistribution")
        task.outputs.files.forEach {
            add(configuration.name, it) {
                builtBy(task)
            }
        }
    }
}

configurations.register("browserAppProduction") {
    isCanBeConsumed = true
    isCanBeResolved = false
}.also { configuration ->
    artifacts {
        val resourcesTask = tasks.getByName("jsBrowserProductionExecutableDistributeResources")
        resourcesTask.outputs.files.forEach { file ->
            add(configuration.name, file) {
                builtBy(resourcesTask)
            }
        }

        val webpackTask = tasks.getByName("jsBrowserProductionWebpack")
        webpackTask.outputs.files.filter { it.name == javascriptAppFilename }.forEach { file ->
            add(configuration.name, file) {
                builtBy(webpackTask)
            }
        }
    }
}
server-app/build.gradle.kts
Copy code
kotlin {
    jvm {
        withJava()
        jvm()
    }
    sourceSets {
        getByName("jvmMain") {
            dependencies {
                if (isJavascriptMinificationEnabled) {
                    implementation(
                        project(
                            mapOf(
                                "path" to ":browser-app",
                                "configuration" to "browserAppProduction"
                            )
                        )
                    )
                } else {
                    implementation(
                        project(
                            mapOf(
                                "path" to ":browser-app",
                                "configuration" to "browserAppDevelopment"
                            )
                        )
                    )
                }
            }
        }
    }
}
t
cc @Ilya Goncharov [JB]
i
Why do you not use
jsBrowserDistribution
task for
browserAppProduction
. As I can see for
browserAppDevelopment
you use output of
distribution
task, but for production one you don’t use it, but as I understand, you can. As for
jsBrowserProductionWebpack
, there is no
js
file output for this task anymore. Now there is output directory for webpack output (because webpack could emit more than 1 file). That’s why this filter
filter { it.name == javascriptAppFilename }
can filter out all outputs
c
Thanks Ilya—I gave this a try. It does help with the js files not getting picked up, so that’s good progress. What about the issue with the task ‘server appstartScripts’?
i
What is the task? Could you please write how it declared. If it is just start of java application, it should be ok since using jsBrowserDistribution in browserAppProduction
c
I don’t declare the task!
I’m guessing that it comes from the
application
plugin?
This is the full error message, which is failing my build so I can’t just ignore it.
Copy code
A problem was found with the configuration of task ':server-app:startScripts' (type 'CreateStartScripts').
  - Gradle detected a problem with the following location: 'build/js/packages/Chat-browser-app/webpack.config.js'.
    
    Reason: Task ':server-app:startScripts' uses this output of task ':browser-app:jsBrowserDevelopmentWebpack' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
    
    Possible solutions:
      1. Declare task ':browser-app:jsBrowserDevelopmentWebpack' as an input of ':server-app:startScripts'.
      2. Declare an explicit dependency on ':browser-app:jsBrowserDevelopmentWebpack' from ':server-app:startScripts' using Task#dependsOn.
      3. Declare an explicit dependency on ':browser-app:jsBrowserDevelopmentWebpack' from ':server-app:startScripts' using Task#mustRunAfter.