You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

M1 Mac下gopls加载包阻塞、无法跳转定义的求助

解决M1 MacBook上Go项目serviceB的gopls加载阻塞问题

环境信息

当前开发环境配置如下:

$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/ryan/Library/Caches/go-build"
GOENV="/Users/ryan/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/ryan/go/pkg/mod"
GONOPROXY="*.xlcx.work"
GONOSUMDB="*.xlcx.work"
GOOS="darwin"
GOPATH="/Users/ryan/go"
GOPRIVATE="*.xlcx.work"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.12"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/yt/pk31kvcj3yjfr488_7qv5t9h0000gn/T/go-build2846723005=/tmp/go-build -gno-record-gcc-switches -fno-common"

$ gopls version
golang.org/x/tools/gopls v0.15.3

$ dlv version
Delve Debugger
Version: 1.22.1
Build: $Id: 0c3470054da6feac5f6dcf3e3e5144a64f7a9a48 $

项目表现对比

serviceA(正常)

gopls可正常加载包、跳转定义,日志如下:

[Info  - 12:35:35] 2025/02/16 12:35:35 go info for /Users/ryan/develop/serviceA
(view type GoModView)
(root dir /Users/ryan/develop/serviceA)
(go version go version go1.20.12 darwin/arm64)
(build flags: [])
(go env: {GOOS:darwin GOARCH:arm64 GOCACHE:/Users/ryan/Library/Caches/go-build GOMODCACHE:/Users/ryan/go/pkg/mod GOPATH:/Users/ryan/go GOPRIVATE:*.xlcx.work GOFLAGS: GO111MODULE: GoVersion:20 GoVersionOutput:go version go1.20.12 darwin/arm64
 GOWORK: GOPACKAGESDRIVER:})
(env overlay: map[])


[Info  - 12:35:40] 2025/02/16 12:35:40 go/packages.Load #1
    snapshot=0
    directory=file:///Users/ryan/develop/serviceA
    query=[/Users/ryan/develop/serviceA/... builtin]
    packages=123

[Info  - 12:35:40] 2025/02/16 12:35:40 go/packages.Load #1: updating metadata for 781 packages

[Error - 12:35:44] Request textDocument/semanticTokens/full failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 12:35:44] Request textDocument/semanticTokens/range failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 12:35:44] Request textDocument/semanticTokens/range failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 12:35:46] Request textDocument/semanticTokens/range failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 12:35:49] Request textDocument/semanticTokens/range failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 12:36:02] Request textDocument/semanticTokens/range failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 12:36:03] Request textDocument/semanticTokens/full failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 12:36:04] Request textDocument/semanticTokens/range failed.
  Message: semantictokens are disabled
  Code: 0 
[Error - 12:36:06] Request textDocument/semanticTokens/range failed.
  Message: semantictokens are disabled
  Code: 0 
[Info  - 12:36:15] 2025/02/16 12:36:15 background imports cache refresh starting

[Info  - 12:36:16] 2025/02/16 12:36:16 background refresh finished after 2.365305791s

serviceB(异常)

gopls加载包极慢、疑似阻塞,无法跳转定义,无报错日志,日志如下:

[Info  - 12:38:01] 2025/02/16 12:38:01 go info for /Users/ryan/develop/serviceB
(view type GoModView)
(root dir /Users/ryan/develop/serviceB)
(go version go version go1.20.12 darwin/arm64)
(build flags: [])
(go env: {GOOS:darwin GOARCH:arm64 GOCACHE:/Users/ryan/Library/Caches/go-build GOMODCACHE:/Users/ryan/go/pkg/mod GOPATH:/Users/ryan/go GOPRIVATE:*.xlcx.work GOFLAGS: GO111MODULE: GoVersion:20 GoVersionOutput:go version go1.20.12 darwin/arm64
 GOWORK: GOPACKAGESDRIVER:})
(env overlay: map[])


[Info  - 12:38:01] 2025/02/16 12:38:01 8.485ms for GOROOT= GOPATH=/Users/ryan/go GO111MODULE=off GOPROXY=https://proxy.golang.org,direct PWD=/Users/ryan/develop/serviceB go list -e -f {{context.ReleaseTags}} -- unsafe

[Info  - 12:38:01] 2025/02/16 12:38:01 13.158917ms for GOROOT= GOPATH=/Users/ryan/go GO111MODULE=auto GOPROXY=https://proxy.golang.org,direct PWD=/Users/ryan/develop/serviceB go list -f "{{context.GOARCH}} {{context.Compiler}}" -- unsafe

解决步骤

  • 手动排查项目依赖问题:进入serviceB目录执行以下命令,观察是否卡住或报错,直接定位模块解析或依赖拉取故障
    cd /Users/ryan/develop/serviceB
    go mod tidy
    go list ./...
    
  • 确认私有模块配置生效:针对serviceB目录重新配置私有模块环境变量,避免gopls访问私有模块时走公共代理阻塞
    go env -w GOPRIVATE="*.xlcx.work"
    go env -w GONOPROXY="*.xlcx.work"
    go env -w GONOSUMDB="*.xlcx.work"
    
  • 清理缓存并重启VSCode:清理Go构建缓存和模块缓存,消除缓存损坏导致的阻塞
    rm -rf ~/Library/Caches/go-build
    rm -rf ~/Library/Application Support/go/env
    go clean -modcache
    
    完成后重启VSCode,重新打开serviceB项目
  • 调整gopls配置:在VSCode的settings.json中添加以下配置,减少gopls工作负载
    "gopls": {
      "semanticTokens": false,
      "experimentalWorkspaceModule": false,
      "cacheKey": "${workspaceFolder}"
    }
    
  • 优化项目结构:检查serviceB的go.mod文件,清理未使用的依赖;若模块嵌套过深,拆分模块以减少gopls解析范围
  • 更新gopls版本:旧版本可能存在arm64架构兼容性问题,更新到最新版
    go install golang.org/x/tools/gopls@latest
    

内容的提问来源于stack exchange,提问作者yongxiang chen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 10:10:54