)
func cacheDir(path string) (string, error) {
- if cfg.GOMODCACHE == "" {
- // modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
- // is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
- return "", fmt.Errorf("internal error: cfg.GOMODCACHE not set")
+ if err := checkCacheDir(); err != nil {
+ return "", err
}
enc, err := module.EscapePath(path)
if err != nil {
// along with the directory if the directory does not exist or if the directory
// is not completely populated.
func DownloadDir(m module.Version) (string, error) {
- if cfg.GOMODCACHE == "" {
- // modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
- // is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
- return "", fmt.Errorf("internal error: cfg.GOMODCACHE not set")
+ if err := checkCacheDir(); err != nil {
+ return "", err
}
enc, err := module.EscapePath(m.Path)
if err != nil {
// user's working directory.
// If err is nil, the caller MUST eventually call the unlock function.
func SideLock() (unlock func(), err error) {
- if cfg.GOMODCACHE == "" {
- // modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
- // is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
- base.Fatalf("go: internal error: cfg.GOMODCACHE not set")
+ if err := checkCacheDir(); err != nil {
+ base.Fatalf("go: %v", err)
}
path := filepath.Join(cfg.GOMODCACHE, "cache", "lock")
base.Fatalf("go: failed to write version list: %v", err)
}
}
+
+func checkCacheDir() error {
+ if cfg.GOMODCACHE == "" {
+ // modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
+ // is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
+ return fmt.Errorf("internal error: cfg.GOMODCACHE not set")
+ }
+
+ if !filepath.IsAbs(cfg.GOMODCACHE) {
+ return fmt.Errorf("GOMODCACHE entry is relative; must be absolute path: %q.\n", cfg.GOMODCACHE)
+ }
+ return nil
+}
// local download cache and returns the name of the directory
// corresponding to the root of the module's file tree.
func Download(ctx context.Context, mod module.Version) (dir string, err error) {
- if cfg.GOMODCACHE == "" {
- // modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
- // is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
- base.Fatalf("go: internal error: cfg.GOMODCACHE not set")
+ if err := checkCacheDir(); err != nil {
+ base.Fatalf("go: %v", err)
}
// The par.Cache here avoids duplicate work.