]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: remove cwd global
authorJay Conrod <jayconrod@google.com>
Wed, 23 Oct 2019 21:50:22 +0000 (17:50 -0400)
committerJay Conrod <jayconrod@google.com>
Thu, 24 Oct 2019 20:15:14 +0000 (20:15 +0000)
base.Cwd should be used instead.

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

index 2f1509315890a31bd03d21058cca190f1bcb8eaf..393121df6cd0904765b40c5aad4735bc597e2e62 100644 (file)
@@ -34,7 +34,6 @@ import (
 )
 
 var (
-       cwd            string // TODO(bcmills): Is this redundant with base.Cwd?
        mustUseModules = false
        initialized    bool
 
@@ -132,17 +131,11 @@ func Init() {
                os.Setenv("GIT_SSH_COMMAND", "ssh -o ControlMaster=no")
        }
 
-       var err error
-       cwd, err = os.Getwd()
-       if err != nil {
-               base.Fatalf("go: %v", err)
-       }
-
        if CmdModInit {
                // Running 'go mod init': go.mod will be created in current directory.
-               modRoot = cwd
+               modRoot = base.Cwd
        } else {
-               modRoot = findModuleRoot(cwd)
+               modRoot = findModuleRoot(base.Cwd)
                if modRoot == "" {
                        if !mustUseModules {
                                // GO111MODULE is 'auto', and we can't find a module root.
@@ -272,18 +265,16 @@ func die() {
        if cfg.Getenv("GO111MODULE") == "off" {
                base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
        }
-       if cwd != "" {
-               if dir, name := findAltConfig(cwd); dir != "" {
-                       rel, err := filepath.Rel(cwd, dir)
-                       if err != nil {
-                               rel = dir
-                       }
-                       cdCmd := ""
-                       if rel != "." {
-                               cdCmd = fmt.Sprintf("cd %s && ", rel)
-                       }
-                       base.Fatalf("go: cannot find main module, but found %s in %s\n\tto create a module there, run:\n\t%sgo mod init", name, dir, cdCmd)
+       if dir, name := findAltConfig(base.Cwd); dir != "" {
+               rel, err := filepath.Rel(base.Cwd, dir)
+               if err != nil {
+                       rel = dir
+               }
+               cdCmd := ""
+               if rel != "." {
+                       cdCmd = fmt.Sprintf("cd %s && ", rel)
                }
+               base.Fatalf("go: cannot find main module, but found %s in %s\n\tto create a module there, run:\n\t%sgo mod init", name, dir, cdCmd)
        }
        base.Fatalf("go: cannot find main module; see 'go help modules'")
 }
@@ -370,7 +361,7 @@ func AllowMissingModuleImports() {
 func modFileToBuildList() {
        Target = modFile.Module.Mod
        targetPrefix = Target.Path
-       if rel := search.InDir(cwd, cfg.GOROOTsrc); rel != "" {
+       if rel := search.InDir(base.Cwd, cfg.GOROOTsrc); rel != "" {
                targetInGorootSrc = true
                if Target.Path == "std" {
                        targetPrefix = ""
@@ -584,6 +575,9 @@ var altConfigs = []string{
 }
 
 func findModuleRoot(dir string) (root string) {
+       if dir == "" {
+               panic("dir not set")
+       }
        dir = filepath.Clean(dir)
 
        // Look for enclosing go.mod.
@@ -601,6 +595,9 @@ func findModuleRoot(dir string) (root string) {
 }
 
 func findAltConfig(dir string) (root, name string) {
+       if dir == "" {
+               panic("dir not set")
+       }
        dir = filepath.Clean(dir)
        for {
                for _, name := range altConfigs {
index a9f711733c2fee6546d50f0cb4def8948dc4a3bb..5f28d7cf14a857fbba5a40ae49dbd2483e8721e1 100644 (file)
@@ -95,7 +95,7 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
                                for _, pkg := range pkgs {
                                        dir := pkg
                                        if !filepath.IsAbs(dir) {
-                                               dir = filepath.Join(cwd, pkg)
+                                               dir = filepath.Join(base.Cwd, pkg)
                                        } else {
                                                dir = filepath.Clean(dir)
                                        }
@@ -321,7 +321,7 @@ func DirImportPath(dir string) string {
        }
 
        if !filepath.IsAbs(dir) {
-               dir = filepath.Join(cwd, dir)
+               dir = filepath.Join(base.Cwd, dir)
        } else {
                dir = filepath.Clean(dir)
        }