From 64dfa6d33db6a69143c468b0605debe4ccee7d86 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 29 Mar 2017 17:42:19 +0300 Subject: [PATCH] Do not add current directory to "kotlin" classpath As "java" does, do not include the current directory to the classpath if the explicit classpath is specified. This is more stable than always adding the current directory, and users can easily add the ".:" to the beginning of the explicit classpath themselves #KT-17100 Fixed --- .../src/org/jetbrains/kotlin/runner/Main.kt | 20 +++++---- .../kotlin/cli/LauncherScriptTest.kt | 44 ++++++++++++++----- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt index e56f63e0461..ca70d04791f 100644 --- a/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt +++ b/compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt @@ -40,8 +40,6 @@ object Main { val arguments = arrayListOf() var noReflect = false - classpath.addPaths(".") - var i = 0 while (i < args.size) { val arg = args[i] @@ -65,7 +63,9 @@ object Main { printVersionAndExit() } else if ("-classpath" == arg || "-cp" == arg) { - classpath.addPaths(next()) + for (path in next().split(File.pathSeparator).filter(String::isNotEmpty)) { + classpath.addPath(path) + } } else if ("-expression" == arg || "-e" == arg) { runner = ExpressionRunner(next()) @@ -92,10 +92,14 @@ object Main { i++ } - classpath.addPaths(KOTLIN_HOME.toString() + "/lib/kotlin-runtime.jar") + if (classpath.isEmpty()) { + classpath.addPath(".") + } + + classpath.addPath(KOTLIN_HOME.toString() + "/lib/kotlin-runtime.jar") if (!noReflect) { - classpath.addPaths(KOTLIN_HOME.toString() + "/lib/kotlin-reflect.jar") + classpath.addPath(KOTLIN_HOME.toString() + "/lib/kotlin-reflect.jar") } if (runner == null) { @@ -105,10 +109,8 @@ object Main { runner.run(classpath, arguments) } - private fun MutableList.addPaths(paths: String) { - for (path in paths.split(File.pathSeparator)) { - add(File(path).absoluteFile.toURI().toURL()) - } + private fun MutableList.addPath(path: String) { + add(File(path).absoluteFile.toURI().toURL()) } @JvmStatic diff --git a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt index 04382bb697c..5bde2c33e5a 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/cli/LauncherScriptTest.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.cli import com.intellij.execution.configurations.GeneralCommandLine import com.intellij.execution.util.ExecUtil import com.intellij.openapi.util.SystemInfo +import com.intellij.openapi.util.text.StringUtil import org.jetbrains.kotlin.cli.common.ExitCode import org.jetbrains.kotlin.test.KotlinTestUtils import org.jetbrains.kotlin.test.TestCaseWithTmpdir @@ -31,26 +32,31 @@ class LauncherScriptTest : TestCaseWithTmpdir() { vararg args: String, expectedStdout: String = "", expectedStderr: String = "", - expectedExitCode: ExitCode = ExitCode.OK + expectedExitCode: Int = 0, + workDirectory: File? = null ) { val executableFileName = if (SystemInfo.isWindows) "$executableName.bat" else executableName val launcherFile = File(PathUtil.getKotlinPathsForDistDirectory().homePath, "bin/$executableFileName") assertTrue("Launcher script not found, run 'ant dist': ${launcherFile.absolutePath}", launcherFile.exists()) - val processOutput = ExecUtil.execAndGetOutput(GeneralCommandLine(launcherFile.absolutePath, *args)) - val stdout = processOutput.stdout - val stderr = processOutput.stderr + val cmd = GeneralCommandLine(launcherFile.absolutePath, *args) + workDirectory?.let(cmd::withWorkDirectory) + val processOutput = ExecUtil.execAndGetOutput(cmd) + val stdout = StringUtil.convertLineSeparators(processOutput.stdout) + val stderr = StringUtil.convertLineSeparators(processOutput.stderr) val exitCode = processOutput.exitCode try { assertEquals(expectedStdout, stdout) assertEquals(expectedStderr, stderr) - assertEquals(expectedExitCode.code, exitCode) + assertEquals(expectedExitCode, exitCode) } catch (e: Throwable) { System.err.println("exit code $exitCode") - System.err.println("$stdout") - System.err.println("$stderr") + System.err.println("=== STDOUT ===") + System.err.println(stdout) + System.err.println("=== STDERR ===") + System.err.println(stderr) throw e } } @@ -61,7 +67,6 @@ class LauncherScriptTest : TestCaseWithTmpdir() { fun testKotlincSimple() { runProcess( "kotlinc", - "-Xskip-java-check", "$testDataDirectory/helloWorld.kt", "-d", tmpdir.path ) @@ -70,7 +75,6 @@ class LauncherScriptTest : TestCaseWithTmpdir() { fun testKotlincJvmSimple() { runProcess( "kotlinc-jvm", - "-Xskip-java-check", "$testDataDirectory/helloWorld.kt", "-d", tmpdir.path ) @@ -79,7 +83,6 @@ class LauncherScriptTest : TestCaseWithTmpdir() { fun testKotlincJsSimple() { runProcess( "kotlinc-js", - "-Xskip-java-check", "$testDataDirectory/emptyMain.kt", "-no-stdlib", "-output", File(tmpdir, "out.js").path @@ -89,7 +92,6 @@ class LauncherScriptTest : TestCaseWithTmpdir() { fun testKotlinNoReflect() { runProcess( "kotlinc", - "-Xskip-java-check", "$testDataDirectory/reflectionUsage.kt", "-d", tmpdir.path ) @@ -102,4 +104,24 @@ class LauncherScriptTest : TestCaseWithTmpdir() { expectedStdout = "no reflection" ) } + + fun testDoNotAppendCurrentDirToNonEmptyClasspath() { + runProcess( + "kotlinc", + "$testDataDirectory/helloWorld.kt", + "-d", tmpdir.path + ) + + runProcess("kotlin", "test.HelloWorldKt", expectedStdout = "Hello!\n", workDirectory = tmpdir) + + val emptyDir = KotlinTestUtils.tmpDirForTest(this) + runProcess( + "kotlin", + "-cp", emptyDir.path, + "test.HelloWorldKt", + expectedStderr = "error: could not find or load main class test.HelloWorldKt\n", + expectedExitCode = 1, + workDirectory = tmpdir + ) + } }