Skip to content

Commit 6a4c432

Browse files
committed
bent: write out go.mod by hand and always pass go 1.21
Starting with Go 1.21, if a "too new" language version is discovered in a go.mod, the go command will fail. This is a problem in bent, because bent may use a newer dev toolchain to create a go.mod that can't be used by older Go toolchains. Since Go 1.21 is where this change happened (and older toolchains will happily ignore "too new" language versions) use that as the default minimum go.mod version and write out the go.mod directly. Change-Id: If510968c62c5155eb692b5e03ffbd34afd8959d4 Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/521820 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Michael Knyszek <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent c7f59e4 commit 6a4c432

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

cmd/bent/bent.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"os"
2020
"os/exec"
2121
"path"
22+
"path/filepath"
2223
"runtime"
2324
"strconv"
2425
"strings"
@@ -79,7 +80,8 @@ var explicitAll counterFlag // Include "-a" on "go test -c" test build ; repeati
7980
var shuffle = 2 // Dimensionality of (build) shuffling; 0 = none, 1 = per-benchmark, configuration ordering, 2 = bench, config pairs, 3 = across repetitions.
8081
var haveRsync = true
8182
var reportBuildTime = true
82-
var experiment = false // Don't reset go.mod, for testing purposes
83+
var experiment = false // Don't reset go.mod, for testing purposes
84+
var minGoVersion = "1.21" // This is the release the toolchain started caring about versions of Go that are too new.
8385

8486
//go:embed scripts/*
8587
var scripts embed.FS
@@ -156,6 +158,8 @@ func main() {
156158

157159
flag.Var(&verbose, "v", "print commands and other information (more -v = print more details)")
158160

161+
flag.StringVar(&minGoVersion, "m", minGoVersion, "minimum Go version across all toolchains used for benchmarking")
162+
159163
flag.Usage = func() {
160164
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
161165
flag.PrintDefaults()
@@ -549,23 +553,30 @@ results will also appear in 'bench'.
549553
}
550554
}
551555
if getFiles {
552-
cmd := exec.Command("go", "mod", "init", "build")
553-
cmd.Env = DefaultEnv()
554-
cmd.Dir = bench.BuildDir
555-
556-
if verbose > 0 {
557-
fmt.Println(asCommandLine(dirs.wd, cmd))
558-
} else {
559-
fmt.Print(".")
556+
goModPath := filepath.Join(bench.BuildDir, "go.mod")
557+
f, err := os.Create(goModPath)
558+
if err != nil {
559+
fmt.Printf("Error creating go.mod: %v", err)
560+
os.Exit(2)
560561
}
561-
_, err := cmd.Output()
562+
goMod := fmt.Sprintf(goMod, minGoVersion)
563+
_, err = fmt.Fprintln(f, goMod)
562564
if err != nil {
563-
ee := err.(*exec.ExitError)
564-
fmt.Printf("There was an error running 'go mod init', stderr = %s", ee.Stderr)
565+
fmt.Printf("Error writing go.mod: %v", err)
566+
f.Close()
567+
os.Exit(2)
568+
}
569+
if err := f.Close(); err != nil {
570+
fmt.Printf("Error closing go.mod: %v", err)
565571
os.Exit(2)
566572
}
573+
if verbose > 0 {
574+
fmt.Printf("(cd %s; cat <<EOF > %s\n%s\nEOF)\n", bench.BuildDir, "go.mod", goMod)
575+
} else {
576+
fmt.Print(".")
577+
}
567578

568-
cmd = exec.Command("go", "get", "-d", "-t", "-v", bench.Repo+bench.Version)
579+
cmd := exec.Command("go", "get", "-d", "-t", "-v", bench.Repo+bench.Version)
569580
cmd.Env = DefaultEnv()
570581
cmd.Dir = bench.BuildDir
571582

@@ -1080,6 +1091,11 @@ benchmarks_loop:
10801091
}
10811092
}
10821093

1094+
var goMod = `module build
1095+
1096+
go %s
1097+
`
1098+
10831099
func escape(s string) string {
10841100
s = strings.Replace(s, "\\", "\\\\", -1)
10851101
s = strings.Replace(s, "'", "\\'", -1)

0 commit comments

Comments
 (0)