Allow composition within command internal helpers

This commit is contained in:
2026-10-06 01:08:34 +00:00
parent 791152370c
commit ce3a85e3e6
3 changed files with 51 additions and 3 deletions

View File

@@ -112,7 +112,10 @@ func checkImportDirections(packages []listedPackage) error {
continue
}
for _, imported := range pkg.Imports {
if imported == modulePath || imported == modulePath+"/buildinfo" || strings.HasPrefix(imported, modulePath+"/cmd/") {
commandDependency := strings.HasPrefix(imported, modulePath+"/cmd/")
owner := commandInternalRoot(pkg.ImportPath)
ownHelper := owner != "" && strings.HasPrefix(imported+"/", owner)
if imported == modulePath || imported == modulePath+"/buildinfo" || commandDependency && !ownHelper {
violations = append(violations, fmt.Sprintf("internal package %s imports public or command package %s", pkg.ImportPath, imported))
}
}
@@ -150,8 +153,21 @@ func checkImportDirections(packages []listedPackage) error {
}
func isInternalPackage(path string) bool {
return strings.HasPrefix(path, modulePath+"/internal/") ||
strings.HasPrefix(path, modulePath+"/") && strings.Contains(strings.TrimPrefix(path, modulePath), "/internal/")
return strings.HasPrefix(path, modulePath+"/") &&
(strings.Contains(strings.TrimPrefix(path, modulePath), "/internal/") || strings.HasSuffix(path, "/internal"))
}
// commandInternalRoot identifies the helper subtree owned by one executable.
func commandInternalRoot(path string) string {
if !strings.HasPrefix(path, modulePath+"/cmd/") {
return ""
}
relative := strings.TrimPrefix(path, modulePath+"/cmd/")
owner, _, ok := strings.Cut(relative+"/", "/internal/")
if !ok {
return ""
}
return modulePath + "/cmd/" + owner + "/internal/"
}
func forbiddenRuntimeDependency(path string) bool {