A Swift CLI tool that analyzes Swift Package.swift files, scans target source directories for import statements, and compares declared dependencies against actual usage to identify missing or unused dependencies.
- Dependency Analysis: Identifies missing dependencies (imports without declarations) and unused dependencies (declarations without imports)
- Package.swift Parsing: Extracts target dependencies from Swift Package Manager manifests
- Import Scanning: Uses regex to scan Swift source files for import statements (including
@testable
imports) - Configurable Whitelist: Filter out system frameworks (Foundation, SwiftUI, AppKit, etc.)
- Parallel Processing: High-performance scanning using Swift's TaskGroup concurrency
- Multiple Output Formats: Colored terminal output, JSON, Xcode-compatible, or GitHub Actions format
- Target Filtering: Analyze specific targets or exclude test targets
- Swift 6.1+ Compatible: Built with modern Swift concurrency and strict typing
Clone the repository and build:
git clone https://github.com/yourusername/SwiftDependencyAudit.git
cd SwiftDependencyAudit
swift build -c release
The executable will be available at .build/release/swift-dependency-audit
.
# Analyze current directory
swift run swift-dependency-audit
# Analyze specific package
swift run swift-dependency-audit /path/to/package
# Use built executable
.build/release/swift-dependency-audit /path/to/package
USAGE: swift-dependency-audit [<path>] [--no-color] [--verbose] [--target <target>] [--exclude-tests] [--json] [--quiet] [--whitelist <whitelist>] [--output-format <output-format>]
ARGUMENTS:
<path> Path to Package.swift or package directory (default: current directory)
OPTIONS:
--no-color Disable colored output
-v, --verbose Enable verbose output
--target <target> Analyze specific target only
--exclude-tests Skip test targets
--json Output results in JSON format
-q, --quiet Only show problems, suppress success messages
--whitelist <whitelist> Comma-separated list of system imports to ignore
(e.g., Foundation,SwiftUI,AppKit)
--output-format <format> Output format: default, xcode, or github-actions
(default: default)
--version Show the version.
-h, --help Show help information.
# Verbose analysis with custom whitelist
swift run swift-dependency-audit --verbose --whitelist "Foundation,SwiftUI,AppKit,UIKit"
# JSON output for CI/automation
swift run swift-dependency-audit --json --no-color
# Analyze only a specific target
swift run swift-dependency-audit --target MyLibrary
# Exclude test targets from analysis
swift run swift-dependency-audit --exclude-tests
# Xcode-compatible output for IDE integration
swift run swift-dependency-audit --output-format xcode
# GitHub Actions format for CI/CD workflows
swift run swift-dependency-audit --output-format github-actions
# Quiet mode with Xcode format (only show problems)
swift run swift-dependency-audit --output-format xcode --quiet
📦 Package: MyAwesomePackage
🔍 Found 2 target(s) with dependency issues
🔍 Total missing: 1, unused: 2
📱 Target: MyLibrary
❌ Missing dependencies (1):
• Alamofire
⚠️ Unused dependencies (2):
• SwiftyJSON
• Kingfisher
🧪 Target: MyLibraryTests
✅ All dependencies correct
{
"packageName": "MyAwesomePackage",
"targets": [
{
"name": "MyLibrary",
"type": "regular",
"hasIssues": true,
"missingDependencies": ["Alamofire"],
"unusedDependencies": ["SwiftyJSON", "Kingfisher"],
"correctDependencies": ["Foundation"],
"sourceFiles": 15
}
],
"summary": {
"totalTargets": 2,
"targetsWithIssues": 1,
"totalMissing": 1,
"totalUnused": 2
}
}
Perfect for IDE integration and build systems:
/path/to/Sources/MyLibrary/NetworkManager.swift:15: error: Missing dependency 'Alamofire' is imported but not declared in Package.swift
/path/to/Package.swift:25: warning: Unused dependency 'SwiftyJSON' is declared but never imported
/path/to/Package.swift:26: warning: Unused dependency 'Kingfisher' is declared but never imported
Creates rich annotations in CI/CD workflows:
::error file=Sources/MyLibrary/NetworkManager.swift,line=15::Missing dependency 'Alamofire' is imported but not declared in Package.swift
::warning file=Package.swift,line=25::Unused dependency 'SwiftyJSON' is declared but never imported
::warning file=Package.swift,line=26::Unused dependency 'Kingfisher' is declared but never imported
- Parse Package.swift: Extracts package information and target dependencies
- Scan Source Files: Uses regex to find import statements in Swift files
- Compare Dependencies: Identifies mismatches between declared and actual imports
- Apply Whitelist: Filters out system frameworks and custom ignored modules
- Generate Report: Outputs findings in human-readable or JSON format
- IDE Integration: Seamless Xcode integration with clickable error/warning annotations
- CI/CD Integration: Validate dependencies in automated builds with GitHub Actions support
- Swift Build Plugins: Perfect output formats for Swift Package Manager build plugins
- Code Quality: Identify unused dependencies bloating your package
- Dependency Auditing: Ensure all imports are properly declared
- Package Cleanup: Find and remove unnecessary dependencies
- Migration Assistance: Verify dependencies when updating packages
- Automated Workflows: Rich annotations in GitHub Actions with file/line linking
- Swift 6.1+
- macOS 15+
- Xcode 16.4+ (for development)
swift build
swift test
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Make your changes
- Add tests for new functionality
- Ensure all tests pass (
swift test
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Swift Argument Parser
- Inspired by dependency analysis tools in other ecosystems
- Thanks to the Swift community for excellent tooling and documentation