check-safety/plugin_imports.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

160 lines
3.9 KiB
Go

package main
import (
"go/parser"
"go/token"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
const (
blockCoreImportPrefix = "git.dev.alexdunmow.com/block/core"
pluginSDKImportPrefix = "git.dev.alexdunmow.com/block/pluginsdk"
blockNinjaImportPrefix = "git.dev.alexdunmow.com/block/cms"
orchestratorImportPrefix = "git.dev.alexdunmow.com/block/orchestrator"
firstPartyImportPrefix = "git.dev.alexdunmow.com/block/"
)
// allowedCorePluginImports are the block/core packages that deliberately
// stayed on core after the pluginsdk extraction (2026-07-07) and remain
// plugin-usable; everything else first-party is host-only.
var allowedCorePluginImports = []string{
blockCoreImportPrefix + "/captcha",
blockCoreImportPrefix + "/backup",
}
func hasImportPrefix(importPath, prefix string) bool {
return importPath == prefix || strings.HasPrefix(importPath, prefix+"/")
}
// isForbiddenPluginImport reports whether a standalone plugin may not import
// the package: all BlockNinja first-party Go code (cms, orchestrator, core)
// is off-limits except the plugin SDK and the explicitly kept core packages.
func isForbiddenPluginImport(importPath string) bool {
if hasImportPrefix(importPath, blockNinjaImportPrefix) || hasImportPrefix(importPath, orchestratorImportPrefix) {
return true
}
if hasImportPrefix(importPath, blockCoreImportPrefix) {
for _, allowed := range allowedCorePluginImports {
if hasImportPrefix(importPath, allowed) {
return false
}
}
return true
}
return false
}
type pluginImportViolation struct {
file string
line int
importPath string
}
var templImportPattern = regexp.MustCompile(`"([^"]+)"`)
func shouldCheckStandalonePluginImports(root string) bool {
return isPluginModuleRoot(root) && !isBundledPluginRoot(root)
}
func isBundledPluginRoot(root string) bool {
bundledPluginsDir := filepath.Join(blockNinjaRepoRoot(), "backend", "internal", "plugins")
absRoot, err := filepath.Abs(root)
if err != nil {
return false
}
absBundledPluginsDir, err := filepath.Abs(bundledPluginsDir)
if err != nil {
return false
}
rel, err := filepath.Rel(absBundledPluginsDir, absRoot)
if err != nil {
return false
}
if rel == "." {
return false
}
return !strings.HasPrefix(rel, ".."+string(filepath.Separator)) && rel != ".."
}
func checkStandalonePluginImports(root string) []pluginImportViolation {
var violations []pluginImportViolation
_ = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
switch info.Name() {
case ".git", ".worktrees", "vendor", "node_modules":
return filepath.SkipDir
}
return nil
}
relPath, _ := filepath.Rel(root, path)
if strings.HasSuffix(path, ".templ") {
data, readErr := os.ReadFile(path)
if readErr != nil {
return nil
}
lines := strings.Split(string(data), "\n")
for i, line := range lines {
if !strings.Contains(line, firstPartyImportPrefix) {
continue
}
matches := templImportPattern.FindAllStringSubmatch(line, -1)
for _, match := range matches {
importPath := match[1]
if !isForbiddenPluginImport(importPath) {
continue
}
violations = append(violations, pluginImportViolation{
file: relPath,
line: i + 1,
importPath: importPath,
})
}
}
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly)
if err != nil {
return nil
}
for _, spec := range file.Imports {
importPath, err := strconv.Unquote(spec.Path.Value)
if err != nil {
continue
}
if !isForbiddenPluginImport(importPath) {
continue
}
pos := fset.Position(spec.Pos())
violations = append(violations, pluginImportViolation{
file: relPath,
line: pos.Line,
importPath: importPath,
})
}
return nil
})
return violations
}