Link: cannot open file [Solved]

Hi,

I have a problem (on windows and now on ubuntu, too). I have some libraries from github (among others cobra) and installed them via

go get ...

When I now want to build or run my application I get this message:

/usr/local/go/pkg/tool/linux_amd64/link: cannot open file /usr/local/go/pkg/linux_amd64/github.com/spf13/cobra.a: open /usr/local/go/pkg/linux_amd64/github.com/spf13/cobra.a: no such file or directory

/usr/local/go is the GOROOT
Which is absolutely correct since it is not there but under my GOPATH. I looked around now for a while but cannot solve that problem - Means, I solved the problem with copying the packages manually to GOROOT but this is not a real solution, I guess.

Do not set GOROOT. Do not copy stuff (other than the Go compiler) into /usr/local/go. Do show us the output of go env and go get -v -x $something.

Ok,

I unset GOROOT and deleted all manually copied folders. Result:

09:36 $ go get -v -x github.com/spf13/cobra/cobra
WORK=/tmp/go-build290303357

gGo env:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/.../go/"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build381433808=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

Looks like it worked?

go get works. That is not the problem. But compiling does not work.

Is that the full output you got from go get -v -x github.com/spf13/cobra/cobra? We can’t see how the compiling does not work without seeing what happened.

Go-getting does compilation as part of it’s job. If go-get works, compilation works. But you’re not showing us any errors with your current setup, so it’s hard to say more.

I just removed all go stuff from my system and retried it. Here now the output from the go get:

