cmd/cue: fix race in testscript tests that use go/packages

We have recently been seeing a number of test flakes in the
get_go_types.txt which were ultimately reported as golang/go#44137.

However, it turns out this is actually caused by an interaction between
the ./... pattern and use of go/packages within a testscript test,
specifically go/packages (via cmd/go) writing temporary files and
directories to $WORK/tmp which then end up racing with the walk of $WORK
to find directories/packages that match ./...

As of v1.8.0, in a standard testscript setup a temporary directory is
established at $WORK/.tmp. We now follow that same pattern for the home
directory for consistency's sake.

Change-Id: Ia8c7ffd471f9879d3283a47db5f2a5e165f619e1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9002
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cmd/cue/cmd/script_test.go b/cmd/cue/cmd/script_test.go
index 45ae5d7..c855801 100644
--- a/cmd/cue/cmd/script_test.go
+++ b/cmd/cue/cmd/script_test.go
@@ -23,6 +23,7 @@
 	"os"
 	"path"
 	"path/filepath"
+	goruntime "runtime"
 	"strings"
 	"testing"
 
@@ -36,6 +37,10 @@
 	"cuelang.org/go/internal/cuetest"
 )
 
+const (
+	homeDirName = ".user-home"
+)
+
 // TestLatest checks that the examples match the latest language standard,
 // even if still valid in backwards compatibility mode.
 func TestLatest(t *testing.T) {
@@ -83,9 +88,17 @@
 		Dir:           filepath.Join("testdata", "script"),
 		UpdateScripts: cuetest.UpdateGoldenFiles,
 		Setup: func(e *testscript.Env) error {
+			// Set up a home dir within work dir with a . prefix so that the
+			// Go/CUE pattern ./... does not descend into it.
+			home := filepath.Join(e.WorkDir, homeDirName)
+			if err := os.Mkdir(home, 0777); err != nil {
+				return err
+			}
+
 			e.Vars = append(e.Vars,
 				"GOPROXY="+srv.URL,
 				"GONOSUMDB=*", // GOPROXY is a private proxy
+				homeEnvName()+"="+home,
 			)
 			return nil
 		},
@@ -164,3 +177,17 @@
 		"cue": MainTest,
 	}))
 }
+
+// homeEnvName extracts the logic from os.UserHomeDir to get the
+// name of the environment variable that should be used when
+// seting the user's home directory
+func homeEnvName() string {
+	switch goruntime.GOOS {
+	case "windows":
+		return "USERPROFILE"
+	case "plan9":
+		return "home"
+	default:
+		return "HOME"
+	}
+}
diff --git a/cmd/cue/cmd/testdata/script/get_go_types.txt b/cmd/cue/cmd/testdata/script/get_go_types.txt
index 0198d3f..9a99a24 100644
--- a/cmd/cue/cmd/testdata/script/get_go_types.txt
+++ b/cmd/cue/cmd/testdata/script/get_go_types.txt
@@ -6,8 +6,6 @@
 # that is left for other tests, so that this test can focus
 # on the various rules of get go.
 
-[go1.15] [windows] skip 'flakey per golang.org/issue/44137'
-
 # cue get go
 cue get go --local ./...
 cmp ./pkg3/pkg3_go_gen.cue ./pkg3/pkg3_go_gen.cue.golden
diff --git a/go.mod b/go.mod
index f519209..d838caf 100644
--- a/go.mod
+++ b/go.mod
@@ -10,7 +10,7 @@
 	github.com/lib/pq v1.0.0 // indirect
 	github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de
 	github.com/pkg/errors v0.8.1 // indirect
-	github.com/rogpeppe/go-internal v1.6.2-0.20200830194709-1115b6af0369
+	github.com/rogpeppe/go-internal v1.8.0
 	github.com/spf13/cobra v1.0.0
 	github.com/spf13/pflag v1.0.3
 	github.com/stretchr/testify v1.2.2
diff --git a/go.sum b/go.sum
index dae0f7b..ef6affb 100644
--- a/go.sum
+++ b/go.sum
@@ -75,6 +75,8 @@
 github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
 github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
 github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
+github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
+github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -90,8 +92,8 @@
 github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
 github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
 github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
-github.com/rogpeppe/go-internal v1.6.2-0.20200830194709-1115b6af0369 h1:wdCVGtPadWC/ZuuLC7Hv58VQ5UF7V98ewE71n5mJfrM=
-github.com/rogpeppe/go-internal v1.6.2-0.20200830194709-1115b6af0369/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
+github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
+github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
 github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=