kotlin-compile-testing icon indicating copy to clipboard operation
kotlin-compile-testing copied to clipboard

Pass `javacArguments` to Kapt

Open GavinRay97 opened this issue 2 years ago • 2 comments

Passes javacArguments option through to Kapt. This is required for code which uses things like --enable-preview or --add-opens

(Closes: https://github.com/tschuchortdev/kotlin-compile-testing/issues/385)

Kapt requires a Map<String, String> for it's javac arguments, so convert it from a List<String>

See the function working here:

  • https://pl.kotl.in/JFrNpLMu9
val args = listOf(
    "-verbose",
    "-cp", "path/to/classes",
    "-J-Xmx512m",
    "-g:none",
    "-source", "1.8",
    "--add-opens", "java.base/java.util=ALL-UNNAMED"
)

val result = convertJavacArgumentsListToMap(args)
result.forEach { (key, value) -> println("Key: $key Value: $value") }
Key: -verbose Value: 
Key: -cp Value: path/to/classes
Key: -J-Xmx512m Value: 
Key: -g:none Value: 
Key: -source Value: 1.8
Key: --add-opens Value: java.base/java.util=ALL-UNNAMED
``` 

GavinRay97 avatar Aug 07 '23 23:08 GavinRay97

Thanks for your PR! Does this fix the issue you were having in #385, i.e. did you confirm that the options are actually passed to kapt correctly? It would be good to have at least one test to exercise this, but --enable-preview probably can't be used because it is not available in earlier JDK versions. Perhaps it is sufficient to search the logs for javac options passed to kapt.

tschuchortdev avatar Aug 08 '23 19:08 tschuchortdev

No problem =)

Does this fix the issue you were having in https://github.com/tschuchortdev/kotlin-compile-testing/issues/385, i.e. did you confirm that the options are actually passed to kapt correctly?

It does, the code runs without issue after this patch 👍

It would be good to have at least one test to exercise this, but --enable-preview probably can't be used because it is not available in earlier JDK versions.

I thought about adding a test but like you said, it would mean forcing the JDK to something like a minimum of JDK 17 or so for --enable-preview

Perhaps it is sufficient to search the logs for javac options passed to kapt.

Good idea -- this sounds reasonable to me 👍

GavinRay97 avatar Aug 08 '23 22:08 GavinRay97