From faba9c4272eb820b6e93e02e027436be672439cf Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 11 Feb 2022 10:23:16 +0300 Subject: [PATCH] [CLI] Don't register classpath roots twice There are two ways how content roots are registered in compiler configuration in JVM CLI compiler: 1. Directly from arguments 2. From Module, which build from arguments And there was a problem that both ways used at the same time in some circumstances (regular compilation without .xml module file) which caused duplication of all content roots. Ideal solution for this problem is removal of Module usages at all, because it looks like redundant abstraction which just complicate things, but it's too scary to remove it, because it hove some none trivial usages inside compiler. So to fix problem with duplicated roots this commit just removes registration of roots from arguments if Module is used --- .../org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt | 13 +++++++++---- .../cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt | 8 ++++---- .../org/jetbrains/kotlin/cli/jvm/jvmArguments.kt | 9 +++------ .../compiler/plugin/impl/compilationContext.kt | 3 ++- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index c6f1f6c50ac..7d73948e5b1 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -74,13 +74,18 @@ class K2JVMCompiler : CLICompiler() { val moduleName = arguments.moduleName ?: JvmProtoBufUtil.DEFAULT_MODULE_NAME configuration.put(CommonConfigurationKeys.MODULE_NAME, moduleName) - configuration.configureExplicitContentRoots(arguments) + configuration.configureJavaModulesContentRoots(arguments) configuration.configureStandardLibs(paths, arguments) configuration.configureAdvancedJvmOptions(arguments) configuration.configureKlibPaths(arguments) - if (arguments.buildFile == null && !arguments.version && !arguments.allowNoSourceFiles && - (arguments.script || arguments.expression != null || arguments.freeArgs.isEmpty())) { + if ( + arguments.buildFile == null && + !arguments.version && + !arguments.allowNoSourceFiles && + (arguments.script || arguments.expression != null || arguments.freeArgs.isEmpty()) + ) { + configuration.configureContentRootsFromClassPath(arguments) // script or repl if (arguments.script && arguments.freeArgs.isEmpty()) { @@ -131,7 +136,7 @@ class K2JVMCompiler : CLICompiler() { if (arguments.javaPackagePrefix != null) { strongWarning("The '-Xjava-package-prefix' option is ignored because '-Xbuild-file' is specified") } - + configuration.configureContentRootsFromClassPath(arguments) val sanitizedCollector = FilteringMessageCollector(messageCollector, VERBOSE::contains) configuration.put(JVMConfigurationKeys.MODULE_XML_FILE, buildFile) CompileEnvironmentUtil.loadModuleChunk(buildFile, sanitizedCollector) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt index 75e0f0c7df5..9f8640fe577 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt @@ -165,10 +165,10 @@ object KotlinToJVMBytecodeCompiler { for (module in chunk) { for (classpathRoot in module.getClasspathRoots()) { - configuration.add( - CLIConfigurationKeys.CONTENT_ROOTS, - if (isJava9Module) JvmModulePathRoot(File(classpathRoot)) else JvmClasspathRoot(File(classpathRoot)) - ) + if (isJava9Module) { + configuration.add(CLIConfigurationKeys.CONTENT_ROOTS, JvmModulePathRoot(File(classpathRoot))) + } + configuration.add(CLIConfigurationKeys.CONTENT_ROOTS, JvmClasspathRoot(File(classpathRoot))) } } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt index 60861c8bd8e..96334961c11 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt @@ -158,16 +158,13 @@ fun CompilerConfiguration.configureJdkHome(arguments: K2JVMCompilerArguments): B return true } -fun CompilerConfiguration.configureExplicitContentRoots(arguments: K2JVMCompilerArguments) { +fun CompilerConfiguration.configureJavaModulesContentRoots(arguments: K2JVMCompilerArguments) { for (modularRoot in arguments.javaModulePath?.split(File.pathSeparatorChar).orEmpty()) { add(CLIConfigurationKeys.CONTENT_ROOTS, JvmModulePathRoot(File(modularRoot))) } +} - if (arguments.buildFile != null) { - // In the .xml compilation mode, all content roots except module path will be loaded from the .xml build file. - return - } - +fun CompilerConfiguration.configureContentRootsFromClassPath(arguments: K2JVMCompilerArguments) { for (path in arguments.classpath?.split(File.pathSeparatorChar).orEmpty()) { add(CLIConfigurationKeys.CONTENT_ROOTS, JvmClasspathRoot(File(path))) } diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/compilationContext.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/compilationContext.kt index 38df7cf6147..cade1761e3e 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/compilationContext.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/compilationContext.kt @@ -269,7 +269,8 @@ private fun createInitialCompilerConfiguration( ScriptingCompilerConfigurationComponentRegistrar() ) - configureExplicitContentRoots(baseArguments) + configureJavaModulesContentRoots(baseArguments) + configureContentRootsFromClassPath(baseArguments) if (!baseArguments.noStdlib) { addModularRootIfNotNull(isModularJava, "kotlin.stdlib", KotlinJars.stdlib)