]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: ignore directories when looking for go.mod file in Plan 9
authorFazlul Shahriar <fshahriar@gmail.com>
Sat, 18 Aug 2018 19:11:06 +0000 (15:11 -0400)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 20 Aug 2018 19:14:25 +0000 (19:14 +0000)
This fixes builds in Plan 9 under /n. Since directories in /n are
automatically created, /n/go.mod always exists.

Fixes #27074

Change-Id: Ie9a1155b7c316bdc27655f5b99172550b413838d
Reviewed-on: https://go-review.googlesource.com/129804
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/go/internal/modload/init.go
src/cmd/go/internal/modload/init_plan9_test.go [new file with mode: 0644]

index f995bad13b5433a71246672d1f548c3522000da3..2bab3eede1387d24a2c8be6bb4a9f0520198bc91 100644 (file)
@@ -24,6 +24,7 @@ import (
        "path"
        "path/filepath"
        "regexp"
+       "runtime"
        "strconv"
        "strings"
 )
@@ -379,7 +380,7 @@ func FindModuleRoot(dir, limit string, legacyConfigOK bool) (root, file string)
 
        // Look for enclosing go.mod.
        for {
-               if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
+               if fi, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil && !(runtime.GOOS == "plan9" && fi.IsDir()) {
                        return dir, "go.mod"
                }
                if dir == limit {
@@ -397,7 +398,7 @@ func FindModuleRoot(dir, limit string, legacyConfigOK bool) (root, file string)
                dir = dir1
                for {
                        for _, name := range altConfigs {
-                               if _, err := os.Stat(filepath.Join(dir, name)); err == nil {
+                               if fi, err := os.Stat(filepath.Join(dir, name)); err == nil && !(runtime.GOOS == "plan9" && fi.IsDir()) {
                                        return dir, name
                                }
                        }
diff --git a/src/cmd/go/internal/modload/init_plan9_test.go b/src/cmd/go/internal/modload/init_plan9_test.go
new file mode 100644 (file)
index 0000000..2df9d8a
--- /dev/null
@@ -0,0 +1,42 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package modload
+
+import (
+       "io/ioutil"
+       "os"
+       "path/filepath"
+       "testing"
+)
+
+func TestFindModuleRootIgnoreDir(t *testing.T) {
+       // In Plan 9, directories are automatically created in /n.
+       // For example, /n/go.mod always exist, but it's a directory.
+       // Test that we ignore directories when trying to find go.mod and other config files.
+
+       dir, err := ioutil.TempDir("", "gotest")
+       if err != nil {
+               t.Fatalf("failed to create temporary directory: %v", err)
+       }
+       defer os.RemoveAll(dir)
+       if err := os.Mkdir(filepath.Join(dir, "go.mod"), os.ModeDir|0755); err != nil {
+               t.Fatalf("Mkdir failed: %v", err)
+       }
+       for _, name := range altConfigs {
+               if err := os.MkdirAll(filepath.Join(dir, name), os.ModeDir|0755); err != nil {
+                       t.Fatalf("MkdirAll failed: %v", err)
+               }
+       }
+       p := filepath.Join(dir, "example")
+       if err := os.Mkdir(p, os.ModeDir|0755); err != nil {
+               t.Fatalf("Mkdir failed: %v", err)
+       }
+       if root, _ := FindModuleRoot(p, "", false); root != "" {
+               t.Errorf("FindModuleRoot(%q, \"\", false): %q, want empty string", p, root)
+       }
+       if root, _ := FindModuleRoot(p, "", true); root != "" {
+               t.Errorf("FindModuleRoot(%q, \"\", true): %q, want empty string", p, root)
+       }
+}