cmd/go/internal/modfetch: check errors for if file does not exist

The Repo interface now calls out that for ReadFile if the error
reports that the requested file does not exist, then os.IsNotExist
should return true. Use this information when returning an
error message to the user.

This will help users diagnose errors quicker and more accuratly.

Extracted from https://golang.org/cl/114005

Change-Id: Ifb6122f0e8449c2b5bdad5af6bce1b038a3fe616
Reviewed-on: https://go-review.googlesource.com/119135
Reviewed-by: Russ Cox <rsc@golang.org>
diff --git a/vendor/cmd/go/internal/modfetch/codehost/codehost.go b/vendor/cmd/go/internal/modfetch/codehost/codehost.go
index 0e3bb7d..218bb1a 100644
--- a/vendor/cmd/go/internal/modfetch/codehost/codehost.go
+++ b/vendor/cmd/go/internal/modfetch/codehost/codehost.go
@@ -50,6 +50,9 @@
 
 	// ReadFile reads the given file in the file tree corresponding to revision rev.
 	// It should refuse to read more than maxSize bytes.
+	//
+	// If the requested file does not exist it should return an error for which
+	// os.IsNotExist(err) returns true.
 	ReadFile(rev, file string, maxSize int64) (data []byte, err error)
 
 	// ReadZip downloads a zip file for the subdir subdirectory
diff --git a/vendor/cmd/go/internal/modfetch/coderepo.go b/vendor/cmd/go/internal/modfetch/coderepo.go
index bd815e1..703b350 100644
--- a/vendor/cmd/go/internal/modfetch/coderepo.go
+++ b/vendor/cmd/go/internal/modfetch/coderepo.go
@@ -212,7 +212,10 @@
 		gomod1, err1 := r.code.ReadFile(rev, file1, codehost.MaxGoMod)
 		r.codeMu.Unlock()
 		if err1 != nil {
-			return "", "", nil, fmt.Errorf("missing go.mod")
+			if os.IsNotExist(err1) {
+				return "", "", nil, errors.New("missing go.mod")
+			}
+			return "", "", nil, fmt.Errorf("reading go.mod: %v", err1)
 		}
 		return rev, r.codeDir, gomod1, nil
 	}
@@ -230,6 +233,14 @@
 	gomod1, err1 := r.code.ReadFile(rev, file1, codehost.MaxGoMod)
 	gomod2, err2 := r.code.ReadFile(rev, file2, codehost.MaxGoMod)
 	r.codeMu.Unlock()
+
+	if err1 != nil && !os.IsNotExist(err1) {
+		return "", "", nil, fmt.Errorf("reading %s: %v", file1, err1)
+	}
+	if err2 != nil && !os.IsNotExist(err2) {
+		return "", "", nil, fmt.Errorf("reading %s: %v", file2, err2)
+	}
+
 	found1 := err1 == nil && isMajor(gomod1, r.pathMajor)
 	found2 := err2 == nil && isMajor(gomod2, r.pathMajor)
 
@@ -264,7 +275,7 @@
 	data, err = r.code.ReadFile(rev, path.Join(dir, "go.mod"), codehost.MaxGoMod)
 	r.codeMu.Unlock()
 	if err != nil {
-		if e := strings.ToLower(err.Error()); strings.Contains(e, "not found") || strings.Contains(e, "404") { // TODO
+		if os.IsNotExist(err) {
 			return r.legacyGoMod(rev, dir), nil
 		}
 		return nil, err
diff --git a/vendor/cmd/go/internal/modfetch/gitrepo/fetch.go b/vendor/cmd/go/internal/modfetch/gitrepo/fetch.go
index 532d3f3..8a653a8 100644
--- a/vendor/cmd/go/internal/modfetch/gitrepo/fetch.go
+++ b/vendor/cmd/go/internal/modfetch/gitrepo/fetch.go
@@ -279,7 +279,7 @@
 				return &codehost.RevInfo{Version: rev}, out, nil
 			}
 			if bytes.Contains(err.(*codehost.RunError).Stderr, []byte("did not match any files")) {
-				return nil, nil, fmt.Errorf("file not found")
+				return nil, nil, os.ErrNotExist
 			}
 			if bytes.Contains(err.(*codehost.RunError).Stderr, []byte("Operation not supported by protocol")) {
 				r.canArchive = false
@@ -422,7 +422,7 @@
 	}
 	out, err := codehost.Run(r.dir, "git", "cat-file", "blob", info.Name+":"+file)
 	if err != nil {
-		return nil, fmt.Errorf("file not found")
+		return nil, os.ErrNotExist
 	}
 	return out, nil
 }
@@ -441,7 +441,7 @@
 		archive, err = codehost.Run(r.dir, "git", "archive", "--format=zip", "--prefix=prefix/", info.Name, args)
 		if err != nil {
 			if bytes.Contains(err.(*codehost.RunError).Stderr, []byte("did not match any files")) {
-				return nil, "", fmt.Errorf("file not found")
+				return nil, "", os.ErrNotExist
 			}
 			return nil, "", err
 		}
diff --git a/vendor/cmd/go/internal/modfetch/gitrepo/fetch_test.go b/vendor/cmd/go/internal/modfetch/gitrepo/fetch_test.go
index ca93280..b2d2524 100644
--- a/vendor/cmd/go/internal/modfetch/gitrepo/fetch_test.go
+++ b/vendor/cmd/go/internal/modfetch/gitrepo/fetch_test.go
@@ -173,7 +173,7 @@
 		repo: gitrepo1,
 		rev:  "v2.3.4",
 		file: "another.txt",
-		err:  "file not found",
+		err:  os.ErrNotExist.Error(),
 	},
 }