Search code examples
gomakefile

Solve: package foo is not in GOROOT


I am trying to make my Go project compile, but the way to fix compilation errors is not obvious. Simplified example follows.

// filename: ~/myprojects/automate_things/automate_things.go
package main
import (
    "flag"
    "fmt"
    "github.com/melbahja/goph"
    "github.com/gleich/logoru"
)

func main() {
  // insert many things here
}

Now I use make dep (with Makefile, below) to build a go.mod and try to get all the dependencies, but this does not get them because I keep seeing package foo is not in GOROOT errors.

# filename: ~/myprojects/automate_things/Makefile
GOOS=linux

GO_SOURCE_FILE=automate_things.go
GO_BINARY_FILE=automate_things
GO_BIN_DIR=bin/

.SILENT: build
.DEFAULT_GOAL := build

build:
        -make fmt
        make dep
        go build $(GO_SOURCE_FILE)
        if [ "$(PLATFORM)" = "Linux" ]; then \
                GOARCH=amd64 GOOS=linux go build -ldflags "-s -w" -o ./$(GO_BINARY_FILE) $(GO_SOURCE_FILE); \
        elif [ "$(PLATFORM)" = "Darwin" ]; then \
                GOARCH=amd64 GOOS=darwin go build -ldflags "-s -w" -o ./$(GO_BINARY_FILE) $(GO_SOURCE_FILE); \
        fi
.PHONY: build

fmt:
        go fmt $(GO_SOURCE_FILE)
.PHONY: fmt

dep:
        -rm go.mod
        -rm go.sum
        go mod init automate_things
        go mod tidy
        go mod download
.PHONY: dep

This is my ~/.bashrc, which exports several relevant go env variables.

# filename: ~/.bashrc
# Build the default GOROOT (re-run the command as root)

mkdir -p /usr/local/go
export GOPROXY=https://proxy.golang.org
mkdir -p ~/gobin/bin
export GOPATH=~/gobin
#export GO111MODULE=auto

export PATH=$PATH:$GOPATH/bin

This is the go.mod, which is generated by my Makefile (make dep)

// go.mod
module automate_things

go 1.20

require (
        github.com/gleich/logoru v0.0.0-20230101033757-d86cd895c7a1
        github.com/melbahja/goph v1.4.0
)

require (
        github.com/fatih/color v1.10.0 // indirect
        github.com/kr/fs v0.1.0 // indirect
        github.com/mattn/go-colorable v0.1.8 // indirect
        github.com/mattn/go-isatty v0.0.12 // indirect
        github.com/pkg/errors v0.9.1 // indirect
        github.com/pkg/sftp v1.13.5 // indirect
        golang.org/x/crypto v0.6.0 // indirect
        golang.org/x/sys v0.5.0 // indirect
)

This is what I see when I make dep

$ make
make[1]: Entering directory '/home/me/myprojects/automate_things'
go fmt automate_things.go
make[1]: Leaving directory '/home/me/myprojects/automate_things'
make[1]: Entering directory '/home/me/myprojects/automate_things'
rm go.mod
rm go.sum
go mod init automate_things
go: creating new go.mod: module automate_things
go: to add module requirements and sums:
        go mod tidy
go mod tidy
go: finding module for package github.com/melbahja/goph
go: finding module for package github.com/gleich/logoru
go: found github.com/gleich/logoru in github.com/gleich/logoru v0.0.0-20230101033757-d86cd895c7a1
go: found github.com/melbahja/goph in github.com/melbahja/goph v1.4.0
go mod download
make[1]: Leaving directory '/home/me/myprojects/automate_things'
../../gobin/pkg/mod/golang.org/x/[email protected]/ssh/transport.go:8:2: package bufio is not in GOROOT (/home/me/src/bufio)
../../gobin/pkg/mod/github.com/mattn/[email protected]/noncolorable.go:4:2: package bytes is not in GOROOT (/home/me/src/bytes)

So let's try to download directly with go get and sudo to squash any potential permissions problems...

$ sudo /home/me/bin/go get github.com/melbahja/goph
go: downloading github.com/melbahja/goph v1.4.0
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/pkg/sftp v1.13.5
go: downloading golang.org/x/crypto v0.6.0
go: downloading github.com/kr/fs v0.1.0
go: downloading golang.org/x/sys v0.5.0
github.com/melbahja/goph imports
        context: package context is not in GOROOT (/usr/local/go/src/context)
github.com/melbahja/goph imports
        errors: package errors is not in GOROOT (/usr/local/go/src/errors)
github.com/melbahja/goph imports

Question

My go binary is in ~/bin/go.

What is the most efficient way to get / specify dependencies in my Makefile (without listing endless sub-deps) and make this project compile? I want to fix all the compilation problems listed above.

RTFM is not an answer. RTFM with a direct link to the documentation that works is an answer.


Solution

  • $ sudo /home/me/bin/go get github.com/melbahja/goph

    • The title of the question (and originally, the question body) mentioned GOROOT

    • Short story, your Go install seems to be broken... to fix it:


    1. Download a proper Go source tree for your processor arch (usually it's going to be amd64 for a typical linux install) from go.dev/dl
    2. cd /usr/local
    3. Extract the tarball as root
    4. cd ~/bin
    5. ln -s /usr/local/go/bin/go go
    6. ln -s /usr/local/go/bin/gofmt gofmt
    7. cd into your project directory and build the way you have your Makefile set up. It should work now.