tschuchortdev / kotlin-compile-testing

A library for testing Kotlin and Java annotation processors, compiler plugins and code generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Passing in `-language-version` to `kotlincArguments` fails to parse

ZacSweers opened this issue · comments

KotlinCompilation()
  .apply {
    // ...
    kotlincArguments = listOf("-language-version 1.5.30")
  }

Results in a surprising failure like this when compilation is run

java.lang.IllegalArgumentException: Errors parsing kotlinc CLI arguments:
Invalid argument: -language-version 1.5.30
	at com.tschuchort.compiletesting.AbstractKotlinCompilation.commonArguments(AbstractKotlinCompilation.kt:142)
	at com.tschuchort.compiletesting.KotlinCompilation.commonK2JVMArgs(KotlinCompilation.kt:293)
	at com.tschuchort.compiletesting.KotlinCompilation.stubsAndApt(KotlinCompilation.kt:453)
	at com.tschuchort.compiletesting.KotlinCompilation.compile(KotlinCompilation.kt:636)

which has me wondering if KCT should be validateArguments vs just letting kotlinc do its own validation internally anyway

It's worth noting this fails with 1.5 as an input as well with the same error

Example can be found in this PR: square/moshi#1390

Are you sure kotlinc will validate the arguments again and do different validation? When we call K2JVMCompiler we are asked to pass an instance of K2JVMCompilerArguments and not string options so I suppose that they expect us to have already parsed and validated them.

I am not sure where the validation is run, but I know the above syntax is correct and only fails in kotlin-compile-testing

I finally figured it out: if you are using space to delimit the option name from its value (which I dislike to do), then they are two separate arguments from the perspective of the command-line, thus they have to be two separate elements in the argument list:

kotlincArguments = listOf("-language-version", "1.5") did work.

By all accounts, my preferred way listOf("-language-version=1.5") should work too. It turns out that it does not, due to a bug/inconsistency in the Kotlin CLI parser which has already been fixed but not included in the latest Kotlin release (will come in 1.6.0). This really tripped me up.

I will also include a new property in the next release so you can set languageVersion directly.