# cargo-whys
A cargo subcommand that explains why dependencies are in your tree.
## Features
- **Reverse Dependency Lookup**: Find out which packages depend on a specific crate
- **Version Information**: See all versions of a dependency in your tree
- **Usage Scanning**: Scan your codebase to find where dependencies are actually used
- **Workspace Support**: Distinguishes between workspace members and external dependencies
- **JSON Output**: Machine-readable output for integration with other tools
- **Fast & Accurate**: Uses cargo metadata for precise dependency resolution
## Installation
```bash
cargo install cargo-whys
```
Or build from source:
```bash
git clone https://github.com/polysystems/cargo-whys
cd cargo-whys
cargo install --path .
```
## Usage
### Basic Usage
Find out why a dependency is in your tree:
```bash
cargo why serde
```
Output:
```
Analyzing dependency: serde
Version: 1.0.210
Used by:
• axum 0.7.0 [external]
• config 0.13.0 [external]
• my-app 0.1.0 [workspace]
Total imports found: 32 in codebase
Tip: Run with --where-used to see where it's used
```
### Show Usage Locations
See where in your codebase the dependency is imported:
```bash
cargo why serde --where-used
```
Output:
```
Analyzing dependency: serde
Version: 1.0.210
Used by:
• axum 0.7.0 [external]
• my-app 0.1.0 [workspace]
Total imports found: 32 in codebase
Usage locations:
• src/main.rs:3
use serde::{Serialize, Deserialize};
• src/models/user.rs:1
use serde::Serialize;
• src/config.rs:2
use serde_json;
```
### Show All Versions
If multiple versions of a dependency exist in your tree:
```bash
cargo why serde --all-versions
```
### JSON Output
For programmatic use:
```bash
cargo why serde --json
```
Output:
```json
{
"dependency_name": "serde",
"found": true,
"versions": [
{
"version": "1.0.210",
"used_by": [
{
"name": "my-app",
"version": "0.1.0",
"is_workspace_member": true
},
{
"name": "axum",
"version": "0.7.0",
"is_workspace_member": false
}
]
}
],
"total_usage_count": 32,
"usage_locations": null
}
```
### Custom Manifest Path
Analyze a project in a different directory:
```bash
cargo why serde --manifest-path /path/to/project
```
## Command-Line Options
```
cargo why <DEPENDENCY> [OPTIONS]
Arguments:
<DEPENDENCY> The dependency name to analyze
Options:
--where-used Show where in the codebase the dependency is used
--manifest-path <PATH> Path to the Cargo project directory [default: .]
--all-versions Show all versions of the dependency in the tree
--json Output in JSON format
-h, --help Print help
-V, --version Print version
```
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)