Skip to content

Commit ef06a5f

Browse files
committed
misc/reboot: don't use symlinks when copying GOROOT/src
go:embed disallows using symlinked files by design. crypto/elliptic is the first std package to use it as of CL 380475, and unfortunately that broke the TestRepeatBootstrap long test. The reason it uses symlinks is for speed; it wants to copy GOROOT/src, but regular files aren't going to be modified in any way, so a symlink, if supported, means not needing to copy the contents. Replace the symlink attempt with hard links, which will mean regular files remain as such, fixing go:embed. It's worth noting that on many systems hard links won't work, as the temporary filesystem tends to be separate, but it doesn't hurt to try. In my system, where /tmp is tmpfs, the test now copies more bytes. With the added Logf, I can see overlayDir goes from ~30ms to ~100ms. This makes sense, as GOROOT/src currently weighs around 100MiB. To alleviate that slow-down, stop copying testdata directories, as they currently weigh around 20MiB and aren't needed for the test. This gets overlayDir on my system down to an acceptable ~70ms. I briefly considered teaching overlayDir what files can be symlinks, but that seemed fairly complex long-term, as any file could be embedded. While here, start using testing.T.TempDir and fs.WalkDir. For #50995. Change-Id: I17947e6bdee96237e1ca0606ad0b95e7c5987bc1 Reviewed-on: https://go-review.googlesource.com/c/go/+/383995 Trust: Daniel Martí <[email protected]> Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent c856fbf commit ef06a5f

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

misc/reboot/overlaydir_test.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package reboot_test
66

77
import (
88
"io"
9+
"io/fs"
910
"os"
1011
"path/filepath"
1112
"strings"
@@ -26,17 +27,22 @@ func overlayDir(dstRoot, srcRoot string) error {
2627
return err
2728
}
2829

29-
return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
30+
return filepath.WalkDir(srcRoot, func(srcPath string, entry fs.DirEntry, err error) error {
3031
if err != nil || srcPath == srcRoot {
3132
return err
3233
}
34+
if filepath.Base(srcPath) == "testdata" {
35+
// We're just building, so no need to copy those.
36+
return fs.SkipDir
37+
}
3338

3439
suffix := strings.TrimPrefix(srcPath, srcRoot)
3540
for len(suffix) > 0 && suffix[0] == filepath.Separator {
3641
suffix = suffix[1:]
3742
}
3843
dstPath := filepath.Join(dstRoot, suffix)
3944

45+
info, err := entry.Info()
4046
perm := info.Mode() & os.ModePerm
4147
if info.Mode()&os.ModeSymlink != 0 {
4248
info, err = os.Stat(srcPath)
@@ -46,14 +52,15 @@ func overlayDir(dstRoot, srcRoot string) error {
4652
perm = info.Mode() & os.ModePerm
4753
}
4854

49-
// Always copy directories (don't symlink them).
55+
// Always make copies of directories.
5056
// If we add a file in the overlay, we don't want to add it in the original.
5157
if info.IsDir() {
5258
return os.MkdirAll(dstPath, perm|0200)
5359
}
5460

55-
// If the OS supports symlinks, use them instead of copying bytes.
56-
if err := os.Symlink(srcPath, dstPath); err == nil {
61+
// If we can use a hard link, do that instead of copying bytes.
62+
// Go builds don't like symlinks in some cases, such as go:embed.
63+
if err := os.Link(srcPath, dstPath); err == nil {
5764
return nil
5865
}
5966

misc/reboot/reboot_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,22 @@ import (
1212
"path/filepath"
1313
"runtime"
1414
"testing"
15+
"time"
1516
)
1617

1718
func TestRepeatBootstrap(t *testing.T) {
1819
if testing.Short() {
1920
t.Skipf("skipping test that rebuilds the entire toolchain")
2021
}
2122

22-
goroot, err := os.MkdirTemp("", "reboot-goroot")
23-
if err != nil {
24-
t.Fatal(err)
25-
}
26-
defer os.RemoveAll(goroot)
23+
goroot := t.TempDir()
2724

2825
gorootSrc := filepath.Join(goroot, "src")
26+
overlayStart := time.Now()
2927
if err := overlayDir(gorootSrc, filepath.Join(runtime.GOROOT(), "src")); err != nil {
3028
t.Fatal(err)
3129
}
30+
t.Logf("GOROOT/src overlay set up in %s", time.Since(overlayStart))
3231

3332
if err := os.WriteFile(filepath.Join(goroot, "VERSION"), []byte(runtime.Version()), 0666); err != nil {
3433
t.Fatal(err)

0 commit comments

Comments
 (0)