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() }, }) }