github.com/spf13/afero
    mkdir -p $WORK/github.com/spf13/afero/_obj/
    cd /home/marczahn/go/src/github.com/spf13/afero
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/github.com/spf13/afero.a -trimpath $WORK -p github.com/spf13/afero -complete -buildid 6dbbadc32f1baf68add54ce5f60bddaf80bcc23c -D _/home/marczahn/go/src/github.com/spf13/afero -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./afero.go ./basepath.go ./cacheOnReadFs.go ./const_win_unix.go ./copyOnWriteFs.go ./httpFs.go ./ioutil.go ./memmap.go ./memradix.go ./os.go ./path.go ./readonlyfs.go ./regexpfs.go ./unionFile.go ./util.go
    mv $WORK/github.com/spf13/pflag.a /home/marczahn/go/pkg/linux_amd64/github.com/spf13/pflag.a
    github.com/spf13/cobra
    mkdir -p $WORK/github.com/spf13/cobra/_obj/
    cd /home/marczahn/go/src/github.com/spf13/cobra
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/github.com/spf13/cobra.a -trimpath $WORK -p github.com/spf13/cobra -complete -buildid a8f8bec375d3dfb068ba9d5ca25174cc4aa18e4a -D _/home/marczahn/go/src/github.com/spf13/cobra -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./bash_completions.go ./cobra.go ./command.go ./command_notwin.go
    mkdir -p /home/marczahn/go/pkg/linux_amd64/github.com/fsnotify/
    mv $WORK/github.com/fsnotify/fsnotify.a /home/marczahn/go/pkg/linux_amd64/github.com/fsnotify/fsnotify.a
    mv $WORK/github.com/spf13/cobra.a /home/marczahn/go/pkg/linux_amd64/github.com/spf13/cobra.a
    mv $WORK/github.com/spf13/afero.a /home/marczahn/go/pkg/linux_amd64/github.com/spf13/afero.a
    mkdir -p /home/marczahn/go/pkg/linux_amd64/gopkg.in/
    mv $WORK/gopkg.in/yaml.v2.a /home/marczahn/go/pkg/linux_amd64/gopkg.in/yaml.v2.a
    github.com/spf13/viper
    mkdir -p $WORK/github.com/spf13/viper/_obj/
    cd /home/marczahn/go/src/github.com/spf13/viper
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/github.com/spf13/viper.a -trimpath $WORK -p github.com/spf13/viper -complete -buildid 72cdf8bb335a3d424210efcb5722c48a617313a9 -D _/home/marczahn/go/src/github.com/spf13/viper -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./flags.go ./util.go ./viper.go
    mv $WORK/github.com/spf13/viper.a /home/marczahn/go/pkg/linux_amd64/github.com/spf13/viper.a
    github.com/spf13/cobra/cobra/cmd
    mkdir -p $WORK/github.com/spf13/cobra/cobra/cmd/_obj/
    mkdir -p $WORK/github.com/spf13/cobra/cobra/
    cd /home/marczahn/go/src/github.com/spf13/cobra/cobra/cmd
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/github.com/spf13/cobra/cobra/cmd.a -trimpath $WORK -p github.com/spf13/cobra/cobra/cmd -complete -buildid dada704aef60b8ee09944503d14233e233678005 -D _/home/marczahn/go/src/github.com/spf13/cobra/cobra/cmd -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./add.go ./helpers.go ./init.go ./licenses.go ./root.go
    mkdir -p /home/marczahn/go/pkg/linux_amd64/github.com/spf13/cobra/cobra/
    mv $WORK/github.com/spf13/cobra/cobra/cmd.a /home/marczahn/go/pkg/linux_amd64/github.com/spf13/cobra/cobra/cmd.a
    github.com/spf13/cobra/cobra
    mkdir -p $WORK/github.com/spf13/cobra/cobra/_obj/
    mkdir -p $WORK/github.com/spf13/cobra/cobra/_obj/exe/
    cd /home/marczahn/go/src/github.com/spf13/cobra/cobra
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/github.com/spf13/cobra/cobra.a -trimpath $WORK -p main -complete -buildid 82a41c82fb6a953005296852663848fc5ce3e137 -D _/home/marczahn/go/src/github.com/spf13/cobra/cobra -I $WORK -I /home/marczahn/go/pkg/linux_amd64 -pack ./main.go
    cd .
    /usr/local/go/pkg/tool/linux_amd64/link -o $WORK/github.com/spf13/cobra/cobra/_obj/exe/a.out -L $WORK -L /home/marczahn/go/pkg/linux_amd64 -extld=gcc -buildmode=exe -buildid=82a41c82fb6a953005296852663848fc5ce3e137 $WORK/github.com/spf13/cobra/cobra.a
    mkdir -p /home/marczahn/go/bin/
    mv $WORK/github.com/spf13/cobra/cobra/_obj/exe/a.out /home/marczahn/go/bin/cobra
    github.com/kardianos/osext (download)
    cd .
    git clone https://github.com/kardianos/osext /home/marczahn/go/src/github.com/kardianos/osext
    cd /home/marczahn/go/src/github.com/kardianos/osext
    git submodule update --init --recursive
    cd /home/marczahn/go/src/github.com/kardianos/osext
    git show-ref
    cd /home/marczahn/go/src/github.com/kardianos/osext
    git submodule update --init --recursive
    WORK=/tmp/go-build298494278
    github.com/kardianos/osext
    mkdir -p $WORK/github.com/kardianos/osext/_obj/
    mkdir -p $WORK/github.com/kardianos/
    cd /home/marczahn/go/src/github.com/kardianos/osext
    /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/github.com/kardianos/osext.a -trimpath $WORK -p github.com/kardianos/osext -complete -buildid 331a56d155d7f159deb74d4e0e7d5eb4117d636d -D _/home/marczahn/go/src/github.com/kardianos/osext -I $WORK -pack ./osext.go ./osext_go18.go
    mkdir -p /home/marczahn/go/pkg/linux_amd64/github.com/kardianos/
    mv $WORK/github.com/kardianos/osext.a /home/marczahn/go/pkg/linux_amd64/github.com/kardianos/osext.a

I am wondering why Go does not try to load them from GOPATH…

I kinda solved with providing the -pkgdir parameter but that is not really perfect…

Is your GOPATH not /home/marczahn/go? Your previous output of go env had GOPATH="/home/.../go/" I had hoped you were trying to hide your username and replaced it with .... Also, with g1.8 GOPATH defaults to $HOME/go, which seems to be what you are doing, so there is no need to actually set it.

What do you mean by “Go does not try to load them from GOPATH”? The go get output you provided seems to be using a GOPATH, putting binaries in $GOPATH/bin and compiled packages in $GOPATH/pkg like I expect.

