aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/init.go
diff options
context:
space:
mode:
authorMichael Matloob <matloob@golang.org>2021-08-02 16:02:45 -0400
committerMichael Matloob <matloob@golang.org>2021-08-11 18:06:50 +0000
commitaaf914d0e69198a96683c106abb5a931c4956f88 (patch)
treedf56a6125db2ed28036813a0d179aae83d6cd89f /src/cmd/go/internal/modload/init.go
parent3025ce2fa83c86f3c802aab483535c39bad508ea (diff)
downloadgo-aaf914d0e69198a96683c106abb5a931c4956f88.tar.gz
go-aaf914d0e69198a96683c106abb5a931c4956f88.zip
[dev.cmdgo] cmd/go: remove modload.ModRoot function
In some cases, ModRoot was being called in a multi module context. In those cases, pass in the correct main module. In other cases, a mainModule variable was already available, so call MainModules.ModRoot on that mainModule variable to make it more clear. In yet other cases ModRoot is just needed to determine GoMod, so determine modroot from the current directory in those cases. For #45713 Change-Id: I8c8aa633cfae40d0c8740bdbf985f2b60c9daf2c Reviewed-on: https://go-review.googlesource.com/c/go/+/339171 Trust: Michael Matloob <matloob@golang.org> Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/init.go')
-rw-r--r--src/cmd/go/internal/modload/init.go48
1 files changed, 21 insertions, 27 deletions
diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index 5dd946215b..1a91b83148 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -374,7 +374,6 @@ func Init() {
}
if inWorkspaceMode() {
-
_ = TODOWorkspaces("go.work.sum, and also allow modfetch to fall back to individual go.sums")
_ = TODOWorkspaces("replaces")
var err error
@@ -403,8 +402,7 @@ func Init() {
//
// See golang.org/issue/32027.
} else {
- _ = TODOWorkspaces("Instead of modfile path, find modfile OR workfile path depending on mode")
- modfetch.GoSumFile = strings.TrimSuffix(ModFilePath(), ".mod") + ".sum"
+ modfetch.GoSumFile = strings.TrimSuffix(modFilePath(modRoots[0]), ".mod") + ".sum"
}
}
@@ -463,21 +461,8 @@ func Enabled() bool {
return modRoots != nil || cfg.ModulesEnabled
}
-// ModRoot returns the root of the main module.
-// It calls base.Fatalf if there is no main module.
-func ModRoot() string {
- if !HasModRoot() {
- die()
- }
- if inWorkspaceMode() {
- panic("ModRoot called in workspace mode")
- }
- // This is similar to MustGetSingleMainModule but we can't call that
- // because MainModules may not yet exist when ModRoot is called.
- if len(modRoots) != 1 {
- panic("not in workspace mode but there are multiple ModRoots")
- }
- return modRoots[0]
+func VendorDir() string {
+ return filepath.Join(MainModules.ModRoot(MainModules.mustGetSingleMainModule()), "vendor")
}
func inWorkspaceMode() bool {
@@ -495,12 +480,21 @@ func HasModRoot() bool {
return modRoots != nil
}
-// ModFilePath returns the effective path of the go.mod file. Normally, this
-// "go.mod" in the directory returned by ModRoot, but the -modfile flag may
-// change its location. ModFilePath calls base.Fatalf if there is no main
+// MustHaveModRoot checks that a main module or main modules are present,
+// and calls base.Fatalf if there are no main modules.
+func MustHaveModRoot() {
+ Init()
+ if !HasModRoot() {
+ die()
+ }
+}
+
+// ModFilePath returns the path that would be used for the go.mod
+// file, if in module mode. ModFilePath calls base.Fatalf if there is no main
// module, even if -modfile is set.
func ModFilePath() string {
- return modFilePath(ModRoot())
+ MustHaveModRoot()
+ return modFilePath(findModuleRoot(base.Cwd()))
}
func modFilePath(modRoot string) string {
@@ -674,7 +668,7 @@ func loadModFile(ctx context.Context) (rs *Requirements, needCommit bool) {
mainModule := MainModules.mustGetSingleMainModule()
if cfg.BuildMod == "vendor" {
- readVendorList()
+ readVendorList(mainModule)
index := MainModules.Index(mainModule)
modFile := MainModules.ModFile(mainModule)
checkVendorConsistency(index, modFile)
@@ -719,7 +713,7 @@ func CreateModFile(ctx context.Context, modPath string) {
modRoot := base.Cwd()
modRoots = []string{modRoot}
Init()
- modFilePath := ModFilePath()
+ modFilePath := modFilePath(modRoot)
if _, err := fsys.Stat(modFilePath); err == nil {
base.Fatalf("go: %s already exists", modFilePath)
}
@@ -1344,6 +1338,7 @@ func commitRequirements(ctx context.Context, goVersion string, rs *Requirements)
return
}
mainModule := MainModules.Versions()[0]
+ modFilePath := modFilePath(MainModules.ModRoot(mainModule))
modFile := MainModules.ModFile(mainModule)
var list []*modfile.Require
@@ -1383,8 +1378,7 @@ func commitRequirements(ctx context.Context, goVersion string, rs *Requirements)
}
return
}
- gomod := ModFilePath()
- if _, ok := fsys.OverlayPath(gomod); ok {
+ if _, ok := fsys.OverlayPath(modFilePath); ok {
if dirty {
base.Fatalf("go: updates to go.mod needed, but go.mod is part of the overlay specified with -overlay")
}
@@ -1422,7 +1416,7 @@ func commitRequirements(ctx context.Context, goVersion string, rs *Requirements)
errNoChange := errors.New("no update needed")
- err = lockedfile.Transform(ModFilePath(), func(old []byte) ([]byte, error) {
+ err = lockedfile.Transform(modFilePath, func(old []byte) ([]byte, error) {
if bytes.Equal(old, new) {
// The go.mod file is already equal to new, possibly as the result of some
// other process.