Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: -pkgdir does not always work with relative paths #21309

Closed
yunabe opened this issue Aug 4, 2017 · 3 comments
Closed

cmd/go: -pkgdir does not always work with relative paths #21309

yunabe opened this issue Aug 4, 2017 · 3 comments
Milestone

Comments

@yunabe
Copy link

yunabe commented Aug 4, 2017

What version of Go are you using (go version)?

go version go1.8.3 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/yunabe/local/gocode"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/mp/xzsxb5nn0_j5406pjg8ttsq00000gn/T/go-build252110473=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
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"

What did you do?

I cross compiled Go into Linux binary on MacOSX.

// hello.go
package main

import "fmt"

func main() {
  fmt.Println("Hello Go!")
}

I compiled this hello.go with GOOS=linux GOARCH=amd64 go build hello.go. This worked perfectly but it was a little bit slow. It takes 2 to 3 secs to build this simple program because this command always builds all dependencies from source code.

So, I tried -pkgdir option of go build. I expected the cross-compiled packages will be stored and reused from -pkgdir.

I ran the following command

GOOS=linux GOARCH=amd64 go build -i -v -pkgdir cache hello.go

and just got an error message

runtime/internal/sys
runtime/internal/atomic
runtime
# runtime
/usr/local/go/src/runtime/alg.go:8: can't find import: "runtime/internal/sys"

What did you expect to see?

-pkgdir works with GOOS=linux GOARCH=amd64 environment variables and improves build speed.

What did you see instead?

It seems go build failed to load runtime/internal/sys.a from -pkgdir when it compiled runtime.

@crawshaw
Copy link
Member

crawshaw commented Sep 3, 2017

It looks like the bug occurs when there are no cross-compilation .a files in the GOROOT and you pass a relative path to -pkgdir. Testing here on macOS:

cd /tmp
$ GOOS=linux go build -i -pkgdir /tmp/mycache main.go   # works
$ rm -rf mycache
$ GOOS=linux go build -i -pkgdir mycache main.go
runtime/internal/sys
runtime/internal/atomic
runtime
# runtime
/Users/crawshaw/go/src/runtime/alg.go:8:2: can't find import: "runtime/internal/sys"

(As an aside, you describe using -pkgdir to get cross-compilation caching. You don't need -pkgdir for that if your GOROOT is writable. Just use -i. The intermediate stdlib packages are saved in $GOROOT/pkg/GOOS_GOARCH/.)

@crawshaw crawshaw added this to the Go1.10 milestone Sep 3, 2017
@crawshaw crawshaw changed the title go build -pkgdir does not work with cross-compile cmd/go: -pkgdir does not always work with relative paths Sep 3, 2017
@gopherbot
Copy link

Change https://golang.org/cl/61391 mentions this issue: cmd/go: take absolute path from -pkgdir

@gopherbot
Copy link

Change https://golang.org/cl/76250 mentions this issue: cmd/go: accept relative -pkgdir argument

@golang golang locked and limited conversation to collaborators Nov 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants