cargo-port 0.2.0

A TUI for inspecting and managing Rust projects
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::io::ErrorKind;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;

use cargo_metadata::Error;
use cargo_metadata::Metadata;
use tokio::runtime::Handle;
use tokio::sync::Semaphore;
use tui_pane::PERF_LOG_TARGET;
use walkdir::WalkDir;

use super::BackgroundMsg;
use super::constants::CARGO_OFFLINE_FLAG;
use super::discovery;
use super::disk_usage;
use super::language_stats;
use super::test_counts;
use super::tree;
use crate::channel;
use crate::channel::Receiver;
use crate::channel::Sender;
use crate::config::NonRustInclusion;
use crate::constants::CARGO_METADATA_TIMEOUT;
use crate::constants::CARGO_TOML;
use crate::constants::SCAN_DISK_CONCURRENCY;
use crate::constants::SCAN_METADATA_CONCURRENCY;
use crate::http::HttpClient;
use crate::project::AbsolutePath;
use crate::project::FileStamp;
use crate::project::ManifestFingerprint;
use crate::project::PackageRecord;
use crate::project::PublishPolicy;
use crate::project::RootItem;
use crate::project::TargetRecord;
use crate::project::WorkspaceMetadata;
use crate::project::WorkspaceMetadataStore;

/// Structured failure for a `cargo metadata` invocation. Held inside
/// [`BackgroundMsg::CargoMetadata`] so the main loop can raise a keyed
/// error toast and leave the affected rows in fallback state.
///
/// Variant chosen deliberately so the handler dispatches on cause rather
/// than string-matching: `WorkspaceMissing` is the expected race when the
/// user just deleted a worktree (no toast — the workspace is gone), and
/// `Other` is a real failure surface that needs to be visible.
#[derive(Clone, Debug)]
pub(crate) enum CargoMetadataError {
    /// Workspace root no longer exists on disk between dispatch and run.
    /// Common when a worktree is deleted while a refresh is in flight.
    /// Logged at debug; never shown to the user.
    WorkspaceMissing,
    /// All other failures: cargo subprocess errors, timeouts, parse
    /// failures. Shown verbatim in a timed error toast.
    Other(String),
}

impl CargoMetadataError {
    /// Message body for the error toast — only meaningful for `Other`.
    pub(crate) const fn user_facing_message(&self) -> Option<&str> {
        match self {
            Self::WorkspaceMissing => None,
            Self::Other(message) => Some(message.as_str()),
        }
    }
}

#[derive(Clone)]
pub(super) struct StreamingScanContext {
    pub(super) client:         HttpClient,
    pub(super) sender:         Sender<BackgroundMsg>,
    pub(super) disk_limit:     Arc<Semaphore>,
    pub(super) metadata_store: Arc<Mutex<WorkspaceMetadataStore>>,
    pub(super) metadata_limit: Arc<Semaphore>,
}

