From bb9d4ef36839f317666e3dcf6f09a47e1f129ea6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 9 Mar 2026 15:35:50 +0000 Subject: [PATCH] fix: use path.join for temp file, fix stdout handler race, reject on exit code 0 - Use path.join(TmpDir, ...) instead of TmpDir + "/..." for Windows path separator compatibility (addresses PR #23 from 2018) - Reject the formatter Promise on exit code 0 (no fixable errors found), preventing the VS Code formatting spinner from hanging indefinitely (see issue #39, mirrors PR #41 and PR #48) - Buffer all stdout output and display it as an error message when phpcbf exits with code 3; the previous conditional register of the stdout handler was dead code (phpcbfError was always false at check time) so users never saw the error detail - Replace two separate conditional stdout handler registrations with a single always-registered handler that buffers output and logs to the console in debug mode Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- extension.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/extension.js b/extension.js index 81a61ce..3a3c89a 100644 --- a/extension.js +++ b/extension.js @@ -157,15 +157,16 @@ class PHPCBF { } let text = document.getText(); - let phpcbfError = false; - let fileName = - TmpDir + - "/temp-" + + let stdoutOutput = ""; + let fileName = path.join( + TmpDir, + "temp-" + Math.random() .toString(36) .replace(/[^a-z]+/g, "") .substr(0, 10) + - ".php"; + ".php" + ); fs.writeFileSync(fileName, text); let exec = cp.spawn(this.executablePath, this.getArgs(document, fileName)); @@ -192,6 +193,7 @@ class PHPCBF { */ switch (code) { case 0: + reject(); break; case 1: case 2: @@ -203,7 +205,12 @@ class PHPCBF { } break; case 3: - phpcbfError = true; + if (stdoutOutput.trim()) { + window.showErrorMessage("PHPCBF: " + stdoutOutput.trim()); + } else { + window.showErrorMessage("PHPCBF: general script execution errors."); + } + reject(); break; default: let msgs = { @@ -221,17 +228,12 @@ class PHPCBF { }); }); - if (phpcbfError) { - exec.stdout.on("data", buffer => { - console.log(buffer.toString()); - window.showErrorMessage(buffer.toString()); - }); - } - if (this.debug) { - exec.stdout.on("data", buffer => { + exec.stdout.on("data", buffer => { + stdoutOutput += buffer.toString(); + if (this.debug) { console.log(buffer.toString()); - }); - } + } + }); exec.stderr.on("data", buffer => { console.log(buffer.toString()); });