visual-studio-codevscode-extensionssimple-git

SimpleGit library not working with vscode extension


I'm trying to get SimpleGit to work in my vscode extension. To make sure I'm using it properly I created this typescript file

import simpleGit, { SimpleGit, CleanOptions } from 'simple-git';

const git: SimpleGit = simpleGit().clean(CleanOptions.FORCE);

async function main() {
    try {
        const status = await git.status();
        console.log("STATUS", status);
    } catch (e) {
        console.log("ERROR", e);
    }
}

if (require.main === module) {
    main();
}

and was able to use the library successfully with no issues. When I try and execute the same call in my vscode extension I get in the debug console ERROR Error: fatal: not a git repository (or any of the parent directories): .git

I'm also seeing this as well in debug console as well. Might be related.

enter image description here

How can I get the same call to work in vscode extension? What am I doing wrong? I appreciate any help!

import * as vscode from 'vscode';
import simpleGit, { SimpleGit, CleanOptions } from 'simple-git';

export function activate(context: vscode.ExtensionContext) {

    let disposable = vscode.commands.registerCommand('my-app.createUrl', async (uri: vscode.Uri) => {
        const git: SimpleGit = simpleGit().clean(CleanOptions.FORCE);
        try {
            const status = await git.status();
            console.log("STATUS", status);
        } catch (e) {
            console.log("ERROR", e);
        }
    });

    context.subscriptions.push(disposable);
}

export function deactivate() { }

Solution

  • You have to tell it where the repo is

    const git = simpleGit(__dirname);