Get directory of Current active file when developing a VSCode extension

Viewed 131

I am developing a VSCode extension to generate templates.

However, I cannot figure out how to get the location of the VSCode file that I am currently editing.

When I search for this question, most answers say to do something like vscode.window.activeTextEditor.document.uri.fsPath - however - this gets you the directory for the target VS code window (the Extension Development Host window) - which is not what I want

I want to get the directory of the VS code extension file I am editing (such as the extension.ts file).

Any help greatly appreciated.

1 Answers

If you mean the extension's path:

context.extensionPath

For extension.ts try something like:

context.asAbsolutePath("extension.ts") // if at top level

If you don't know the directory structure of the file you are interested in, like src/otherDirectory/otherSubDirectory, I think you will have to search/walk for it with fs commands starting from the extensionPath. See https://dustinpfister.github.io/2018/07/20/nodejs-ways-to-walk-a-file-system/ for example.

Related