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
This commit is contained in:
Alexander Udalov
2017-03-29 17:42:19 +03:00
parent 3b8110f51c
commit 64dfa6d33d
2 changed files with 44 additions and 20 deletions
@@ -40,8 +40,6 @@ object Main {
val arguments = arrayListOf<String>() val arguments = arrayListOf<String>()
var noReflect = false var noReflect = false
classpath.addPaths(".")
var i = 0 var i = 0
while (i < args.size) { while (i < args.size) {
val arg = args[i] val arg = args[i]
@@ -65,7 +63,9 @@ object Main {
printVersionAndExit() printVersionAndExit()
} }
else if ("-classpath" == arg || "-cp" == arg) { 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) { else if ("-expression" == arg || "-e" == arg) {
runner = ExpressionRunner(next()) runner = ExpressionRunner(next())
@@ -92,10 +92,14 @@ object Main {
i++ 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) { if (!noReflect) {
classpath.addPaths(KOTLIN_HOME.toString() + "/lib/kotlin-reflect.jar") classpath.addPath(KOTLIN_HOME.toString() + "/lib/kotlin-reflect.jar")
} }
if (runner == null) { if (runner == null) {
@@ -105,10 +109,8 @@ object Main {
runner.run(classpath, arguments) runner.run(classpath, arguments)
} }
private fun MutableList<URL>.addPaths(paths: String) { private fun MutableList<URL>.addPath(path: String) {
for (path in paths.split(File.pathSeparator)) { add(File(path).absoluteFile.toURI().toURL())
add(File(path).absoluteFile.toURI().toURL())
}
} }
@JvmStatic @JvmStatic
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.cli
import com.intellij.execution.configurations.GeneralCommandLine import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.util.ExecUtil import com.intellij.execution.util.ExecUtil
import com.intellij.openapi.util.SystemInfo import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.cli.common.ExitCode import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.test.KotlinTestUtils import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.TestCaseWithTmpdir import org.jetbrains.kotlin.test.TestCaseWithTmpdir
@@ -31,26 +32,31 @@ class LauncherScriptTest : TestCaseWithTmpdir() {
vararg args: String, vararg args: String,
expectedStdout: String = "", expectedStdout: String = "",
expectedStderr: String = "", expectedStderr: String = "",
expectedExitCode: ExitCode = ExitCode.OK expectedExitCode: Int = 0,
workDirectory: File? = null
) { ) {
val executableFileName = if (SystemInfo.isWindows) "$executableName.bat" else executableName val executableFileName = if (SystemInfo.isWindows) "$executableName.bat" else executableName
val launcherFile = File(PathUtil.getKotlinPathsForDistDirectory().homePath, "bin/$executableFileName") val launcherFile = File(PathUtil.getKotlinPathsForDistDirectory().homePath, "bin/$executableFileName")
assertTrue("Launcher script not found, run 'ant dist': ${launcherFile.absolutePath}", launcherFile.exists()) assertTrue("Launcher script not found, run 'ant dist': ${launcherFile.absolutePath}", launcherFile.exists())
val processOutput = ExecUtil.execAndGetOutput(GeneralCommandLine(launcherFile.absolutePath, *args)) val cmd = GeneralCommandLine(launcherFile.absolutePath, *args)
val stdout = processOutput.stdout workDirectory?.let(cmd::withWorkDirectory)
val stderr = processOutput.stderr val processOutput = ExecUtil.execAndGetOutput(cmd)
val stdout = StringUtil.convertLineSeparators(processOutput.stdout)
val stderr = StringUtil.convertLineSeparators(processOutput.stderr)
val exitCode = processOutput.exitCode val exitCode = processOutput.exitCode
try { try {
assertEquals(expectedStdout, stdout) assertEquals(expectedStdout, stdout)
assertEquals(expectedStderr, stderr) assertEquals(expectedStderr, stderr)
assertEquals(expectedExitCode.code, exitCode) assertEquals(expectedExitCode, exitCode)
} }
catch (e: Throwable) { catch (e: Throwable) {
System.err.println("exit code $exitCode") System.err.println("exit code $exitCode")
System.err.println("<stdout>$stdout</stdout>") System.err.println("=== STDOUT ===")
System.err.println("<stderr>$stderr</stderr>") System.err.println(stdout)
System.err.println("=== STDERR ===")
System.err.println(stderr)
throw e throw e
} }
} }
@@ -61,7 +67,6 @@ class LauncherScriptTest : TestCaseWithTmpdir() {
fun testKotlincSimple() { fun testKotlincSimple() {
runProcess( runProcess(
"kotlinc", "kotlinc",
"-Xskip-java-check",
"$testDataDirectory/helloWorld.kt", "$testDataDirectory/helloWorld.kt",
"-d", tmpdir.path "-d", tmpdir.path
) )
@@ -70,7 +75,6 @@ class LauncherScriptTest : TestCaseWithTmpdir() {
fun testKotlincJvmSimple() { fun testKotlincJvmSimple() {
runProcess( runProcess(
"kotlinc-jvm", "kotlinc-jvm",
"-Xskip-java-check",
"$testDataDirectory/helloWorld.kt", "$testDataDirectory/helloWorld.kt",
"-d", tmpdir.path "-d", tmpdir.path
) )
@@ -79,7 +83,6 @@ class LauncherScriptTest : TestCaseWithTmpdir() {
fun testKotlincJsSimple() { fun testKotlincJsSimple() {
runProcess( runProcess(
"kotlinc-js", "kotlinc-js",
"-Xskip-java-check",
"$testDataDirectory/emptyMain.kt", "$testDataDirectory/emptyMain.kt",
"-no-stdlib", "-no-stdlib",
"-output", File(tmpdir, "out.js").path "-output", File(tmpdir, "out.js").path
@@ -89,7 +92,6 @@ class LauncherScriptTest : TestCaseWithTmpdir() {
fun testKotlinNoReflect() { fun testKotlinNoReflect() {
runProcess( runProcess(
"kotlinc", "kotlinc",
"-Xskip-java-check",
"$testDataDirectory/reflectionUsage.kt", "$testDataDirectory/reflectionUsage.kt",
"-d", tmpdir.path "-d", tmpdir.path
) )
@@ -102,4 +104,24 @@ class LauncherScriptTest : TestCaseWithTmpdir() {
expectedStdout = "no reflection" 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
)
}
} }