I'm building a VS Code extension which runs git blame as part of the process.
I read that I can execute command line commands from JavaScript with Node's API, using a child process like this:
var exec = require('child_process').exec;
exec(`ls`
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
Running this very simple ls command does print out in the console the directory.
However, when I replace exec(ls
with exec(git blame -L ${startLine},${endLine} extension.ts
it says I'm not in a git repository. Please note that executing the git blame command from my actual terminal does work.
I tried running the command with sudo as well. It throws the error
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper sudo: a password is required
/private/var/folders/th/40j77cv1587_zndgl34g0m100000gn/T/AppTranslocation/C0CFF9C7-F5C7-4C37-B7BD-1990096D4E41/d/Visual Studio Code.app/Contents/Resources/app/out/bootstrap-fork.js:5
What can I do to make it work?
Turns out that Node's child process was running the command on the root directory, not the working directory.
Since I'm developing a VS Code extension, var currentlyOpenTabfilePath = vscode.window.activeTextEditor?.document.uri.fsPath;
solved it.
It's worth mentioning that I tried different things that didn't work. Those are: