From 484ed3cac68682495b32f68e2070045549de7e0e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 2 Apr 2026 07:08:41 +0000 Subject: [PATCH] fix: replace console.group/time with VS Code OutputChannel for debug logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes issue #14 ('TypeError: console.group is not a function'). Problems fixed: - console.group(), console.time(), console.timeEnd(), console.groupEnd() are not available in all VS Code extension host environments and throw TypeError when debug mode is enabled. - The phpcbfError stdout-capture pattern was completely broken: the flag was set inside an async callback but checked synchronously immediately after, so the stdout listener was never registered. - Exit code 3 left the Promise unresolved (hung), and referenced the removed phpcbfError variable which would throw in strict mode. Changes: - Add _debugLog() helper that writes to a VS Code OutputChannel (visible under View → Output → phpcbf) with a console.log fallback. - Replace console.group/time/timeEnd/groupEnd with _debugLog() calls. - Replace stderr console.log with _debugLog() so stderr is always captured in the Output panel (not just in debug mode). - Remove the dead phpcbfError pattern; stdout is now always streamed to _debugLog when debug is enabled. - Fix exit code 3: call reject() so the Promise settles and show an error notification. - Create OutputChannel in activate() and pass it to PHPCBF constructor. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- extension.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/extension.js b/extension.js index 6457e71..c1671de 100644 --- a/extension.js +++ b/extension.js @@ -16,10 +16,19 @@ const { findFiles } = require("./lib/utils"); const TmpDir = os.tmpdir(); class PHPCBF { - constructor() { + constructor(outputChannel) { + this.outputChannel = outputChannel; this.loadSettings(); } + _debugLog(message) { + if (this.outputChannel) { + this.outputChannel.appendLine(message); + } else { + console.log(message); + } + } + loadSettings(uri) { // Use the provided URI (e.g. the document being formatted), fall back to // the active editor, or null (global settings) if neither is available. @@ -96,10 +105,7 @@ class PHPCBF { args.push("--standard=" + this.standard); } if (this.debug) { - console.group("PHPCBF"); - console.log( - "PHPCBF args: " + this.executablePath + " " + args.join(" ") - ); + this._debugLog("PHPCBF args: " + this.executablePath + " " + args.join(" ")); } return args; } @@ -133,11 +139,10 @@ class PHPCBF { this.loadSettings(document.uri); if (this.debug) { - console.time("phpcbf"); + this._debugStart = Date.now(); } let text = document.getText(); - let phpcbfError = false; let fileName = TmpDir + "/temp-" + @@ -156,7 +161,7 @@ class PHPCBF { let promise = new Promise((resolve, reject) => { exec.on("error", err => { reject(); - console.log(err); + this._debugLog("PHPCBF spawn error: " + err.message); if (err.code == "ENOENT") { window.showErrorMessage( "PHPCBF: " + err.message + ". executablePath not found." @@ -183,7 +188,9 @@ class PHPCBF { } break; case 3: - phpcbfError = true; + // General phpcbf execution error — reject so the Promise settles. + window.showErrorMessage("PHPCBF: general script execution errors."); + reject(); break; default: let msgs = { @@ -201,25 +208,17 @@ 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 => { - console.log(buffer.toString()); + this._debugLog(buffer.toString()); }); } exec.stderr.on("data", buffer => { - console.log(buffer.toString()); + this._debugLog(buffer.toString()); }); exec.on("close", code => { - // console.log(code); if (this.debug) { - console.timeEnd("phpcbf"); - console.groupEnd(); + this._debugLog("phpcbf done (" + (Date.now() - (this._debugStart || Date.now())) + "ms)"); } }); @@ -259,7 +258,9 @@ class PHPCBF { } exports.activate = context => { - let phpcbf = new PHPCBF(); + const outputChannel = vscode.window.createOutputChannel("phpcbf"); + context.subscriptions.push(outputChannel); + let phpcbf = new PHPCBF(outputChannel); context.subscriptions.push( workspace.onWillSaveTextDocument(event => { @@ -312,7 +313,6 @@ exports.activate = context => { } }) .catch(err => { - console.log(err); reject(); }); });