From d9b267c26f8b7466422da753eb591476424012d6 Mon Sep 17 00:00:00 2001 From: Chad Miller Date: Sun, 19 May 2024 14:38:41 -0400 Subject: [PATCH 1/2] Search many locations for unqualified dot program If the name of the dot executable is not qualified with a full path, we should search through some likely locations for it. Use the 'env' program with some likely paths to search through. (A better way to know where to search would be to get the path from the user's environment, but the javascript runtime seems to scour that away.) If we fail to complete running, tell the user what the problem probably is. From env man page, `An exit status of 127 indicates that utility could not be found.` Closes: #7 Closes: #20 --- README.md | 1 + src/processors.ts | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 77de488..2cca788 100644 --- a/README.md +++ b/README.md @@ -95,3 +95,4 @@ Feng Peng(@QAMichaelPeng) With contributions from - Dos Santos(@d0ssant0s) - ebdavison(@ebdavison) +- Chad Miller(@chadmiller-saq / @chadmiller) diff --git a/src/processors.ts b/src/processors.ts index 7ecd224..b8244da 100644 --- a/src/processors.ts +++ b/src/processors.ts @@ -19,13 +19,18 @@ export class Processors { ]); private async writeDotFile(sourceFile: string): Promise { + + const LIKELY_LOCATIONS = `/usr/local/bin:/opt/homebrew/bin:/snap/bin:/bin:/usr/bin`; + return new Promise((resolve, reject) => { - const cmdPath = this.plugin.settings.dotPath; + const cmdPath = this.plugin.settings.dotPath.trim(); const imageFormat = this.plugin.settings.imageFormat; - const parameters = [ `-T${imageFormat}`, `-Gbgcolor=transparent`, `-Gstylesheet=obs-gviz.css`, sourceFile ]; + const alreadyQualified = (cmdPath.contains("/") || cmdPath.contains('\\')); + const execPrefix = alreadyQualified ? [] : [ `/usr/bin/env`, `-P`, LIKELY_LOCATIONS ]; + const execFull = execPrefix.concat([ cmdPath, `-T${imageFormat}`, `-Gbgcolor=transparent`, `-Gstylesheet=obs-gviz.css`, sourceFile ]); - console.debug(`Starting dot process ${cmdPath}, ${parameters}`); - const dotProcess = spawn(cmdPath, parameters); + console.debug(`Starting dot process [${execFull}]`); + const dotProcess = spawn(execFull[0], execFull.slice(1)); const outData: Array = []; let errData = ''; @@ -37,14 +42,16 @@ export class Processors { }); dotProcess.stdin.end(); dotProcess.on('exit', function (code) { - if (code !== 0) { - reject(`"${cmdPath} ${parameters}" failed, error code: ${code}, stderr: ${errData}`); - } else { + if (code == 0) { resolve(Buffer.concat(outData)); + } else if (code == 127) { + reject(`spawn [${execFull}] failed, stderr: ${errData}. Check the dot file path is correct.`); + } else { + reject(`exit code: ${code}, stderr: ${errData}`); } }); dotProcess.on('error', function (err: Error) { - reject(`"${cmdPath} ${parameters}" failed, ${err}`); + reject(`spawn [${execFull}] failed, ${err}`); }); }); } From 72c6897ae4ff104100323c2a83b9735a1e43c284 Mon Sep 17 00:00:00 2001 From: Chad Miller Date: Wed, 6 Nov 2024 09:02:08 -0500 Subject: [PATCH 2/2] Set PATH in env invocation The "-P" parameter is a BSDism, and isn't available in GNU env. This works because PATH is treated specially and is taken out and used internally inside the env program, in addition to sending it to the child program as an environment variable. --- src/processors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/processors.ts b/src/processors.ts index b8244da..1bdc8d0 100644 --- a/src/processors.ts +++ b/src/processors.ts @@ -26,7 +26,7 @@ export class Processors { const cmdPath = this.plugin.settings.dotPath.trim(); const imageFormat = this.plugin.settings.imageFormat; const alreadyQualified = (cmdPath.contains("/") || cmdPath.contains('\\')); - const execPrefix = alreadyQualified ? [] : [ `/usr/bin/env`, `-P`, LIKELY_LOCATIONS ]; + const execPrefix = alreadyQualified ? [] : [ `/usr/bin/env`, `PATH=`+LIKELY_LOCATIONS ]; const execFull = execPrefix.concat([ cmdPath, `-T${imageFormat}`, `-Gbgcolor=transparent`, `-Gstylesheet=obs-gviz.css`, sourceFile ]); console.debug(`Starting dot process [${execFull}]`);