Guillaume Greffe
Guillaume Greffe

Reputation: 11

Visual Studio Code import library references for Powershell extension

I have Powershell files in the same folder with a number of helper functions. Unfortunately the Visual Studio Code editor does not recognize those functions when using intellisense. How do I import reference libraries and get VSCode to search those files ?

Upvotes: 1

Views: 4070

Answers (1)

user847990
user847990

Reputation:

If you want the code available to intellisense you have to load it into one of two places, either the PowerShell Integrated Terminal or your editor. In your case though, if you need it loaded all the time I would simply dot source those files in the VS Code PS Profile. You can create this simply by going to the PS Integrated Terminal and using New-Item $PROFILE -ItemType File -Force and it will create a Microsoft.VSCode_profile.ps1. Anything you add in that file will get loaded into the editor and PS Integrated Terminal upload startup.

More details on the whole engine and how intellisense works in VS Code...

The reason this works is that the main engine in the PowerShell extension is the PowerShellEditorService. This is the engine that creates the host behind the PS Integrated Terminal, and the PS features you have in the editor itself (e.g intellisense, script analyzer, etc.).

You can see an example of this by adding Import-Module somemodule to a new file in VS Code that has not been loaded into the PS Terminal. It can take a few seconds but after the engine loads the module you will have intellisense for that module. Just like you will your code.

enter image description here

Prior to adding the commands in that editor I verified that the module was not loaded. After adding the import command and then using a command from that module the module gets loaded in the background. You can then see the module was loaded:

enter image description here

Upvotes: 3

Related Questions