]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: print error if GOROOT is inside a module
authorJay Conrod <jayconrod@google.com>
Wed, 22 Jan 2020 23:30:03 +0000 (15:30 -0800)
committerJay Conrod <jayconrod@google.com>
Thu, 23 Jan 2020 00:22:41 +0000 (00:22 +0000)
Fixes #36701

Change-Id: I22738235e7a7ee06bc5d748213aab523aad8cf12
Reviewed-on: https://go-review.googlesource.com/c/go/+/215939
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Marwan Sulaiman <marwan.sameer@gmail.com>
src/cmd/dist/build.go

index 206c65f52f3971546a4df6c7728716e54b430aa8..7a6ba52e374a9de8066ae871e5188ec21e009c9b 100644 (file)
@@ -110,6 +110,9 @@ func xinit() {
                fatalf("$GOROOT must be set")
        }
        goroot = filepath.Clean(b)
+       if modRoot := findModuleRoot(goroot); modRoot != "" {
+               fatalf("found go.mod file in %s: $GOROOT must not be inside a module", modRoot)
+       }
 
        b = os.Getenv("GOROOT_FINAL")
        if b == "" {
@@ -1590,6 +1593,20 @@ func checkCC() {
        }
 }
 
+func findModuleRoot(dir string) (root string) {
+       for {
+               if fi, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
+                       return dir
+               }
+               d := filepath.Dir(dir)
+               if d == dir {
+                       break
+               }
+               dir = d
+       }
+       return ""
+}
+
 func defaulttarg() string {
        // xgetwd might return a path with symlinks fully resolved, and if
        // there happens to be symlinks in goroot, then the hasprefix test