Add workaround for external plugin modifying options on JS task execution

Currently know problematic place is in 'common-configuration.gradle.kts'
 setting '-Xklib-relative-path-base'. This is done in task doFirst{}
 action to avoid remote build cache misses due to absolute path usage.

^KT-27301 In Progress
This commit is contained in:
Yahor Berdnikau
2022-09-17 13:58:09 +02:00
parent 9de0fe48f3
commit ad62262f51
2 changed files with 58 additions and 6 deletions
@@ -60,6 +60,7 @@ import org.jetbrains.kotlin.gradle.targets.js.ir.DISABLE_PRE_IR
import org.jetbrains.kotlin.gradle.targets.js.ir.PRODUCE_JS
import org.jetbrains.kotlin.gradle.targets.js.ir.PRODUCE_UNZIPPED_KLIB
import org.jetbrains.kotlin.gradle.targets.js.ir.PRODUCE_ZIPPED_KLIB
import org.jetbrains.kotlin.gradle.tasks.internal.KotlinJsOptionsCompat
import org.jetbrains.kotlin.gradle.utils.*
import org.jetbrains.kotlin.incremental.*
import org.jetbrains.kotlin.incremental.ClasspathChanges.ClasspathSnapshotDisabled
@@ -935,10 +936,10 @@ abstract class Kotlin2JsCompile @Inject constructor(
@Suppress("DEPRECATION")
@Deprecated("Replaced by compilerOptions input", replaceWith = ReplaceWith("compilerOptions"))
override val kotlinOptions: KotlinJsOptions = object : KotlinJsOptions {
override val options: CompilerJsOptions
get() = compilerOptions
}
override val kotlinOptions: KotlinJsOptions = KotlinJsOptionsCompat(
{ this },
compilerOptions
)
@get:Input
internal var incrementalJsKlib: Boolean = true
@@ -991,6 +992,16 @@ abstract class Kotlin2JsCompile @Inject constructor(
@get:Input
internal abstract val enhancedFreeCompilerArgs: ListProperty<String>
/**
* Workaround for those "nasty" plugins that are adding 'freeCompilerArgs' on task execution phase.
* With properties api it is not possible to update property value after task configuration is finished.
*
* Marking it as `@Internal` as anyway on the configuration phase, when Gradle does task inputs snapshot,
* this input will always be empty.
*/
@get:Internal
internal var additionalFreeCompilerArgs: List<String> = listOf()
override fun createCompilerArgs(): K2JSCompilerArguments =
K2JSCompilerArguments()
@@ -1015,9 +1026,9 @@ abstract class Kotlin2JsCompile @Inject constructor(
}
// Rewriting default outputFile property back to outputFilePropertyValue
args.outputFile = outputFileProperty.get().absoluteFile.normalize().absolutePath
// Overriding freeArgs from compilerOptions with enhanced one
// Overriding freeArgs from compilerOptions with enhanced one + additional one set on execution phase
// containing additional arguments based on the js compilation configuration
args.freeArgs = enhancedFreeCompilerArgs.get()
args.freeArgs = enhancedFreeCompilerArgs.get().union(additionalFreeCompilerArgs).toList()
}
@get:InputFiles
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.tasks.internal
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
/**
* Temporary workaround for external plugins that tries to set up freeCompilerArgs
* in task execution phase.
*/
@Suppress("DEPRECATION")
class KotlinJsOptionsCompat(
private val task: () -> Kotlin2JsCompile,
override val options: CompilerJsOptions
) : KotlinJsOptions {
override var freeCompilerArgs: List<String>
get() = if (isTaskExecuting) {
task().enhancedFreeCompilerArgs.get()
.union(task().additionalFreeCompilerArgs)
.toList()
} else {
options.freeCompilerArgs.get()
}
set(value) = if (isTaskExecuting) {
task().logger.warn(
"kotlinOptions.freeCompilerArgs were changed on task execution phase: ${value.joinToString()}\n" +
"This behaviour will be deprecated and become an error in future releases!"
)
task().additionalFreeCompilerArgs = value
} else {
options.freeCompilerArgs.set(value)
}
private val isTaskExecuting: Boolean
get() = task().state.executing
}