目录

Vscode-ssh远程开发PATH环境变量不能自动刷新的bug

目录

在使用 vscode 远程连到 Ubuntu 上开发 golang 时偶然遇到了一个奇怪的问题, vscode 提示找不到 go 的二进制文件, 然后列出了环境变量, go 相关的环境变量不再里面

1
2
3
Failed to run "go env" to find GOPATH as the "go" binary cannot be found in either GOROOT or PATH...
.....
/home/lomot/.vscode-server/bin/c3f126316369cd610563c75b1b1725e0679adfb3/bin:/home/lomot/.zinit/polaris/sbin:/home/lomot/.zinit/polaris/bin:/home/lomot/.vscode-server/bin/c3f126316369cd610563c75b1b1725e0679adfb3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/nodejs/bin

但是使用内置终端输出一下环境变量却是有的

1
2
dev2 ~ ᐅ echo $PATH
/home/lomot/.vscode-server/bin/c3f126316369cd610563c75b1b1725e0679adfb3/bin:/home/lomot/.zinit/polaris/sbin:/home/lomot/.zinit/polaris/bin:/home/lomot/.vscode-server/bin/c3f126316369cd610563c75b1b1725e0679adfb3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/nodejs/bin:/opt/go/bin:/home/lomot/go/bin:/opt/nodejs/bin:/opt/go/bin:/home/lomot/go/bin

于是去网上查了一下, 好像跟关于.zshrc没有被加载有关, 但是奇怪的是我的.zshrc里面的部分环境变量是已近加载了的, 比如我的/opt/nodejs/bin是通过.zshrc加载的. 对比报错中的环境变量和 vscode 内置终端中输出的环境变量, 发现少了后面 go 相关的部分, 而且/opt/nodejs/bin还被加载了两遍.

结论是远程 vscode-server 只会使用它启动时候的环境变量, 如果需要刷新环境变量需要重启 vscode-server,

可以在本地 vscode 上ctrl+shift+p, 执行Remote-SSH: Kill VS Code Server..

或者连上 linux 服务器执行

1
ps -aux | grep code

查看 vscode 服务端的进程然后杀掉, 再重新使用 vscode 远程连进来. 我在安装 go 之前就使用过 vscode 远程服务器, 断开连接后服务器上的 code 进程并不会退出, 这就导致了我修改的环境变量没有生效. 结果就是我杀掉了服务器上的 code 后问他就解决了.

参考

https://github.com/microsoft/vscode-remote-release/issues/1671 https://github.com/microsoft/vscode-remote-release/issues/83