如何修复VSCode半屏窗口模式下运行代码崩溃报错的问题
问题现象
- 把VSCode调整为半屏窗口模式(效果参考:
),打算在另一半屏幕处理其他工作时,运行C++文件会触发报错(报错截图:
),运行Python文件也会出现完全相同的报错(报错截图:
) - 已经尝试过通过命令
code --disable-extensions --max-memory=12288mb启动VSCode排查,问题没有解决 - 故障仅在半屏模式下触发,VSCode全屏运行时一切正常
原始报错日志
第一份报错:
Last 200 Keys: - - s t d o u t = M i c r o s o f t - M I E n g i n e - O u t - c b g l g b d j . l 0 l ' Space ' - - s t d e r r = M i c r o s o f t - M I E n g i n e - E r r o r - 3 u m y i n v f . m w i ' Space ' - - p i d = M i c r o s o f t - M I E n g i n e - P i d - 0 b r z o z y 0 . q 0 f ' Space ' - - d b g E x e = C : \ m s y s 6 4 \ m i n g w 6 4 \ b i n \ g d b . e x e ' Space ' - - i n t e r p r e t e r = m i ' Space Enter Exception: System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension. Parameter name: left Actual value was -1. at System.Console.SetCursorPosition(Int32 left, Int32 top) at Microsoft.PowerShell.Internal.VirtualTerminal.set_CursorLeft(Int32 value) at Microsoft.PowerShell.PSConsoleReadLine.ReallyRender(RenderData renderData, String defaultColor) at Microsoft.PowerShell.PSConsoleReadLine.ForceRender() at Microsoft.PowerShell.PSConsoleReadLine.Insert(Char c) at Microsoft.PowerShell.PSConsoleReadLine.SelfInsert(Nullable`1 key, Object arg) at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(ConsoleKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg) at Microsoft.PowerShell.PSConsoleReadLine.InputLoop() at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)
第二份报错:
Last 6 Keys: & Space C : / U Exception: System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension. Parameter name: left Actual value was -1. at System.Console.SetCursorPosition(Int32 left, Int32 top) at Microsoft.PowerShell.Internal.VirtualTerminal.set_CursorLeft(Int32 value) at Microsoft.PowerShell.PSConsoleReadLine.ReallyRender(RenderData renderData, String defaultColor) at Microsoft.PowerShell.PSConsoleReadLine.ForceRender() at Microsoft.PowerShell.PSConsoleReadLine.Insert(Char c) at Microsoft.PowerShell.PSConsoleReadLine.SelfInsert(Nullable`1 key, Object arg) at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(ConsoleKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg) at Microsoft.PowerShell.PSConsoleReadLine.InputLoop() at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)
故障原因
这个问题和VSCode内存占用、扩展冲突、C++/Python运行环境本身都没有关系,是Windows自带PowerShell的PSReadLine组件的已知bug:当VSCode半屏时,如果集成终端宽度被压缩到过小,PSReadLine计算光标渲染位置时会算出值为-1的非法横坐标,触发参数越界异常,不管运行什么代码只要终端宽度触发阈值就会报错。
解决方法
按顺序尝试以下方案即可修复:
- 优先直接更换VSCode集成终端的默认shell:打开VSCode设置,搜索
terminal.integrated.defaultProfile.windows,下拉选择Command Prompt或者本地已安装的Git Bash,重启VSCode后生效,完全绕开PowerShell的bug - 如果需要保留PowerShell作为默认终端,以管理员身份打开系统PowerShell,执行命令
Install-Module PSReadLine -Force -AllowClobber升级PSReadLine到最新版本,执行完成后重启所有PowerShell窗口和VSCode - 临时规避方案:半屏使用VSCode时,不要把终端面板放在侧边,改回底部停靠,保证终端面板宽度不被压缩得过窄,就不会触发光标位置计算错误
- 如果上面方法都无效,打开VSCode设置搜索
terminal.integrated.gpuAcceleration,将值改为off,重启VSCode即可
内容的提问来源于stack exchange,提问作者Jacob Creutzfeldt
相关产品推荐
相关产品推荐

