Skip to content

$pseditor.GetEditorContext().CurrentFile.GetTextLines() throws on Untitled (never saved) file #810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rkeithhill opened this issue May 31, 2017 · 0 comments · Fixed by #811 or #1580
Labels
Milestone

Comments

@rkeithhill
Copy link
Contributor

rkeithhill commented May 31, 2017

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()<---
@daviwil daviwil added Area-psEditor Issue-Bug A bug to squash. labels May 31, 2017
@daviwil daviwil added this to the 1.2.1 milestone May 31, 2017
daviwil added a commit to daviwil/vscode-powershell that referenced this issue May 31, 2017
This change fixes an issue which causes the language server to crash
when running a registered $psEditor command from within an untitled
file.  This happens because the workspace code in the language server
expects to receive Uri paths instead of local file paths when referring
to an editor buffer.  The fix is to send the untitled file's Uri instead
of its non-existent file path.

Resolves PowerShell#810.
Resolves PowerShell/PowerShellEditorServices PowerShell#434.
daviwil added a commit that referenced this issue May 31, 2017
This change fixes an issue which causes the language server to crash
when running a registered $psEditor command from within an untitled
file.  This happens because the workspace code in the language server
expects to receive Uri paths instead of local file paths when referring
to an editor buffer.  The fix is to send the untitled file's Uri instead
of its non-existent file path.

Resolves #810.
Resolves PowerShell/PowerShellEditorServices #434.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants