Files
mcp/internal/rules/scaffold_rules.go
T

105 lines
3.5 KiB
Go
Raw Normal View History

package rules
import (
"go/ast"
"strings"
)
// Scaffolding conventions: keep main.go clean, load .env, and compose framework
// component configs instead of reading EINHERJAR_* env vars by hand. These catch
// the "cochinero in main.go" an AI produces when starting a project from zero.
func init() {
registered = append(registered,
Rule{
ID: "main.dirty",
Severity: SeverityError,
Module: "wire",
Check: checkMainDirty,
},
Rule{
ID: "main.no-godotenv-autoload",
Severity: SeverityWarning,
Module: "wire",
Check: checkMainGodotenv,
},
Rule{
ID: "config.raw-getenv",
Severity: SeverityWarning,
Module: "wire",
Check: checkConfigRawGetenv,
},
)
}
func isMainPackage(c *Context) bool {
return c.File != nil && c.File.Name != nil && c.File.Name.Name == "main"
}
// hasGodotenv reports whether the file loads a local .env, by either the blank
// autoload import (the standard) or the plain godotenv package.
func hasGodotenv(c *Context) bool {
return c.Importing("godotenv/autoload") || c.Importing("joho/godotenv")
}
// checkMainDirty flags a main.go that constructs the framework launcher itself.
// The launcher and every component belong in internal/wire; main.go must contain
// nothing but the godotenv autoload and wire.Run().
func checkMainDirty(c *Context) []Finding {
if !isMainPackage(c) {
return nil
}
if !c.Importing("einherjar/core/launcher") && !c.Called("launcher.New") {
return nil
}
return []Finding{{
Message: "main.go constructs the launcher/components directly — composition belongs in internal/wire, not main",
Hint: `Keep main.go to _ "github.com/joho/godotenv/autoload" + wire.Run(). Move launcher.New/Append and every component into internal/wire/wire.go Run().`,
}}
}
// checkMainGodotenv flags a wire-convention main.go that never loads .env, so
// local APP_*/EINHERJAR_* variables would be missing at config.Load().
func checkMainGodotenv(c *Context) []Finding {
if !isMainPackage(c) {
return nil
}
if !c.Called("wire.Run") && !c.Importing("internal/wire") {
return nil
}
if hasGodotenv(c) {
return nil
}
return []Finding{{
Message: "main.go calls wire.Run() but never loads .env — local config will be missing at boot",
Hint: `Add the blank import _ "github.com/joho/godotenv/autoload" (the documented standard); it never overrides real environment variables and a missing file is not an error.`,
}}
}
// checkConfigRawGetenv flags reading a framework EINHERJAR_* env var directly via
// os.Getenv, which bypasses the component-config composition. (EINHERJAR_LOG_* is
// left to logz.direct-env-read, which carries a logz-specific hint.)
func checkConfigRawGetenv(c *Context) []Finding {
var hits []Finding
ast.Inspect(c.File, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok || exprName(call.Fun) != "os.Getenv" || len(call.Args) == 0 {
return true
}
lit, ok := call.Args[0].(*ast.BasicLit)
if !ok {
return true
}
key := strings.Trim(lit.Value, `"`)
if !strings.HasPrefix(key, "EINHERJAR_") || strings.HasPrefix(key, "EINHERJAR_LOG_") {
return true
}
hits = append(hits, Finding{
Message: "reading " + key + " directly via os.Getenv bypasses the framework config composition",
Hint: "Compose the component's Config type (e.g. postgres.Config) into your Config struct and let caarlos0/env load its EINHERJAR_* tags through config.Load().",
Line: c.Fset.Position(call.Pos()).Line,
})
return true
})
return hits
}