Skip to content

Commit 2e49d86

Browse files
committed
doc: improve README
1 parent 725dda0 commit 2e49d86

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

README.md

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Check Dependency Inheritance in Cargo Workspaces
22

3-
A Cargo subcommand that enforces dependency inheritance hygiene in Cargo workspaces. It detects crates that specify dependency versions directly instead of using [`workspace = true`](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#inheriting-a-dependency-from-a-workspace), flags version mismatches, and suggests candidates for workspace promotion.
3+
A Cargo subcommand that detects workspace dependency inheritance issues in Cargo workspaces. It finds crates that specify dependency versions directly instead of using [`workspace = true`](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#inheriting-a-dependency-from-a-workspace), flags version mismatches, and suggests candidates for workspace promotion.
44

55
## Why?
66

7-
Cargo workspaces support [dependency inheritance](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#inheriting-a-dependency-from-a-workspace),you declare shared dependency versions once in the root `Cargo.toml` under `[workspace.dependencies]`, and member crates reference them with `{ workspace = true }`. This prevents version drift and duplicated dependency specs across workspace members.
7+
Cargo workspaces support [dependency inheritance](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#inheriting-a-dependency-from-a-workspace), you declare shared dependency versions once in the root `Cargo.toml` under `[workspace.dependencies]`, and member crates reference them with `{ workspace = true }`. This prevents version drift and duplicated dependency specs across workspace members.
88

99
However, **Cargo has no built-in lint** for detecting when a crate specifies a version directly instead of inheriting from the workspace. [Clippy issue #10306](https://github.com/rust-lang/rust-clippy/issues/10306) tracks this as a feature request but remains unimplemented.
1010

@@ -30,7 +30,7 @@ error: `rand = "0.7"` in crates/bar/Cargo.toml has a different version than work
3030

3131
### 3. Candidate for workspace promotion (warning)
3232

33-
A dependency appears in multiple crates but isn't declared in `[workspace.dependencies]`.
33+
A dependency appears in multiple crates but isn't declared in `[workspace.dependencies]`. This check helps you identify dependencies that should be centralized.
3434

3535
```
3636
warning: `serde_yaml` appears in 3 crates but is not in [workspace.dependencies]
@@ -40,6 +40,8 @@ warning: `serde_yaml` appears in 3 crates but is not in [workspace.dependencies]
4040
hint: consider adding `serde_yaml = "0.9"` to [workspace.dependencies]
4141
```
4242

43+
By default, this is reported as a **warning** and does not cause the tool to exit with a non-zero code. Use `--promotion-failure` to treat promotion candidates as errors.
44+
4345
## Installation
4446

4547
```bash
@@ -48,37 +50,51 @@ cargo install cargo-workspace-inheritance-check
4850

4951
## Usage
5052

51-
Run from your workspace root:
53+
### Default behavior
54+
55+
Running without any flags checks all workspace members against `[workspace.dependencies]` in the root `Cargo.toml`. It reports errors for dependencies not using inheritance ([checks 1 and 2](#what-it-checks)) and warnings for promotion candidates ([check 3](#3-candidate-for-workspace-promotion-warning)). The tool exits with code 1 if any **errors** are found, and 0 if there are only warnings or no issues.
5256

5357
```bash
54-
# Check workspace dependency inheritance
5558
cargo workspace-inheritance-check
59+
```
60+
61+
### Options
5662

57-
# Specify a different workspace root
63+
| Flag | Description | Default |
64+
|------|-------------|---------|
65+
| `--path <PATH>` | Path to the workspace root | `.` |
66+
| `--promotion-threshold <N>` | Minimum number of crates a dependency must appear in before it is flagged as a promotion candidate. For example, `--promotion-threshold 3` means a dependency must appear in at least 3 crates to trigger a warning. | `2` |
67+
| `--promotion-failure` | Promote promotion candidate warnings to errors, causing the tool to exit with code 1 when candidates are found. Useful in CI when you want to enforce that all shared dependencies are declared in `[workspace.dependencies]`. | `false` |
68+
| `--format <FORMAT>` | Output format: `human` or `json` | `human` |
69+
| `--no-fail` | Always exit with code 0, regardless of errors found. Useful when you want to see the report without failing a CI pipeline. | `false` |
70+
71+
### Examples
72+
73+
```bash
74+
# Check a workspace at a specific path
5875
cargo workspace-inheritance-check --path /path/to/workspace
5976

60-
# Set promotion threshold (default: 2)
77+
# Only flag dependencies used in 3+ crates as promotion candidates
6178
cargo workspace-inheritance-check --promotion-threshold 3
6279

63-
# Treat promotion candidates as errors
80+
# Fail CI if any dependency could be promoted to workspace level
6481
cargo workspace-inheritance-check --promotion-failure
6582

66-
# Output as JSON
83+
# Get machine-readable output
6784
cargo workspace-inheritance-check --format json
6885

69-
# Don't fail on errors (always exit 0)
86+
# Report issues without failing
7087
cargo workspace-inheritance-check --no-fail
7188
```
7289

73-
### Options
90+
## Exit Codes
7491

75-
| Flag | Description | Default |
76-
|------|-------------|---------|
77-
| `--path <PATH>` | Path to workspace root | `.` |
78-
| `--promotion-threshold <N>` | Min crate count before suggesting workspace promotion | `2` |
79-
| `--promotion-failure` | Treat promotion candidates as errors | `false` |
80-
| `--format <FORMAT>` | Output format: `human`, `json` | `human` |
81-
| `--no-fail` | Exit 0 even when errors are found | `false` |
92+
| Code | Meaning |
93+
|------|---------|
94+
| `0` | No errors found (warnings are OK unless `--promotion-failure` is set) |
95+
| `1` | Errors found (or promotion warnings promoted to errors via `--promotion-failure`) |
96+
97+
`--no-fail` overrides the exit code to always be `0`.
8298

8399
## Ignore Rules
84100

@@ -87,16 +103,18 @@ You can skip specific dependencies from being checked by adding ignore rules to
87103
```toml
88104
[workspace.metadata.inheritance-check]
89105
ignore = [
90-
# Ignore rand in a specific crate
106+
# Ignore rand in a specific crate (e.g., it intentionally uses a different version)
91107
{ dependency = "rand", member = "crates/bar" },
92108
# Ignore openssl in all crates
93109
{ dependency = "openssl" },
94110
]
95111
```
96112

113+
Ignored dependencies are skipped in both reporting and fixing.
114+
97115
## JSON Output
98116

99-
Use `--format json` for machine-readable output, useful for integrating with other tools:
117+
Use `--format json` for machine-readable output, useful for integrating with other tools or custom CI scripts:
100118

101119
```json
102120
{
@@ -129,6 +147,13 @@ Use `--format json` for machine-readable output, useful for integrating with oth
129147
run: cargo workspace-inheritance-check
130148
```
131149
150+
To also enforce that shared dependencies are promoted to workspace level:
151+
152+
```yaml
153+
- name: Check workspace dependency inheritance (strict)
154+
run: cargo workspace-inheritance-check --promotion-failure
155+
```
156+
132157
## Dependency Sections Checked
133158
134159
All dependency sections in member crates are checked:

0 commit comments

Comments
 (0)