golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.68k stars 17.49k forks source link

cmd/go: mod - Recreate $GOPATH from go.mod/go.sum files #28295

Closed rkedward closed 5 years ago

rkedward commented 5 years ago

Please answer these questions before submitting your issue. Thanks!

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

1.11.1

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/nfs/orto/home/rkedward/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/hnfs/torcfs01n04a/vol/ret_users_rkedward/dev/play/grpc_golang_examples/.gospace"
GOPROXY=""
GORACE=""
GOROOT="/p/ret/rettools/stream/pkgs/golang/1.11.1"
GOTMPDIR=""
GOTOOLDIR="/p/ret/rettools/stream/pkgs/golang/1.11.1/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/hnfs/torcfs01n04a/vol/ret_users_rkedward/dev/play/grpc_golang_examples/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build592164460=/tmp/go-build -gno-record-gcc-switches"

What did you do?

% go 
go version go1.11.1 linux/amd64

# 1. Setup a project with a local gospace
% mkdir -p /path/to/mymod && cd /path/to/mymod
% export GOPATH=/path/to/mymod/.gospace
% mkdir -p  $GOPATH/pkg  # create the local gospace (aka virtual environment)
% mkdir -p  $GOPATH/bin

# 2. Install a module and package into the venv
% go mod  init example.com/myname/mymod
% go get -u google.golang.org/grpc  # install module
% go get -u github.com/golang/protobuf/protoc-gen-go  # install cmd/package
% ls $GOPATH/bin
protoc-gen-go

# 3. Wipe $GOPATH and reinstall from go.mod/go.sum files
% rm -rf  $GOPATH
% mkdir -p  $GOPATH/pkg
% mkdir -p  $GOPATH/bin
% go mod download 
% ls %GOPATH/bin
<empty>
...

What did you expect to see?

The protoc-gen-go file $GOPATH/bin directory after running go mod download

What did you see instead?

An empty directory

Rational

I would like to be able to recreate the $GOPATH exactly (bin, pkgs) from go.mod and go.sum. Since go.mod and go.sum are checked into the repository, another could clone the repo and recreate my development environment. This has many advantages for debugging/recreating issues to sharing project development environments between members.

thepudds commented 5 years ago

@gopherbot, please add label modules

thepudds commented 5 years ago

@rkedward I'm not sure that go mod download is the what you are looking for here. go mod download is for more narrow use cases such as pre-populating the module download cache, such as in base docker images: https://tip.golang.org/cmd/go/#hdr-Download_modules_to_local_cache

It sounds like part of what you are trying to do is track precise versions of tools as a dependency of your module? If so, you might want to give this FAQ on the modules wiki a quick read: https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module

and there is a related runnable example of tracking tools as dependencies here (from @myitcv): https://github.com/go-modules-by-example/index/blob/master/010_tools/README.md

thepudds commented 5 years ago

And sorry if that is not a perfect answer. You might want to give those a quick read, and then follow up back here with additional questions, or more comments on your use case.

myitcv commented 5 years ago

@rkedward - what you want to do here is use go mod vendor (see https://github.com/go-modules-by-example/index/blob/master/008_vendor_example/README.md for an example) and then move the vendor to be your $GOPATH/src directory. I do something similar in my CI for testing with Go 1.10 (ignore the GopherJS hack)

I'll close this on the basis that answers your question; but please shout it not and we can re-open.