diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index a3c1fa1..46fc882 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -53,6 +53,12 @@ jobs: - name: go mod tidy run: go mod tidy -diff + # The SQLite driver works only with the exact modernc.org/libc its + # go.mod names, and a newer libc would break it without a compile + # error. + - name: libc matches the SQLite driver + run: go run ./tools/libcpin + - name: go vet run: go vet ./... diff --git a/CLAUDE.md b/CLAUDE.md index 4a8a84d..d8e0701 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -51,8 +51,10 @@ it to the server in its config, so the generator never builds the package that embeds its own output. `tools/genlicenses` writes `THIRD-PARTY-LICENSES.md` and the copy beside `internal/app/uberbringer`'s `licenses.go`; it imports nothing from the module and learns the linked -modules from `go list`. The module path is the bare -`uberbringer`, so imports read `uberbringer/internal/...`. Imports point down: +modules from `go list`. `tools/libcpin` checks the libc pin from +`go mod graph` and imports nothing from the module either. The module path +is the bare `uberbringer`, so imports read `uberbringer/internal/...`. +Imports point down: - server → modules, printqueue, receipt, printer, escpos - modules → receipt, ntfy @@ -77,6 +79,7 @@ works from PowerShell and fails from Git Bash. gofmt -l . # must print nothing (fix: gofmt -w .) go tool goimports -l . # must print nothing (fix: go tool goimports -w .) go mod tidy -diff + go run ./tools/libcpin # modernc.org/libc is the version modernc.org/sqlite requires go vet ./... go tool staticcheck ./... go generate ./... && git diff --exit-code -- docs/swagger.json THIRD-PARTY-LICENSES.md internal/app/uberbringer/licenses_embed.md # generated files are up to date @@ -153,7 +156,8 @@ main. A change is done when the gate passes and the docs under `docs/` (or - Dependencies are at their latest stable release when added or bumped. The one exception is `modernc.org/libc`, which stays at the exact version `modernc.org/sqlite` requires and moves only with it (owner, 2026-12-01; - design.md, Stack choices). + design.md, Stack choices). `go get -u` would move it, so after one run + `go run ./tools/libcpin`, which fails until libc is back at that version. - Commit on main, land a branch, push, tag or release only when the owner asks. Releases are `vX.Y.Z` tags on `main`; `release.yml` refuses a tag whose commit is not on `main`. diff --git a/Makefile b/Makefile index 325ef06..6a42e92 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build build-arm test lint fmt fmt-check vet tidy tidy-check vuln generate generate-check release-check snapshot check run +.PHONY: build build-arm test lint fmt fmt-check vet tidy tidy-check libc-check vuln generate generate-check release-check snapshot check run # Without the leading v, as goreleaser's {{ .Version }} reports it. VERSION ?= $(patsubst v%,%,$(shell git describe --tags --always --dirty 2>/dev/null || echo dev)) @@ -43,6 +43,11 @@ tidy: tidy-check: go mod tidy -diff +# Fails unless modernc.org/libc is the exact version modernc.org/sqlite +# requires, which the driver needs. +libc-check: + go run ./tools/libcpin + vuln: go tool govulncheck ./... @@ -68,7 +73,7 @@ snapshot: # The gate .gitea/workflows/test.yml runs, in the same order. # .gitea/workflows/security.yml runs vuln. -check: fmt-check tidy-check vet lint generate-check test build-arm release-check +check: fmt-check tidy-check libc-check vet lint generate-check test build-arm release-check run: go run ./cmd/uberbringer --config config.toml diff --git a/docs/design.md b/docs/design.md index 15d3ad2..8e205ec 100644 --- a/docs/design.md +++ b/docs/design.md @@ -301,7 +301,9 @@ client ─HTTPS─▶ Caddy ─▶ server ── validate ──▶ printqueue ( - `modernc.org/libc` is the one module held below its latest release. It stays at the exact version `modernc.org/sqlite` requires, since the driver is C transpiled to Go against that libc and works with it alone, - and moves only with the driver. + and moves only with the driver. A routine `go get -u` would move it and + break the driver without a compile error, so the gate runs + `tools/libcpin`, which fails when the two differ. - **Packaging.** The daemon serves no TLS itself and binds 127.0.0.1 by default; a reverse proxy in front terminates HTTPS and sets `X-Real-IP`. - The release tarballs are built for linux amd64, armv6, armv7 and arm64. diff --git a/docs/development.md b/docs/development.md index cb73ddc..184e7a1 100644 --- a/docs/development.md +++ b/docs/development.md @@ -47,8 +47,10 @@ make snapshot # release archives in dist/; publishes nothing ``` `make check` runs the same steps as `.gitea/workflows/test.yml`, in the same -order: gofmt and goimports, `go mod tidy -diff`, `go vet`, staticcheck, a -check that `go generate ./...` leaves the generated files unchanged, +order: gofmt and goimports, `go mod tidy -diff`, `go run ./tools/libcpin`, +which fails unless `modernc.org/libc` is the exact version +`modernc.org/sqlite` requires, `go vet`, staticcheck, a check that +`go generate ./...` leaves the generated files unchanged, `go test -race -timeout 5m`, builds for linux/arm (ARMv6) and linux/arm64 without cgo, as releases are built, whose binaries it discards, and `goreleaser check`, which validates the release config without building. @@ -105,6 +107,7 @@ examples/ Example jobs, such as the block calibration receipt packaging/ systemd unit tools/genlicenses/ Generates THIRD-PARTY-LICENSES.md and its embedded copy from the linked modules tools/genopenapi/ Generates docs/swagger.json from the route table +tools/libcpin/ Checks that modernc.org/libc is the version modernc.org/sqlite requires docs/ User docs, API reference, OpenAPI document, design notes and feature plans ``` diff --git a/tools/libcpin/main.go b/tools/libcpin/main.go new file mode 100644 index 0000000..1fdb5be --- /dev/null +++ b/tools/libcpin/main.go @@ -0,0 +1,91 @@ +// Command libcpin fails unless the module builds with the exact +// modernc.org/libc that modernc.org/sqlite requires. The driver is C +// transpiled to Go against one libc and works with that one only, so a +// libc that a routine `go get -u` moved would break it without a compile +// error. The gate runs it from the repository: +// +// go run ./tools/libcpin +package main + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "strings" +) + +// The driver, and the module whose version it fixes. +const ( + driver = "modernc.org/sqlite" + libc = "modernc.org/libc" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, "libcpin:", err) + os.Exit(1) + } +} + +func run() error { + selected, err := moduleVersion(libc) + if err != nil { + return err + } + driverVersion, err := moduleVersion(driver) + if err != nil { + return err + } + graph, err := goOutput("mod", "graph") + if err != nil { + return err + } + want, err := requirement(graph, driver+"@"+driverVersion, libc) + if err != nil { + return err + } + if selected != want { + return fmt.Errorf("%s is %s, but %s %s requires exactly %s: run go get %s@%s", + libc, selected, driver, driverVersion, want, libc, want) + } + fmt.Printf("libcpin: %s %s is the version %s %s requires\n", libc, selected, driver, driverVersion) + return nil +} + +// requirement returns the version of path that module, written +// path@version, requires in graph, the output of go mod graph. +func requirement(graph, module, path string) (string, error) { + for line := range strings.Lines(graph) { + from, to, ok := strings.Cut(strings.TrimSpace(line), " ") + if !ok || from != module { + continue + } + if p, version, ok := strings.Cut(to, "@"); ok && p == path { + return version, nil + } + } + return "", fmt.Errorf("go mod graph shows no %s required by %s", path, module) +} + +// moduleVersion returns the version of path the main module selects. +func moduleVersion(path string) (string, error) { + out, err := goOutput("list", "-m", "-f", "{{.Version}}", path) + if err != nil { + return "", err + } + return strings.TrimSpace(out), nil +} + +// goOutput runs the go command and returns what it writes to standard +// output. +func goOutput(args ...string) (string, error) { + cmd := exec.Command("go", args...) + var stderr bytes.Buffer + cmd.Stderr = &stderr + out, err := cmd.Output() + if err != nil { + return "", fmt.Errorf("go %s: %w\n%s", strings.Join(args, " "), err, stderr.Bytes()) + } + return string(out), nil +} diff --git a/tools/libcpin/main_test.go b/tools/libcpin/main_test.go new file mode 100644 index 0000000..6cf6c2d --- /dev/null +++ b/tools/libcpin/main_test.go @@ -0,0 +1,28 @@ +package main + +import "testing" + +func TestRequirement(t *testing.T) { + graph := "uberbringer modernc.org/sqlite@v1.64.0\n" + + "uberbringer modernc.org/libc@v1.82.14\n" + + "modernc.org/sqlite@v1.63.0 modernc.org/libc@v1.79.0\n" + + "modernc.org/sqlite@v1.64.0 golang.org/x/sys@v0.49.0\n" + + "modernc.org/sqlite@v1.64.0 modernc.org/libc@v1.80.1\n" + + "modernc.org/libc@v1.80.1 modernc.org/memory@v1.12.1\n" + tests := []struct { + name, module, path, want string + }{ + {"the driver's own requirement", "modernc.org/sqlite@v1.64.0", "modernc.org/libc", "v1.80.1"}, + {"another version of the driver", "modernc.org/sqlite@v1.63.0", "modernc.org/libc", "v1.79.0"}, + {"a module the driver does not require", "modernc.org/sqlite@v1.64.0", "modernc.org/memory", ""}, + {"a driver version not in the graph", "modernc.org/sqlite@v1.65.0", "modernc.org/libc", ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := requirement(graph, tt.module, tt.path) + if got != tt.want || (err != nil) != (tt.want == "") { + t.Errorf("requirement = %q, %v; want %q", got, err, tt.want) + } + }) + } +}