VSCode在使用Go模块时仍然尝试使用GOROOT。

huangapple go评论82阅读模式
英文:

VSCode keeps trying to use GOROOT despite being a go module

问题

我创建了一个新的Go项目,并尝试使用VSCode。在父目录中,我有一个名为go.mod的文件,内容如下:

module main

go 1.19

我使用Bazel和Gazelle创建了这个项目,并且可以在终端中成功构建和运行一个简单的“hello world”程序,该程序从项目的子目录中导入,使用命令bazel run //src/server:main

然而,VSCode似乎无法理解这一切。我已经安装了Bazel和Go插件。我的VSCode给我一个导入错误,错误信息如下:

could not import src/server/models (no required module provides package "src/server/models")

当我保存更改时,VSCode尝试自动编译,但是IDE的“输出”窗口给我以下错误信息:

/Project/path/.../src/server/main.go:6:2 package src/server/models is not in GOROOT (/opt/homebrew/Cellar/go/1.19.4/libexec/src/src/server/models)

为什么它要使用GOROOT?为什么它不能与Bazel基础设施配合使用?

英文:

I've created a new Go project and am trying to use VSCode. I have a go.mod in the parent directory with the following:

module main

go 1.19

I created the project with Bazel and Gazelle, and can successfully build and run a simple "hello world" in my Terminal that imports from subdirectories in the project using the command bazel run //src/server:main.

However, VSCode doesnt seem to understand any of this. I have Bazel and Go plugins installed. My VSCode gives me an import error that says

could not import src/server/models (no required module provides package "src/server/models")

And when i save my changes, VSCode tries to auto-compile and the IDE "Output" gives me the following error

/Project/path/.../src/server/main.go:6:2 package src/server/models is not in GOROOT (/opt/homebrew/Cellar/go/1.19.4/libexec/src/src/server/models)

Why in the world is it trying to use GOROOT? And why is it not working with the Bazel infrastructure?

答案1

得分: 1

不知道是怎么发生的,但现在事情正常了。

  1. 恢复所有默认的 GO VSCode 设置(我觉得这不是原因)。
  2. 项目结构有代码更改(我觉得这是原因)。
项目
  | 工作区
  | 构建
  | src
     | go.mod (module src     go 1.19)
     | BUILD  (# gazelle:prefix src) <-- 这是文件中唯一的一行
     | server
         | main.go     <-- 这里的导入最终修复了
         | BUILD
         | models
            | BUILD
            | models.go
英文:

No idea how this happened but things are working now.

  1. Reverted to all default GO VSCode settings (dont think this was it)
  2. Project structure with code changes (i think this was it)
project
  | WORKSPACE
  | BUILD
  | src
     | go.mod (module src     go 1.19)
     | BUILD  (# gazelle:prefix src) <-- this is the only line in this file
     | server
         | main.go     <-- imports here finally fixed
         | BUILD
         | models
            | BUILD
            | models.go

huangapple
  • 本文由 发表于 2023年1月6日 09:50:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定