What problem did you solve by providing -pkgdir?

Yeah - Tried to hide my username - Fail :smiley: And even not worth it…

Go seems to not use GOPATH when I want to compile my application (go build main.go) - Otherwise I would not have this error:

/usr/local/go/pkg/tool/linux_amd64/link: cannot open file /usr/local/go/pkg/linux_amd64/github.com/spf13/cobra.a: open /usr/local/go/pkg/linux_amd64/github.com/spf13/cobra.a: no such file or directory

correct?

This works:

go build -pkgdir /home/marczahn/go/pkg/ -o main main.go

What version of go are you using? How did you install it?

My attempts at replicating this problem have failed. I get more useful error messages like:

➜  ~ go build -v -o main main.go
main.go:5:2: cannot find package "github.com/pkg/errors" in any of:
	/usr/local/go/src/github.com/pkg/errors (from $GOROOT)
	/home/alaster/go/src/github.com/pkg/errors (from $GOPATH)
➜  ~ file /home/alaster/go
/home/alaster/go: cannot open `/home/alaster/go' (No such file or directory)

Notice that this tries to use the default GOPATH, which does not exist on that system.

Could you post a main.go that this problem occurs with and the path where you keep it?

A potentially relevant issue: https://github.com/golang/go/issues/14683 (found by this search)

Indeed - do not use go build somefile.go. Many things will be different and confusing when you are giving it a single file. Use go build on packages, for example the package you are standing in when running the command:

$ go build -i -v

You should not need to use -pkgdir.

@nathankerr
Go 1.8; I installed it as described on the official website -> Downloading, Moving to usr/local, setting up go path and create GOPATH-variable

/home/marczahn/go/src/engine_backoffice
main.go:

package main

import (
    "github.com/spf13/cobra"
    "./cmd"
    "./application"
    "fmt"
    "os"
)

var rootCmd *cobra.Command

func init() {
    config := application.Config()
    rootCmd = &cobra.Command{Use: "app"}
    rootCmd.PersistentFlags().BoolVarP(&config.Verbose, "verbose", "v", false, "verbose output")
    rootCmd.AddCommand(cmd.CmdServe(application.Config().HttpListen, application.UserMgmt(), application.Router()))
    rootCmd.AddCommand(cmd.CmdAddUser(application.UserMgmt()))
    rootCmd.AddCommand(cmd.CmdListUsers(application.UserMgmt()))
    rootCmd.AddCommand(cmd.CmdDeleteUser(application.UserMgmt()))
}

func main() {
    if err := rootCmd.Execute(); err != nil {
        fmt.Println(err)
        os.Exit(-1)
    }
}

go build -v -o main main.go outout:

08:33 $ go build -v -o main main.go
_/home/marczahn/go/src/engine_backoffice/resources/views
_/home/marczahn/go/src/engine_backoffice/models/auth
_/home/marczahn/go/src/engine_backoffice/models
_/home/marczahn/go/src/engine_backoffice/config
_/home/marczahn/go/src/engine_backoffice/web
_/home/marczahn/go/src/engine_backoffice/cmd
_/home/marczahn/go/src/engine_backoffice/application
command-line-arguments
# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: cannot open file /usr/local/go/pkg/linux_amd64/github.com/spf13/cobra.a: open /usr/local/go/pkg/linux_amd64/github.com/spf13/cobra.a: no such file or directory

@calmh

08:35 $ go build -i -v
main.go:6:5: local import "./application" in non-local package
main.go:5:5: local import "./cmd" in non-local package

Strange thing is that it works fine on another system where we installed and get’ed the packages the same way

btw: Thank you very much for your help :slight_smile:

1 Like

I found now the solution - In fact the problem seems to be the relative imports. With

import "<project>/<package>"

it works as expected.

1 Like

Yes, do not use relative imports.

Sorry to be saying so much “do not” here, but you seem to have found a perfect storm of practices that are shunned or just not recommended when developing Go. :wink:

1 Like

Glad a way that works was found.

Hey

thank you guys for your time!

@calmh Absolutely fine - How should one learn otherwise? :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.