/// Spawn a streaming scan using a hybrid approach:
///
/// - **Discovery (scan thread):** Walk the directory tree, discover projects, and emit rows
///   quickly.
/// - **Local enrichment (tokio blocking pool):** Git info runs behind its own semaphore so it does
///   not block discovery.
/// - **Disk usage (tokio blocking pool):** `dir_size()` runs behind its own semaphore so disk walks
///   cannot monopolize startup.
/// - **HTTP (tokio):** CI runs, repo metadata, crates.io info, and connectivity checks run on the
///   async runtime behind a shared semaphore.
///
/// `ScanResult` is sent after discovery/local work has finished, containing the complete tree
/// and flat entries. Disk and HTTP results may continue to stream in afterward.
pub(crate) fn spawn_streaming_scan(
    scan_dirs: Vec<AbsolutePath>,
    inline_dirs: &[String],
    non_rust: NonRustInclusion,
    client: HttpClient,
    metadata_store: Arc<Mutex<WorkspaceMetadataStore>>,
) -> (Sender<BackgroundMsg>, Receiver<BackgroundMsg>) {
    let (sender, receiver) = channel::unbounded();
    let inline_dirs = inline_dirs.to_vec();

    let scan_sender = sender.clone();
    thread::spawn(move || {
        let scan_context = StreamingScanContext {
            client,
            sender: scan_sender.clone(),
            disk_limit: Arc::new(tokio::sync::Semaphore::new(SCAN_DISK_CONCURRENCY)),
            metadata_store,
            metadata_limit: Arc::new(tokio::sync::Semaphore::new(SCAN_METADATA_CONCURRENCY)),
        };

        let phase1_started = std::time::Instant::now();
        let phase1 = discovery::phase1_discover(&scan_dirs, non_rust);
        tracing::trace!(
            target: PERF_LOG_TARGET,
            elapsed_ms = tui_pane::perf_log_ms(phase1_started.elapsed().as_millis()),
            scan_dirs = scan_dirs.len(),
            visited_dirs = phase1.stats.visited_dirs,
            manifests = phase1.stats.manifests,
            projects = phase1.stats.projects,
            non_rust_projects = phase1.stats.non_rust_projects,
            disk_entries = phase1.disk_entries.len(),
            "phase1_discover_total"
        );

        let tree_started = std::time::Instant::now();
        let projects = tree::build_tree(&phase1.items, &inline_dirs);
        tracing::trace!(
            target: PERF_LOG_TARGET,
            elapsed_ms = tui_pane::perf_log_ms(tree_started.elapsed().as_millis()),
            input_items = phase1.items.len(),
            tree_items = projects.len(),
            "scan_tree_build"
        );

        let workspace_roots = collect_cargo_metadata_roots(&projects);
        let _ = scan_sender.send(BackgroundMsg::ScanResult {
            projects,
            disk_entries: phase1.disk_entries.clone(),
        });
        disk_usage::spawn_initial_disk_usage(&scan_context, &phase1.disk_entries);
        language_stats::spawn_initial_language_stats(&scan_context, &phase1.disk_entries);
        test_counts::spawn_initial_test_counts(&scan_context, &phase1.disk_entries);
        spawn_cargo_metadata_tree(&scan_context, workspace_roots);
    });

    (sender, receiver)
}

/// Collect distinct workspace roots that warrant a `cargo metadata`
/// dispatch — every Rust leaf project (workspace or standalone package),
/// worktree members included. Non-Rust roots are skipped.
fn collect_cargo_metadata_roots(projects: &[RootItem]) -> Vec<AbsolutePath> {
    let mut seen: HashSet<AbsolutePath> = HashSet::new();
    let mut roots = Vec::new();
    for item in projects {
        for root in cargo_metadata_roots_for_item(item) {
            if seen.insert(root.clone()) {
                roots.push(root);
            }
        }
    }
    roots
}

/// The `cargo metadata` workspace roots a single `RootItem` owns: a
/// standalone Rust project's own path, every checkout of a worktree
/// group, or nothing for a non-Rust project. Incremental insertion
/// paths dispatch a refresh per root so a project never lands in the
/// list without its metadata scheduled.
pub(crate) fn cargo_metadata_roots_for_item(item: &RootItem) -> Vec<AbsolutePath> {
    match item {
        RootItem::Rust(rust) => vec![rust.path().clone()],
        RootItem::Worktrees(group) => group.iter_paths().cloned().collect(),
        RootItem::NonRust(_) => Vec::new(),
    }
}

fn spawn_cargo_metadata_tree(scan_context: &StreamingScanContext, roots: Vec<AbsolutePath>) {
    for workspace_root in roots {
        let dispatch = MetadataDispatchContext {
            handle:         scan_context.client.handle.clone(),
            sender:         scan_context.sender.clone(),
            metadata_store: Arc::clone(&scan_context.metadata_store),
            metadata_limit: Arc::clone(&scan_context.metadata_limit),
        };
        spawn_cargo_metadata_refresh(dispatch, workspace_root);
    }
}

/// Context shared by any caller that wants to kick off a
/// `cargo metadata --no-deps --offline` task for a single workspace root.
/// The scan thread uses this to do initial dispatch; the watcher uses it
/// to re-run on manifest/config edits.
#[derive(Clone)]
pub(crate) struct MetadataDispatchContext {
    pub handle:         Handle,
    pub sender:         Sender<BackgroundMsg>,
    pub metadata_store: Arc<Mutex<WorkspaceMetadataStore>>,
    pub metadata_limit: Arc<Semaphore>,
}

