Files
tailscale/misc/bumpdeps/gomod.go
Brad Fitzpatrick 63f625397b misc/bumpdeps, .github/workflows: unify the bumpdeps tool and bumpdep workflow
The bumpdep workflow grew 170 lines of inline shell that duplicated
much of what misc/bumpdeps already did in Go (resolving versions,
special branches, running go get), and disagreed with it in small ways
(the workflow fetched wireguard-go with GOPROXY=direct; the tool asked
the proxy for the branch head). It was also untestable except by
extracting the run block and running it by hand.

Move all of that logic into misc/bumpdeps and make the workflow a thin
wrapper that runs it with -github. The tool now:

  - accepts the workflow's argument forms: "go" for the toolchain (via
    ./pull-toolchain.sh), the "wireguard-go" and "gvisor" aliases,
    exact module paths, "path@version", and modules not yet in go.mod,
    alongside its existing substring filters; comma-separated arguments
    are split, so the workflow input passes straight through;
  - finds branch heads with git ls-remote and then asks the proxy for
    the pseudo-version of that commit, so a just-pushed commit is seen
    without ever cloning (GOPROXY=direct on github.com/gokrazy/kernel.*
    hung the workflow for hours);
  - refuses downgrades by diffing go.mod before and after go get,
    rather than grepping go get's output;
  - runs "make tidy" and "make updatedeps" itself (-tidy=false to skip);
  - renders the PR title, body (GitHub compare links derived from the
    module path, including subdirectory-tagged modules), and commit
    message, printing a suggested commit message locally and, with
    -github, writing step outputs and the step summary. -issue accepts
    the issue URL or "#N" form and becomes the "Updates" line.

The branch name changes from a slug of every module path, which
produced names like actions/bumpdep/github.com-gokrazy-kernel.amd64-
main-github.com-gokrazy-kernel.arm64-main-github, to
actions/<workflow>/<actor>/<yyyymmdd-hhmmss> (no actor for scheduled
runs). Each run is a fresh PR; delete-branch cleans up after merge or
close.

The bumpdep workflow gains an optional exclude-newer-than-days input for
the cooldown. All of the formerly-inline logic now has unit tests,
including a fake module proxy.

