dependencies

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package dependencies analyzes import dependencies in Go projects.

The dependency analyzer categorizes imports into three types: - Standard library (e.g., fmt, os, strings) - Internal packages (within the same module) - External packages (third-party dependencies)

It also detects circular dependencies between internal packages using depth-first search (DFS) with cycle detection.

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

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)

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

func NewAnalyzer(projectPath string) (*Analyzer, error)

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 (*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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL