1

I have opened multiple angular projects in visual studio code and i am trying to get cuttenlty opened file's project folder path inside my extension. But I am not getting the correct path . Anyone know please help to find the solution.

Below script only will work if I open single angular project in VS Code but how do for if I open multiple project folders.

extension.js:

exports.activate = context => {
    const mymodules = vscode.commands.registerCommand('extension.search', () => {

    var rootUri = vscode.workspace.workspaceFolders[0].uri;
    var folderPathNameOfCurrentlyOpenedFile=rootUri.fsPath;  // currently opened file's project folder path[Opened single project]

    ...
}
Malli
  • 83
  • 1
  • 8
  • With VSCode 1.52 (Nov. 2020), you will have [`${fileWorkspaceFolder}`](https://stackoverflow.com/a/64870853/6309) which could help. – VonC Nov 17 '20 at 07:23

1 Answers1

1

If you want to get the workspace folder from which the currently active document is, use this:

vscode.commands.registerCommand('extension.search', () => {

  if (vscode.window.activeTextEditor) {

    let workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri);
    let rootUri = workspaceFolder.uri;
  }
}

The workspace.getWorkspaceFolder(...) function finds the workspace folder, where the document belongs.

Jan Dolejsi
  • 1,389
  • 13
  • 25