impl MetadataDispatchContext {
    /// Lock the store briefly and clone the resolved `target_directory`
    /// for any `path` inside a known workspace. Callers that hold a live
    /// `App` should use `App::resolve_target_dir` instead; this shim
    /// exists for the watcher, which holds the dispatch context but has
    /// no direct App handle.
    pub(crate) fn resolved_target_dir(&self, path: &AbsolutePath) -> Option<AbsolutePath> {
        self.metadata_store
            .lock()
            .ok()
            .and_then(|store| store.resolved_target_dir(path).cloned())
    }
}

/// Queue a `cargo metadata` invocation for `workspace_root` on the shared
/// tokio handle. Captures the fingerprint and bumps the store's dispatch
/// generation before the blocking `exec()` fires; arrivals round-trip
/// through `BackgroundMsg::CargoMetadata` so the main loop can gate on
/// the latest generation.
pub(crate) fn spawn_cargo_metadata_refresh(
    dispatch: MetadataDispatchContext,
    workspace_root: AbsolutePath,
) {
    let MetadataDispatchContext {
        handle,
        sender,
        metadata_store: store,
        metadata_limit: limit,
    } = dispatch;

    handle.spawn(async move {
        let Ok(_permit) = limit.acquire_owned().await else {
            return;
        };

        let workspace_root_for_task = workspace_root.clone();
        let blocking = tokio::task::spawn_blocking(move || {
            run_cargo_metadata_for_root(&workspace_root_for_task, &store)
        });
        let task_result = match tokio::time::timeout(CARGO_METADATA_TIMEOUT, blocking).await {
            Ok(Ok(output)) => output,
            Ok(Err(_)) => {
                tracing::warn!(
                    workspace_root = %workspace_root.display(),
                    "cargo_metadata_task_join_failed"
                );
                return;
            },
            Err(_) => {
                let fingerprint = ManifestFingerprint::capture(workspace_root.as_path())
                    .unwrap_or_else(|_| synthetic_fingerprint());
                CargoMetadataTaskOutput {
                    generation: 0,
                    fingerprint,
                    result: Err(CargoMetadataError::Other(format!(
                        "cargo metadata timed out after {}s",
                        CARGO_METADATA_TIMEOUT.as_secs()
                    ))),
                }
            },
        };

        let CargoMetadataTaskOutput {
            generation,
            fingerprint,
            result,
        } = task_result;
        let _ = sender.send(BackgroundMsg::CargoMetadata {
            workspace_root,
            generation,
            fingerprint,
            result,
        });
    });
}

struct CargoMetadataTaskOutput {
    generation:  u64,
    fingerprint: ManifestFingerprint,
    result:      Result<WorkspaceMetadata, CargoMetadataError>,
}

/// Walk `target_dir` on a blocking thread and emit its total byte size via
/// [`BackgroundMsg::OutOfTreeTargetSize`]. Used when workspace metadata
/// reports a `target_directory` outside its `workspace_root`; the scan-time
/// walker's per-project breakdown doesn't reach there, so this fills in the
/// sharer target size for the detail pane.
pub(crate) fn spawn_out_of_tree_target_walk(
    handle: &Handle,
    sender: Sender<BackgroundMsg>,
    workspace_root: AbsolutePath,
    target_dir: AbsolutePath,
) {
    handle.spawn(async move {
        let walk_target = target_dir.clone();
        let bytes = tokio::task::spawn_blocking(move || sum_dir_bytes(walk_target.as_path())).await;
        let bytes = match bytes {
            Ok(bytes) => bytes,
            Err(err) => {
                tracing::warn!(
                    workspace_root = %workspace_root.display(),
                    target_dir = %target_dir.display(),
                    error = %err,
                    "out_of_tree_target_walk_join_failed"
                );
                return;
            },
        };
        tracing::debug!(
            workspace_root = %workspace_root.display(),
            target_dir = %target_dir.display(),
            bytes,
            "out_of_tree_target_walk_done"
        );
        let _ = sender.send(BackgroundMsg::OutOfTreeTargetSize {
            workspace_root,
            target_dir,
            bytes,
        });
    });
}

fn sum_dir_bytes(dir: &Path) -> u64 {
    WalkDir::new(dir)
        .into_iter()
        .flatten()
        .filter(|entry| entry.file_type().is_file())
        .filter_map(|entry| entry.metadata().ok().map(|meta| meta.len()))
        .sum()
}

