vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

Extension crashes when formating

Open happywino opened this issue 7 years ago • 9 comments

Issue Description

Whenever I try to format code, either from the context menu or via Ctrl-K Ctrl-F, the extension crashes, even on a file only containing "Import-Module ActiveDirectory". The log contains an entry "Exception: PSScriptAnalyzer cannot be imported, AnalysisService will be disabled", should I be installing that or is it expected to be installed by the extension?

Attached Logs

EditorServices.log vscode-powershell.log

Environment Information

Visual Studio Code

Name Version
Operating System Windows_NT x64 6.3.9600
VSCode 1.35.1
PowerShell Extension Version 2019.5.0

PowerShell Information

Name Value
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 6.3.9600.19170
PSCompatibleVersions 1.0 2.0 3.0 4.0
PSRemotingProtocolVersion 2.2

Visual Studio Code Extensions

Visual Studio Code Extensions(Click to Expand)
Extension Author Version
gitlens eamodio 9.8.2
powershell ms-vscode 2019.5.0

happywino avatar Jun 17 '19 19:06 happywino

Can you report the output of the following in the Integrated Console:

  • Import-Module PSScriptAnalyzer -PassThru
  • gmo -list PSScriptAnalyzer
  • gci ~\.vscode\extensions\ms-vscode.powershell-2019.5.0\modules
  • $env:PSModulePath

SydneyhSmith avatar Jun 18 '19 21:06 SydneyhSmith

PS C:\Users\ad_jcphilli> Import-Module PSScriptAnalyzer -PassThru
Import-Module : The specified module 'PSScriptAnalyzer' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-Module PSScriptAnalyzer -PassThru
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (PSScriptAnalyzer:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
 
PS C:\Users\ad_jcphilli> gmo -list PSScriptAnalyzer
PS C:\Users\ad_jcphilli> gci ~\.vscode\extensions\ms-vscode.powershell-2019.5.0\modules


    Directory: C:\Users\ad_jcphilli\.vscode\extensions\ms-vscode.powershell-2019.5.0\modules


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         6/17/2019   9:32 AM            docs
d----         6/17/2019   9:32 AM            Plaster
d----         6/17/2019   9:32 AM            PowerShellEditorServices
d----         6/17/2019   9:32 AM            PowerShellEditorServices.VSCode
d----         6/17/2019   9:32 AM            PSScriptAnalyzer
-a---         6/17/2019   9:32 AM        370 README.md


PS C:\Users\ad_jcphilli> $env:PSModulePath
C:\Users\ad_jcphilli\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\Microsoft Monitoring Agent\Agent\PowerShell\;C:\Program Files (x86)\Microsoft SQL Server\120\Tool
s\PowerShell\Modules\;C:\Program Files\Common Files\Skype for Business Server 2015\Modules\;C:\Users\ad_jcphilli\.vscode\extensions\ms-vscode.powershell-2019.5.0\modules
PS C:\Users\ad_jcphilli>

happywino avatar Jun 18 '19 22:06 happywino

Hmmm that's odd; the shipped PSScriptAnalyzer seems to be there, and that directory is on the path, but it's not available for some reason.

Maybe try:

Import-Module C:\Users\ad_jcphilli\.vscode\extensions\ms-vscode.powershell-2019.5.0\modules\PSScriptAnalyzer -PassThru

And let us know what the output is

rjmholt avatar Jun 18 '19 23:06 rjmholt

PS C:\Users\ad_jcphilli> Import-Module C:\Users\ad_jcphilli.vscode\extensions\ms-vscode.powershell-2019.5.0\modules\PSScriptAnalyzer -PassThru Import-Module : The specified module 'C:\Users\ad_jcphilli.vscode\extensions\ms-vscode.powershell-2019.5.0\modules\PSScriptAnalyzer' was not loaded because no valid module file was found in any module directory. At line:1 char:1

  • Import-Module C:\Users\ad_jcphilli.vscode\extensions\ms-vscode.powershell-2019. ...
  •   + CategoryInfo          : ResourceUnavailable: (C:\Users\ad_jcp...SScriptAnalyzer:String) [Import-Module], FileNotFoundException
      + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
    
    

The contents of "C:\Users\ad_jcphilli.vscode\extensions\ms-vscode.powershell-2019.5.0\modules\PSScriptAnalyzer" is a single folder named "1.18.0", is that expected?

happywino avatar Jun 20 '19 00:06 happywino

is a single folder named "1.18.0", is that expected?

Yes. This is expected. This is the structure of how PowerShellGet installs modules:

...
/ModuleName/VersionNumber/MyModule.psd1
...

However... @rjmholt and I learned that support for this version number part didn't come until v5.0... and since you're on v4, it doesn't work.

We will need to restructure our build logic here to move the contents of the VersionNumber folder under the ModuleName folder. That way the new layout will look like:

...
ModuleName/ModuleName.psd1
...

If you have any interest in contributing to the project, this change would be pure PowerShell and pretty straightforward. Otherwise we will get to it as soon as we can!

TylerLeonhardt avatar Jun 20 '19 05:06 TylerLeonhardt

Just so I've written in down for myself, this change only needs to go in the legacy/1.x branch

TylerLeonhardt avatar Jun 20 '19 05:06 TylerLeonhardt

I'd like to pick this one up :)

ghvanderweg avatar Jun 20 '19 06:06 ghvanderweg

Awesome! Let me know if anything is not clear.

I should note that this fix will require to you verify your fix on PowerShell v4 if you can... Since this fix is for PowerShell v4 and v3 as well, you should work off of the legacy/1.x branch instead of the master branch.

When you clone both vscode-powershell and PowerShellEditorServices make sure you run:

git checkout legacy/1.x

in each folder to work off of that code.

Success here is: "I can format my scripts when using PowerShell v4 with the PowerShell extension."

TylerLeonhardt avatar Jun 20 '19 06:06 TylerLeonhardt

Thanks for the clear instructions! Verifying on PowerShell v3 and v4 is not a problem, I can spin up VM's for that.

ghvanderweg avatar Jun 20 '19 06:06 ghvanderweg