Skip to content

Commit 64c369c

Browse files
committed
Merge PR #189
Fix Maven resolver blocking on reading from stdio.
2 parents a84f7c3 + 3f21d92 commit 64c369c

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.javacs.kt.classpath
22

33
import org.javacs.kt.LOG
44
import org.javacs.kt.util.findCommandOnPath
5+
import org.javacs.kt.util.execAndReadStdoutAndStderr
56
import java.nio.file.Path
67
import java.nio.file.Files
78
import java.io.File
@@ -60,20 +61,14 @@ private fun mavenJarName(a: Artifact, source: Boolean) =
6061

6162
private fun generateMavenDependencyList(pom: Path): Path {
6263
val mavenOutput = Files.createTempFile("deps", ".txt")
63-
val workingDirectory = pom.toAbsolutePath().parent.toFile()
64-
val cmd = "$mvnCommand dependency:list -DincludeScope=test -DoutputFile=$mavenOutput"
65-
LOG.info("Run {} in {}", cmd, workingDirectory)
66-
val process = Runtime.getRuntime().exec(cmd, null, workingDirectory)
67-
68-
process.inputStream.bufferedReader().use { reader ->
69-
while (process.isAlive) {
70-
val line = reader.readLine()?.trim() ?: break
71-
if (line.isNotEmpty() && !line.startsWith("Progress")) {
72-
LOG.info("Maven: {}", line)
73-
}
74-
}
64+
val command = "$mvnCommand dependency:list -DincludeScope=test -DoutputFile=$mavenOutput"
65+
val workingDirectory = pom.toAbsolutePath().parent
66+
LOG.info("Run {} in {}", command, workingDirectory)
67+
val (result, errors) = execAndReadStdoutAndStderr(command, workingDirectory)
68+
LOG.debug(result)
69+
if ("BUILD FAILURE" in errors) {
70+
LOG.warn("Maven task failed: {}", errors.lines().firstOrNull())
7571
}
76-
7772
return mavenOutput
7873
}
7974

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ internal class WithStdlibResolver(private val wrapped: ClassPathResolver) : Clas
1313

1414
private fun wrapWithStdlib(paths: Set<Path>): Set<Path> {
1515
// Ensure that there is exactly one kotlin-stdlib present, and/or exactly one of kotlin-stdlib-common, -jdk8, etc.
16-
val isStdlib: ((Path) -> Boolean) = { it.toString().contains("kotlin-stdlib") }
16+
val isStdlib: ((Path) -> Boolean) = {
17+
val pathString = it.toString()
18+
pathString.contains("kotlin-stdlib") && !pathString.contains("kotlin-stdlib-common")
19+
}
1720

1821
val linkedStdLibs = paths.filter(isStdlib)
1922
.mapNotNull { StdLibItem.from(it) }

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ fun execAndReadStdoutAndStderr(shellCommand: String, directory: Path): Pair<Stri
1717
val process = Runtime.getRuntime().exec(shellCommand, null, directory.toFile())
1818
val stdout = process.inputStream
1919
val stderr = process.errorStream
20-
val output = stdout.bufferedReader().use { it.readText() }
21-
val errors = stderr.bufferedReader().use { it.readText() }
20+
var output = ""
21+
var errors = ""
22+
val outputThread = Thread { stdout.bufferedReader().use { output += it.readText() } }
23+
val errorsThread = Thread { stderr.bufferedReader().use { errors += it.readText() } }
24+
outputThread.start()
25+
errorsThread.start()
26+
outputThread.join()
27+
errorsThread.join()
2228
return Pair(output, errors)
2329
}
2430

0 commit comments

Comments
 (0)