Skip to content

Commit acb9ac0

Browse files
author
Bryan C. Mills
committed
cmd/dist: skip GOCACHE and .git when making GOROOT read-only
When we run tests, we may need to write the test binary (and/or test variants of its dependencies) to GOCACHE. (This also fixes several test cases in cmd/go, which preserves the GOCACHE variable for efficiency.) It is highly unlikely that tests will try to modify .git, and that directory contains many files, so don't bother with it. Updates #30316 Change-Id: Id11136c6c64d8f0afc6c6ba5d94c9269df231052 Reviewed-on: https://go-review.googlesource.com/c/go/+/207441 Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 440f7d6 commit acb9ac0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/cmd/dist/test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,25 @@ func (t *tester) makeGOROOTUnwritable() (undo func()) {
14491449
}
14501450
}
14511451

1452+
gocache := os.Getenv("GOCACHE")
1453+
if gocache == "" {
1454+
panic("GOCACHE not set")
1455+
}
1456+
gocacheSubdir, _ := filepath.Rel(dir, gocache)
1457+
14521458
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
1459+
if suffix := strings.TrimPrefix(path, dir+string(filepath.Separator)); suffix != "" {
1460+
if suffix == gocacheSubdir {
1461+
// Leave GOCACHE writable: we may need to write test binaries into it.
1462+
return filepath.SkipDir
1463+
}
1464+
if suffix == ".git" {
1465+
// Leave Git metadata in whatever state it was in. It may contain a lot
1466+
// of files, and it is highly unlikely that a test will try to modify
1467+
// anything within that directory.
1468+
return filepath.SkipDir
1469+
}
1470+
}
14531471
if err == nil {
14541472
mode := info.Mode()
14551473
if mode&0222 != 0 && (mode.IsDir() || mode.IsRegular()) {

0 commit comments

Comments
 (0)