PowerShell / vscode-powershell

Provides PowerShell language and debugging support for Visual Studio Code
https://marketplace.visualstudio.com/items/ms-vscode.PowerShell
MIT License
1.72k stars 495 forks source link

VMware.PowerCLI "Import-Module : The specified mount name 'vis' is already in use." #1383

Closed ili101 closed 6 years ago

ili101 commented 6 years ago

Issue Description

If you have the command "Get-VM" in your editor and you run "Import-Module VMware.PowerCLI" you get the error "Import-Module : The specified mount name 'vis' is already in use."

What is the Powershell extension doing when "Get-VM" is in the editor that brake the module import? is it partially loading something?

Steps to Reproduce

  1. Open Visual Studio Code.
  2. File > New File.
  3. Paste code:
    Import-Module VMware.PowerCLI
    if ($false)
    {
    Get-VM 
    }
  4. Run (F5) or just run "Import-Module VMware.PowerCLI" in console
    • Can be reproduced again by killing the terminal and restarting it.

Environment Information

Visual Studio Code

Name Version
Operating System Windows_NT x64 10.0.17134
VSCode 1.24.1
PowerShell Extension Version 1.7.0

PowerShell Information

Name Value
PSVersion 5.1.17134.81
PSEdition Desktop
PSCompatibleVersions 1.0 2.0 3.0 4.0 5.0 5.1.17134.81
BuildVersion 10.0.17134.81
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Visual Studio Code Extensions

Visual Studio Code Extensions(Click to Expand) |Extension|Author|Version| |---|---|---| |Bookmarks|alefragnani|0.19.1| |bracket-pair-colorizer|CoenraadS|1.0.58| |code-settings-sync|Shan|2.9.2| |githistory|donjayamanne|0.4.1| |gitlens|eamodio|8.4.1| |PowerShell|ms-vscode|1.7.0| |rest-client|humao|0.18.4| |snippet-creator|nikitaKunevich|0.0.4| |vscode-auto-scroll|pejmannikram|1.0.2| |vscode-icons|robertohuertasm|7.24.0| |vscode-postgres|ckolkman|1.0.9|;
dsolodow commented 6 years ago

This was reported as #1041 before; was closed as an issue with the module. What version of PowerCLI are you running?

ili101 commented 6 years ago

VMware.PowerCLI Version : 10.1.1.8827524 I can't replicate the problem mentioned in #1041 so I don't think its identical

rjmholt commented 6 years ago

@ili101 Would you be able to try running it in the ISE, Visual Studio or another PowerShell IDE and report back? We just need to know if VSCode/the extension is causing this, or if this is a behaviour caused by the module itself.

ili101 commented 6 years ago

Tested it in ISE, VSCode without Powershell extension and PowerShell Studio 2018 diden't get the error.

rjmholt commented 6 years ago

Thanks for that! I suspect it might be PSScriptAnalyzer thing, possibly importing the module in the background and creating the provider. I'll take a look, but given the specificity of the issue, it might require a community member with more familiarity with the VMWare.PowerCLI module (hence "Up-for-Grabs").

TylerLeonhardt commented 6 years ago

Yeah, do you get that same error if you import the same module twice in a regular PowerShell window?

PS > Import-Module VMware.PowerCLI
PS > Import-Module VMware.PowerCLI
ili101 commented 6 years ago

I don't get the error importing it twice in regular PowerShell or in vscode-powershell

TylerLeonhardt commented 6 years ago

Can you try one more thing for me?

Open two PowerShell windows... run this in each while both open.

PS > Import-Module VMware.PowerCLI

Do you get the error?

I just want to find some repo outside of VSCode if possible.

SeeminglyScience commented 6 years ago

Also if you don't mind try this as well

Import-Module VMware.PowerCLI
$ps = [powershell]::Create('NewRunspace').AddScript('Import-Module VMware.PowerCLI')
$ps.Invoke()
$ps.Streams.Error
$ps.InvocationStateInfo.Reason
ili101 commented 6 years ago

@SeeminglyScience Nice catch! PowerCLI do not support multiple runspace https://communities.vmware.com/thread/513253 But I still don't understand way writing "Get-VM" in the editor triggers this bug? Do vscode-powershell spins a new runspace and load the module in it?

SeeminglyScience commented 6 years ago

That was @rjmholt's catch :)

PSScriptAnalyzer (what creates syntax markers) runs in a separate runspace, and some rules cause modules to import. As a workaround you could disable script analysis by adding this to your workspace settings.

"powershell.scriptAnalysis.enable": false,

Ultimately the fix should come from VMWare, it seems like it's just an oversight. I'm not sure there is anything that can be done about it from this extension unfortunately.

rjmholt commented 6 years ago

Very theoretically it might be possible to make analysis happen in the same runspace? But there would be plenty of problems with that, and non-trivial work. I feel like for a workaround to an issue in a particular module, that's too high a price.

I tried looking through VMWare's scripts, but I'm guessing this part is done in C# -- it should be as simple as just checking if the drive already exists.

SeeminglyScience commented 6 years ago

I tried looking through VMWare's scripts, but I'm guessing this part is done in C# -- it should be as simple as just checking if the drive already exists.

If by drive you mean PSDrive then it won't exist as they are specific to the ExecutionContext. From the error message I'm guessing it's not really related to PSDrives but instead VMWare mounting to an ESX host/VCenter/etc. They probably need to store whatever handle they have for the mount statically so it can be pulled from another thread. If that's what you meant then ignore me 😉

rjmholt commented 6 years ago

Ah I didn't know any of that! Ok, that makes lots of sense. Thanks!

rjmholt commented 6 years ago

Closing this as a bug in the VMWare extension.