While I'm writing PowerShell code in Visual Studio Code on Mac, I notice that messages are randomly printed to the Integrated Terminal. Those messages typically indicate that a module is being imported, and sometimes my environment will change as a result (eg. posh-git changes my prompt) against my will.
While the rich Intellisense metadata is useful for development, we should investigate a method of providing this metadata while simultaneously preventing unwanted changes to the development environment. Ideas proposed by @daviwil and myself include:
CompletionResult objects, instead of importing moduleCheers,
Trevor Sullivan
posh-git should not change the prompt function if it detects the prompt function is not the PowerShell default prompt i.e. the use has customized it. You could workaround the prompt changing issue by creating a custom prompt function in the Microsoft.VSCode_profile.ps1 file. Although, it would be better if posh-git were never imported as this module is meant for interactive use and isn't something you'd ever script against.
BTW you should be able to turn off module autoloading with:
$PSModuleAutoloadingPreference = 'None'
You could try putting that in the profile until a setting is added for it.
Not a bad idea for a workaround, but I don't want to disable module auto-loading, as that would break virtually all of my code.
@lzybkr recommended using CommandInvocationIntrinsics.GetCommand instead of calling Get-Command directly, this approach won't cause the module to be loaded. I think I'll give that a shot first then try some other approaches if it doesn't work for our scenarios.
Is that more or less the equivalent of using Reflection-only loading of .NET assemblies?
I suppose, but we don't use reflection only loads of binary modules, so in that sense, definitely not.
Did some work on this but I think I want to polish it a bit more before I ship it.
Marking this as resolved-external as this comes from PowerShell's tab completion experience.
I'm sorry for commenting on such an old issue, I can open a new one if requested but I disagree with the conclusion here.
This is not a Powershell issue, if you open up ISE or a PS console with/without PSReadline it won't auto import modules when you invoke command completion. This only happens in VS code, specifically when you are hovering over a command in the intellisense menu.
It's pretty easy to demonstrate:
I think the problem is that VS code runs some additional code when you are hovering over an item in the intellisense menu to display command help. Powershell already provides the parameter sets as a tooltip if a module has been imported so the only thing VS code adds is the short synopsis for what the command does.
This feature needs to either be removed or made smarter so it only shows that help info if the module has already been imported, otherwise I don't see how intellisense is ever going to feel good in VS code for Powershell.
That seems reasonable @MartinGC94, I'll reopen this so you can close it again with your PR.
Most helpful comment
I'm sorry for commenting on such an old issue, I can open a new one if requested but I disagree with the conclusion here.
This is not a Powershell issue, if you open up ISE or a PS console with/without PSReadline it won't auto import modules when you invoke command completion. This only happens in VS code, specifically when you are hovering over a command in the intellisense menu.
It's pretty easy to demonstrate:
I think the problem is that VS code runs some additional code when you are hovering over an item in the intellisense menu to display command help. Powershell already provides the parameter sets as a tooltip if a module has been imported so the only thing VS code adds is the short synopsis for what the command does.
This feature needs to either be removed or made smarter so it only shows that help info if the module has already been imported, otherwise I don't see how intellisense is ever going to feel good in VS code for Powershell.