From d65faae84bfd941ee8d6a84ba8f904820f01d1c5 Mon Sep 17 00:00:00 2001 From: "pieter.noordhuis" Date: Tue, 10 Nov 2026 14:13:08 +0000 Subject: [PATCH] Discourage package-level regexp and reflect work in Go style guide Package-level initializers run on every CLI invocation, so agents should defer such work with sync.OnceValue. The rule lives next to the existing sync.OnceValue guidance in the Go style guide, which is loaded for Go files only. Co-authored-by: Isaac --- .agents/rules/style-guide-go.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.agents/rules/style-guide-go.md b/.agents/rules/style-guide-go.md index ab2dad3b6e9..59638574e08 100644 --- a/.agents/rules/style-guide-go.md +++ b/.agents/rules/style-guide-go.md @@ -173,6 +173,14 @@ func GetConfig() (*Config, error) { Caveats that apply to both: results are cached forever (including errors), and a panic is rethrown on every subsequent call. Create a new instance if you need retry semantics. +**RULE: Do not compile regexps, call `reflect`, or do other non-trivial work in package-level `var` initializers or `init()`.** They run on every CLI invocation for every linked package. Wrap the value in `sync.OnceValue` (as above) and make sure a test calls it, so a bad pattern panics in tests rather than at runtime. + +BAD: + +```go +var taskKeyRe = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) +``` + ### Constructors When a constructor's parameter list is long enough that callers forget the order or misread positional arguments, consider grouping dependencies into a struct. This is a judgment call. The CLI has many ordinary constructors with 4+ parameters, and that's fine when the arguments are obvious at the call site. The signal for switching is readability, not parameter count.