casid / jte

Secure and speedy templates for Java and Kotlin.
https://jte.gg
Apache License 2.0
819 stars 59 forks source link

Unable to change jvmTarget #293

Closed wyaeld closed 11 months ago

wyaeld commented 11 months ago

This is in a multi module project with a lot of Ktor

I'm encountering this error

Failed to compile template, error at components/Pagination.kte:40
                jteOutput.writeUserContent(page.href(currentLocation, pagingResult.currentPage - 1))
/Users/brad/simply/omega/apps/serviceportal/build/jte-classes/gg/jte/generated/precompiled/components/JtePaginationGenerated.kt:39:37
Reason: Cannot inline bytecode built with JVM target 17 into bytecode that is being built with JVM target 1.8. Please specify proper '-jvm-target' option

Now page.href is a function that is proxying of the Ktor Resources Plugin, it looks like the below code. The catch is that the underlying function needs the inline and reified modifies.

    inline fun <reified T : Any> href(resource: T): String {
        return io.ktor.resources.href(resourcesFormat, resource)
    }

I have tried everything I can think of to get Jte to compile at a Jvm Target of 17, to match the rest our project. I'm a little stumped why none of these work. Right now I - temporarily - have all of this in file

plugins.withType<JteGradle> {
    tasks.withType<JavaCompile>().configureEach {
        sourceCompatibility = "17"
        targetCompatibility = "17"
    }
}

jte {
    precompile()
    sourceDirectory.set(file("src/resources/templates").toPath())
    targetDirectory.set(file("build/jte-classes").toPath())
    htmlPolicyClass.set("sel.HotwirePrecompileHtmlPolicy")
}

kotlin {
    jvmToolchain(17)
}

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

None of it makes a difference. Both precompiled templates and the runtime ones with DirectoryCodeResolver emit at Java 8 level.

casid commented 11 months ago

@wyaeld I've created a PR to set the JVM target version for Kotlin templates through Gradle/Maven.

I could reproduce your error. Calling an inline method without setting the JVM target version, I got the same error as you did test.TemplateEngineTest#helloInlineMethod$kte_runtime_test_gradle_kotlin_convention_test. After setting the JVM target version the test passes.

It would be nice to adopt this setting from Gradle automatically. I left a note on the PR, if you have an idea how to accomplish this, let me know :-)

wyaeld commented 11 months ago

Fix looks nice and easy to use, I'm afraid I'm as lost as you are trying to understand how to interact with Gradle on something like this.

casid commented 11 months ago

Sorry, merging the PR automatically closed this issue.

I'll release a new jte version this evening after work.

casid commented 11 months ago

@wyaeld I just released jte 3.1.4 containing a fix for this issue.

wyaeld commented 11 months ago

I pulled in 3.1.4 and confirmed it fixes the jte issue, thanks.