Documentation
¶
Overview ¶
Package dependencies analyzes import dependencies and provides language-agnostic cycle detection infrastructure. Language-specific analyzers (e.g., Go, Kotlin) build their own dependency graphs and delegate circular dependency detection to the shared algorithms here.
For Go projects, the dependency analyzer also categorizes imports into three types: - Standard library (e.g., fmt, os, strings) - Internal packages (within the same module) - External packages (third-party dependencies)
Import Categorization ¶
The analyzer uses the following heuristics: - Imports without a dot in the first path element are stdlib - golang.org/x/* imports are treated as stdlib - Imports matching the module path are internal - All other imports are external
Circular Dependency Detection ¶
Circular dependencies are detected using DFS traversal of the dependency graph. Only internal packages are considered for cycle detection. The algorithm: 1. Builds a graph of package -> imported packages 2. Runs DFS from each unvisited node 3. Detects back edges (cycles) during traversal 4. Normalizes and deduplicates cycles
Usage ¶
For Go analysis:
analyzer, err := dependencies.NewAnalyzer("/path/to/project")
if err != nil {
log.Fatal(err)
}
// Analyze imports
deps, err := analyzer.Analyze(fileMetrics)
// Detect circular dependencies
cycles, err := analyzer.DetectCircularDependencies(fileMetrics)
For other languages, build a dependency graph and use the shared cycle detector:
// Language providers with their own graph can reuse cycle detection: cycles := dependencies.FindCycles(graph)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Analyzer ¶
type Analyzer struct {
// contains filtered or unexported fields
}
Analyzer analyzes package dependencies
func NewAnalyzer ¶
NewAnalyzer creates a new dependency analyzer
func (*Analyzer) Analyze ¶
func (a *Analyzer) Analyze(files []*parser.FileMetrics) ([]*PackageDependencies, error)
Analyze analyzes dependencies across all files
func (*Analyzer) DetectCircularDependencies ¶
func (a *Analyzer) DetectCircularDependencies(files []*parser.FileMetrics) ([]*CircularDependency, error)
DetectCircularDependencies finds circular dependencies using DFS
type CircularDependency ¶
type CircularDependency struct {
Cycle []string `json:"cycle"` // The circular dependency chain
}
CircularDependency represents a circular dependency between packages
func FindCycles ¶ added in v1.4.0
func FindCycles(graph map[string][]string) []*CircularDependency
FindCycles detects circular dependencies in a package graph using DFS. The graph maps each package to the packages it imports. Language providers build their own graph and delegate cycle detection here.
Results are order-dependent: the algorithm reports a representative set of cycles per strongly-connected component, not all simple cycles, and both the reported set and each cycle's starting node depend on Go's randomized map iteration order.
func (*CircularDependency) FormatCycle ¶
func (cd *CircularDependency) FormatCycle() string
FormatCycle returns a human-readable representation of a circular dependency
type PackageDependencies ¶
type PackageDependencies struct {
PackageName string `json:"package_name"`
StdlibImports []string `json:"stdlib_imports"`
InternalImports []string `json:"internal_imports"`
ExternalImports []string `json:"external_imports"`
TotalImports int `json:"total_imports"`
ExternalImportCount int `json:"external_import_count"`
}
PackageDependencies represents dependency information for a single package