Skip to content

Commit

Permalink
Make jdk root processing more robust
Browse files Browse the repository at this point in the history
#KT-54337 fixed
  • Loading branch information
ligee authored and qodana-bot committed Oct 21, 2022
1 parent 942def5 commit b50a803
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,8 @@ val CompilerConfiguration.javaSourceRoots: Set<String>
fun CompilerConfiguration.configureJdkClasspathRoots() {
if (getBoolean(JVMConfigurationKeys.NO_JDK)) return

val jdkHome = get(JVMConfigurationKeys.JDK_HOME)
val (javaRoot, classesRoots) = if (jdkHome == null) {
val javaHome = File(System.getProperty("java.home"))
put(JVMConfigurationKeys.JDK_HOME, javaHome)

javaHome to PathUtil.getJdkClassesRootsFromCurrentJre()
} else {
jdkHome to PathUtil.getJdkClassesRoots(jdkHome)
}
val javaRoot = get(JVMConfigurationKeys.JDK_HOME) ?: File(System.getProperty("java.home"))
val classesRoots = PathUtil.getJdkClassesRootsFromJdkOrJre(javaRoot)

if (!CoreJrtFileSystem.isModularJdk(javaRoot)) {
if (classesRoots.isEmpty()) {
Expand Down
6 changes: 4 additions & 2 deletions compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,12 @@ fun CompilerConfiguration.configureJdkHome(arguments: K2JVMCompilerArguments): B
messageCollector.report(ERROR, "JDK home directory does not exist: $jdkHome")
return false
}

messageCollector.report(LOGGING, "Using JDK home directory $jdkHome")

put(JVMConfigurationKeys.JDK_HOME, jdkHome)
} else {
val javaHome = File(System.getProperty("java.home"))
messageCollector.report(LOGGING, "Using JDK home inferred from java.home: $javaHome")
put(JVMConfigurationKeys.JDK_HOME, javaHome)
}

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Buildfile: [TestData]/build.xml
build:
[kotlinc] Compiling [[TestData]/hello.kt] => [[Temp]/hello.jar]
[kotlinc] logging: using Kotlin home directory [KotlinProjectHome]/dist/kotlinc
[kotlinc] logging: using JDK home inferred from java.home: [JavaHome]
[kotlinc] logging: exception on loading scripting plugin: java.lang.ClassNotFoundException: org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar
[kotlinc] logging: using JVM IR backend
[kotlinc] logging: configuring the compilation environment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected String normalizeOutput(@NotNull File testDataDir, @NotNull String cont
content = normalizePath(content, tmpdir, "[Temp]");
content = normalizePath(content, getCompilerLib(), "[CompilerLib]");
content = normalizePath(content, new File(KtTestUtil.getHomeDirectory()), "[KotlinProjectHome]");
content = normalizePath(content, new File(System.getProperty("java.home")), "[JavaHome]");
content = content.replaceAll(Pattern.quote(KotlinCompilerVersion.VERSION), "[KotlinVersion]");
content = content.replaceAll("\\(JRE .+\\)", "(JRE [JREVersion])");
content = StringUtil.convertLineSeparators(content);
Expand Down
24 changes: 24 additions & 0 deletions compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,28 @@ println(42)
"kotlinc", "$testDataDirectory/interpreterClassLoader.kt", "-d", tmpdir.path
)
}

fun testImplicitModularJdk() {
// see KT-54337
val moduleInfo = tmpdir.resolve("module-info.java").apply {
writeText(
"""
module test {
requires kotlin.stdlib;
}
""".trimIndent()
)
}
val testKt = tmpdir.resolve("test.kt").apply {
writeText("fun main() {}")
}
val jdk11 = mapOf("JAVA_HOME" to KtTestUtil.getJdk11Home().absolutePath)
runProcess(
"kotlinc", moduleInfo.absolutePath, testKt.absolutePath, "-d", tmpdir.path,
environment = jdk11,
expectedExitCode = 0,
expectedStdout = "",
expectedStderr = ""
)
}
}
6 changes: 6 additions & 0 deletions compiler/util/src/org/jetbrains/kotlin/utils/PathUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,10 @@ object PathUtil {
@JvmStatic
fun getJdkClassesRoots(jdkHome: File): List<File> =
JavaSdkUtil.getJdkClassesRoots(jdkHome, false)

@JvmStatic
fun getJdkClassesRootsFromJdkOrJre(javaRoot: File): List<File> {
val isJdk = File(javaRoot, "jre/lib").exists()
return JavaSdkUtil.getJdkClassesRoots(javaRoot, !isJdk)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.cli.jvm.compiler.NoScopeRecordCliBindingTrace
import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM
import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot
import org.jetbrains.kotlin.cli.jvm.config.JvmModulePathRoot
import org.jetbrains.kotlin.codegen.ClassBuilderFactories
import org.jetbrains.kotlin.codegen.DefaultCodegenFactory
import org.jetbrains.kotlin.codegen.KotlinCodegenFacade
Expand Down Expand Up @@ -144,7 +145,13 @@ private fun compileImpl(
// TODO: make this logic obsolete by injecting classpath earlier in the pipeline
val depsFromConfiguration = get(dependencies)?.flatMapTo(HashSet()) { (it as? JvmDependency)?.classpath ?: emptyList() }
val depsFromCompiler = context.environment.configuration.getList(CLIConfigurationKeys.CONTENT_ROOTS)
.mapNotNull { if (it is JvmClasspathRoot && !it.isSdkRoot) it.file else null }
.mapNotNull {
when {
it is JvmClasspathRoot && !it.isSdkRoot -> it.file
it is JvmModulePathRoot -> it.file
else -> null
}
}
if (!depsFromConfiguration.isNullOrEmpty()) {
val missingDeps = depsFromCompiler.filter { !depsFromConfiguration.contains(it) }
if (missingDeps.isNotEmpty()) {
Expand Down

0 comments on commit b50a803

Please sign in to comment.