tschuchortdev / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation
Mozilla Public License 2.0
642 stars 61 forks source link

Pass `javacArguments` to Kapt #387

Closed GavinRay97 closed 1 month ago

GavinRay97 commented 11 months ago

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:

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
``` 
tschuchortdev commented 11 months ago

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.

GavinRay97 commented 11 months ago

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 👍