Vscode-powershell: Untitled document changing language from powershell crashes extension

Created on 1 Jan 2019  ·  11Comments  ·  Source: PowerShell/vscode-powershell

Issue Description

When Untitled documents are set to PowerShell for a language, and then you change the language on one to another language (have tested with xml, sql, and batch) the PowerShell extension crashes.

When setting the default language to Plain Text, and then changing the file type to PowerShell and off of PowerShell it still crashes.

The one scenario where I can consistently get it to not crash:

  1. Launch vscode fresh
  2. before opening any files open the PowerShell Command Explorer
  3. Wait for it to load
  4. Open new untitled file
  5. Change the file language to PowerShell
  6. Change the file language to another language

I cannot fathom the reason for it not crashing here...

Attached Logs

1546304255-2ff2cd90-e2df-4afd-b137-6f33758ff4241546304149403.zip

Environment Information

Visual Studio Code

| Name | Version |
| --- | --- |
| Operating System | Windows_NT x64 10.0.17763 |
| VSCode | 1.31.0-insider|
| PowerShell Extension Version | 1.10.2 |

PowerShell Information

|Name|Value|
|---|---|
|PSVersion|5.1.17763.134|
|PSEdition|Desktop|
|PSCompatibleVersions|1.0 2.0 3.0 4.0 5.0 5.1.17763.134|
|BuildVersion|10.0.17763.134|
|CLRVersion|4.0.30319.42000|
|WSManStackVersion|3.0|
|PSRemotingProtocolVersion|2.3|
|SerializationVersion|1.1.0.1|

Visual Studio Code Extensions

Visual Studio Code Extensions(Click to Expand)

|Extension|Author|Version|
|---|---|---|
|bracket-pair-colorizer|CoenraadS|1.0.61|
|cpptools|ms-vscode|0.21.0-insiders|
|csharp|ms-vscode|1.17.1|
|debugger-for-chrome|msjsdiag|4.11.1|
|EditorConfig|EditorConfig|0.12.6|
|gc-excelviewer|GrapeCity|2.1.26|
|githistory|donjayamanne|0.4.4|
|gitignore|michelemelluso|1.0.1|
|gitlens|eamodio|9.2.4|
|indent-rainbow|oderwat|7.2.4|
|PowerShell|ms-vscode|1.10.2|
|theme-cobalt2|wesbos|2.1.6|
|theme-solarized-minimal|jibsen|1.2.1|
|tslint|eg2|1.0.42|
|vsc-material-theme|Equinusocio|2.6.3|
|vscode-markdownlint|DavidAnson|0.22.0|
|vscode-npm-script|eg2|0.3.5|
|vscode-react-native|vsmobile|0.7.0|
|vsliveshare|ms-vsliveshare|0.3.1071|
|xml|DotJoshJohnson|2.4.0|;

Area-Integrated Console Issue-Bug

All 11 comments

🤦‍♂️ Apparently I might have uploaded a file where there was no issue happening....

Which appears to be because when you tell it to reload you get a new folder. I've reproduced with an empty logs folder and attached the correct logs to this comment:

1546305476-d962364b-cb9d-44f6-bb72-9c6ca4561af21546305468134.zip

This is the pertinent part of the log attached above:

2018-12-31 17:18:13.360 [VERBOSE] tid:22 in 'ReadMessage' C:\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\MessageReader.cs: line 138
    Received Request 'textDocument/foldingRange' with id 7

    {
      "jsonrpc": "2.0",
      "id": 7,
      "method": "textDocument/foldingRange",
      "params": {
        "textDocument": {
          "uri": "untitled:Untitled-1"
        }
      }
    }

2018-12-31 17:18:13.360 [VERBOSE] tid:22 in 'ResolveFilePath' C:\PowerShellEditorServices\src\PowerShellEditorServices\Workspace\Workspace.cs: line 471
    Resolved path: untitled:Untitled-1

2018-12-31 17:18:13.370 [ERROR] tid:13 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.NotSupportedException: The given path's format is not supported.
       at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
       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.LanguageServer.Fold(String documentUri)
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.<HandleFoldingRangeRequestAsync>d__60.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.NotSupportedException: The given path's format is not supported.
       at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
       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.LanguageServer.Fold(String documentUri)
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.<HandleFoldingRangeRequestAsync>d__60.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()<---

