'"package XXX is not in GOROOT" when building a Go project
I have a weird issue that arose when I took a break from this project. Upon starting up Goland, I'm riddled with errors when trying to run my project.
The specific error, when building one of my packages, is:
start.go: package project/game is not in GOROOT (C:\Go\src\project\game)
I have a folder structure as such under C:\Users\username
go
|-src
|-project
|-game
|-entity
|-whatever.go
|-game_stuff.go
|-server
and my env vars are as such:
GOROOT=C:\Go
GOPATH=C:\Users\ketchup\go
for each of the modules (project/game/entity, project/game, project/server), I did a git mod init
.
When building, Goland will try to run this:
C:\Go\bin\go.exe build -o C:\Users\ketchup\AppData\Local\Temp\___go_build_project_server.exe project/server
and return the error.
Can anyone help me with this issue? Kind of lost since Goland was working fine the last time I opened it. Also not even sure what direction to look at - I'm pretty new to Go and I'm not really sure what documentation to look at :\ Thank you everyone!
Solution 1:[1]
A pretty dumb conclusion (mostly on my part) but my issue came from having done go mod init
in each of the folders. after removing go.mod
and go.dep
from each of the folders I did go mod init
in, I could build without issue (through terminal)
Also, my packages in GoLand were not being detected because I had Go Modules enabled in the Settings. I disabled it and GoLand was able to index the external packages and my own packages.
Solution 2:[2]
You may have GO111MODULE set "on", which will be on the go mod. Turning off the GO111MODULE may resolve this problem.
go env -w GO111MODULE=off
Solution 3:[3]
In newer versions (post 1.13) of Go, you don't need to set environment variables like GOPATH
, GOBIN
, etc.
You also need to have a go.mod
file at the project root. This will make the directory a Go module. This is also where the .git/
is located. This means that only one go.mod
is needed per repository. Inside the project root you could do a go mod init remote-repo.com/username/repository
I installed Go using Homebrew on macOS so GOROOT
is /opt/homebrew/Cellar/go/1.17.5/libexec
. This location contains the standard library and runtimes for Go.
test
and run
commands are run in the format go COMMAND package_path/xxx
. Without specifying the package_path ./
and just running go COMMAND xxx
, the compiler assumes that the module xxx is located in GOROOT, and throws error package xxx is not in GOROOT (path/to/GOROOT/src/xxx)
because it doesn't exist.
This behavior is expected because the package we are working with is not part of the Go SDK, i.e., not in GOROOT
. The package we are working with will either end up in the go workspace or in the current working directory. Running go install
compiles and puts an executable binary in $GOBIN
(a.k.a $GOPATH/bin
- here $GOPATH
is the Go workspace). Running go build
from inside a package compiles and puts an execuatble in that directory.
You haven't listed the files inside the server/
package and which file has the main function, so I'll emulate 3 workflows of a calculator each demonstrating more complexity. The last workflow is similar to your directory structure.
Directory Structure
Version 1:
Getting started with packages
Basic functionality
calculatorv1
??? go.mod <- go mod init github.com/yourname/calculatorv1
??? basic/
??? add.go
??? add_test.go
??? main.go
??? multiply.go
??? multiply_test.go
Version 2:
More functionality
Multiple packages
calculatorv2
??? go.mod <- go mod init github.com/yourname/calculatorv2
??? main.go
??? basic/
? ??? add.go
? ??? add_test.go
? ??? multiply.go
? ??? multiply_test.go
???? advanced/
??? square.go
??? square_test.go
Version 3:
Even more functionality
Nested packages
calculatorv3
??? go.mod <- go mod init github.com/yourname/calculatorv3
??? main.go
??? basic/
? ??? add.go
? ??? add_test.go
? ??? multiply.go
? ??? multiply_test.go
???? advanced/
??? square.go
??? square_test.go
??? scientific/
??? declog.go
??? declog_test.go
Workflow
Note: Substitute xxx
with basic
, advanced
, or advanced/scientific
depending on the version you're working with.
Initialize Go module in the project directory (one of
calculatorv1
,calculatorv2
, orcalculatorv3
) usinggo mod init
Run tests
go test -v ./...
(from the project root, recursively execute all test suites)OR
go test -v ./xxx
(from the project root, run the test suite in package "xxx")OR
cd xxx/ go test -v # (from inside the package)
Compile and execute package
go run ./...
(from the project root, recursively run all.go
files except tests)OR
go run ./xxx
(from the project root, run all.go
files in "xxx" package except tests)OR
cd xxx go run . # (from inside the package)
NOTE: Only files in the main package are executable, i.e., files having declaration
package main
. This means thatgo run ./xxx
will only work with version1, and not versions 2 and 3. So instead for versions 2 and 3, rungo run main.go
Code
Very easy to fill in incomplete bits
Version 1
add.go
package main
func addition(x int, y int) int {
return x + y
}
add_test.go
package main
import "testing"
func TestAdd(t *testing.T) {
t.Run("adding two positive numbers", func(t *testing.T) {
sum := addition(2, 2)
expected := 4
if sum != expected {
t.Errorf("Expected %d; but got %d", expected, sum)
}
})
t.Run("adding two negative numbers", func(t *testing.T) {
sum := addition(-3, -4)
expected := -7
if sum != expected {
t.Errorf("Expected %d; but got %d", expected, sum)
}
})
t.Run("adding one positive and one negative integer", func(t *testing.T) {
sum := addition(1, -3)
expected := -2
if sum != expected {
t.Errorf("Expected %d; but got %d", expected, sum)
}
})
}
main.go
package main
import "fmt"
func main() {
var num1 int = 1
var num2 int = 2
sum := addition(num1, num2)
product := multiplication(num1, num2)
fmt.Printf("The sum of %d and %d is %d\n", num1, num2, sum)
fmt.Printf("The multiplication of %d and %d is %d\n", num1, num2, product)
}
Version 2
main.go
package main
import (
"fmt"
"github.com/yourname/calculatorv2/basic"
"github.com/yourname/calculatorv2/advanced"
)
func main() {
var num1 int = 1
var num2 int = 2
product := basic.Multiplication(num1, num2)
square := advanced.Square(num2)
fmt.Printf("The product of %d and %d is %d\n", num1, num2, product)
fmt.Printf("The square of %d is %d\n", num2, square)
}
multiply.go
package basic
func Multiplication(x int, y int) int {
return x * y
}
multiply_test.go
package basic
import "testing"
func TestMultiply(t *testing.T) {
t.Run("multiplying two positive numbers", func(t *testing.T) {
sum := Multiplication(2, 2)
expected := 4
if sum != expected {
t.Errorf("Expected %d; but got %d", expected, sum)
}
})
t.Run("multiplying two negative numbers", func(t *testing.T) {
sum := Multiplication(-3, -4)
expected := 12
if sum != expected {
t.Errorf("Expected %d; but got %d", expected, sum)
}
})
t.Run("multiplying one positive and one negative integer", func(t *testing.T) {
sum := Multiplication(1, -3)
expected := -3
if sum != expected {
t.Errorf("Expected %d; but got %d", expected, sum)
}
})
}
square.go
package advanced
func Square(x int) int {
return x * x
}
Version 3
main.go
package main
import (
"fmt"
"github.com/yourname/calculatorv3/basic"
"github.com/yourname/calculatorv3/advanced"
"github.com/yourname/calculatorv3/advanced/scientific"
)
func main() {
var num1 int = 1
var num2 int = 2
var num3 float64 = 2
product := basic.Multiplication(num1, num2)
square := advanced.Square(num2)
decimallog := scientific.DecimalLog(num3)
fmt.Printf("The product of %d and %d is %d\n", num1, num2, product)
fmt.Printf("The square of %d is %d\n", num2, square)
fmt.Printf("The decimal log (base 10) of %f is %f\n", num3, decimallog)
}
square.go
package advanced
func Square(x int) int {
return x * x
}
declog.go
package scientific
import "math"
func DecimalLog(x float64) float64 {
return math.Log10(x)
}
declog_test.go
package scientific
import "testing"
func TestDecimalLog(t *testing.T) {
t.Run("adding two positive numbers", func(t *testing.T) {
sum := DecimalLog(100)
expected := 2.0
if sum != expected {
t.Errorf("Expected %f; but got %f", expected, sum)
}
})
t.Run("adding two negative numbers", func(t *testing.T) {
sum := DecimalLog(10)
expected := 1.0
if sum != expected {
t.Errorf("Expected %f; but got %f", expected, sum)
}
})
}
Solution 4:[4]
To anyone who does want modules to work with GoLand after they have stopped doing so, make sure 'Enable Go modules integration' is checked in the Preferences as such:
Solution 5:[5]
So it looks like if you running go mod init 'xxx' the xxx is core name of your project. In there main packages complete name is 'xxx/main' so if you have your project root folder like this:
root -> go mod init xxx
|- main.go -> package "main"
|- tools
|- helper.go -> package "tools"
and you want to import tools package from main.go you need to import this "xxx/tools"
Solution 6:[6]
I met the same error with the project layout
project
|- pgk
|- src
|-module1
|- some.go
|- main.go
some.go
package module1
...
main.go
import "project/module1"
Originally, I import module1 through ./module1
, the error package module1 is not in GOROOT
comes up. I solve it through import "project/module1"
.
PS: per golang project layout, src
should NOT in golang project.
Solution 7:[7]
I got the same error when I had a spelling mistake in the package name. Therefore, it might be the root cause behind this error. Maybe it will be useful for someone.
Solution 8:[8]
I made the mistake of changing the module name inside go.mod
file manually. After fixing it, it worked fine.
??? hello
??? go.mod
??? go.sum
??? hello.go
??? morestrings
??? reverse.go
??? reverse_test.go
Excerpts of hello.go
import (
...
"hello/morestrings"
...
)
Running go build
in hello directory was giving following error:
hello.go:5:2: package hello/morestrings is not in GOROOT (/usr/local/go/src/hello/morestrings)
Solution 9:[9]
While running command go mod init
you need to add folder name, not main.go page name.
Like: go mod init confirm_enrl
. confirm_enrl
is project folder name
Solution 10:[10]
For my case just updating my go version to the latest solved the issue .
I downloaded the latest binary and installed it and boom all was sorted !
Solution 11:[11]
For me it was because my main method wasn't inside package main
package main
func main() {
{
}
Solution 12:[12]
I came across the same like issue when i was following go getting started tutorial with goland default awesomeProject
.
What i did after crating project run this command go mod init example.com/hello
but my root directory name was different than example.com/hello
.
After manually edit the go.mod file and replace awesomeProject
with example.com/hello
the build works successfully.
Solution 13:[13]
I knew this is about one year ago. I just want to share same experience for those who have, have modules in local folders..
The problem is because you have not publish your module to a central location like github. You have to run this command to tell go that the module is a local module
go mod edit --replace external_folder_module_name= ../pathItsLocated
Solution 14:[14]
I was also faced similar error while building the docker image. The error was #19 0.785 controllers/vasaprovider_controller.go:28:2: package vasa-operator/config/configs is not in GOROOT
.
After adding the go file location in Dockerfile, it worked for me. The lines I had added are:
COPY config/configs config/configs/
COPY --from=builder /workspace/config/configs config/configs/
Solution 15:[15]
In my case, I was trying to build from the wrong directory. The old directory didn't even have any .go
files.
Solution 16:[16]
Make sure your import statement matches the module name that you defined in your go mod.
Solution 17:[17]
just initialise the project with mod file and start working on on root folder.
go mod init <name-anything>
Solution 18:[18]
We can solve this problem by writing this command on terminal: go env -w GO111MODULE=off
But after this command, you will be not able to use go mod init
through terminal. You must create "go.mod" file manually.
Solution 19:[19]
In my case, it is cause by changing goroot path.
GOROOT is a variable that defines where your Go SDK is located
it will work if you change you goroot to where go sdk you installed
Solution 20:[20]
It seems a little funny, but I faced same error because of running:
go run build
instead of
go build main.go
So it shows that everything after build or run should have a recognized path by go.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow