Skip to content

Improve kotlin home error #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
7 changes: 7 additions & 0 deletions simple_test.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kscript.scriplet

fun main() {
println("Hello from simple_test.kts")
println("Kotlin version: ${System.getProperty("kotlin.version")}")
println("Java version: ${System.getProperty("java.version")}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ object Templates {
}

fun createWrapperForScript(packageName: PackageName, className: String): String {
val classReference = packageName.value + "." + className
// val classReference = packageName.value + "." + className // Original
// Assuming the script class compiled by kotlinc will be named <className>Kt
// and be at the root of the JAR if the input script had no package declaration
// and ScriptUtils.resolveCode also doesn't add one (if packageName is null/empty).
val targetClassName = "${className}Kt"

return """
|class Main_${className}{
| companion object {
| @JvmStatic
| fun main(args: Array<String>) {
| val script = Main_${className}::class.java.classLoader.loadClass("$classReference")
| val script = Main_${className}::class.java.classLoader.loadClass("$targetClassName")
| script.getDeclaredConstructor(Array<String>::class.java).newInstance(args);
| }
| }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ class ConfigBuilder(
val kotlinHomeDir: OsPath =
kotlinHomeDir ?: environment.getEnvVariableOrNull("KOTLIN_HOME")?.toOsPathFromNative()
?: ShellUtils.guessKotlinHome(osType)?.toOsPathFromNative()
?: throw IllegalStateException("KOTLIN_HOME is not set and could not be inferred from context.")
?: throw IllegalStateException(
"kscript requires a Kotlin installation.\n" +
"KOTLIN_HOME is not set and it could not be inferred from the system PATH.\n\n" +
"Please take one of the following actions:\n" +
" 1. Set the KOTLIN_HOME environment variable to point to your Kotlin installation directory.\n" +
" Example: export KOTLIN_HOME=/path/to/your/kotlin/kotlinc\n" +
" 2. Ensure 'kotlinc' is available in your system PATH. Installing Kotlin via SDKMAN (sdk install kotlin) or Homebrew (brew install kotlin) usually handles this.\n\n" +
"Note: Even if your script specifies a specific Kotlin version using the '//KOTLIN_VERSION' directive, \n" +
"kscript currently needs a base Kotlin installation to be discoverable to operate."
)

val configFile: OsPath = configFile ?: kscriptDir?.resolve("kscript.properties") ?: when {
osType.isWindowsLike() -> environment.getEnvVariableOrNull("LOCALAPPDATA")?.toOsPathFromNative()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.github.kscripting.kscript.model.OsConfig
import io.github.kscripting.shell.model.OsPath
import io.github.kscripting.shell.model.OsType
import io.github.kscripting.shell.model.toNativeOsPath
import java.io.File // ADDED IMPORT

class CommandResolver(val osConfig: OsConfig) {
private val classPathSeparator =
Expand Down Expand Up @@ -49,9 +50,23 @@ class CommandResolver(val osConfig: OsConfig) {
val compilerOptsStr = resolveCompilerOpts(compilerOpts)
val classpath = resolveClasspath(dependencies)
val jarFile = resolveJarFile(jar)
val files = resolveFiles(filePaths)
val files = resolveFiles(filePaths) // This 'files' variable contains the paths to the script(s) to be compiled
val kotlinc = resolveKotlinBinary("kotlinc")

// START OF MODIFICATION: Print content of files to be compiled
filePaths.forEach { osPath ->
try {
val actualFilePath = osPath.toNativeOsPath().stringPath()
val content = File(actualFilePath).readText(Charsets.UTF_8) // Use actualFilePath
println(" KOTLINC_INPUT_CODE_START ")
println(content)
println(" KOTLINC_INPUT_CODE_END ")
} catch (e: Exception) {
println(" KOTLINC_INPUT_CODE_ERROR Failed to read content of $osPath: ${e.message} ")
}
}
// END OF MODIFICATION

return "$kotlinc $compilerOptsStr $classpath -d $jarFile $files"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ object ScriptUtils {
return if (code.contains("fun main")) ScriptType.KT else ScriptType.KTS
}

// Restored original logic
fun resolveCode(packageName: PackageName?, importNames: Set<ImportName>, scriptNode: ScriptNode): String {
val sortedImports = importNames.sortedBy { it.value }.toList()
val sb = StringBuilder()
Expand Down
3 changes: 3 additions & 0 deletions test_script_1.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//KOTLIN_ARGS -J-Dkotlin.main.kts.jvm.target=11
//KOTLIN_VERSION = 2.2.1-RC
println("Hello from Kotlin 2.2.1-RC!")
6 changes: 6 additions & 0 deletions test_script_5.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//KOTLIN_ARGS -J-Dkotlin.main.kts.jvm.target=11
//KOTLIN_VERSION = 2.2.1-RC
data class Message(val text: String)
fun createMessage(name: String): Message = Message("Hello ${'$'}name from Kotlin 2.2.1-RC!")
val message = createMessage("kscript")
println(message.text)
3 changes: 3 additions & 0 deletions version_test.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//KOTLIN_VERSION 2.2.0-RC
println("Hello from Kotlin ${System.getProperty("kotlin.version")}")
println("Java version: ${System.getProperty("java.version")}")
Loading