From fb311d8e9b80b177a39c0700b86686095d902a4e Mon Sep 17 00:00:00 2001 From: Boston Cartwright Date: Fri, 25 Mar 2022 15:22:02 -0600 Subject: [PATCH 1/2] cmd/go: fail if the user tries to modify the GOMOD environment variable GOMOD was previously silently ignored. now fails if the user tries to modify the GOMOD environment variable and it is not the same as the module file that would be used. Fixes #51217 --- src/cmd/go/internal/modload/init.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index f960edd251..f494876f95 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -6,12 +6,18 @@ package modload import ( "bytes" + "cmd/go/internal/base" + "cmd/go/internal/cfg" + "cmd/go/internal/fsys" + "cmd/go/internal/lockedfile" + "cmd/go/internal/modconv" + "cmd/go/internal/modfetch" + "cmd/go/internal/search" "context" "encoding/json" "errors" "fmt" "go/build" - "internal/lazyregexp" "io/ioutil" "os" "path" @@ -20,13 +26,7 @@ import ( "strings" "sync" - "cmd/go/internal/base" - "cmd/go/internal/cfg" - "cmd/go/internal/fsys" - "cmd/go/internal/lockedfile" - "cmd/go/internal/modconv" - "cmd/go/internal/modfetch" - "cmd/go/internal/search" + "internal/lazyregexp" "golang.org/x/mod/modfile" "golang.org/x/mod/module" @@ -409,6 +409,10 @@ func Init() { if !mustUseModules { return } + } else if os.Getenv("GOMOD") != "" && filepath.Join(modRoot, "go.mod") != filepath.Join(modRoot, os.Getenv("GOMOD")) { + // if GOMOD is set and does not point to module file, error as they must use -modfile + // see golang.org/issue/51217 + base.Fatalf("go: GOMOD cannot be used to set the module file. use -modfile") } else { modRoots = []string{modRoot} } @@ -1420,9 +1424,7 @@ Run 'go help mod init' for more information. return "", fmt.Errorf(msg, dir, reason) } -var ( - importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`) -) +var importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`) func findImportComment(file string) string { data, err := os.ReadFile(file) From 95efed774edffc44f69168ba44c8e459dac70808 Mon Sep 17 00:00:00 2001 From: Boston Cartwright Date: Sat, 26 Mar 2022 01:55:01 +0000 Subject: [PATCH 2/2] cmd/go: undo import order changes in modinfo init --- src/cmd/go/internal/modload/init.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index f494876f95..565d04b998 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -6,18 +6,12 @@ package modload import ( "bytes" - "cmd/go/internal/base" - "cmd/go/internal/cfg" - "cmd/go/internal/fsys" - "cmd/go/internal/lockedfile" - "cmd/go/internal/modconv" - "cmd/go/internal/modfetch" - "cmd/go/internal/search" "context" "encoding/json" "errors" "fmt" "go/build" + "internal/lazyregexp" "io/ioutil" "os" "path" @@ -26,7 +20,13 @@ import ( "strings" "sync" - "internal/lazyregexp" + "cmd/go/internal/base" + "cmd/go/internal/cfg" + "cmd/go/internal/fsys" + "cmd/go/internal/lockedfile" + "cmd/go/internal/modconv" + "cmd/go/internal/modfetch" + "cmd/go/internal/search" "golang.org/x/mod/modfile" "golang.org/x/mod/module" @@ -1424,7 +1424,9 @@ Run 'go help mod init' for more information. return "", fmt.Errorf(msg, dir, reason) } -var importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`) +var ( + importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`) +) func findImportComment(file string) string { data, err := os.ReadFile(file)