Commit c4c37547 authored by Jay Conrod's avatar Jay Conrod

cmd/go/internal/modload: remove cwd global

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: default avatarBryan C. Mills <bcmills@google.com>
parent f922cc64
...@@ -34,7 +34,6 @@ import ( ...@@ -34,7 +34,6 @@ import (
) )
var ( var (
cwd string // TODO(bcmills): Is this redundant with base.Cwd?
mustUseModules = false mustUseModules = false
initialized bool initialized bool
...@@ -132,17 +131,11 @@ func Init() { ...@@ -132,17 +131,11 @@ func Init() {
os.Setenv("GIT_SSH_COMMAND", "ssh -o ControlMaster=no") 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 { if CmdModInit {
// Running 'go mod init': go.mod will be created in current directory. // Running 'go mod init': go.mod will be created in current directory.
modRoot = cwd modRoot = base.Cwd
} else { } else {
modRoot = findModuleRoot(cwd) modRoot = findModuleRoot(base.Cwd)
if modRoot == "" { if modRoot == "" {
if !mustUseModules { if !mustUseModules {
// GO111MODULE is 'auto', and we can't find a module root. // GO111MODULE is 'auto', and we can't find a module root.
...@@ -272,18 +265,16 @@ func die() { ...@@ -272,18 +265,16 @@ func die() {
if cfg.Getenv("GO111MODULE") == "off" { if cfg.Getenv("GO111MODULE") == "off" {
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'") base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
} }
if cwd != "" { if dir, name := findAltConfig(base.Cwd); dir != "" {
if dir, name := findAltConfig(cwd); dir != "" { rel, err := filepath.Rel(base.Cwd, dir)
rel, err := filepath.Rel(cwd, dir) if err != nil {
if err != nil { rel = dir
rel = dir }
} cdCmd := ""
cdCmd := "" if rel != "." {
if rel != "." { cdCmd = fmt.Sprintf("cd %s && ", 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, 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'") base.Fatalf("go: cannot find main module; see 'go help modules'")
} }
...@@ -370,7 +361,7 @@ func AllowMissingModuleImports() { ...@@ -370,7 +361,7 @@ func AllowMissingModuleImports() {
func modFileToBuildList() { func modFileToBuildList() {
Target = modFile.Module.Mod Target = modFile.Module.Mod
targetPrefix = Target.Path targetPrefix = Target.Path
if rel := search.InDir(cwd, cfg.GOROOTsrc); rel != "" { if rel := search.InDir(base.Cwd, cfg.GOROOTsrc); rel != "" {
targetInGorootSrc = true targetInGorootSrc = true
if Target.Path == "std" { if Target.Path == "std" {
targetPrefix = "" targetPrefix = ""
...@@ -584,6 +575,9 @@ var altConfigs = []string{ ...@@ -584,6 +575,9 @@ var altConfigs = []string{
} }
func findModuleRoot(dir string) (root string) { func findModuleRoot(dir string) (root string) {
if dir == "" {
panic("dir not set")
}
dir = filepath.Clean(dir) dir = filepath.Clean(dir)
// Look for enclosing go.mod. // Look for enclosing go.mod.
...@@ -601,6 +595,9 @@ func findModuleRoot(dir string) (root string) { ...@@ -601,6 +595,9 @@ func findModuleRoot(dir string) (root string) {
} }
func findAltConfig(dir string) (root, name string) { func findAltConfig(dir string) (root, name string) {
if dir == "" {
panic("dir not set")
}
dir = filepath.Clean(dir) dir = filepath.Clean(dir)
for { for {
for _, name := range altConfigs { for _, name := range altConfigs {
......
...@@ -95,7 +95,7 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match { ...@@ -95,7 +95,7 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
for _, pkg := range pkgs { for _, pkg := range pkgs {
dir := pkg dir := pkg
if !filepath.IsAbs(dir) { if !filepath.IsAbs(dir) {
dir = filepath.Join(cwd, pkg) dir = filepath.Join(base.Cwd, pkg)
} else { } else {
dir = filepath.Clean(dir) dir = filepath.Clean(dir)
} }
...@@ -321,7 +321,7 @@ func DirImportPath(dir string) string { ...@@ -321,7 +321,7 @@ func DirImportPath(dir string) string {
} }
if !filepath.IsAbs(dir) { if !filepath.IsAbs(dir) {
dir = filepath.Join(cwd, dir) dir = filepath.Join(base.Cwd, dir)
} else { } else {
dir = filepath.Clean(dir) dir = filepath.Clean(dir)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment