check-safety/check_sdkboundaries.go
Alex Dunmow 0749c22d30 feat(2c): forbid all first-party imports in standalone plugins; fix --plugin-dir-only scans
Import boundary now covers the whole first-party tree, not just block/cms:
block/core and block/orchestrator imports are violations too, with an
explicit carve-out for the packages that deliberately stayed core after
the pluginsdk extraction (core/captcha, core/backup — calcomblock uses
captcha today). The .templ import scan gets the same rule.

defaultScanTargetDir: --plugin-dir with no positional target used to
leave targetDir at cwd, so the checker scanned its own repo and
self-reported failures while never scanning the plugin. It now targets
the first plugin root, matching the positional form.

Verified green across cms + all 11 v0.2.1 fleet repos (bidmasters still
fails the version anchor as expected — it pins v0.2.0 on a detached
HEAD, unrelated to these changes).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 21:48:01 +08:00

90 lines
3.6 KiB
Go

package main
func init() {
register(Check{
Seq: 22,
ID: "2c",
Title: "Standalone plugin SDK import boundaries",
Run: func(ctx *ScanContext, rep *Reporter) {
var cmsGoModViolations []pluginGoModViolation
var pluginImportViolations []pluginImportViolation
var pluginGoModViolations []pluginGoModViolation
var standalonePluginLabels []string
checkedStandalonePluginRoots := make(map[string]bool)
// Version anchor = the CMS backend's pluginsdk pin; empty (skip
// version enforcement) until the CMS itself migrates to pluginsdk.
requiredSDKVersion, requiredSDKVersionErr := currentCMSPluginSDKVersion()
if requiredSDKVersionErr != nil {
rep.Fatal("failed to resolve CMS SDK version: %v", requiredSDKVersionErr)
}
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 {
// A codeless plugin repo (plugin.mod, no Go) has no go.mod by
// design — nothing for the replace-directive check to parse.
if samePath(target.root, ctx.backendDir) && !isCodelessPluginRepo(target.root) {
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 {
rep.Fail("%d CMS backend go.mod violation(s)", len(cmsGoModViolations))
for _, v := range cmsGoModViolations {
if v.line > 0 {
rep.Findingf("%s:%d [%s] %s", v.file, v.line, v.rule, v.detail)
continue
}
rep.Findingf("%s [%s] %s", v.file, v.rule, v.detail)
}
}
if len(pluginImportViolations) > 0 {
rep.Fail("%d standalone plugin import violation(s)", len(pluginImportViolations))
for _, v := range pluginImportViolations {
rep.Findingf("%s:%d imports first-party BlockNinja package %q (plugins may only use block/pluginsdk)", v.file, v.line, v.importPath)
}
}
if len(pluginGoModViolations) > 0 {
rep.Fail("%d standalone plugin go.mod violation(s)", len(pluginGoModViolations))
for _, v := range pluginGoModViolations {
if v.line > 0 {
rep.Findingf("%s:%d [%s] %s", v.file, v.line, v.rule, v.detail)
continue
}
rep.Findingf("%s [%s] %s", v.file, v.rule, v.detail)
}
}
} else if len(standalonePluginLabels) > 0 {
if requiredSDKVersion != "" {
rep.OK("Standalone plugin imports and go.mod stay on SDK version %s", requiredSDKVersion)
} else {
rep.OK("Standalone plugin imports and go.mod build against the plugin SDK")
}
} else if ctx.includeCoreTargets {
rep.OK("%s/go.mod does not locally replace %s", ctx.backendTargets[0].displayOrRoot(), blockCoreImportPrefix)
} else {
rep.Skip("no standalone plugin roots scanned")
}
},
})
}