Make jdk root processing more robust

#KT-54337 fixed
This commit is contained in:
Ilya Chernikov
2022-10-07 13:06:52 +02:00
committed by Space Team
parent 942def5828
commit b50a803b6f
7 changed files with 46 additions and 12 deletions
@@ -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()) {
@@ -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
@@ -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
@@ -88,6 +88,7 @@ public abstract class KotlinIntegrationTestBase extends TestCaseWithTmpdir {
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);
@@ -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 = ""
)
}
}
@@ -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)
}
}
@@ -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
@@ -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()) {