Skip to content

Commit 17bbc6e

Browse files
committed
Spawn subprocesses without shell
1 parent 0dc7623 commit 17bbc6e

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

shared/src/main/kotlin/org/javacs/kt/classpath/GradleClassPathResolver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private fun readDependenciesViaGradleCLI(projectDirectory: Path, gradleScripts:
7676
val tmpScripts = gradleScripts.map { gradleScriptToTempFile(it, deleteOnExit = false).toPath().toAbsolutePath() }
7777
val gradle = getGradleCommand(projectDirectory)
7878

79-
val command = "$gradle ${tmpScripts.map { "-I $it" }.joinToString(" ")} ${gradleTasks.joinToString(" ")} --console=plain"
79+
val command = listOf(gradle.toString()) + tmpScripts.flatMap { listOf("-I", it.toString()) } + gradleTasks + listOf("--console=plain")
8080
val dependencies = findGradleCLIDependencies(command, projectDirectory)
8181
?.also { LOG.debug("Classpath for task {}", it) }
8282
.orEmpty()
@@ -87,7 +87,7 @@ private fun readDependenciesViaGradleCLI(projectDirectory: Path, gradleScripts:
8787
return dependencies
8888
}
8989

90-
private fun findGradleCLIDependencies(command: String, projectDirectory: Path): Set<Path>? {
90+
private fun findGradleCLIDependencies(command: List<String>, projectDirectory: Path): Set<Path>? {
9191
val (result, errors) = execAndReadStdoutAndStderr(command, projectDirectory)
9292
if ("FAILURE: Build failed" in errors) {
9393
LOG.warn("Gradle task failed: {}", errors)

shared/src/main/kotlin/org/javacs/kt/classpath/MavenClassPathResolver.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,19 @@ private fun mavenJarName(a: Artifact, source: Boolean) =
104104

105105
private fun generateMavenDependencyList(pom: Path): Path {
106106
val mavenOutput = Files.createTempFile("deps", ".txt")
107-
val command = "${mvnCommand(pom)} dependency:list -DincludeScope=test -DoutputFile=$mavenOutput -Dstyle.color=never"
107+
val command = listOf(mvnCommand(pom).toString(), "dependency:list", "-DincludeScope=test", "-DoutputFile=$mavenOutput", "-Dstyle.color=never")
108108
runCommand(pom, command)
109109
return mavenOutput
110110
}
111111

112112
private fun generateMavenDependencySourcesList(pom: Path): Path {
113113
val mavenOutput = Files.createTempFile("sources", ".txt")
114-
val command = "${mvnCommand(pom)} dependency:sources -DincludeScope=test -DoutputFile=$mavenOutput -Dstyle.color=never"
114+
val command = listOf(mvnCommand(pom).toString(), "dependency:sources", "-DincludeScope=test", "-DoutputFile=$mavenOutput", "-Dstyle.color=never")
115115
runCommand(pom, command)
116116
return mavenOutput
117117
}
118118

119-
private fun runCommand(pom: Path, command: String) {
119+
private fun runCommand(pom: Path, command: List<String>) {
120120
val workingDirectory = pom.toAbsolutePath().parent
121121
LOG.info("Run {} in {}", command, workingDirectory)
122122
val (result, errors) = execAndReadStdoutAndStderr(command, workingDirectory)

shared/src/main/kotlin/org/javacs/kt/util/Utils.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import java.nio.file.Path
66
import java.nio.file.Paths
77
import java.util.concurrent.CompletableFuture
88

9-
fun execAndReadStdout(shellCommand: String, directory: Path): String {
10-
val process = Runtime.getRuntime().exec(shellCommand, null, directory.toFile())
9+
fun execAndReadStdout(shellCommand: List<String>, directory: Path): String {
10+
val process = ProcessBuilder(shellCommand).directory(directory.toFile()).start()
1111
val stdout = process.inputStream
1212
return stdout.bufferedReader().use { it.readText() }
1313
}
1414

15-
fun execAndReadStdoutAndStderr(shellCommand: String, directory: Path): Pair<String, String> {
16-
val process = Runtime.getRuntime().exec(shellCommand, null, directory.toFile())
15+
fun execAndReadStdoutAndStderr(shellCommand: List<String>, directory: Path): Pair<String, String> {
16+
val process = ProcessBuilder(shellCommand).directory(directory.toFile()).start()
1717
val stdout = process.inputStream
1818
val stderr = process.errorStream
1919
var output = ""

0 commit comments

Comments
 (0)