11

I am trying to get project folder path in visual studio code extension but very difficult to find the answer. My code is not working. I do not know it is possible or not. I have checked in google no one answer for this question. Anyone know about this please help me to find the answer.

enter image description here

extension.js:

var vscode = require("vscode");
var path = require("path");

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

   let findprojectfolderPath = vscode.workspace.uri.fsPath; // Not working

   console.log(findprojectfolderPath);

});
}
Apple Orange
  • 646
  • 5
  • 12
  • 27
  • https://github.com/microsoft/vscode/wiki/Adopting-Multi-Root-Workspace-APIs – rioV8 Sep 08 '19 at 16:56
  • Getting confusion – Apple Orange Sep 08 '19 at 17:29
  • getEditorInfo() this function not working in extension.js file. https://github.com/microsoft/vscode-extension-samples/blob/master/basic-multi-root-sample/src/extension.ts – Apple Orange Sep 08 '19 at 17:33
  • which line in `getEditorInfo()` is not working? – rioV8 Sep 08 '19 at 19:25
  • Possible duplicate of [How to get currently opened file's project folder path in visual studio code extension.js](https://stackoverflow.com/questions/57811209/how-to-get-currently-opened-files-project-folder-path-in-visual-studio-code-ext) – Scott McPeak Sep 08 '19 at 21:06

6 Answers6

9

I would like to update Pushprajsinh Chudasama's answer as vscode.workspace.rootPath is now deprecated. It is being replaced by vscode.workspace.workspaceFolders which returns an array of WorkspaceFolders | undefined. To retrieve the path to all your workspace folders, you may now use:

vscode.workspace.workspaceFolders?.map(folder => folder.uri.path)
Gaetan C.
  • 1,742
  • 13
  • 21
7

If you open the folder, then you can get the workspace variable.

let folderName = vscode.workspace.name; // get the open folder name
let folderPath = vscode.workspace.rootPath; // get the open folder path
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
3

If you want to find the path of the file currently is opened by the user, then you need this

console.log(vscode.window.activeTextEditor.document.uri.fsPath);

Read this documentation for information further: https://code.visualstudio.com/api/references/vscode-api#TextDocument

Marfin. F
  • 434
  • 4
  • 16
1

This function gets the workspace directory for the file currently open. If there is no active editor or the file doesn't belong to a workspace, it returns undefined.

import * as vscode from "vscode";

function getDocumentWorkspaceFolder(): string | undefined {
  const fileName = vscode.window.activeTextEditor?.document.fileName;
  return vscode.workspace.workspaceFolders
    ?.map((folder) => folder.uri.fsPath)
    .filter((fsPath) => fileName?.startsWith(fsPath))[0];
}
Xuan
  • 5,255
  • 1
  • 34
  • 30
1

As It turned out vscode.workspace.rootPath is deprecated and the only solution that I found so far is described in this answer

VS Code extension - get full path

0

You must change the code. Like the example below.

vscode.commands.registerCommand('extension.search', (p: { fsPath: string }) => {

   // p is an object whose path is fsPath.

   console.log(p.fsPath);

});

Maybe this link can help you.

Amir Kabir
  • 33
  • 5