PowerShell / vscode-powershell

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

Extension crashes when formating #2027

Open happywino opened 5 years ago

happywino commented 5 years ago

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|
SydneyhSmith commented 5 years ago

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

happywino commented 5 years ago
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>
rjmholt commented 5 years ago

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

happywino commented 5 years ago

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

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?

TylerLeonhardt commented 5 years ago

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 commented 5 years ago

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

ghvanderweg commented 5 years ago

I'd like to pick this one up :)

TylerLeonhardt commented 5 years ago

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."

ghvanderweg commented 5 years ago

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