From a8d0047e473510db1b1a5e35c03fdf41a13b5733 Mon Sep 17 00:00:00 2001 From: Baokun Lee Date: Tue, 15 Jan 2019 18:53:44 +0800 Subject: [PATCH] cmd/go/internal/modcmd: allow mod download without go.mod Fixes #29522 Change-Id: I48f3a945d24c23c7c7ef5c7f1fe5046b6b2898e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/157937 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Bryan C. Mills --- src/cmd/go/internal/modcmd/download.go | 5 +++-- src/cmd/go/internal/modload/init.go | 4 +++- src/cmd/go/testdata/script/mod_download.txt | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/cmd/go/internal/modcmd/download.go b/src/cmd/go/internal/modcmd/download.go index bbaba444f50..9f8c410b82f 100644 --- a/src/cmd/go/internal/modcmd/download.go +++ b/src/cmd/go/internal/modcmd/download.go @@ -5,13 +5,14 @@ package modcmd import ( + "encoding/json" + "os" + "cmd/go/internal/base" "cmd/go/internal/modfetch" "cmd/go/internal/modload" "cmd/go/internal/module" "cmd/go/internal/par" - "encoding/json" - "os" ) var cmdDownload = &base.Command{ diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index eaf44075295..ef2786109b6 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -253,9 +253,11 @@ func Init() { func init() { load.ModInit = Init - // Set modfetch.PkgMod unconditionally, so that go clean -modcache can run even without modules enabled. + // Set modfetch.PkgMod and codehost.WorkRoot unconditionally, + // so that go clean -modcache and go mod download can run even without modules enabled. if list := filepath.SplitList(cfg.BuildContext.GOPATH); len(list) > 0 && list[0] != "" { modfetch.PkgMod = filepath.Join(list[0], "pkg/mod") + codehost.WorkRoot = filepath.Join(list[0], "pkg/mod/cache/vcs") } } diff --git a/src/cmd/go/testdata/script/mod_download.txt b/src/cmd/go/testdata/script/mod_download.txt index 38043b25974..62b9eee5478 100644 --- a/src/cmd/go/testdata/script/mod_download.txt +++ b/src/cmd/go/testdata/script/mod_download.txt @@ -82,5 +82,12 @@ exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.3-pre1.zip go mod download -json rsc.io/quote@v1.5.1 exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.1.zip +# allow go mod download without go.mod +env GO111MODULE=auto +rm go.mod +rm $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.2.1.zip +go mod download rsc.io/quote@v1.2.1 +exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.2.1.zip + -- go.mod -- module m