]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: diagnose missing replacement directories
authorBryan C. Mills <bcmills@google.com>
Fri, 20 Dec 2019 14:34:30 +0000 (09:34 -0500)
committerBryan C. Mills <bcmills@google.com>
Fri, 20 Dec 2019 15:33:31 +0000 (15:33 +0000)
I noticed the missing diagnostic when writing a regression test for #33795.

Change-Id: Ic3249436a6109d71f9ff720b7096f9b872f6a94b
Reviewed-on: https://go-review.googlesource.com/c/go/+/212201
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/mod_replace_import.txt

index 2df7bd04b7a7baf47417f2226e4ba927e8392d2f..58e2141f65fdd3bc4b8c1180daed42f1cfe20eb2 100644 (file)
@@ -1320,6 +1320,21 @@ func fetch(mod module.Version) (dir string, isLocal bool, err error) {
                        if !filepath.IsAbs(dir) {
                                dir = filepath.Join(ModRoot(), dir)
                        }
+                       // Ensure that the replacement directory actually exists:
+                       // dirInModule does not report errors for missing modules,
+                       // so if we don't report the error now, later failures will be
+                       // very mysterious.
+                       if _, err := os.Stat(dir); err != nil {
+                               if os.IsNotExist(err) {
+                                       // Semantically the module version itself “exists” — we just don't
+                                       // have its source code. Remove the equivalence to os.ErrNotExist,
+                                       // and make the message more concise while we're at it.
+                                       err = fmt.Errorf("replacement directory %s does not exist", r.Path)
+                               } else {
+                                       err = fmt.Errorf("replacement directory %s: %w", r.Path, err)
+                               }
+                               return dir, true, module.VersionError(mod, err)
+                       }
                        return dir, true, nil
                }
                mod = r
index 646b3b081deb3a473bc47fbee96daa9310321ea3..fd5b04a498739b4b358aef1ea2514c2202eee5ed 100644 (file)
@@ -28,7 +28,8 @@ stdout 'example.com/v v1.12.0 => ./v12'
 cd fail
 ! go list all
 stdout 'localhost.fail'
-stderr '^can.t load package: m.go:3:8: module w@latest found \(v0.0.0-00010101000000-000000000000, replaced by ../w\), but does not contain package w$'
+stderr '^can''t load package: m.go:4:2: module w@latest found \(v0.0.0-00010101000000-000000000000, replaced by ../w\), but does not contain package w$'
+stderr '^can''t load package: m.go:5:2: nonexist@v0.1.0: replacement directory ../nonexist does not exist$'
 
 -- go.mod --
 module example.com/m
@@ -128,7 +129,10 @@ package i
 -- fail/m.go --
 package main
 
-import _ "w"
+import (
+       _ "w"
+       _ "nonexist"
+)
 
 func main() {}
 
@@ -137,3 +141,4 @@ module localhost.fail
 
 replace w => ../w
 
+replace nonexist v0.1.0 => ../nonexist