Also add a gokrazy-bump workflow that runs the same tool every Sunday
night on the direct github.com/gokrazy/* modules (kernels, firmware,
gokrazy itself) and opens a PR labeled run-natlab-tests, so the natlab
VM tests boot the new kernels before merge. Substring filters now skip
indirect dependencies unless -indirect is set, so that "github.com/
gokrazy/" doesn't drag in gokapi; naming an indirect module exactly
still selects it.

Updates tailscale/corp#48312
Updates #8043
Updates #1866

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I7c3e91a4d2f58b0e6a1c9d47f3b2e8a5c6d0f1e2
2026-09-23 06:42:52 -07:00

190 lines
5.6 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package main
import (
"fmt"
"os"
"slices"
"strings"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
)
// goMod is the parts of go.mod that bumpdeps cares about.
type goMod struct {
requires []*modfile.Require // in file order
versions map[string]string // module path to required version
replaced map[string]bool // module paths with a replace directive
}
// readGoMod reads go.mod from the current directory.
func readGoMod() (*goMod, error) {
data, err := os.ReadFile("go.mod")
if err != nil {
return nil, err
}
return parseGoMod(data)
}
func parseGoMod(data []byte) (*goMod, error) {
mf, err := modfile.Parse("go.mod", data, nil)
if err != nil {
return nil, err
}
gm := &goMod{
requires: mf.Require,
versions: make(map[string]string, len(mf.Require)),
replaced: make(map[string]bool, len(mf.Replace)),
}
for _, req := range mf.Require {
gm.versions[req.Mod.Path] = req.Mod.Version
}
for _, r := range mf.Replace {
gm.replaced[r.Old.Path] = true
}
return gm, nil
}
// selection is what the command line asked bumpdeps to do.
type selection struct {
toolchain bool // run ./pull-toolchain.sh
lookups []module.Version // modules to resolve via the proxy, with their current versions ("" if new)
explicit []update // modules to hand to go get at a given version, no lookup
names []string // what was asked for, for the report when nothing changed
}
// splitArgs splits args on commas and whitespace and drops empty pieces.
// The GitHub workflow passes its comma-separated input as one argument.
func splitArgs(args []string) []string {
var out []string
for _, a := range args {
for _, f := range strings.FieldsFunc(a, func(r rune) bool { return r == ',' || r == ' ' || r == '\t' || r == '\n' }) {
out = append(out, f)
}
}
return out
}
// parseArgs turns the command line into a selection against gm. With no
// args, every direct dependency is selected (plus indirect ones if
// indirect is set). See the package comment for the argument forms.
func parseArgs(gm *goMod, args []string, indirect bool) (*selection, error) {
sel := &selection{}
args = splitArgs(args)
if len(args) == 0 {
for _, req := range gm.requires {
if req.Indirect && !indirect {
continue
}
sel.addLookup(gm, req.Mod)
}
return sel, nil
}
seen := map[string]bool{}
for _, arg := range args {
if arg == "go" {
sel.toolchain = true
sel.names = append(sel.names, "Go toolchain")
continue
}
if path, ok := aliases[arg]; ok {
sel.names = append(sel.names, arg)
sel.addKnown(gm, path, seen)
continue
}
if path, ver, ok := strings.Cut(arg, "@"); ok {
if err := module.CheckPath(path); err != nil {
return nil, fmt.Errorf("bad module path in %q: %v", arg, err)
}
if ver == "" {
return nil, fmt.Errorf("empty version in %q", arg)
}
sel.names = append(sel.names, path)
if seen[path] {
return nil, fmt.Errorf("%s selected more than once", path)
}
seen[path] = true
if gm.replaced[path] {
return nil, fmt.Errorf("%s has a replace directive; edit go.mod by hand", path)
}
if ver == "latest" {
sel.lookups = append(sel.lookups, module.Version{Path: path, Version: gm.versions[path]})
} else {
sel.explicit = append(sel.explicit, update{Path: path, Current: gm.versions[path], Latest: ver})
}
continue
}
if _, ok := gm.versions[arg]; ok {
sel.names = append(sel.names, arg)
sel.addKnown(gm, arg, seen)
continue
}
var matched bool
for _, req := range gm.requires {
if req.Indirect && !indirect {
continue
}
if strings.Contains(strings.ToLower(req.Mod.Path), strings.ToLower(arg)) {
matched = true
sel.addKnown(gm, req.Mod.Path, seen)
}
}
if matched {
sel.names = append(sel.names, arg)
continue
}
// Nothing in go.mod matches. If it looks like a module path,
// add it at its latest version.
if strings.Contains(arg, "/") && module.CheckPath(arg) == nil {
sel.names = append(sel.names, arg)
seen[arg] = true
sel.lookups = append(sel.lookups, module.Version{Path: arg})
continue
}
return nil, fmt.Errorf("no modules in go.mod match %q", arg)
}
return sel, nil
}
// addKnown selects the module at path, which is required by gm, unless
// it was already selected or has a replace directive.
func (sel *selection) addKnown(gm *goMod, path string, seen map[string]bool) {
if seen[path] {
return
}
seen[path] = true
sel.addLookup(gm, module.Version{Path: path, Version: gm.versions[path]})
}
// addLookup queues mod for a proxy lookup, skipping replaced modules.
func (sel *selection) addLookup(gm *goMod, mod module.Version) {
if gm.replaced[mod.Path] {
fmt.Fprintf(os.Stderr, "skipping %s: has a replace directive\n", mod.Path)
return
}
sel.lookups = append(sel.lookups, mod)
}
// checkDowngrades returns an error if any module required by both before
// and after has a lower version in after. A downgrade means something
// went wrong and needs a human's rollback commit, not an unattended bump.
func checkDowngrades(before, after *goMod) error {
var downgraded []string
for path, oldVer := range before.versions {
newVer, ok := after.versions[path]
if ok && semver.Compare(newVer, oldVer) < 0 {
downgraded = append(downgraded, fmt.Sprintf("%s %s => %s", path, oldVer, newVer))
}
}
if len(downgraded) == 0 {
return nil
}
slices.Sort(downgraded)
return fmt.Errorf("refusing to downgrade modules (go.mod and go.sum are left modified; revert them):\n\t%s", strings.Join(downgraded, "\n\t"))
}