Vscode-powershell: Using Code Insiders 1.21.0 - Terminal crashes when formatting script with The term 'Invoke-Formatter' is not recognized as the name of a cmdlet

Created on 9 Feb 2018  路  6Comments  路  Source: PowerShell/vscode-powershell

System Details

09/02/2018 10:51:31 [NORMAL] - Method "StartLogging" at line 144 of C:\projects\powershelleditorservices\src\PowerShellEditorServices.Host\EditorServicesHost.cs

PowerShell Editor Services Host v1.5.1.0 starting (pid 7776)...

  Host application details:

    Name:      Visual Studio Code Host
    ProfileId: Microsoft.VSCode
    Version:   1.5.1
    Arch:      64-bit

  Operating system details:

    Version: Microsoft Windows NT 10.0.16299.0
    Arch:    64-bit

Name Value
---- -----
PSVersion 5.1.16299.98
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.98
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

DavidAnson.[email protected]
eamodio.[email protected]
formulahendry.[email protected]
gerane.[email protected]
ms-mssql.[email protected]
ms-vscode.[email protected]
ms-vscode.[email protected]
ms-vscode.[email protected]
ms-vsts.[email protected]
msazurermtools.[email protected]
PeterJausovec.[email protected]
PKief.[email protected]
Shan.[email protected]

Insiders version 1.21 - updated to latest

Issue Description

Crashes when formatting PowerShell script whether saved or not whether 3 lines or 600 lines

Attached Logs

09/02/2018 10:51:53 [VERBOSE] - Method "ExecuteCommand" at line 551 of C:\projects\powershelleditorservices\src\PowerShellEditorServices\Session\PowerShellContext.cs

Execution completed successfully.

09/02/2018 10:52:02 [VERBOSE] - Method "ReadMessage" at line 123 of C:\projects\powershelleditorservices\src\PowerShellEditorServices.Protocol\MessageProtocol\MessageReader.cs

Received Request 'textDocument/rangeFormatting' with id 5

09/02/2018 10:52:02 [VERBOSE] - Method "ResolveFilePath" at line 366 of C:\projects\powershelleditorservices\src\PowerShellEditorServices\Workspace\Workspace.cs

Resolved path: c:\Users\REDACTED

09/02/2018 10:52:02 [ERROR] - Method "OnListenTaskCompleted" at line 391 of C:\projects\powershelleditorservices\src\PowerShellEditorServices.Protocol\MessageProtocol\ProtocolEndpoint.cs

ProtocolEndpoint message loop terminated due to unhandled exception:

System.AggregateException: One or more errors occurred. ---> System.Management.Automation.CommandNotFoundException: The term 'Invoke-Formatter' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.AnalysisService.InvokePowerShell(String command, IDictionary`2 paramArgMap)
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.AnalysisService.<InvokePowerShellAsync>d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.AnalysisService.<Format>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.<Format>d__52.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.<HandleDocumentRangeFormattingRequest>d__50.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<DispatchMessage>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.<ListenForMessages>d__36.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger)
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.Management.Automation.CommandNotFoundException: The term 'Invoke-Formatter' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.AnalysisService.InvokePowerShell(String command, IDictionary`2 paramArgMap)
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.AnalysisService.<InvokePowerShellAsync>d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.AnalysisService.<Format>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.<Format>d__52.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.<HandleDocumentRangeFormattingRequest>d__50.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<DispatchMessage>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.<ListenForMessages>d__36.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger)
   at System.Threading.Tasks.Task.Execute()<---

09/02/2018 10:52:02 [ERROR] - Method "ProtocolEndpoint_UnhandledException" at line 418 of C:\projects\powershelleditorservices\src\PowerShellEditorServices.Host\EditorServicesHost.cs

PowerShell Editor Services is terminating due to an unhandled exception, see previous logs for details.

All 6 comments

In this case, the crash is telling us the extension can't find the PSScriptAnalyzer module (or it finds one without the Invoke-Formatting command). Look in your log file for a few lines that should look like this:

2/9/2018 9:43:50 AM [NORMAL] - Method "FindPSScriptAnalyzerModule" at line 354 of C:\projects\powershelleditorservices\src\PowerShellEditorServices\Analysis\AnalysisService.cs

    PSScriptAnalyzer found at C:\Users\Keith\.vscode\extensions\ms-vscode.powershell-1.5.1\modules\PSScriptAnalyzer\PSScriptAnalyzer.psd1

2/9/2018 9:43:50 AM [VERBOSE] - Method "EnumeratePSScriptAnalyzerCmdlets" at line 389 of C:\projects\powershelleditorservices\src\PowerShellEditorServices\Analysis\AnalysisService.cs

    The following cmdlets are available in the imported PSScriptAnalyzer module:
        Get-ScriptAnalyzerRule
        Invoke-Formatter
        Invoke-ScriptAnalyzer

