【茶包射手日記】PowerShell 命令列一按 $ 就噴錯
| | 2 | |
在 VSCode Terminal 執行 PowerShell 遇到奇怪問題。 [xml]$xml = Get-Content -Path sample.xml
將 XML 讀入 $xml 變數打算操作 $xml 解析內容,才按了 $ 就噴錯結束:
An error occurred while running the REPL loop:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Text.StringBuilder.get_Chars(Int32 index)
at Microsoft.PowerShell.PSConsoleReadLine.ConvertOffsetToPoint(Int32 offset)
at Microsoft.PowerShell.PSConsoleReadLine.ReallyRender(RenderData renderData, String defaultColor)
at Microsoft.PowerShell.PSConsoleReadLine.ForceRender()
at Microsoft.PowerShell.PSConsoleReadLine.Render()
at Microsoft.PowerShell.PSConsoleReadLine.Insert(String s)
at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken, Nullable`1 lastRunStatus)
at Microsoft.PowerShell.EditorServices.Services.PowerShell.Console.PsrlReadLine.InvokePSReadLine(CancellationToken cancellationToken) in C:\__w\1\s\src\PowerShellEditorServices\Services\PowerShell\Console\PsrlReadLine.cs:line 48
at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousDelegateTask`1.Run(CancellationToken cancellationToken) in C:\__w\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousDelegateTask.cs:line 63
at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteSynchronously(CancellationToken executorCancellationToken) in C:\__w\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 92
--- End of stack trace from previous location ---
at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.get_Result() in C:\__w\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 58
at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteAndGetResult(CancellationToken cancellationToken) in C:\__w\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 108
at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.InvokeDelegate[TResult](String representation, ExecutionOptions executionOptions, Func`2 func, CancellationToken cancellationToken) in C:\__w\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 526
at Microsoft.PowerShell.EditorServices.Services.PowerShell.Console.PsrlReadLine.ReadLine(CancellationToken cancellationToken) in C:\__w\1\s\src\PowerShellEditorServices\Services\PowerShell\Console\PsrlReadLine.cs:line 35
at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.InvokeReadLine(CancellationToken cancellationToken) in C:\__w\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 1064
at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.DoOneRepl(CancellationToken cancellationToken) in C:\__w\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 963
推測爆炸點在 PowerShell 的指令自動完成功能,按 $ 帶出先前輸入過的 $t = ....
內容,上回用 $t 裝了超長文字(大約一萬個字元),超出 REPL 某段程式長度上限,導致程式爆炸。
理論上將這筆超長指令從歷史清除就可了事,但 Clear-History 只會清除當前工作階段的歷史,爬文查到用 (Get-PSReadLineOption).HistorySavePath
可檢視指令歷史記錄檔路徑在 %APPDATA%\Microsoft\Windows\PowerShell\PSReadLine\Visual Studio Code Host_history.txt
,果然在檔案找到該筆記錄。
註1:留意輸入指令時會覆寫檔案 註2:不同 PowerShell 執行環境有自己的存檔,VSCode 的 PowerShell Extension 在 Visual Studio Code Host_history.txt
,一般指令視窗在 ConsoleHost_history.txt
刪掉過長文字指令再重啟 PowerShell Shell,總算排除一按 $ 就出錯的鬼問題。
In VSCode’s PowerShell terminal, encountering an error when using long input history can crash due to autocompletion. Deleting the problematic entry from the history file located in %APPDATA%\Microsoft\Windows\PowerShell\PSReadLine\Visual Studio Code Host_history.txt resolves the issue.
Comments
# by Timi
在等了快一個月之後終於又看到黑大文了
# by Jeffrey
To Tim, 寫作二十餘載,有鐵粉如斯,夫復何求?(遞上鐵粉勳章)