Skip to content

Commit

Permalink
Fix task module-name is not propagated to compiler arguments
Browse files Browse the repository at this point in the history
Currently, we have three ways to set '-module-name' compiler argument:
- via KotlinCompilation.compilerOptions.options.moduleName
- via KotlinCompile.compilerOptions.moduleName
- via KotlinCompile.moduleName

Last one was ignored and overwritten by compilerOptions when they are
filling arguments leading to wrong argument in case of Android UI tests
compilation.

^KT-55334 Fixed
  • Loading branch information
Tapchicoma committed Dec 15, 2022
1 parent d40ebc3 commit 52b225d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.junit.Assume
import org.junit.Ignore
import org.junit.Test
import java.io.File
import kotlin.test.assertTrue

open class Kapt3Android41IT : Kapt3AndroidIT() {
override val androidGradlePluginVersion: AGPVersion
Expand Down Expand Up @@ -439,6 +440,34 @@ abstract class Kapt3AndroidIT : BaseGradleIT() {
}
}

// KT-55334: Kapt generate stubs and related compile task use same -module-name value
@Test
fun kaptGenerateStubsModuleName() {
with(
Project(
"android-dagger",
directoryPrefix = "kapt2",
minLogLevel = LogLevel.DEBUG
)
) {
build(":app:compileDebugAndroidTestKotlin") {
val stubsFile = projectDir
.resolve("app/build/tmp/kapt3/stubs/debugAndroidTest/com/example/dagger/kotlin/TestClass.java")
assertTrue(stubsFile.exists(), "File does not exist: ${stubsFile.absolutePath}")
assertTrue(
stubsFile.readText()
.contains("public final void bar${'$'}app_debug() {"),
"Actual generated stub content:\n${stubsFile.readText()}"
)

val compilerClassFile = projectDir
.resolve("app/build/tmp/kotlin-classes/debugAndroidTest/com/example/dagger/kotlin/TestClass.class")
assertTrue(compilerClassFile.exists(), "File does not exist: ${compilerClassFile.absolutePath}")
checkBytecodeContains(compilerClassFile, "public final bar${'$'}app_debug()V")
}
}
}

private fun setupDataBinding(project: Project) {
project.setupWorkingDir()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@ class NewMultiplatformIT : BaseGradleIT() {
"Hello.internalFun\$new_mpp_associate_compilations",
"HelloTest.internalTestFun\$new_mpp_associate_compilations"
)
assertFileExists("build/classes/kotlin/jvm/integrationTest/META-INF/new-mpp-associate-compilations_integrationTest.kotlin_module")
assertFileExists("build/classes/kotlin/jvm/integrationTest/META-INF/new-mpp-associate-compilations.kotlin_module")

// JS:
assertFileExists(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ dependencies {

implementation 'com.google.dagger:dagger:2.9'
kapt 'com.google.dagger:dagger-compiler:2.9'
kapt 'javax.annotation:javax.annotation-api:1.3.2'
kaptAndroidTest 'com.google.dagger:dagger-compiler:2.9'
kaptAndroidTest 'javax.annotation:javax.annotation-api:1.3.2'
compileOnly 'org.glassfish:javax.annotation:10.0-b28'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 com.example.dagger.kotlin

class TestClass {
internal fun bar() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 com.example.dagger.kotlin

import com.example.dagger.kotlin.ui.HomeActivity
import dagger.Component
import javax.inject.Singleton

@Singleton
@Component
interface TestComponent {
fun inject(application: BaseApplication)
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,9 @@ internal open class KotlinJvmCompilerArgumentsContributor(
args.destinationAsFile = destinationDir

compilerOptions.fillCompilerArguments(args)
// Restoring moduleName overwritten by fillCompilerArguments default value
if (args.moduleName == null) {
args.moduleName = moduleName
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,9 @@ abstract class KaptGenerateStubsTask @Inject constructor(
args.verbose = verbose.get()
args.classpathAsList = this.libraries.filter { it.exists() }.toList()
args.destinationAsFile = this.destinationDirectory.get().asFile
// Setting moduleName from task input if it has default complierOptions value
if (args.moduleName == null) {
args.moduleName = moduleName.get()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ abstract class KotlinCompile @Inject constructor(
UsesKotlinJavaToolchain {

init {
compilerOptions.moduleName.convention(moduleName)
compilerOptions.verbose.convention(logger.isDebugEnabled)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal open class BaseKotlinCompileConfig<TASK : KotlinCompile> : AbstractKotl
task.associatedJavaCompileTaskTargetCompatibility.value(javaTaskProvider.map { it.targetCompatibility })
task.associatedJavaCompileTaskName.value(javaTaskProvider.map { it.name })
task.ownModuleName.value(
(compilation.compilerOptions.options as KotlinJvmCompilerOptions).moduleName.convention(compilation.ownModuleName)
(compilation.compilerOptions.options as KotlinJvmCompilerOptions).moduleName.orElse(compilation.ownModuleName)
)
}
}
Expand Down

0 comments on commit 52b225d

Please sign in to comment.