Skip to content

Commit e2a9ea0

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modload: suppress the 'go mod init' hint for GOROOT more aggressively
Previously, we suppressed a `to create a module there, run: … go mod init' warning only if the config file itself (such as .git/config) was found in GOROOT. However, our release tarballs don't include the .git/config, so that case was not encountered, and the warning could occur based on a config file found in some parent directory (outside of GOROOT entirely). Instead, skip the directory walk completely if the working directory is anywhere in GOROOT. Fixes golang#34191 Change-Id: I9f774901bfbb53b700407c4882f37d6339d023fe Reviewed-on: https://go-review.googlesource.com/c/go/+/223340 Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent cbcb031 commit e2a9ea0

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/cmd/go/internal/modload/init.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -624,13 +624,14 @@ func findAltConfig(dir string) (root, name string) {
624624
panic("dir not set")
625625
}
626626
dir = filepath.Clean(dir)
627+
if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel != "" {
628+
// Don't suggest creating a module from $GOROOT/.git/config
629+
// or a config file found in any parent of $GOROOT (see #34191).
630+
return "", ""
631+
}
627632
for {
628633
for _, name := range altConfigs {
629634
if fi, err := os.Stat(filepath.Join(dir, name)); err == nil && !fi.IsDir() {
630-
if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel == "." {
631-
// Don't suggest creating a module from $GOROOT/.git/config.
632-
return "", ""
633-
}
634635
return dir, name
635636
}
636637
}

src/cmd/go/testdata/script/mod_convert_git.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ cd $GOROOT
1818
! go list .
1919
! stderr 'go mod init'
2020

21+
# We should also not suggest creating a go.mod file in $GOROOT if its own
22+
# .git/config has been stripped away and we find one in a parent directory.
23+
# (https://golang.org/issue/34191)
24+
env GOROOT=$WORK/parent/goroot
25+
cd $GOROOT
26+
! go list .
27+
! stderr 'go mod init'
28+
29+
cd $GOROOT/doc
30+
! go list .
31+
! stderr 'go mod init'
32+
2133
-- $WORK/test/.git/config --
2234
-- $WORK/test/x/x.go --
2335
package x // import "m/x"
36+
-- $WORK/parent/.git/config --
37+
-- $WORK/parent/goroot/README --
38+
This directory isn't really a GOROOT, but let's pretend that it is.
39+
-- $WORK/parent/goroot/doc/README --
40+
This is a subdirectory of our fake GOROOT.

0 commit comments

Comments
 (0)