illyrix
illyrix

Reputation: 163

How does an extension for VS Code get the install path of VS Code?

I'm developing an extension for VS code(Using javascript). Now I need the path where VS Code installed. There is a way for windows:

var child = require('child_process');
child.exec('reg query HKEY_CLASSES_ROOT\\*\\shell\\VSCode /v Icon', function (error, strOut, strError) { 
//some code...
})

But it works while user installed VS Code correctly only. If this folder was copied from other machine (it means nothing of VS Code in Registry), this function will fail.

On the other hand, it couldn't work at all on Linux or OS X.

I wonder if there are APIs which can be helpful(I found nothing), or other ways can get that path.

Upvotes: 3

Views: 5784

Answers (4)

Eric Amodio
Eric Amodio

Reputation: 723

You can now use

const vscodeInstallPath = vscode.env.appRoot;

Upvotes: 6

Dan_
Dan_

Reputation: 1245

VSCode is written and uses node.js, therefore you can access both the computer, user and node environment variables.

To get the used install path of VSCode you can use the following;

process.env.VSCODE_CWD

For instance, if the first thing my extension did was; console.log(process.env.VSCODE_CWD) it would print out the following in the debug console C:\Program Files\Microsoft VS Code (This is where I have installed VSCode to).

Upvotes: 4

Nigel Scott
Nigel Scott

Reputation: 683

I thought I'd add the answer that you found yourself in case other people come looking for the same thing.

path.dirname(require.main.filename);

in Ubuntu returns (for me)

/usr/share/code/resources/app/out

and in Windows, returns

c:\Program Files\Microsoft VS Code\resources\app\out

It should return something appropriate for OSX too.

This is the folder containing bootstrap.js, which is enough to determine where the application is installed (the default locations in this instance).

In my case, I wanted the path to one of the node modules (vscode-ripgrep) which is built as part of vscode, so I have to process the path a bit more, but it does the job.

Upvotes: 1

KillerAll
KillerAll

Reputation: 315

I don't know why are you need directory of VSCODE but I needed the directory where are my extesion. And it can be accessed as follows:

var myExtDir = vscode.extensions.getExtension ("publisher.name").extensionPath;

Where publisher and name are in package.json

Upvotes: 8

Related Questions