My repro has more debug info (debug build):

    ProtocolEndpoint message loop terminated due to unhandled exception:

    System.AggregateException: One or more errors occurred. (Could not find file 'C:\Users\Keith\GitHub\PowerShell\Plaster\untitled:Untitled-2'.) ---> System.IO.FileNotFoundException: Could not find file 'C:\Users\Keith\GitHub\PowerShell\Plaster\untitled:Untitled-2'.
       at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
       at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
       at Microsoft.PowerShell.EditorServices.Workspace.GetFile(String filePath) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices\Workspace\Workspace.cs:line 118
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.Fold(String documentUri) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\Server\LanguageServer.cs:line 1385
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.HandleFoldingRangeRequestAsync(FoldingRangeParams foldingParams, RequestContext`1 requestContext) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\Server\LanguageServer.cs:line 1338
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.DispatchMessage(Message messageToDispatch, MessageWriter messageWriter) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\MessageDispatcher.cs:line 160
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.ListenForMessages(CancellationToken cancellationToken) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\ProtocolEndpoint.cs:line 379
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices\Utility\AsyncContext.cs:line 49
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContextThread.<>c__DisplayClass4_0.<Run>b__0() in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices\Utility\AsyncContextThread.cs:line 66
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
       --- End of inner exception stack trace ---
    ---> (Inner Exception #0) System.IO.FileNotFoundException: Could not find file 'C:\Users\Keith\GitHub\PowerShell\Plaster\untitled:Untitled-2'.
    File name: 'C:\Users\Keith\GitHub\PowerShell\Plaster\untitled:Untitled-2'
       at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
       at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
       at Microsoft.PowerShell.EditorServices.Workspace.GetFile(String filePath) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices\Workspace\Workspace.cs:line 118
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.Fold(String documentUri) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\Server\LanguageServer.cs:line 1385
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.HandleFoldingRangeRequestAsync(FoldingRangeParams foldingParams, RequestContext`1 requestContext) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\Server\LanguageServer.cs:line 1338
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.DispatchMessage(Message messageToDispatch, MessageWriter messageWriter) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\MessageDispatcher.cs:line 160
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.ListenForMessages(CancellationToken cancellationToken) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\ProtocolEndpoint.cs:line 379
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger) in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices\Utility\AsyncContext.cs:line 49
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContextThread.<>c__DisplayClass4_0.<Run>b__0() in C:\Users\Keith\GitHub\rkeithhill\PowerShellEditorServices\src\PowerShellEditorServices\Utility\AsyncContextThread.cs:line 66
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)<---

So this is not necessarily a great API design but where-ever we use this GetFile() API we tend to verify the file is not an in-memory file first e.g.:

            if (ScriptFile.IsUntitledPath(scriptToLaunch))
            {
                ScriptFile untitledScript = _editorSession.Workspace.GetFile(scriptToLaunch);

                launchTask = _editorSession.PowerShellContext
                    .ExecuteScriptString(untitledScript.Contents, true, true);
            }

Clearly the API requires a better design. That said, for an untitled document there is no file so GetFile() can't return something that doesn't exist. We could A) throw an ArgumentException if it is passed a filePath that is not a valid file-system path of B) return null if we can't get a file for the specified path.

Sorry for the unintended close. Getting a little sloppy with the mouse.

BTW we have both ScriptFile.IsUntitledPath() and Workspace.IsInMemory(). It seems like the second one would be "more encompassing" and correct.

Also, the repro is a little weird. I can create an untitled doc, put in this text:

function foo() {

}

switch the lang to powershell and you will note that the PowerShell session menu doesn't appear in the status bar. But if I switch to a PS1 file and then back to this untitled doc, the session menu remains and the crash happens. So, seems we have another bug with the session menu not appearing when the language mode is changed to PowerShell on a non ps1/psm1/psd1 file.

This is shown as fixed in the last release notes but while testing it in v1.11.0 and v2.0.0-preview.1 the system still crashes for me.
can you verify that it's not just me and if so why the fix not working or was not actually included in the releases?

The fix was included in the latest release--it would really helpful to determine why the fix didn't solve your issue if you could provide logs. Instructions for how to do that are here
Thanks for reporting this!

  1. Launch VSCode fresh.
  2. Open new untitled file.
  3. Change the file language to PowerShell.
  4. Wait for it to load.
  5. Change the file language to another language.
    ps
    vscode-powershell.log
    EditorServices.log

Extension version: 1.11.0
VS Code version: Code 1.30.2 (61122f88f0bf01e2ac16bdb9e1bc4571755f5bd8, 2019-01-07T22:54:13.295Z)
OS version: Windows_NT x64 10.0.16299

Bah, it looks like the untitled scheme path is slipping through to the .NET apis within TryGetFile(). I'll take a look at this tonight:

    ---> (Inner Exception #0) System.NotSupportedException: The given path's format is not supported.
       at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
       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.Workspace.TryGetFile(String filePath, ScriptFile& scriptFile)
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.Fold(String documentUri)
       at Microsoft.PowerShell.EditorServices.Protocol.Server.LanguageServer.<HandleFoldingRangeRequestAsync>d__60.MoveNext()

The fix will be available in the next release 1.12 due sometime next week - hopefully.

Was this page helpful?
0 / 5 - 0 ratings