31

I am writing a plugin for VS Code and I need to know the path of the file which is calling the extension, either if it was called from the editor context menu or explorer context menu or the user simply typed the extension command.

function activate(context){
    // get full path of the file somehow
}

Thanks in advance!

Mark
  • 143,421
  • 24
  • 428
  • 436
JM. Benitez
  • 420
  • 1
  • 4
  • 9
  • So you want to know the currently active file once the extension is activated - just wanna make sure I understand the question correctly as this requirement would be somewhat unusual; instead one usually wants to know it when a specific command is run. – DAXaholic Sep 25 '16 at 09:37

5 Answers5

36

If you need the File use uri.fsPath If you need the Workspace Folder use uri.path

if(vscode.workspace.workspaceFolders !== undefined) {
    let wf = vscode.workspace.workspaceFolders[0].uri.path ;
    let f = vscode.workspace.workspaceFolders[0].uri.fsPath ; 

    message = `YOUR-EXTENSION: folder: ${wf} - ${f}` ;

    vscode.window.showInformationMessage(message);
} 
else {
    message = "YOUR-EXTENSION: Working folder not found, open a folder an try again" ;

    vscode.window.showErrorMessage(message);
}

More detail can get from VS Code API

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
Bingoabs
  • 539
  • 5
  • 11
  • 5
    rootPath is deprecated. I think you can get path using: vscode.workspace.workspaceFolders[0].uri.fsPath – bkelley Sep 25 '19 at 18:11
  • When I tried this, workspaceFolders[0] always returned the first folder in the list, not necessarily the currently opened one. – thehamzarocks Jan 07 '23 at 19:45
21

You can invoke the vscode window property to retrieve the file path or name depending on what you are looking for. This will give you the name of the file open in the current Tab when you execute the command. I don't know how it works if called from the explorer context.

var vscode = require("vscode");
var path = require("path");
function activate(context) {
   var currentlyOpenTabfilePath = vscode.window.activeTextEditor.document.fileName;
   var currentlyOpenTabfileName = path.basename(currentlyOpenTabfilePath);
   //...
}
Federico
  • 1,925
  • 14
  • 19
Mehdi
  • 2,263
  • 1
  • 18
  • 26
  • 1
    Please note that `fileName` for a new, untitled buffer is simply untitled, so don't assume that it'll contain a proper path... – wildeyes Apr 18 '17 at 11:35
  • 1
    Also, this only seems to apply to text editors, not e.g. image viewers. – JW. Dec 14 '18 at 21:52
7
import * as vscode from "vscode";
import * as fs from "fs";   

var currentlyOpenTabfilePath = vscode.window.activeTextEditor?.document.uri.fsPath;

The above code is used to find the the path of file that is currently activated on vscode.

vscode.window.activeTextEditor gets editor's reference and document.uri.fsPath returns the path to that file in string format

matthias_h
  • 11,356
  • 9
  • 22
  • 40
Faisal Iqbal
  • 71
  • 1
  • 4
6

Here are examples of various paths returned by vscode in windows:

Extension path:

vscode.extensions.getExtension('extension.id').extensionUri.path
> /c:/Users/name/GitHub/extensionFolder 
vscode.extensions.getExtension('extension.id').extensionUri.fsPath
> c:\Users\name\GitHub\extensionFolder

Current folder:

vscode.workspace.workspaceFolders[0].uri.path
> /c:/Users/name/Documents/Notes 
vscode.workspace.workspaceFolders[0].uri.fsPath
> c:\Users\name\Documents\Notes 

Current editor file:

vscode.window.activeTextEditor.document.uri.path
> /c:/Users/name/Documents/Notes/temp.md 
vscode.window.activeTextEditor.document.uri.fsPath
> c:\Users\name\Documents\Notes\temp.md 

Note that path and fsPath refer to the same folder. fsPath provides the path in the form appropriate for the os.

marzetti
  • 369
  • 4
  • 8
0

If you need the path to the current extension, use context object. This will work both in debug mode and production mode:

export async function activate(context: ExtensionContext) {
  console.log(context.extension);
  console.log(context.extensionPath);
  console.log(context.extensionUri);
}

If you are going to call another extension:

extensions.getExtension('extensionId');

In debug mode, this returns undefined for current extension since it is not installed.

snnsnn
  • 10,486
  • 4
  • 39
  • 44