What do you get when the extension tries to load this module? Where does it find the module and is there a valid module at that path?

It finds the module in my local user path

10/02/2018 09:11:01 [NORMAL] - Method "FindPSScriptAnalyzerModule" at line 354 of C:\projects\powershelleditorservices\src\PowerShellEditorServices\Analysis\AnalysisService.cs

PSScriptAnalyzer found at C:\Users\mrrob\OneDrive\Documents\WindowsPowerShell\Modules\PSScriptAnalyzer\1.10.0\PSScriptAnalyzer.psd1

10/02/2018 09:11:01 [VERBOSE] - Method "EnumeratePSScriptAnalyzerCmdlets" at line 389 of C:\projects\powershelleditorservices\src\PowerShellEditorServices\Analysis\AnalysisService.cs

The following cmdlets are available in the imported PSScriptAnalyzer module:
    Get-ScriptAnalyzerRule
    Invoke-ScriptAnalyzer

and doesnt have PSScriptAnalyzer in the vscode modules path

Directory: C:\Users\mrrob\.vscode-insiders\extensions\ms-vscode.powershell-1.5.1\modules

Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 16/11/2017 19:25 Plaster
d----- 16/11/2017 19:25 PowerShellEditorServices
d----- 16/11/2017 19:25 PowerShellEditorServices.VSCode
-a---- 16/11/2017 19:25 370 README.md

Copying the PSScriptAnalyzer module folder from the .vscode\extensions\ms-vscode.powershell-1.5.1\modules

fixed it

Thank you for your help @rkeithhill

I have a repro of the error:

2019-01-09 07:18:26.180 [ERROR] tid:104 in 'OnListenTaskCompleted' C:\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\ProtocolEndpoint.cs: line 391
    ProtocolEndpoint message loop terminated due to unhandled exception:

    System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object.
       at Microsoft.PowerShell.EditorServices.Workspace.RecursivelyFindReferences(ScriptFile scriptFile, Dictionary`2 referencedScriptFiles)
       at Microsoft.PowerShell.EditorServices.Workspace.ExpandScriptReferences(ScriptFile scriptFile)
       at Microsoft.PowerShell.EditorServices.LanguageService.<GetDefinitionOfSymbol>d__19.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.<HandleDefinitionRequest>d__42.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<DispatchMessage>d__7.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.<ListenForMessages>d__36.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger)
       at System.Threading.Tasks.Task.Execute()
       --- End of inner exception stack trace ---
    ---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
       at Microsoft.PowerShell.EditorServices.Workspace.RecursivelyFindReferences(ScriptFile scriptFile, Dictionary`2 referencedScriptFiles)
       at Microsoft.PowerShell.EditorServices.Workspace.ExpandScriptReferences(ScriptFile scriptFile)
       at Microsoft.PowerShell.EditorServices.LanguageService.<GetDefinitionOfSymbol>d__19.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.<HandleDefinitionRequest>d__42.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<DispatchMessage>d__7.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.<ListenForMessages>d__36.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger)
       at System.Threading.Tasks.Task.Execute()<---


2019-01-09 07:18:26.181 [ERROR] tid:104 in 'ProtocolEndpoint_UnhandledException' C:\PowerShellEditorServices\src\PowerShellEditorServices.Host\EditorServicesHost.cs: line 448
    PowerShell Editor Services is terminating due to an unhandled exception, see previous logs for details.

EditorServices.log

code -v
1.30.1
dea8705087adb1b5e5ae1d9123278e178656186a
ia32

code --list-extensions --show-versions
DotJoshJohnson.[email protected]
ms-azuretools.[email protected]
ms-vscode.[email protected]
ms-vscode.[email protected]
ms-vscode.[email protected]

$psEditor.EditorServicesVersion

Major Minor Build Revision


1 10 2 0

@craig-martin I believe this to be a different root cause. Can you open a new issue with the info above?

@rjmholt I wonder if this line:

https://github.com/PowerShell/PowerShellEditorServices/blob/4b39d013c4dde376b8811a80300465bc97c9866d/src/PowerShellEditorServices/Workspace/Workspace.cs#L436

Should be using TryGetFile() instead. GetFile() can return null and if that does, it is passed recursively to RecursivelyFindReferences() which dereferences it. I "think" that is where the NRE is coming from.

Done. Thanks!

1689

@rkeithhill Agreed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GQnzo picture GQnzo  路  3Comments

bgelens picture bgelens  路  3Comments

guidooliveira picture guidooliveira  路  3Comments

daviwil picture daviwil  路  4Comments

daviwil picture daviwil  路  3Comments