Golang:在VsCode中安装gopls失败的问题解决方案
Golang:在VsCode中安装gopls失败的问题解决方案 我刚接触Go语言,在VSCode上安装gopls时遇到了困难。
Tools environment: GOPATH=/Users/maxgod/go
Installing 1 tool at /Users/maxgod/go/bin in module mode.
gopls
Installing golang.org/x/tools/gopls@latest FAILED
{
"code": 1,
"killed": false,
"signal": null,
"cmd": "/usr/local/go/bin/go install -v golang.org/x/tools/gopls@latest",
"stdout": "",
"stderr": "runtime/cgo\n# runtime/cgo\nxcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun\n"
}
1 tools failed to install.
gopls: failed to install gopls(golang.org/x/tools/gopls@latest): Error: Command failed: /usr/local/go/bin/go install -v golang.org/x/tools/gopls@latest
runtime/cgo
# runtime/cgo
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
更多关于Golang:在VsCode中安装gopls失败的问题解决方案的实战教程也可以访问 https://www.itying.com/category-94-b0.html
2 回复
missing xcrun
我不太了解Mac,但看起来你可能需要(重新)安装Xcode?
更多关于Golang:在VsCode中安装gopls失败的问题解决方案的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
这个错误是因为你的 macOS 缺少 Xcode 命令行工具。gopls 编译需要 C 编译器,而 macOS 默认没有安装完整的开发工具。
解决方案
1. 安装 Xcode 命令行工具
打开终端执行:
xcode-select --install
会弹出安装窗口,点击"安装"即可。
2. 如果已经安装但路径有问题,重置路径:
sudo xcode-select --reset
3. 或者指定正确的路径:
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
4. 安装完成后验证:
xcrun --version
应该显示类似:
xcrun version 239.
5. 重新安装 gopls:
go install golang.org/x/tools/gopls@latest
6. 在 VSCode 中重新安装:
- 打开命令面板 (Cmd+Shift+P)
- 输入 “Go: Install/Update Tools”
- 选择所有工具或只选择 gopls
替代方案:手动安装
如果仍然有问题,可以手动编译安装:
# 清理旧的安装
go clean -cache
go clean -modcache
# 重新安装
cd /tmp
git clone https://go.googlesource.com/tools
cd tools/gopls
go install .
验证安装
安装完成后检查:
which gopls
gopls version
应该显示类似:
/Users/maxgod/go/bin/gopls
golang.org/x/tools/gopls v0.15.3
配置 VSCode
在 VSCode 的 settings.json 中添加:
{
"go.useLanguageServer": true,
"go.languageServerFlags": [
"-rpc.trace"
],
"go.languageServer": "gopls"
}
这个问题是 macOS 环境特有的,安装 Xcode 命令行工具后就能正常编译 C 依赖,gopls 安装就会成功。

