cargo-upkeep 0.1.7

Unified Rust project maintenance CLI (cargo subcommand)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use cargo_metadata::{CargoOpt, DependencyKind, MetadataCommand, Package, PackageId};
use std::collections::{HashMap, HashSet};

use crate::cli::TreeArgs;
use crate::core::error::{ErrorCode, Result, UpkeepError};
use crate::core::output::{print_json, TreeNode, TreeOutput, TreeStats};

/// Maximum recursion depth for tree traversal.
/// This prevents stack overflow on pathologically deep dependency graphs.
/// Set to 200 as a conservative limit - typical dependency trees are much shallower,
/// and deep recursion can exhaust stack space on some platforms.
const MAX_TREE_DEPTH: usize = 200;

#[derive(Clone)]
struct Edge {
    id: PackageId,
    is_dev: bool,
    is_build: bool,
}

/// Context for tree building operations, grouping related parameters
/// to avoid passing many arguments through recursive calls.
struct TreeBuildContext<'a> {
    graph: &'a HashMap<PackageId, Vec<Edge>>,
    packages_by_id: &'a HashMap<PackageId, &'a Package>,
    features_by_id: &'a HashMap<PackageId, Vec<String>>,
    args: &'a TreeArgs,
    duplicate_names: &'a HashSet<String>,
    expanded: HashSet<PackageId>,
    path: HashSet<PackageId>,
}

pub async fn run(json: bool, args: TreeArgs) -> Result<()> {
    let cwd = std::env::current_dir()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|err| {
            tracing::warn!("could not determine current directory: {err}");
            "<unknown>".to_string()
        });

    let metadata = MetadataCommand::new()
        .features(CargoOpt::AllFeatures)
        .exec()
        .map_err(|err| {
            UpkeepError::context(
                ErrorCode::Metadata,
                format!("failed to load cargo metadata in directory: {cwd}"),
                err,
            )
        })?;
    let resolve = metadata.resolve.as_ref().ok_or_else(|| {
        UpkeepError::message(ErrorCode::InvalidData, "metadata missing resolve data")
    })?;

    let mut packages_by_id: HashMap<PackageId, &Package> = HashMap::new();
    let mut versions_by_name: HashMap<String, HashSet<String>> = HashMap::new();
    for package in &metadata.packages {
        versions_by_name
            .entry(package.name.to_string())
            .or_default()
            .insert(package.version.to_string());
        packages_by_id.insert(package.id.clone(), package);
    }

    let duplicate_names: HashSet<String> = versions_by_name
        .into_iter()
        .filter_map(|(name, versions)| if versions.len() > 1 { Some(name) } else { None })
        .collect();

    let mut features_by_id = HashMap::new();
    let mut forward_graph: HashMap<PackageId, Vec<Edge>> = HashMap::new();

    for node in &resolve.nodes {
        let features: Vec<String> = node.features.iter().map(ToString::to_string).collect();
        features_by_id.insert(node.id.clone(), features);
        for dep in &node.deps {
            if args.no_dev && !includes_non_dev(dep) {
                continue;
            }

            let is_dev = dep
                .dep_kinds
                .iter()
                .any(|kind| kind.kind == DependencyKind::Development);
            let is_build = dep
                .dep_kinds
                .iter()
                .any(|kind| kind.kind == DependencyKind::Build);

            forward_graph
                .entry(node.id.clone())
                .or_default()
                .push(Edge {
                    id: dep.pkg.clone(),
                    is_dev,
                    is_build,
                });
        }
    }

    let invert_target = args.invert.as_deref();
    let graph = if invert_target.is_some() {
        invert_graph(&forward_graph)
    } else {
        forward_graph
    };

    let mut ctx = TreeBuildContext {
        graph: &graph,
        packages_by_id: &packages_by_id,
        features_by_id: &features_by_id,
        args: &args,
        duplicate_names: &duplicate_names,
        expanded: HashSet::new(),
        path: HashSet::new(),
    };

    let root = match invert_target {
        Some(name) => build_inverted_root(name, &mut ctx)?,
        None => {
            // Handle virtual workspaces by creating a synthetic root node
            match metadata.root_package() {
                Some(root_package) => build_node(&root_package.id, 0, false, false, &mut ctx)?
                    .unwrap_or_else(|| TreeNode::empty("root")),
                None => {
                    // Virtual workspace: create a synthetic root containing all workspace members
                    let mut root = TreeNode::virtual_root();
                    for member_id in &metadata.workspace_members {
                        if let Some(child) = build_node(member_id, 1, false, false, &mut ctx)? {
                            root.dependencies.push(child);
                        }
                    }
                    if root.dependencies.is_empty() {
                        return Err(UpkeepError::message(
                            ErrorCode::InvalidData,
                            "no packages found in workspace",
                        ));
                    }
                    root
                }
            }
        }
    };

    let stats = build_stats(&root);
    let output = TreeOutput { root, stats };

    emit_output(json, &args, &output)
}

