Skip to content

Commit ac2c2de

Browse files
committed
(#79) PSLanguageHostUtils: improve diagnostics for failing processes
1 parent ad8cb04 commit ac2c2de

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/main/kotlin/com/intellij/plugin/powershell/lang/lsp/languagehost/PSLanguageHostUtils.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,35 @@ private suspend fun readPowerShellVersion(exePath: String): PSVersionInfo {
5858
var process: Process? = null
5959
val qInner = if (SystemInfo.isWindows) '\'' else '"'
6060
val commandString = "(\$PSVersionTable.PSVersion, \$PSVersionTable.PSEdition) -join $qInner $qInner"
61+
val commandLine = GeneralCommandLine(exePath, "-command", commandString)
6162
return coroutineScope {
6263
try {
63-
process = GeneralCommandLine(arrayListOf(exePath, "-command", commandString)).createProcess()
64+
process = commandLine.createProcess()
6465
fun readStream(stream: InputStream) = async {
6566
runInterruptible { stream.reader().use { it.readText() } }
6667
}
6768

6869
val stdOutReader = readStream(process!!.inputStream)
69-
readStream(process!!.errorStream)
70+
val stdErrReader = readStream(process!!.errorStream)
7071
val exitCode = process!!.awaitExit()
7172
if (exitCode != 0) {
72-
error("Process exit code $exitCode.")
73+
val stdOut = stdOutReader.await()
74+
val stdErr = stdErrReader.await()
75+
val message = buildString {
76+
append("Process exit code $exitCode.")
77+
if (stdOut.isNotBlank()) {
78+
append("\nStandard output:\n$stdOut")
79+
}
80+
if (stdErr.isNotBlank()) {
81+
append("\nStandard error:\n$stdErr")
82+
}
83+
}
84+
error(message)
7385
}
7486

7587
PSVersionInfo.parse(stdOutReader.await().trim())
7688
} catch (e: Exception) {
77-
PSLanguageHostUtils.LOG.warn("Command execution failed: ${arrayListOf(exePath, "--version")} ${e.message}", e)
89+
PSLanguageHostUtils.LOG.warn("Command execution failed for ${commandLine.preparedCommandLine}", e)
7890
throw PowerShellControlFlowException(e.message, e.cause)
7991
} finally {
8092
process?.destroy()

0 commit comments

Comments
 (0)