1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! Dependency staleness detection and transitive closure computation.
//!
//! This module implements the core fix for the dependency staleness bug that caused
//! version conflicts during publishing. When a workspace dependency is bumped, all
//! packages referencing the old version become "stale" and must be republished.
//!
//! # Problem
//!
//! The original publishing algorithm only detected packages with local changes
//! (via `publish_need()` which compares local vs published crate archives). It
//! completely missed packages whose dependencies were bumped but had no local changes.
//!
//! **Example failure:**
//! ```text
//! former: 2.36.0 → 2.37.0 (detected: version bump)
//! wca: requires former ~2.36.0 (NOT detected: no local changes)
//! willbe: requires wca ~0.36.0, former ~2.37.0
//!
//! Result: cargo resolves wca 0.36.0 from crates.io → requires former ~2.36.0
//! BUT former 2.37.0 already published
//! → VERSION CONFLICT ❌
//! ```
//!
//! # Solution
//!
//! Two-phase detection algorithm:
//!
//! **Phase 1: Staleness Detection** - `detect_stale_dependencies()`
//! - For each package, check all workspace dependencies
//! - Mark as stale if: workspace version doesn't satisfy package requirement
//! - Mark as stale if: dependency is in current publishing batch
//!
//! **Phase 2: Transitive Closure** - `compute_transitive_closure()`
//! - Start with initial set (packages with local changes)
//! - Iterate: find packages with stale dependencies, add to set
//! - Repeat until fixed point (no new packages added)
//! - Safeguard: `MAX_ITERATIONS=100` to prevent infinite loops
//!
//! # Known Pitfalls
//!
//! ## Semver Tilde Operator Strictness
//!
//! The tilde operator `~X.Y.Z` is STRICT on minor version, not lenient like caret `^X.Y.Z`.
//!
//! - `~2.36.0` means `>=2.36.0, <2.37.0` (NOT just >=2.36.0)
//! - Minor version bumps (2.36→2.37) BREAK tilde requirements
//! - Many developers assume tilde is lenient, causing surprise when dependencies break
//!
//! **Lesson:** After ANY minor version bump, check ALL dependents using tilde requirements.
//!
//! ## Transitive Staleness Requires Iteration
//!
//! One pass over packages is insufficient. Consider the chain A→B→C:
//!
//! - Iteration 1: C bumped → B becomes stale (direct dependency)
//! - Iteration 2: B being published → A becomes stale (depends on B)
//! - Convergence: Both B and A must be republished
//!
//! **Lesson:** Must iterate to fixed point. Single-pass algorithms WILL miss packages.
//!
//! ## Batch Publishing Creates Additional Staleness
//!
//! During batch publishing, packages depending on ANYTHING in the batch become stale,
//! even if versions match:
//!
//! ```text
//! Scenario: A and B both depend on C, C being published
//! Result: BOTH A and B are stale (BeingPublished reason)
//! ```
//!
//! **Why:** If A publishes with old C (from crates.io), but C is about to publish
//! new version, dependency resolution fails. Must use fresh versions.
//!
//! **Lesson:** Check for `BeingPublished` staleness SEPARATELY from version incompatibility.
//!
//! ## Fixed-Point Iteration Can Be Slow
//!
//! In pathological cases (deep dependency chains, large workspaces), convergence
//! may take many iterations:
//!
//! - Typical case (5-10 packages): 2-3 iterations
//! - Large workspace (100+ packages): 5-10 iterations
//! - Pathological (deep chains): 20+ iterations
//!
//! **Mitigation:** `MAX_ITERATIONS=100` safeguard prevents infinite loops but allows
//! even worst-case scenarios to complete.
//!
//! # Architecture Decision: Why Two Separate Functions?
//!
//! `detect_stale_dependencies()` and `compute_transitive_closure()` could be merged
//! into a single function. We keep them separate because:
//!
//! 1. **Testability:** Can test staleness detection independently from closure computation
//! 2. **Clarity:** Each function has single, clear responsibility
//! 3. **Reusability:** Staleness detection useful for diagnostics without full closure
//! 4. **Performance:** Can optimize each phase independently
//!
//! # Integration Point
//!
//! This module is called from `tool::graph::remove_not_required_to_publish()`:
//!
//! ```rust,ignore
//! // Phase 1: Detect local changes (existing)
//! let packages_with_changes = detect_via_publish_need();
//!
//! // Phase 2: Compute transitive closure (NEW - this module)
//! let packages_to_publish = compute_transitive_closure(workspace, &packages_with_changes);
//!
//! // Phase 3: Add all to publish set (existing)
//! for package in packages_to_publish { ... }
//! ```
//
crate mod_interface!