$pseditor.GetEditorContext().CurrentFile.GetTextLines() throws on Untitled (never saved) file #810
Description
System Details
Windows 10 CU
VSCode 1.12.2
Extension 1.2.0
PS 5.1.15063.296
Issue Description
This is the error I get in the PSIC:
PowerShell Integrated Console
~\Documents\WindowsPowerShell
1:74ms>
A terminating error occurred in PowerShell Editor Services:
One or more errors occurred.
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.WaitForExit()
at CallSite.Target(Closure , CallSite , Object )
One or more errors occurred.
Could not find file 'C:\Users\hillr\Documents\WindowsPowerShell\Untitled-1'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize,
FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at Microsoft.PowerShell.EditorServices.Workspace.GetFile(String filePath)
at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServerEditorOperations.ConvertClientEditorContext(ClientEditorContext clientCont
ext)
at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.HandleInvokeExtensionCommandRequest(InvokeExtensionCommandRequest command
Details, RequestContext`1 requestContext)
at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<>c__DisplayClass3_0`4.<SetRequestHandler>b__0(Message reques
tMessage, MessageWriter messageWriter)
at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<DispatchMessage>d__6.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__47.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)
at System.Threading.Tasks.Task.Execute()
It might actually be failing on accessing CurrentFile
.
Here are the contents of my Microsoft.VSCode_profile.ps1 file:
# Lame semicolon remover - doesn't understand here strings or comments
function Remove-TrailingSemicolon {
param(
[Parameter()]
[Microsoft.PowerShell.EditorServices.Extensions.EditorContext]
$editorContext = $psEditor.GetEditorContext()
)
$currentFile = $editorContext.CurrentFile;
$lines = $currentFile.GetTextLines();
for ($i = 0; $i -lt $lines.Count; $i++) {
$line = $lines[$i]
if ($line -match '^(.*?);(\s*)$') {
$startCol = $matches[1].Length + 1
$currentFile.InsertText(' ', $i+1, $startCol, $i+1, $startCol+1)
}
}
}
$params = @{
Name = 'KH.RemoveUnnecessarySemicolons'
DisplayName = 'Remove unnecessary semicolons'
Function = 'Remove-TrailingSemicolon'
SuppressOutput = $true
}
Register-EditorCommand @params
Attached Logs
5/31/2017 4:35:21 PM [VERBOSE] - Method "ReadMessage" at line 111 of C:\projects\powershelleditorservices\src\PowerShellEditorServices.Protocol\MessageProtocol\MessageReader.cs
READ MESSAGE:
{
"jsonrpc": "2.0",
"id": 17,
"method": "powerShell/invokeExtensionCommand",
"params": {
"name": "KH.RemoveUnnecessarySemicolons",
"context": {
"currentFilePath": "Untitled-1",
"cursorPosition": {
"line": 9,
"character": 0
},
"selectionRange": {
"start": {
"line": 9,
"character": 0
},
"end": {
"line": 9,
"character": 0
}
}
}
}
}
5/31/2017 4:35:21 PM [VERBOSE] - Method "ResolveFilePath" at line 316 of C:\projects\powershelleditorservices\src\PowerShellEditorServices\Workspace\Workspace.cs
Resolved path: C:\Users\hillr\Documents\WindowsPowerShell\Untitled-1
5/31/2017 4:35:21 PM [ERROR] - Method "OnListenTaskCompleted" at line 494 of C:\projects\powershelleditorservices\src\PowerShellEditorServices.Protocol\MessageProtocol\ProtocolEndpoint.cs
MessageDispatcher loop terminated due to unhandled exception:
System.AggregateException: One or more errors occurred. ---> System.IO.FileNotFoundException: Could not find file 'C:\Users\hillr\Documents\WindowsPowerShell\Untitled-1'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at Microsoft.PowerShell.EditorServices.Workspace.GetFile(String filePath)
at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServerEditorOperations.ConvertClientEditorContext(ClientEditorContext clientContext)
at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.HandleInvokeExtensionCommandRequest(InvokeExtensionCommandRequest commandDetails, RequestContext`1 requestContext)
at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<>c__DisplayClass3_0`4.<SetRequestHandler>b__0(Message requestMessage, MessageWriter messageWriter)
at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<DispatchMessage>d__6.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__47.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)
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.IO.FileNotFoundException: Could not find file 'C:\Users\hillr\Documents\WindowsPowerShell\Untitled-1'.
File name: 'C:\Users\hillr\Documents\WindowsPowerShell\Untitled-1'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at Microsoft.PowerShell.EditorServices.Workspace.GetFile(String filePath)
at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServerEditorOperations.ConvertClientEditorContext(ClientEditorContext clientContext)
at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.HandleInvokeExtensionCommandRequest(InvokeExtensionCommandRequest commandDetails, RequestContext`1 requestContext)
at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<>c__DisplayClass3_0`4.<SetRequestHandler>b__0(Message requestMessage, MessageWriter messageWriter)
at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<DispatchMessage>d__6.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__47.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)
at System.Threading.Tasks.Task.Execute()<---