Skip to content

Commit

Permalink
cmd/go: move module cache from $GOPATH/src/mod to $GOPATH/pkg/mod
Browse files Browse the repository at this point in the history
Using $GOPATH/src/mod confuses too many tools.
$GOPATH/pkg/mod seems better for now.
It's also next to dep's cache, $GOPATH/pkg/dep.
If we do eliminate GOPATH/pkg for holding .a files (#4719)
then we could still keep it around for pkg/mod.
(Or we could move the module cache again then.)

Fixes #26401.
Fixes #26635.

Change-Id: I18f7da216ed9f490eded3c00d837fb086ae5b6a4
Reviewed-on: https://go-review.googlesource.com/126755
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Rob Pike <r@golang.org>
  • Loading branch information
rsc committed Aug 1, 2018
1 parent 27e546b commit b8f42d7
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 101 deletions.
111 changes: 65 additions & 46 deletions src/cmd/go/alldocs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/cmd/go/internal/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ func runClean(cmd *base.Command, args []string) {
}

if cleanModcache {
if modfetch.SrcMod == "" {
if modfetch.PkgMod == "" {
base.Fatalf("go clean -modcache: no module cache")
}
if err := removeAll(modfetch.SrcMod); err != nil {
if err := removeAll(modfetch.PkgMod); err != nil {
base.Errorf("go clean -modcache: %v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/help/helpdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ See https://golang.org/doc/code.html for an example.
GOPATH and Modules
When using modules, GOPATH is no longer used for resolving imports.
However, it is still used to store downloaded source code (in GOPATH/src/mod)
However, it is still used to store downloaded source code (in GOPATH/pkg/mod)
and compiled commands (in GOPATH/bin).
Internal Directories
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func LoadImport(path, srcDir string, parent *Package, stk *ImportStack, importPo

if strings.HasPrefix(path, "mod/") {
// Paths beginning with "mod/" might accidentally
// look in the module cache directory tree in $GOPATH/src/mod/.
// look in the module cache directory tree in $GOPATH/pkg/mod/.
// This prefix is owned by the Go core for possible use in the
// standard library (since it does not begin with a domain name),
// so it's OK to disallow entirely.
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modconv/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func testMain(m *testing.M) int {
log.Fatal(err)
}
defer os.RemoveAll(dir)
modfetch.SrcMod = filepath.Join(dir, "src/mod")
modfetch.PkgMod = filepath.Join(dir, "pkg/mod")
codehost.WorkRoot = filepath.Join(dir, "codework")

return m.Run()
Expand Down
16 changes: 8 additions & 8 deletions src/cmd/go/internal/modfetch/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ import (

var QuietLookup bool // do not print about lookups

var SrcMod string // $GOPATH/src/mod; set by package modload
var PkgMod string // $GOPATH/pkg/mod; set by package modload

func cacheDir(path string) (string, error) {
if SrcMod == "" {
return "", fmt.Errorf("internal error: modfetch.SrcMod not set")
if PkgMod == "" {
return "", fmt.Errorf("internal error: modfetch.PkgMod not set")
}
enc, err := module.EncodePath(path)
if err != nil {
return "", err
}
return filepath.Join(SrcMod, "cache/download", enc, "/@v"), nil
return filepath.Join(PkgMod, "cache/download", enc, "/@v"), nil
}

func CachePath(m module.Version, suffix string) (string, error) {
Expand All @@ -54,8 +54,8 @@ func CachePath(m module.Version, suffix string) (string, error) {
}

func DownloadDir(m module.Version) (string, error) {
if SrcMod == "" {
return "", fmt.Errorf("internal error: modfetch.SrcMod not set")
if PkgMod == "" {
return "", fmt.Errorf("internal error: modfetch.PkgMod not set")
}
enc, err := module.EncodePath(m.Path)
if err != nil {
Expand All @@ -71,7 +71,7 @@ func DownloadDir(m module.Version) (string, error) {
if err != nil {
return "", err
}
return filepath.Join(SrcMod, enc+"@"+encVer), nil
return filepath.Join(PkgMod, enc+"@"+encVer), nil
}

// A cachingRepo is a cache around an underlying Repo,
Expand Down Expand Up @@ -287,7 +287,7 @@ func readDiskStat(path, rev string) (file string, info *RevInfo, err error) {
// just to find out about a commit we already know about
// (and have cached under its pseudo-version).
func readDiskStatByHash(path, rev string) (file string, info *RevInfo, err error) {
if SrcMod == "" {
if PkgMod == "" {
// Do not download to current directory.
return "", nil, errNotCached
}
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/go/internal/modfetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ var downloadCache par.Cache
// local download cache and returns the name of the directory
// corresponding to the root of the module's file tree.
func Download(mod module.Version) (dir string, err error) {
if SrcMod == "" {
if PkgMod == "" {
// Do not download to current directory.
return "", fmt.Errorf("missing modfetch.SrcMod")
return "", fmt.Errorf("missing modfetch.PkgMod")
}

// The par.Cache here avoids duplicate work but also
Expand All @@ -53,7 +53,7 @@ func Download(mod module.Version) (dir string, err error) {
if _, err := os.Stat(zipfile); err == nil {
// Use it.
// This should only happen if the mod/cache directory is preinitialized
// or if src/mod/path was removed but not src/mod/cache/download.
// or if pkg/mod/path was removed but not pkg/mod/cache/download.
fmt.Fprintf(os.Stderr, "go: extracting %s %s\n", mod.Path, mod.Version)
} else {
if err := os.MkdirAll(filepath.Dir(zipfile), 0777); err != nil {
Expand Down Expand Up @@ -200,7 +200,7 @@ func readGoSum(file string, data []byte) {

// checkSum checks the given module's checksum.
func checkSum(mod module.Version) {
if SrcMod == "" {
if PkgMod == "" {
// Do not use current directory.
return
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func checkOneSum(mod module.Version, h string) {
// Sum returns the checksum for the downloaded copy of the given module,
// if present in the download cache.
func Sum(mod module.Version) string {
if SrcMod == "" {
if PkgMod == "" {
// Do not use current directory.
return ""
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go/internal/modfetch/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ archive.
Even when downloading directly from version control systems,
the go command synthesizes explicit info, mod, and zip files
and stores them in its local cache, $GOPATH/src/mod/cache/download,
and stores them in its local cache, $GOPATH/pkg/mod/cache/download,
the same as if it had downloaded them directly from a proxy.
The cache layout is the same as the proxy URL space, so
serving $GOPATH/src/mod/cache/download at (or copying it to)
serving $GOPATH/pkg/mod/cache/download at (or copying it to)
https://example.com/proxy would let other users access those
cached module versions with GOPROXY=https://example.com/proxy.
`,
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modload/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ GOPATH/src and itself contains a go.mod file or is below a directory
containing a go.mod file.
In module-aware mode, GOPATH no longer defines the meaning of imports
during a build, but it still stores downloaded dependencies (in GOPATH/src/mod)
during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod)
and installed commands (in GOPATH/bin, unless GOBIN is set).
Defining a module
Expand Down
Loading

0 comments on commit b8f42d7

Please sign in to comment.