`-pkgdir` option does not work with `go get` command

My go workspace is simple:

[000]kdr2@Debian-X230:~/gows$ pwd
/home/kdr2/gows
[000]kdr2@Debian-X230:~/gows$ tree
.
└── src
    └── test
        └── m.go

2 directories, 1 file

And the content of m.go is from https://raw.githubusercontent.com/facebookgo/grace/master/gracedemo/demo.go . All ENV variables are set.

When I run go get -v -x test, all goes well, it downloads the dependencies and build out an executable. But when I run go get -v -x -pkgdir pkg-1 test, it leads to an error:

...
mkdir -p $WORK/net/http/_obj/
cd /usr/local/go-1.8/src/net/http
/usr/local/go-1.8/pkg/tool/linux_amd64/compile -o $WORK/net/http.a -trimpath $WORK -p net/http -complete -buildid 49a4487ab4c506c0ef8aa1f5f4ceea3fc8b1243d -importmap golang_org/x/net/http2/hpack=vendor/golang_org/x/net/http2/hpack -importmap golang_org/x/net/idna=vendor/golang_org/x/net/idna -importmap golang_org/x/net/lex/httplex=vendor/golang_org/x/net/lex/httplex -importmap golang_org/x/text/unicode/norm=vendor/golang_org/x/text/unicode/norm -importmap golang_org/x/text/width=vendor/golang_org/x/text/width -D _/usr/local/go-1.8/src/net/http -I $WORK -I pkg-1 -pack ./client.go ./cookie.go ./doc.go ./filetransport.go ./fs.go ./h2_bundle.go ./header.go ./http.go ./jar.go ./method.go ./request.go ./response.go ./server.go ./sniff.go ./status.go ./transfer.go ./transport.go
cp $WORK/net/http.a pkg-1/net/http.a
github.com/facebookgo/httpdown
mkdir -p $WORK/github.com/facebookgo/httpdown/_obj/
cd /home/kdr2/gows/src/github.com/facebookgo/httpdown
/usr/local/go-1.8/pkg/tool/linux_amd64/compile -o $WORK/github.com/facebookgo/httpdown.a -trimpath $WORK -p github.com/facebookgo/httpdown -complete -buildid 45d38b6b6de48eff9fe8461b5764b769ea669b4c -D _/home/kdr2/gows/src/github.com/facebookgo/httpdown -I $WORK -I pkg-1 -pack ./httpdown.go
# github.com/facebookgo/httpdown
src/github.com/facebookgo/httpdown/httpdown.go:16: can't find import: "github.com/facebookgo/clock"
[002]kdr2@Debian-X230:~/gows$

When I run go build -v -x -pkgdir pkg-1 test, the executable can be built well, but it does not create a pkg-1 dir to place the tmp files.

So, my question is, how to use -pkgdir with go get and go build? does -pkgdir work as what its document says?

Thanks.

TL;DR; -pkgdir doesn’t work with go get.

What is the problem you’re trying to solve? Maybe there is a better way to solve your problem.

1 Like

Hey @KDr2,

What is it that you think the -pkgdir flag should do in this case and what are you actually trying to do here?

Here is the description for the pkgdir flag:

install and load all packages from dir instead of the usual locations. For example, when building with a non-standard configuration, use -pkgdir to keep generated packages in a separate location.

Edit: Beaten to my question lol :stuck_out_tongue:

I think at least it should work, but not leads to an error.

Actually, my situation is:

  • I am using a read-only GOROOT
  • I run go get with option -a to recompile all of my code and its dependencies, but that will lead to an error with the read-only GOROOT
  • so I use pkgdir option, to place all the tmp files to a specific dir, but it leads to an error too.

There maybe have another way to solve this, but I want to know is the -pkgdir option implemented right and how to use it.

If you don’t need to fetch code from the internet, go install or go build -i might be better candidates.

As another possibility, rm -rf $GOPATH/pkg.

Hey again @KDr2,

I feel like you have confused the go install and/or the go build commands with the go get command which is what you mainly use if you need to install remote packages, not to install local packages which I think you’re trying to do.

Thanks, rm -fr $GOPATH/pkg maybe a good idea :slight_smile:

But, is it a bug:

  • error occurs when run go get with -pkgdir option
  • -pkgdir option has no effect in go build command

Why not just use go get -u pkg if you need to update the dependencies of the package and then use go build or go install afterwards to install the actual package?

The -u flag instructs get to use the network to update the named packages and their dependencies. By default, get uses the network to check out missing packages but does not use it to look for updates to existing packages.

This is probably not the answer you are looking for, but if you don’t use -a then you won’t need -pkgdir.

Can you explain why you use -a, what is the problem you are trying to solve?

Hi, thank you all very much, my problem about recompiling all source code has been resolved by removing the pkg dir.

My new problem is, why:

  1. error occurs when run go get with -pkgdir option
  2. -pkgdir option has no effect in go build command

It seems a bug of that option?

I don’t know, but for what it’s worth it works for me (not that I’ve ever used it before):

jb@unu:~ $ go get -v -pkgdir /tmp/foo github.com/calmh/du
runtime/internal/sys
runtime/internal/atomic
...
github.com/calmh/du
jb@unu:~ $ ls -l /tmp/foo
total 8520
-rw-r--r--  1 jb  staff     3748 Mar 31 11:53 errors.a
drwxr-xr-x  3 jb  wheel      102 Mar 31 11:53 github.com/
drwxr-xr-x  3 jb  wheel      102 Mar 31 11:53 internal/
...
jb@unu:~ $ go version
go version go1.8 darwin/amd64

Hi, @calmh, thank you for the reply, I’m using go1.8 too but on Linux x64, would you like to try my example?

Many thanks again.

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