kscript icon indicating copy to clipboard operation
kscript copied to clipboard

INCLUDE DependsOn with class packages in different namespace

Open HoffiMuc opened this issue 4 years ago • 1 comments

In my case, I have my main in package x.y and want to INCLUDE / DependsOn a file in package x.a as well as in package x.y.z

@file:DependOn(...)

@file:Include("../helpers/CamelCase.kt")
@file:Include("./mappings/Jira.kt")

@file:EntryPoint("com.hoffi.something.AppKt")

package com.hoffi.something

import DependsOn
import EntryPoint
import Include
import com.hoffi.something.mappings.Histories

fun main(args: Array<String>) {
    App().main(args)
}

class App : CliktCommand() {
    override fun run() {
        ...
    }
}

doesn't find the class which is one of the defined classes in ./mappings/Jira.kt

package com.hoffi.something.mappings

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.Instant

...
@SerialName("histories")
@Serializable data class Histories(
    val author: Author,
    @Serializable(InstantSerializer::class) val created: Instant,
    val id: String,
    val items: MutableList<Items>,
)
...

gives me:

/tmp/tmp17398417963705588090.tmp/scriptlet.248cdc652452029b.kt:18:18: error: unresolved reference: something
import com.hoffi.something.mappings.Histories
                 ^

btw: ... it took me a long time to really understand your comment you added in https://github.com/holgerbrandl/kscript/commit/ee7748e8843e474ad41535a3d9d33852ac56fb02

HoffiMuc avatar Nov 29 '21 17:11 HoffiMuc

I guess this use case is not supported, and it's unlikely that this will ever work. @file:Include works by literally rewriting your script code prior to compilation to include the include-file at the given location. kscript tries to be a bit more clever, to at least sort imports to the top of the merged script. But it can't resolve the conflict you've created in your example where a single source file has 2 package statements. This is not legit according to the kotlin language specification.

Does this answer help?

holgerbrandl avatar Nov 30 '21 19:11 holgerbrandl