Skip to content

Commit

Permalink
Better command-not-found error reporting
Browse files Browse the repository at this point in the history
Trying to fix problem where the user PATH environment variable isn't
being read when using Node's exec() function in the VSC extension.
That still happens, but at least the error when it does is a bit
more clear(!)
  • Loading branch information
kosude committed Jun 7, 2024
1 parent e504ed9 commit 71d09d4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
37 changes: 36 additions & 1 deletion integration/vscode-ext/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion integration/vscode-ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
"devDependencies": {
"@types/node": "^20.12.12",
"@types/vscode": "^1.88.0",
"@types/which": "^3.0.4",
"esbuild": "^0.21.4",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"which": "^4.0.0"
}
}
32 changes: 30 additions & 2 deletions integration/vscode-ext/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import * as child_process from "node:child_process";
import * as util from "node:util";
import which from "which";

/**
* Variants of devinit subcommands
Expand Down Expand Up @@ -35,9 +36,23 @@ export class Runner {
* `then` is run after a succesful execution completes, otherwise `err` is run instead.
* @returns The executed command as a verbatim string, for diagnostics
*/
public run(): Promise<{ stdout: string, stderr: string }> {
public async run(): Promise<{ stdout: string, stderr: string }> {
const path = (this.execPath !== "")
? this.execPath
: await this.findDevinitPath();
if (!path) {
return Promise.reject(
"Couldn't find devinit installed on your machine. If you haven't already, download its latest release " +
"from GitHub: https://github.com/kosude/devinit/releases"
);
}

const cmd = `"${path}" ${this.buildArgs().join(" ")}`;

console.log(cmd);

const exec = util.promisify(child_process.exec);
return exec(`"${this.execPath}" ${this.buildArgs().join(" ")}`);
return exec(cmd);
}

/**
Expand Down Expand Up @@ -80,6 +95,19 @@ export class Runner {
return args;
}

/**
* Look for the devinit installation on the system; undefined is returned if none could be found.
*/
private async findDevinitPath(): Promise<string | undefined> {
// search PATH for devinit
const fromPath = await which("devinit", { nothrow: true });
if (fromPath) {
return fromPath;
}

return undefined;
}

/**
* Path to the `devinit` executable
*/
Expand Down
3 changes: 1 addition & 2 deletions integration/vscode-ext/src/runnerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export class RunnerState {
* This should be done when config changes (via listening to `vscode.workspace.onDidChangeConfiguration()`)
*/
public updateUserConfigProperties() {
let execPath = userConfig.getExecutablePath();
this.execPath = (execPath.length > 0) ? execPath : "devinit";
this.execPath = userConfig.getExecutablePath();

let configPath = userConfig.getConfigPath();
this.configPath = (configPath.length > 0) ? configPath : undefined;
Expand Down
3 changes: 2 additions & 1 deletion integration/vscode-ext/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedParameters": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"esModuleInterop": true
}
}

0 comments on commit 71d09d4

Please sign in to comment.