diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c1d0c38..5534066 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,10 +14,10 @@ permissions: # - every lane runs twice where it matters, on rbgo's default build and on # its rbgo_core build (-tags rbgo_core: the language and the numeric stack, # no gem), which is what a notebook worker and a browser page link; -# - the cross-compile lane covers the targets rbgo itself builds for (its -# release matrix plus js/wasm), not every GOOS/GOARCH: rbgo's internal/vm -# does not compile for plan9, aix, solaris, illumos, the BSDs or 32-bit -# targets, so neither can anything that embeds it. +# - the cross-compile lane covers every `go tool dist list` target but plan9 +# (aix, solaris, illumos, the BSDs and 32-bit targets included, since rbgo +# v0.8.3; go-embedded-ruby/ruby#832): rbgo's internal/vm does not compile +# for plan9, so neither can anything that embeds it. jobs: test: name: test (${{ matrix.os }}, ${{ matrix.tags || 'default' }}) @@ -146,32 +146,79 @@ jobs: GOARCH=386 go test ./... GOARCH=386 go test -tags rbgo_core ./... - cross: - name: compile on every target rbgo builds for + # The cross-compile lane used to be one job looping ~48 targets x 2 tag sets + # serially, 1h20 and more, with no timeout. It is now one job per target: + # cross-plan derives the target list from `go tool dist list` (internal/ + # crossplan, tested: a new Go port is vetted from its first day), cross-target + # vets one target with both tag sets, and `cross` aggregates them under the + # name this lane has always had. + cross-plan: + name: cross plan runs-on: ubuntu-latest + timeout-minutes: 10 + outputs: + matrix: ${{ steps.plan.outputs.matrix }} steps: - uses: actions/checkout@v7 - uses: actions/setup-go@v7 with: go-version: '1.27.4' - # Since rbgo v0.8.3 that is every `go tool dist list` pair but plan9 - # (go-embedded-ruby/ruby#832). go vet type-checks every package and its - # tests without linking; android and ios link only through cgo, so their - # library packages are built instead. - - name: go vet for each target, both builds + # Since rbgo v0.8.3 the targets are every `go tool dist list` pair but + # plan9 (go-embedded-ruby/ruby#832). The step prints every target+tags + # pair the jobs below run. + - id: plan + run: go run ./internal/crossplan + + cross-target: + name: cross ${{ matrix.target }} + needs: cross-plan + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: ${{ fromJSON(needs.cross-plan.outputs.matrix) }} + env: + CGO_ENABLED: "0" + TARGET_OS: ${{ matrix.goos }} + TARGET_ARCH: ${{ matrix.goarch }} + TAG_FLAGS: ${{ matrix.tagflags }} + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-go@v7 + with: + go-version: '1.27.4' + # go vet type-checks every package and its tests without linking; android + # and ios link only through cgo, so their library packages are built + # instead. + - name: go vet, both builds shell: bash - env: - CGO_ENABLED: "0" run: | fail=0 - for t in $(go tool dist list | grep -v '^plan9/'); do - os=${t%/*}; arch=${t#*/} - for tags in "" rbgo_core; do - case $os in - android|ios) cmd="go build -tags=$tags $(go list -f '{{if ne .Name "main"}}{{.ImportPath}}{{end}}' ./...)" ;; - *) cmd="go vet -tags=$tags ./..." ;; - esac - GOOS=$os GOARCH=$arch $cmd >/dev/null 2>err.txt || { echo "::error::$t [$tags]"; cat err.txt; fail=1; } - done + # The tag flags come from the plan (internal/crossplan), not from here. + pkgs=$(go list -f '{{if ne .Name "main"}}{{.ImportPath}}{{end}}' ./...) + test -n "$pkgs" || { echo "::error::go list found no package"; exit 1; } + for flag in $TAG_FLAGS; do + case $TARGET_OS in + android|ios) cmd="go build $flag $pkgs" ;; + *) cmd="go vet $flag ./..." ;; + esac + echo "$TARGET_OS/$TARGET_ARCH [$flag]" + GOOS=$TARGET_OS GOARCH=$TARGET_ARCH $cmd >/dev/null 2>err.txt || { echo "::error::$TARGET_OS/$TARGET_ARCH [$flag]"; cat err.txt; fail=1; } done + test -n "$TAG_FLAGS" || { echo "::error::the plan gave no tag flags"; exit 1; } exit $fail + + cross: + name: compile on every target rbgo builds for + needs: [cross-plan, cross-target] + if: always() + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: every target job succeeded + env: + PLAN: ${{ needs.cross-plan.result }} + TARGETS: ${{ needs.cross-target.result }} + run: | + echo "cross-plan: $PLAN, cross-target: $TARGETS" + [ "$PLAN" = success ] && [ "$TARGETS" = success ] diff --git a/internal/crossplan/main.go b/internal/crossplan/main.go new file mode 100644 index 0000000..9685073 --- /dev/null +++ b/internal/crossplan/main.go @@ -0,0 +1,114 @@ +// Command crossplan prints the GitHub Actions matrix of the cross-compile lane: +// every GOOS/GOARCH pair of `go tool dist list` but plan9, which rbgo does not +// build for (go-embedded-ruby/ruby#832). Deriving the list from the toolchain +// means a new Go port is vetted from the day it appears, never silently +// skipped. Each target is vetted with every tag set in Tags, in its own job. +package main + +import ( + "encoding/json" + "fmt" + "io" + "os" + "os/exec" + "sort" + "strings" +) + +// Tags are the build-tag sets every target is vetted with: rbgo's default +// build and its rbgo_core build. +var Tags = []string{"", "rbgo_core"} + +// Skipped are the GOOS values rbgo does not build for. +var Skipped = []string{"plan9"} + +// Target is one matrix entry. +type Target struct { + Target string `json:"target"` + GOOS string `json:"goos"` + GOARCH string `json:"goarch"` + // TagFlags are the go flags the job runs the target with, one per tag + // set, space separated: "-tags= -tags=rbgo_core". The job loops over them, + // so the plan, not the YAML, says what is vetted. + TagFlags string `json:"tagflags"` +} + +// Plan turns the output of `go tool dist list` into the sorted targets. +func Plan(distList string) ([]Target, error) { + var out []Target + for _, line := range strings.Fields(distList) { + goos, goarch, ok := strings.Cut(line, "/") + if !ok || goos == "" || goarch == "" { + return nil, fmt.Errorf("malformed target %q in go tool dist list", line) + } + skip := false + for _, s := range Skipped { + skip = skip || goos == s + } + if !skip { + flags := make([]string, len(Tags)) + for i, tags := range Tags { + flags[i] = "-tags=" + tags + } + out = append(out, Target{line, goos, goarch, strings.Join(flags, " ")}) + } + } + if len(out) == 0 { + return nil, fmt.Errorf("go tool dist list gave no target") + } + sort.Slice(out, func(i, j int) bool { return out[i].Target < out[j].Target }) + return out, nil +} + +// distList is `go tool dist list`; a seam for tests. +var distList = func() (string, error) { + b, err := exec.Command("go", "tool", "dist", "list").Output() + return string(b), err +} + +// run writes the matrix as a `matrix=` line, then the flat list of +// target+tags pairs the jobs will run, one per line, to log. +func run(w, log io.Writer) error { + list, err := distList() + if err != nil { + return err + } + targets, err := Plan(list) + if err != nil { + return err + } + b, _ := json.Marshal(struct { // plain strings: Marshal cannot fail + Include []Target `json:"include"` + }{targets}) + for _, t := range targets { + for _, tags := range Tags { + fmt.Fprintf(log, "%s [%s]\n", t.Target, tags) + } + } + _, err = fmt.Fprintf(w, "matrix=%s\n", b) + return err +} + +// exit is os.Exit, a seam for tests. +var exit = os.Exit + +// stderr is where main reports a failure; a seam for tests. +var stderr io.Writer = os.Stderr + +func main() { + out, log := io.Writer(os.Stdout), stderr + if p := os.Getenv("GITHUB_OUTPUT"); p != "" { + f, err := os.OpenFile(p, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644) + if err != nil { + fmt.Fprintln(stderr, "crossplan: output:", err) + exit(1) + return + } + defer f.Close() + out, log = f, os.Stdout + } + if err := run(out, log); err != nil { + fmt.Fprintln(stderr, "crossplan: plan:", err) + exit(1) + } +} diff --git a/internal/crossplan/main_test.go b/internal/crossplan/main_test.go new file mode 100644 index 0000000..bb4b55f --- /dev/null +++ b/internal/crossplan/main_test.go @@ -0,0 +1,136 @@ +package main + +import ( + "bytes" + "encoding/json" + "errors" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestPlanDropsPlan9AndKeepsTheRest(t *testing.T) { + got, err := Plan("plan9/386\nlinux/amd64\nandroid/arm64\nplan9/amd64\njs/wasm\n") + if err != nil { + t.Fatal(err) + } + var names []string + for _, x := range got { + names = append(names, x.Target) + } + if strings.Join(names, " ") != "android/arm64 js/wasm linux/amd64" { + t.Fatalf("targets = %v", names) + } + if got[2].GOOS != "linux" || got[2].GOARCH != "amd64" { + t.Fatalf("%+v", got[2]) + } + for _, bad := range []string{"", "plan9/386", "linux", "/amd64", "linux/"} { + if _, err := Plan(bad); err == nil { + t.Errorf("Plan(%q) accepted", bad) + } + } +} + +// The union of the matrix's targets and tag sets is exactly what the old +// serial loop covered: every `go tool dist list` pair but plan9, times both +// tag sets. The real toolchain's list is used. +func TestMatrixCoversTheOldLoop(t *testing.T) { + var out, log bytes.Buffer + if err := run(&out, &log); err != nil { + t.Fatal(err) + } + line, ok := strings.CutPrefix(strings.TrimSpace(out.String()), "matrix=") + if !ok { + t.Fatalf("output %q", out.String()) + } + var m struct{ Include []Target } + if err := json.Unmarshal([]byte(line), &m); err != nil { + t.Fatal(err) + } + have := map[string]bool{} + for _, x := range m.Include { + // What the job runs: the flags the matrix entry carries. + for _, flag := range strings.Fields(x.TagFlags) { + have[x.Target+" ["+strings.TrimPrefix(flag, "-tags=")+"]"] = true + } + } + want := map[string]bool{} + list, err := distList() + if err != nil { + t.Fatal(err) + } + for _, f := range strings.Fields(list) { // the old loop: grep -v '^plan9/' + if !strings.HasPrefix(f, "plan9/") { + for _, tags := range []string{"", "rbgo_core"} { + want[f+" ["+tags+"]"] = true + } + } + } + if len(have) != len(want) || len(want) < 80 { + t.Fatalf("matrix covers %d pairs, the old loop %d", len(have), len(want)) + } + for k := range want { + if !have[k] { + t.Errorf("matrix misses %s", k) + } + } + if got := strings.Count(log.String(), "\n"); got != len(want) { + t.Errorf("logged %d pairs, want %d", got, len(want)) + } +} + +type failWriter struct{} + +func (failWriter) Write([]byte) (int, error) { return 0, errors.New("disk full") } + +func TestRunFailures(t *testing.T) { + saved := distList + defer func() { distList = saved }() + // A failing go tool dist list fails run even when it printed a valid list. + distList = func() (string, error) { return "linux/amd64", errors.New("no go") } + if err := run(&bytes.Buffer{}, &bytes.Buffer{}); err == nil { + t.Error("run ignored a failing go tool dist list") + } + distList = func() (string, error) { return "linux", nil } + if err := run(&bytes.Buffer{}, &bytes.Buffer{}); err == nil { + t.Error("run accepted a malformed list") + } + distList = func() (string, error) { return "linux/amd64", nil } + if err := run(failWriter{}, &bytes.Buffer{}); err == nil { + t.Error("run ignored a failing writer") + } +} + +func TestMain_(t *testing.T) { + code := 0 + var msg bytes.Buffer + exit = func(c int) { code = c } + stderr = &msg + defer func() { exit, stderr = os.Exit, os.Stderr }() + // GITHUB_OUTPUT: the matrix is appended to the file. + f := filepath.Join(t.TempDir(), "out") + t.Setenv("GITHUB_OUTPUT", f) + main() + b, err := os.ReadFile(f) + if err != nil || !strings.HasPrefix(string(b), "matrix=") || code != 0 { + t.Fatalf("output %q, err %v, code %d", b, err, code) + } + // An unwritable GITHUB_OUTPUT exits 1. + t.Setenv("GITHUB_OUTPUT", filepath.Join(t.TempDir(), "missing", "out")) + main() + if code != 1 || !strings.HasPrefix(msg.String(), "crossplan: output: ") || strings.Count(msg.String(), "\n") != 1 { + t.Fatalf("code %d, stderr %q", code, &msg) + } + msg.Reset() + // Without it the matrix goes to stdout; a failing plan exits 1. + t.Setenv("GITHUB_OUTPUT", "") + code = 0 + saved := distList + defer func() { distList = saved }() + distList = func() (string, error) { return "", errors.New("no go") } + main() + if code != 1 || !strings.HasPrefix(msg.String(), "crossplan: plan: ") { + t.Fatalf("code %d, stderr %q", code, &msg) + } +}