Visual Studio Code更新后内置端口转发致Perl扩展报错求助
解决VSCode更新后Perl扩展调试失败的问题
问题说明
VSCode完成最新更新后,Perl扩展调试运行失败,报错信息如下:
""C:\StrawberryPerl\perl\bin\perl.exe" "-d" "X:\Perl\a.pl"" Unable to connect to remote host: localhost:57937 Compilation failed in require. at X:\Perl\a.pl line 0. main::BEGIN() called at X:\Perl\a.pl line 0 eval {...} called at X:\Perl\a.pl line 0 BEGIN failed--compilation aborted. at X:\Perl\a.pl line 0.
问题根源是VSCode新增的**内置端口转发(Built-in port forwarding)**功能,Perl扩展默认使用5000端口,但当前调试配置未显式指定该端口,现有配置文件如下:
{ "launch": { "version": "0.2.0", "configurations": [ { "type": "perl", "request": "launch", "name": "Perl-Debug local", "console": "externalTerminal", "program": "${file}", "exec": "C:\\StrawberryPerl\\perl\\bin\\perl.exe", "execArgs": [], "root": "${workspaceRoot}/", "inc": [], "args": [], "env": {}, "debugRaw": false, "debugLog": false, "stopOnEntry": false, "sessions": "single" } ], "compounds": [] }, "terminal.external.windowsExec": "X:\\Perl\\vsc_cmd.cmd", "workbench.editor.tabSizing": "shrink", "editor.accessibilitySupport": "off", }
可行解决方案
方案1:显式指定调试端口
在Perl调试配置中添加"port": 5000字段,让扩展明确使用固定端口,避免与内置端口转发冲突。修改后的配置片段示例:
{ "type": "perl", "request": "launch", "name": "Perl-Debug local", "console": "externalTerminal", "program": "${file}", "exec": "C:\\StrawberryPerl\\perl\\bin\\perl.exe", "port": 5000, // 新增端口指定 "execArgs": [], // 其余原有配置保持不变 }
方案2:关闭VSCode内置端口转发功能
若无需使用内置端口转发,可通过以下步骤禁用:
- 打开VSCode设置(快捷键
Ctrl+,) - 搜索
remote.autoForwardPorts,将其设置为false,彻底关闭自动端口转发 - 额外可搜索
remote.forwardingLocalPortHost,将值设置为"localhost",确保本地端口转发逻辑不影响调试连接
方案3:更新Perl扩展
前往VSCode扩展市场搜索Perl扩展,检查是否有适配最新VSCode版本的更新包,更新后可能已修复端口冲突问题。
验证方法
修改配置或关闭功能后,重启VSCode,重新运行Perl调试任务,确认是否仍出现连接失败报错。
内容的提问来源于stack exchange,提问作者Codr
相关产品推荐
相关产品推荐

