Naji
Naji

Reputation: 742

VSCode keeps trying to use GOROOT despite being a go module

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?

Upvotes: 0

Views: 159

Answers (1)

Naji
Naji

Reputation: 742

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

Upvotes: 1

Related Questions