fn emit_output(json: bool, args: &TreeArgs, output: &TreeOutput) -> Result<()> {
    if json {
        print_json(output)
    } else {
        let rendered = render_tree(&output.root, args.features);
        println!("{rendered}");
        println!(
            "\nCrates: {}  Direct deps: {}  Transitive deps: {}  Duplicate crates: {}",
            output.stats.total_crates,
            output.stats.direct_deps,
            output.stats.transitive_deps,
            output.stats.duplicate_crates
        );
        Ok(())
    }
}

fn includes_non_dev(dep: &cargo_metadata::NodeDep) -> bool {
    dep.dep_kinds.iter().any(|kind| match kind.kind {
        DependencyKind::Development => false,
        DependencyKind::Normal | DependencyKind::Build | DependencyKind::Unknown => true,
    })
}

fn invert_graph(graph: &HashMap<PackageId, Vec<Edge>>) -> HashMap<PackageId, Vec<Edge>> {
    let mut inverted: HashMap<PackageId, Vec<Edge>> = HashMap::new();
    for (from, children) in graph {
        for child in children {
            inverted.entry(child.id.clone()).or_default().push(Edge {
                id: from.clone(),
                is_dev: child.is_dev,
                is_build: child.is_build,
            });
        }
    }
    inverted
}

fn build_inverted_root(name: &str, ctx: &mut TreeBuildContext<'_>) -> Result<TreeNode> {
    let mut matches = Vec::new();
    for (id, package) in ctx.packages_by_id {
        if package.name == name {
            matches.push(id.clone());
        }
    }

    if matches.is_empty() {
        return Err(UpkeepError::message(
            ErrorCode::InvalidData,
            format!("no package named '{name}' found in metadata"),
        ));
    }

    if matches.len() == 1 {
        return build_node(&matches[0], 0, false, false, ctx)?.ok_or_else(|| {
            UpkeepError::message(
                ErrorCode::InvalidData,
                format!("no dependencies to display for {name}"),
            )
        });
    }

    let mut root = TreeNode::empty(&format!("reverse:{name}"));
    for id in matches {
        if let Some(child) = build_node(&id, 1, false, false, ctx)? {
            root.dependencies.push(child);
        }
    }

    Ok(root)
}

fn build_node(
    id: &PackageId,
    depth: usize,
    is_dev: bool,
    is_build: bool,
    ctx: &mut TreeBuildContext<'_>,
) -> Result<Option<TreeNode>> {
    // Safety limit to prevent stack overflow on pathologically deep graphs
    if depth > MAX_TREE_DEPTH {
        let package_name = ctx
            .packages_by_id
            .get(id)
            .map(|p| p.name.as_str())
            .unwrap_or("<unknown>");
        return Err(UpkeepError::message(
            ErrorCode::InvalidData,
            format!(
                "dependency tree exceeds maximum depth of {MAX_TREE_DEPTH} at package '{package_name}'; \
                 possible circular dependency or extremely deep graph"
            ),
        ));
    }

    let package = ctx.packages_by_id.get(id).ok_or_else(|| {
        UpkeepError::message(
            ErrorCode::InvalidData,
            format!("package {id} missing from metadata"),
        )
    })?;

    let duplicate = ctx.duplicate_names.contains(package.name.as_str());
    let mut node = TreeNode {
        name: package.name.to_string(),
        version: package.version.to_string(),
        package_id: package.id.to_string(),
        features: if ctx.args.features {
            ctx.features_by_id.get(id).cloned().unwrap_or_default()
        } else {
            Vec::new()
        },
        dependencies: Vec::new(),
        is_dev,
        is_build,
        duplicate,
    };

    if ctx.args.depth.map(|limit| depth >= limit).unwrap_or(false) {
        return Ok(if ctx.args.duplicates && !duplicate && depth > 0 {
            None
        } else {
            Some(node)
        });
    }

    if !ctx.expanded.insert(id.clone()) {
        return Ok(if ctx.args.duplicates && !duplicate && depth > 0 {
            None
        } else {
            Some(node)
        });
    }

    // Track current path for cycle detection (checked in child loop below)
    // Use a scope guard pattern to ensure path is always cleaned up, even on error
    ctx.path.insert(id.clone());

    let result = (|| -> Result<()> {
        let mut children = ctx.graph.get(id).cloned().unwrap_or_default();
        children.sort_by(|left, right| {
            let left_name = ctx
                .packages_by_id
                .get(&left.id)
                .map(|pkg| pkg.name.as_str())
                .unwrap_or("~");
            let right_name = ctx
                .packages_by_id
                .get(&right.id)
                .map(|pkg| pkg.name.as_str())
                .unwrap_or("~");
            left_name.cmp(right_name)
        });

        for child in children {
            if ctx.path.contains(&child.id) {
                continue;
            }
            if let Some(child_node) =
                build_node(&child.id, depth + 1, child.is_dev, child.is_build, ctx)?
            {
                node.dependencies.push(child_node);
            }
        }
        Ok(())
    })();

    // Always clean up path, even if an error occurred
    ctx.path.remove(id);
    result?;

    if ctx.args.duplicates && !duplicate && depth > 0 && node.dependencies.is_empty() {
        return Ok(None);
    }

    Ok(Some(node))
}

