check-safety/check_sdkboundaries.go
Alex Dunmow cd88c808b0 initial: standalone check-safety module hoisted from CMS
Static safety/lint runner for the BlockNinja codebase. ~25 invariant
checks across Go and frontend sources. Was at git.dev.alexdunmow.com:block/ninja
in backend/cmd/check-safety/ until the 2026-06-06 consolidation moved
the BlockNinja repos under a shared ~/src/blockninja/ parent.

This repo is the standalone extraction:
- Own go.mod (git.dev.alexdunmow.com/block/check-safety, go 1.26.4)
- Vendored internal/{helpers,theme} from CMS (Go's internal/ rule
  blocks cross-module imports; vendoring is the workaround)
- CLI contract unchanged: `check-safety <target-dir> [--flags]`
- CMS Makefile shells into ../check-safety for safety-check /
  install-safety-checker targets

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 13:04:02 +08:00

98 lines
3.9 KiB
Go

package main
import (
"fmt"
"os"
)
func init() {
register(Check{
Seq: 22,
ID: "2c",
Title: "Standalone plugin SDK import boundaries",
Run: func(ctx *ScanContext, rep *Reporter) {
// Check 2c: Standalone plugin SDK import boundaries
fmt.Println("=== Check 2c: Standalone plugin SDK import boundaries ===")
var cmsGoModViolations []pluginGoModViolation
var pluginImportViolations []pluginImportViolation
var pluginGoModViolations []pluginGoModViolation
var standalonePluginLabels []string
checkedStandalonePluginRoots := make(map[string]bool)
requiredSDKVersion, requiredSDKVersionErr := currentCMSCoreSDKVersion()
if requiredSDKVersionErr != nil {
fmt.Printf(" ERROR: failed to resolve CMS SDK version: %v\n", requiredSDKVersionErr)
os.Exit(2)
}
checkStandalonePluginRoot := func(root string, label string) {
if checkedStandalonePluginRoots[root] || !shouldCheckStandalonePluginImports(root) {
return
}
checkedStandalonePluginRoots[root] = true
standalonePluginLabels = append(standalonePluginLabels, label)
for _, v := range checkStandalonePluginImports(root) {
v.file = prefixDisplayPath(label, v.file)
pluginImportViolations = append(pluginImportViolations, v)
}
for _, v := range checkStandalonePluginGoMod(root, requiredSDKVersion) {
v.file = prefixDisplayPath(label, v.file)
pluginGoModViolations = append(pluginGoModViolations, v)
}
}
for _, target := range ctx.backendTargets {
if samePath(target.root, ctx.backendDir) {
for _, v := range checkCMSCoreSDKGoMod(target.root) {
v.file = prefixDisplayPath(target.displayOrRoot(), v.file)
cmsGoModViolations = append(cmsGoModViolations, v)
}
}
checkStandalonePluginRoot(target.root, target.displayOrRoot())
}
for _, target := range ctx.pluginTargets {
checkStandalonePluginRoot(target.root, target.display)
}
if len(cmsGoModViolations) > 0 || len(pluginImportViolations) > 0 || len(pluginGoModViolations) > 0 {
if len(cmsGoModViolations) > 0 {
fmt.Printf(" FAIL: %d CMS backend go.mod violation(s):\n", len(cmsGoModViolations))
for _, v := range cmsGoModViolations {
if v.line > 0 {
fmt.Printf(" %s:%d [%s] %s\n", v.file, v.line, v.rule, v.detail)
continue
}
fmt.Printf(" %s [%s] %s\n", v.file, v.rule, v.detail)
}
}
if len(pluginImportViolations) > 0 {
fmt.Printf(" FAIL: %d standalone plugin import violation(s):\n", len(pluginImportViolations))
for _, v := range pluginImportViolations {
fmt.Printf(" %s:%d imports BlockNinja CMS package %q\n", v.file, v.line, v.importPath)
}
}
if len(pluginGoModViolations) > 0 {
fmt.Printf(" FAIL: %d standalone plugin go.mod violation(s):\n", len(pluginGoModViolations))
for _, v := range pluginGoModViolations {
if v.line > 0 {
fmt.Printf(" %s:%d [%s] %s\n", v.file, v.line, v.rule, v.detail)
continue
}
fmt.Printf(" %s [%s] %s\n", v.file, v.rule, v.detail)
}
}
fmt.Printf("\n Fix: CMS backend must not replace %s locally, and standalone plugins must use %s imports, require %s %s, and declare no replace directives.\n", blockCoreImportPrefix, blockCoreImportPrefix, blockCoreImportPrefix, requiredSDKVersion)
rep.Fail()
} else if len(standalonePluginLabels) > 0 {
fmt.Printf(" OK: Standalone plugin imports and go.mod stay on SDK version %s\n", requiredSDKVersion)
printPerTargetOKLines(standalonePluginLabels)
if ctx.includeCoreTargets {
fmt.Printf(" - %s/go.mod does not locally replace %s\n", ctx.backendTargets[0].displayOrRoot(), blockCoreImportPrefix)
}
} else if ctx.includeCoreTargets {
fmt.Printf(" OK: %s/go.mod does not locally replace %s\n", ctx.backendTargets[0].displayOrRoot(), blockCoreImportPrefix)
} else {
fmt.Println(" SKIP: no standalone plugin roots scanned")
}
fmt.Println()
},
})
}