VSCode Code Runner在 wsl.exe 终端下路径替换

2025-05-25 23:59:53

1、首先找到 Code Runner 插件的安装位置,下面提供几种快速到达的方式1. Win+R 后粘贴 %USERPROFILE%\.vscode\extensions\formulahendry.code-runner-0.9.132. 或在 cmd 中输入explorer.exe %USERPROFILE%\.vscode\extensions\formulahendry.code-runner-0.9.133. 或在 powershell 中输入explorer (Resolve-Path $HOME\.vscode\extensions\formulahendry.code-runner*)若为了简单修复,可直接进入要编辑的位置1.Win+R或cmd中输入: notepad.exe%USERPROFILE%\.vscode\extensions\formulahendry.code-runner-0.9.13\out\src\codeManager.js2.powershell中输入: notepad.exe (rvpa $HOME\.vscode\extensions\formulahendry.code-runner*\out\src\codeManager.js)打开 codeManager.js 文件Note:鉴于code runner版本会更新, 因此建议使用powershell的命令!!!

VSCode Code Runner在 wsl.exe 终端下路径替换
VSCode Code Runner在 wsl.exe 终端下路径替换

2、按 Ctrl+F 或 进入 编辑>查找,输入 "bash" (双引号不可省略)找到要输出给终端的代码段,找到图中所示的内容。可以看出这一部分只判断了bash和windows,并且下面的字符串替换方式正是我们需要的。注:即将 /bin/bash "D:\Files\test.sh" 替换为 /bin/bash "/mnt/d/Files/test.sh"

VSCode Code Runner在 wsl.exe 终端下路径替换
VSCode Code Runner在 wsl.exe 终端下路径替换

3、由Code Runner 插件的发送内容可以得出,我们只需要模仿他加上一个判断终端路径是否为 wsl.exe 的条件。具体操作:在上述代码段中添加如图所示的内容else if (windowsShell && windowsShell.toLowerCase().indexOf("wsl") > -1) { // 修复不能识别 wsl.exe 终端的问题 command = command.replace(/([A-Za-z]):/g, this.replacer).replace(/\\/g,"/")}最后重启 VSCode 运行一个 bash 脚本实验发现已经被冒号和正斜杆已经被替换了

VSCode Code Runner在 wsl.exe 终端下路径替换
VSCode Code Runner在 wsl.exe 终端下路径替换

4、最后简单说明一下思路通过搜索bash发现 codeManager.js 中发送的 command 字符串未做替换,借助扩展作者编写的代码,添加对 wsl.exe 终端的处理对接

VSCode Code Runner在 wsl.exe 终端下路径替换
VSCode Code Runner在 wsl.exe 终端下路径替换
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