fn build_stats(root: &TreeNode) -> TreeStats {
    let mut ids = HashSet::new();
    collect_ids(root, &mut ids);

    let mut versions_by_name: HashMap<String, HashSet<String>> = HashMap::new();
    collect_versions(root, &mut versions_by_name);
    let duplicate_crates = versions_by_name
        .values()
        .filter(|versions| versions.len() > 1)
        .count();

    let root_is_real = !root.package_id.is_empty();
    let direct_deps = root.dependencies.len();
    let total_crates = ids.len();
    let transitive_deps = total_crates.saturating_sub(direct_deps + usize::from(root_is_real));

    TreeStats {
        total_crates,
        direct_deps,
        transitive_deps,
        duplicate_crates,
    }
}

fn collect_ids(node: &TreeNode, ids: &mut HashSet<String>) {
    if !node.package_id.is_empty() {
        ids.insert(node.package_id.clone());
    }
    for child in &node.dependencies {
        collect_ids(child, ids);
    }
}

fn collect_versions(node: &TreeNode, versions_by_name: &mut HashMap<String, HashSet<String>>) {
    if !node.version.is_empty() {
        versions_by_name
            .entry(node.name.clone())
            .or_default()
            .insert(node.version.clone());
    }
    for child in &node.dependencies {
        collect_versions(child, versions_by_name);
    }
}

fn render_tree(root: &TreeNode, show_features: bool) -> String {
    let mut lines = Vec::new();
    lines.push(format_label(root, show_features));

    let children_len = root.dependencies.len();
    for (index, child) in root.dependencies.iter().enumerate() {
        let is_last = index + 1 == children_len;
        render_node(child, "", is_last, show_features, &mut lines);
    }

    lines.join("\n")
}

fn render_node(
    node: &TreeNode,
    prefix: &str,
    is_last: bool,
    show_features: bool,
    lines: &mut Vec<String>,
) {
    let connector = if is_last { "`-- " } else { "|-- " };
    lines.push(format!(
        "{prefix}{connector}{}",
        format_label(node, show_features)
    ));

    let next_prefix = if is_last {
        format!("{prefix}    ")
    } else {
        format!("{prefix}|   ")
    };

    let children_len = node.dependencies.len();
    for (index, child) in node.dependencies.iter().enumerate() {
        let child_last = index + 1 == children_len;
        render_node(child, &next_prefix, child_last, show_features, lines);
    }
}

fn format_label(node: &TreeNode, show_features: bool) -> String {
    let mut annotations: Vec<String> = Vec::new();
    if node.duplicate {
        annotations.push("dup".to_string());
    }
    if node.is_dev {
        annotations.push("dev".to_string());
    }
    if node.is_build {
        annotations.push("build".to_string());
    }

    if show_features && !node.features.is_empty() {
        annotations.push(format!("features: {}", node.features.join(", ")));
    }

    let version_suffix = if node.version.is_empty() {
        String::new()
    } else {
        format!(" v{}", node.version)
    };

    if annotations.is_empty() {
        format!("{}{}", node.name, version_suffix)
    } else {
        format!(
            "{}{} [{}]",
            node.name,
            version_suffix,
            annotations.join(", ")
        )
    }
}

impl TreeNode {
    /// Creates an empty tree node with a given name.
    /// Used for synthetic nodes like inverted tree roots.
    fn empty(name: &str) -> Self {
        Self {
            name: name.to_string(),
            version: String::new(),
            package_id: String::new(),
            features: Vec::new(),
            dependencies: Vec::new(),
            is_dev: false,
            is_build: false,
            duplicate: false,
        }
    }

    /// Creates a synthetic root node for virtual workspaces.
    ///
    /// Virtual workspaces have no root package, so we create a placeholder
    /// root that contains all workspace members as direct dependencies.
    fn virtual_root() -> Self {
        Self {
            name: "(workspace)".to_string(),
            version: String::new(),
            package_id: String::new(),
            features: Vec::new(),
            dependencies: Vec::new(),
            is_dev: false,
            is_build: false,
            duplicate: false,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::core::output::{TreeNode, TreeOutput, TreeStats};
    use serde_json::Value;

    #[test]
    fn emit_output_json_shape() {
        let output = TreeOutput {
            root: TreeNode {
                name: "root".to_string(),
                version: "0.1.0".to_string(),
                package_id: "root 0.1.0".to_string(),
                features: Vec::new(),
                dependencies: vec![TreeNode {
                    name: "dep".to_string(),
                    version: "1.2.3".to_string(),
                    package_id: "dep 1.2.3".to_string(),
                    features: Vec::new(),
                    dependencies: Vec::new(),
                    is_dev: false,
                    is_build: false,
                    duplicate: false,
                }],
                is_dev: false,
                is_build: false,
                duplicate: false,
            },
            stats: TreeStats {
                total_crates: 2,
                direct_deps: 1,
                transitive_deps: 0,
                duplicate_crates: 0,
            },
        };

        let value = serde_json::to_value(&output).expect("serialize");
        assert_eq!(value["stats"]["direct_deps"], Value::Number(1.into()));
        assert_eq!(
            value["root"]["dependencies"][0]["name"],
            Value::String("dep".into())
        );
    }
}