aboutsummaryrefslogtreecommitdiffstats
path: root/community/go/0001-Fix-FTBFS-on-HOME-managed-with-git.patch
blob: 85d4eee96aad90b0576a6e1a69e30d67f6687084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
From: "Bryan C. Mills" <bcmills@google.com>
Date: Fri, 13 Mar 2020 12:51:09 -0400
Subject: Fix FTBFS on $HOME managed with git.
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

This is a cherry-pick of
https://github.com/golang/go/commit/e2a9ea035d73df84c813132417366e8e01777c8d

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 #34191

Change-Id: I9f774901bfbb53b700407c4882f37d6339d023fe
Reviewed-on: https://go-review.googlesource.com/c/go/+/223340
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
---
 src/cmd/go/internal/modload/init.go            |  9 +++++----
 src/cmd/go/testdata/script/mod_convert_git.txt | 17 +++++++++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index 61cbdf2..a73d858 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -740,13 +740,14 @@ func findAltConfig(dir string) (root, name string) {
 		panic("dir not set")
 	}
 	dir = filepath.Clean(dir)
+	if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel != "" {
+		// Don't suggest creating a module from $GOROOT/.git/config
+		// or a config file found in any parent of $GOROOT (see #34191).
+		return "", ""
+	}
 	for {
 		for _, name := range altConfigs {
 			if fi, err := os.Stat(filepath.Join(dir, name)); err == nil && !fi.IsDir() {
-				if rel := search.InDir(dir, cfg.BuildContext.GOROOT); rel == "." {
-					// Don't suggest creating a module from $GOROOT/.git/config.
-					return "", ""
-				}
 				return dir, name
 			}
 		}
diff --git a/src/cmd/go/testdata/script/mod_convert_git.txt b/src/cmd/go/testdata/script/mod_convert_git.txt
index ece505a..6ff1eb5 100644
--- a/src/cmd/go/testdata/script/mod_convert_git.txt
+++ b/src/cmd/go/testdata/script/mod_convert_git.txt
@@ -18,6 +18,23 @@ cd $GOROOT
 ! go list .
 ! stderr 'go mod init'
 
+# We should also not suggest creating a go.mod file in $GOROOT if its own
+# .git/config has been stripped away and we find one in a parent directory.
+# (https://golang.org/issue/34191)
+env GOROOT=$WORK/parent/goroot
+cd $GOROOT
+! go list .
+! stderr 'go mod init'
+
+cd $GOROOT/doc
+! go list .
+! stderr 'go mod init'
+
 -- $WORK/test/.git/config --
 -- $WORK/test/x/x.go --
 package x // import "m/x"
+-- $WORK/parent/.git/config --
+-- $WORK/parent/goroot/README --
+This directory isn't really a GOROOT, but let's pretend that it is.
+-- $WORK/parent/goroot/doc/README --
+This is a subdirectory of our fake GOROOT.