fn run_cargo_metadata_for_root(
    workspace_root: &AbsolutePath,
    store: &Arc<Mutex<WorkspaceMetadataStore>>,
) -> CargoMetadataTaskOutput {
    let generation = store
        .lock()
        .map_or(0, |mut guard| guard.next_generation(workspace_root));
    let fingerprint = match ManifestFingerprint::capture(workspace_root.as_path()) {
        Ok(fp) => fp,
        Err(err) => {
            // `NotFound` here means the workspace root vanished between
            // dispatch and run — the user just deleted a worktree, or a
            // similar race. Classify it as `WorkspaceMissing` so the
            // handler can suppress the toast at the type level. All other
            // I/O errors (permissions, etc.) flow into `Other`.
            let result = if err.kind() == ErrorKind::NotFound {
                Err(CargoMetadataError::WorkspaceMissing)
            } else {
                Err(CargoMetadataError::Other(format!(
                    "fingerprint capture failed: {err}"
                )))
            };
            return CargoMetadataTaskOutput {
                generation,
                fingerprint: synthetic_fingerprint(),
                result,
            };
        },
    };

    let manifest_path = workspace_root.as_path().join(CARGO_TOML);
    let started_at = std::time::Instant::now();
    let result = match execute_cargo_metadata(&manifest_path) {
        Ok(metadata) => Ok(build_workspace_metadata(
            workspace_root.clone(),
            &metadata,
            fingerprint.clone(),
        )),
        Err(err) => Err(err),
    };
    tracing::trace!(
        target: PERF_LOG_TARGET,
        elapsed_ms = tui_pane::perf_log_ms(started_at.elapsed().as_millis()),
        workspace_root = %workspace_root.display(),
        ok = result.is_ok(),
        "cargo_metadata_exec"
    );

    CargoMetadataTaskOutput {
        generation,
        fingerprint,
        result,
    }
}

fn execute_cargo_metadata(manifest_path: &Path) -> Result<Metadata, CargoMetadataError> {
    // Wall-clock cap lives on the caller via `tokio::time::timeout`;
    // `MetadataCommand::exec` itself has no timeout knob.
    let mut cmd = cargo_metadata::MetadataCommand::new();
    cmd.manifest_path(manifest_path).no_deps();
    cmd.other_options(vec![CARGO_OFFLINE_FLAG.to_string()]);
    cmd.exec()
        .map_err(|err| CargoMetadataError::Other(format_cargo_metadata_error(&err)))
}

fn format_cargo_metadata_error(err: &Error) -> String {
    let text = err.to_string();
    text.lines().next().unwrap_or(&text).to_string()
}

const fn synthetic_fingerprint() -> ManifestFingerprint {
    ManifestFingerprint {
        manifest:       FileStamp {
            content_hash: [0_u8; 32],
        },
        lockfile:       None,
        rust_toolchain: None,
        configs:        BTreeMap::new(),
    }
}

fn build_workspace_metadata(
    workspace_root: AbsolutePath,
    metadata: &Metadata,
    fingerprint: ManifestFingerprint,
) -> WorkspaceMetadata {
    let target_directory =
        AbsolutePath::from(PathBuf::from(metadata.target_directory.as_std_path()));
    let packages = metadata
        .packages
        .iter()
        .map(|pkg| {
            let record = PackageRecord {
                name:          pkg.name.to_string(),
                version:       pkg.version.clone(),
                edition:       pkg.edition.to_string(),
                description:   pkg.description.clone(),
                license:       pkg.license.clone(),
                homepage:      pkg.homepage.clone(),
                repository:    pkg.repository.clone(),
                manifest_path: AbsolutePath::from(PathBuf::from(pkg.manifest_path.as_std_path())),
                targets:       pkg
                    .targets
                    .iter()
                    .map(|target| TargetRecord {
                        name:              target.name.clone(),
                        kinds:             target.kind.clone(),
                        src_path:          AbsolutePath::from(PathBuf::from(
                            target.src_path.as_std_path(),
                        )),
                        required_features: target.required_features.clone(),
                    })
                    .collect(),
                publish:       PublishPolicy::from_cargo_publish(pkg.publish.as_deref()),
            };
            (pkg.id.clone(), record)
        })
        .collect();
    WorkspaceMetadata {
        workspace_root,
        target_directory,
        packages,
        fingerprint,
        out_of_tree_target_bytes: None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::project::Package;
    use crate::project::RustProject;
    use crate::project::Workspace;
    use crate::project::WorktreeStatus;
    use crate::scan::tree;

    fn status_for(is_linked_worktree: bool, primary_abs: Option<&str>) -> WorktreeStatus {
        match (is_linked_worktree, primary_abs) {
            (_, None) => WorktreeStatus::NotGit,
            (true, Some(p)) => WorktreeStatus::Linked {
                primary: AbsolutePath::from(p.to_string()),
            },
            (false, Some(p)) => WorktreeStatus::Primary {
                root: AbsolutePath::from(p.to_string()),
            },
        }
    }

    fn make_workspace(
        name: Option<&str>,
        abs_path: &str,
        is_linked_worktree: bool,
        primary_abs: Option<&str>,
    ) -> RootItem {
        RootItem::Rust(RustProject::Workspace(Workspace {
            path: AbsolutePath::from(abs_path),
            name: name.map(String::from),
            worktree_status: status_for(is_linked_worktree, primary_abs),
            ..Workspace::default()
        }))
    }

    fn make_package(
        name: Option<&str>,
        abs_path: &str,
        is_linked_worktree: bool,
        primary_abs: Option<&str>,
    ) -> RootItem {
        RootItem::Rust(RustProject::Package(Package {
            path: AbsolutePath::from(abs_path),
            name: name.map(String::from),
            worktree_status: status_for(is_linked_worktree, primary_abs),
            ..Package::default()
        }))
    }

    #[test]
    fn collect_cargo_metadata_roots_yields_one_root_per_rust_leaf() {
        let ws = make_workspace(Some("ws"), "/ws", false, Some("/ws"));
        let pkg = make_package(Some("pkg"), "/pkg", false, Some("/pkg"));
        let roots = collect_cargo_metadata_roots(&[ws, pkg]);

        assert_eq!(
            roots,
            vec![AbsolutePath::from("/ws"), AbsolutePath::from("/pkg"),],
            "each Rust leaf produces exactly one metadata root, preserving input order"
        );
    }

    #[test]
    fn collect_cargo_metadata_roots_skips_non_rust_projects() {
        let non_rust = RootItem::NonRust(crate::project::NonRustProject::new(
            AbsolutePath::from("/notes"),
            Some("notes".into()),
        ));
        let pkg = make_package(Some("pkg"), "/pkg", false, Some("/pkg"));

        let roots = collect_cargo_metadata_roots(&[non_rust, pkg]);

        assert_eq!(
            roots,
            vec![AbsolutePath::from("/pkg")],
            "non-rust leaves never receive a metadata dispatch"
        );
    }

    #[test]
    fn collect_cargo_metadata_roots_unions_primary_and_linked_worktrees() {
        // Merge a primary + two linked worktrees into a group, then assert
        // every worktree gets its own metadata root.
        let primary = make_workspace(Some("ws"), "/ws", false, Some("/ws"));
        let linked_a = make_workspace(Some("ws_feat"), "/ws_feat", true, Some("/ws"));
        let linked_b = make_workspace(Some("ws_bug"), "/ws_bug", true, Some("/ws"));
        let mut items = vec![primary, linked_a, linked_b];
        // Use the merge logic the production scan uses.
        tree::merge_worktrees_new(&mut items);
        assert_eq!(items.len(), 1, "merged into one worktree group");

        let mut roots = collect_cargo_metadata_roots(&items);
        roots.sort_by(|a, b| a.as_path().cmp(b.as_path()));
        assert_eq!(
            roots,
            vec![
                AbsolutePath::from("/ws"),
                AbsolutePath::from("/ws_bug"),
                AbsolutePath::from("/ws_feat"),
            ],
            "primary + every linked worktree gets its own metadata root"
        );
    }

    #[test]
    fn collect_cargo_metadata_roots_dedupes_repeated_paths() {
        // Shouldn't happen in practice (each project has a unique path),
        // but the deduping logic is cheap and catches any future caller
        // that accidentally feeds the same root twice.
        let pkg_a = make_package(Some("a"), "/pkg", false, Some("/pkg"));
        let pkg_b = make_package(Some("b"), "/pkg", false, Some("/pkg"));

        let roots = collect_cargo_metadata_roots(&[pkg_a, pkg_b]);
        assert_eq!(roots, vec![AbsolutePath::from("/pkg")]);
    }
}