cargo-brief 0.12.1

Visibility-aware Rust API extractor — pseudo-Rust output for AI agent consumption
Documentation
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
use std::collections::{HashMap, HashSet};
use std::env;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, OnceLock};

use anyhow::{Context, Result, bail};

type LibTargetNameMap = HashMap<String, LibTargetName>;

#[derive(Clone)]
enum LibTargetName {
    Target(String),
    Ambiguous,
}

enum LibTargetNameLookup {
    Target(String),
    Ambiguous,
    Unknown,
}

/// Package names and versions from Cargo.lock.
///
/// Tracks all package names for validation, plus version lists for
/// disambiguating multi-version crates.
pub struct LockfilePackages {
    names: HashSet<String>,
    /// name -> sorted versions (ascending semver). Only populated when 2+ versions exist.
    multi_versions: HashMap<String, Vec<String>>,
}

impl LockfilePackages {
    pub fn contains(&self, name: &str) -> bool {
        self.names.contains(name)
    }

    pub fn is_empty(&self) -> bool {
        self.names.is_empty()
    }

    /// Resolve a crate name to its cargo spec.
    ///
    /// Returns `Some("name@latest")` if multiple versions exist,
    /// `Some("name")` if single version, `None` if not found.
    /// Tries underscore->hyphen fallback internally.
    pub fn resolve_spec(&self, name: &str) -> Option<String> {
        if let Some(spec) = self.resolve_spec_exact(name) {
            return Some(spec);
        }
        let hyphenated = name.replace('_', "-");
        if hyphenated != name {
            return self.resolve_spec_exact(&hyphenated);
        }
        None
    }

    fn resolve_spec_exact(&self, name: &str) -> Option<String> {
        if !self.names.contains(name) {
            return None;
        }
        if let Some(versions) = self.multi_versions.get(name) {
            // versions are pre-sorted ascending by semver — last is highest
            let highest = versions.last().unwrap();
            Some(format!("{name}@{highest}"))
        } else {
            Some(name.to_string())
        }
    }
}

/// Guard: runs the toolchain check at most once per process.
static TOOLCHAIN_CHECKED: AtomicBool = AtomicBool::new(false);

static LIB_TARGET_NAME_CACHE: OnceLock<Mutex<HashMap<String, LibTargetNameMap>>> = OnceLock::new();

#[cfg(test)]
static LIB_TARGET_METADATA_LOADS: std::sync::atomic::AtomicUsize =
    std::sync::atomic::AtomicUsize::new(0);

/// Pre-check that the required rustup toolchain is available.
///
/// Uses `rustup which rustdoc --toolchain {toolchain}` (~10ms) to detect.
/// When the toolchain is missing and stderr is a TTY, prompts the user to
/// install it interactively (reading from `/dev/tty` on Unix, `CONIN$` on
/// Windows). In non-TTY mode, bails with an actionable error message.
///
/// The check runs at most once per process via an `AtomicBool` guard.
fn ensure_toolchain_available(toolchain: &str) -> Result<()> {
    if TOOLCHAIN_CHECKED.load(Ordering::Relaxed) {
        return Ok(());
    }

    let result = Command::new("rustup")
        .args(["which", "rustdoc", "--toolchain", toolchain])
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();

    match result {
        Ok(status) if status.success() => {
            TOOLCHAIN_CHECKED.store(true, Ordering::Relaxed);
            return Ok(());
        }
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            bail!(
                "rustup is not installed. Install it from https://rustup.rs/ \
                 then run: rustup toolchain install {toolchain}"
            );
        }
        _ => {} // toolchain missing — fall through to prompt/error
    }

    if std::io::stderr().is_terminal() {
        eprintln!("[cargo-brief] The '{toolchain}' toolchain is required but not installed.");
        eprint!("[cargo-brief] Install it now? [y/N] ");

        let response = read_tty_line();
        if !response
            .as_ref()
            .is_ok_and(|s| matches!(s.trim(), "y" | "Y"))
        {
            bail!(
                "The '{toolchain}' toolchain is not installed.\n\
                 Install it with: rustup toolchain install {toolchain}"
            );
        }

        eprintln!("[cargo-brief] Installing '{toolchain}' toolchain...");
        let install_status = Command::new("rustup")
            .args(["toolchain", "install", toolchain])
            .stderr(Stdio::inherit())
            .stdout(Stdio::inherit())
            .status()
            .context("Failed to run `rustup toolchain install`")?;

        if !install_status.success() {
            bail!(
                "Failed to install the '{toolchain}' toolchain.\n\
                 Try manually: rustup toolchain install {toolchain}"
            );
        }

        TOOLCHAIN_CHECKED.store(true, Ordering::Relaxed);
        Ok(())
    } else {
        bail!(
            "The '{toolchain}' toolchain is not installed.\n\
             Install it with: rustup toolchain install {toolchain}"
        );
    }
}

/// Read a single line from the controlling terminal, bypassing stdin.
fn read_tty_line() -> std::io::Result<String> {
    #[cfg(unix)]
    const TTY_PATH: &str = "/dev/tty";
    #[cfg(windows)]
    const TTY_PATH: &str = "CONIN$";
    #[cfg(not(any(unix, windows)))]
    return Err(std::io::Error::new(
        std::io::ErrorKind::Unsupported,
        "TTY input not supported on this platform",
    ));

    #[cfg(any(unix, windows))]
    {
        use std::io::BufRead;
        let tty = std::fs::File::open(TTY_PATH)?;
        let mut reader = std::io::BufReader::new(tty);
        let mut line = String::new();
        reader.read_line(&mut line)?;
        Ok(line)
    }
}

/// Find the rustdoc JSON file for a package in a doc directory.
///
/// Uses `cargo metadata` to discover the actual lib target name when available.
/// Falls back to the package-name-based path (`pkg_name → underscored → .json`).
/// This handles crates where `[lib] name` differs from the package name
/// (e.g. `rustls-webpki` generates `webpki.json`, not `rustls_webpki.json`).
pub fn find_lib_json_path(
    crate_spec: &str,
    manifest_path: Option<&str>,
    doc_dir: &Path,
) -> Option<PathBuf> {
    let base_name = crate_spec.split('@').next().unwrap_or(crate_spec);
    let expected_stem = base_name.replace('-', "_");
    let expected = doc_dir.join(format!("{expected_stem}.json"));
    match query_lib_target_name(crate_spec, manifest_path) {
        LibTargetNameLookup::Target(lib_name) if lib_name != expected_stem => {
            let alt = doc_dir.join(format!("{lib_name}.json"));
            alt.exists().then_some(alt)
        }
        LibTargetNameLookup::Ambiguous => None,
        LibTargetNameLookup::Target(_) | LibTargetNameLookup::Unknown => {
            expected.exists().then_some(expected)
        }
    }
}

fn manifest_cache_key(manifest_path: Option<&str>) -> String {
    if let Some(manifest) = manifest_path {
        return Path::new(manifest)
            .canonicalize()
            .unwrap_or_else(|_| PathBuf::from(manifest))
            .to_string_lossy()
            .into_owned();
    }

    env::current_dir()
        .ok()
        .and_then(|cwd| cwd.canonicalize().ok())
        .unwrap_or_else(|| PathBuf::from("."))
        .to_string_lossy()
        .into_owned()
}

/// Run `cargo metadata` to find lib/proc-macro target names for all packages.
///
/// Runs without `--no-deps` so that external crates (e.g. crates.io deps in an
/// isolated temp workspace) are also visible in the packages list.
fn load_lib_target_names(manifest_path: Option<&str>) -> Option<LibTargetNameMap> {
    #[cfg(test)]
    LIB_TARGET_METADATA_LOADS.fetch_add(1, Ordering::Relaxed);

    let mut cmd = Command::new("cargo");
    cmd.args(["metadata", "--format-version=1"]);
    if let Some(m) = manifest_path {
        cmd.args(["--manifest-path", m]);
    }
    let output = cmd.output().ok()?;
    if !output.status.success() {
        return None;
    }
    let meta: serde_json::Value = serde_json::from_slice(&output.stdout).ok()?;
    let packages = meta.get("packages")?.as_array()?;

    let mut entries = Vec::new();
    let mut package_counts: HashMap<String, usize> = HashMap::new();
    for pkg in packages {
        let package_name = pkg["name"].as_str()?;
        let package_key = package_name.replace('-', "_");
        let version = pkg["version"].as_str()?;
        for target in pkg["targets"].as_array()? {
            let kinds = target["kind"].as_array()?;
            let is_lib = kinds
                .iter()
                .any(|k| matches!(k.as_str(), Some("lib") | Some("proc-macro")));
            if is_lib {
                let target_name = target["name"].as_str()?.replace('-', "_");
                *package_counts.entry(package_key.clone()).or_default() += 1;
                entries.push((package_key, version.to_string(), target_name));
                break;
            }
        }
    }

    let mut names = HashMap::new();
    for (package_key, version, target_name) in entries {
        names.insert(
            format!("{package_key}@{version}"),
            LibTargetName::Target(target_name.clone()),
        );
        if package_counts.get(&package_key) == Some(&1) {
            names.insert(package_key, LibTargetName::Target(target_name));
        } else {
            names.insert(package_key, LibTargetName::Ambiguous);
        }
    }
    Some(names)
}

fn lib_target_cache_lookup_key(crate_spec: &str) -> String {
    let (base_name, version) = crate_spec
        .split_once('@')
        .map_or((crate_spec, None), |(name, version)| (name, Some(version)));
    let norm = base_name.replace('-', "_");
    version.map_or(norm.clone(), |version| format!("{norm}@{version}"))
}

fn lookup_target_name(names: &LibTargetNameMap, lookup_key: &str) -> LibTargetNameLookup {
    match names.get(lookup_key) {
        Some(LibTargetName::Target(name)) => LibTargetNameLookup::Target(name.clone()),
        Some(LibTargetName::Ambiguous) => LibTargetNameLookup::Ambiguous,
        None => LibTargetNameLookup::Unknown,
    }
}

fn query_lib_target_name(crate_spec: &str, manifest_path: Option<&str>) -> LibTargetNameLookup {
    let lookup_key = lib_target_cache_lookup_key(crate_spec);
    let cache_key = manifest_cache_key(manifest_path);
    let cache = LIB_TARGET_NAME_CACHE.get_or_init(|| Mutex::new(HashMap::new()));

    if let Ok(guard) = cache.lock()
        && let Some(cached) = guard.get(&cache_key)
    {
        return lookup_target_name(cached, &lookup_key);
    }

    let Some(names) = load_lib_target_names(manifest_path) else {
        return LibTargetNameLookup::Unknown;
    };
    let target_name = lookup_target_name(&names, &lookup_key);
    if let Ok(mut guard) = cache.lock() {
        guard.insert(cache_key, names);
    }
    target_name
}

fn describe_lib_json_fallback(
    crate_spec: &str,
    manifest_path: Option<&str>,
    doc_dir: &Path,
) -> String {
    let base_name = crate_spec.split('@').next().unwrap_or(crate_spec);
    let expected_stem = base_name.replace('-', "_");
    let lib_name = match query_lib_target_name(crate_spec, manifest_path) {
        LibTargetNameLookup::Target(lib_name) => lib_name,
        LibTargetNameLookup::Ambiguous => {
            return format!(
                "cargo metadata found multiple package versions for unversioned '{crate_spec}'"
            );
        }
        LibTargetNameLookup::Unknown => {
            return format!(
                "cargo metadata did not resolve a lib/proc-macro target for '{crate_spec}'"
            );
        }
    };
    if lib_name == expected_stem {
        return format!("cargo metadata resolved '{crate_spec}' to the expected target name");
    }
    format!(
        "cargo metadata resolved '{crate_spec}' to lib target '{lib_name}', but {} was also missing",
        doc_dir.join(format!("{lib_name}.json")).display()
    )
}

/// Invoke `cargo +nightly rustdoc` and return the path to the generated JSON file.
///
/// When `verbose` is true, cargo's stderr (compilation progress) is streamed to
/// the terminal in real time via `Stdio::inherit()`.
pub fn generate_rustdoc_json(
    crate_name: &str,
    toolchain: &str,
    manifest_path: Option<&str>,
    document_private_items: bool,
    target_dir: &Path,
    verbose: bool,
    use_cache: bool,
) -> Result<PathBuf> {
    if use_cache {
        let doc_dir = target_dir.join("doc");
        if let Some(json_path) = find_lib_json_path(crate_name, manifest_path, &doc_dir) {
            if verbose {
                eprintln!("[cargo-brief] Using cached rustdoc JSON for '{crate_name}'");
            }
            return Ok(json_path);
        }
    }

    ensure_toolchain_available(toolchain)?;

    let mut cmd = Command::new("cargo");
    cmd.arg(format!("+{toolchain}"));
    cmd.args(["rustdoc", "-p", crate_name, "--lib"]);

    if let Some(manifest) = manifest_path {
        cmd.args(["--manifest-path", manifest]);
    }

    cmd.arg("--");
    cmd.args(["--output-format", "json", "-Z", "unstable-options"]);

    if document_private_items {
        cmd.arg("--document-private-items");
    }

    if verbose {
        // Stream cargo's stderr (Compiling/Checking progress) to terminal
        cmd.stderr(Stdio::inherit());
        let status = cmd.status().with_context(|| {
            format!(
                "Failed to execute `cargo +{toolchain} rustdoc`. \
                 Is the '{toolchain}' toolchain installed? Try: rustup toolchain install {toolchain}"
            )
        })?;
        if !status.success() {
            bail!("cargo rustdoc failed for '{crate_name}' (see output above)");
        }
    } else {
        let output = cmd.output().with_context(|| {
            format!(
                "Failed to execute `cargo +{toolchain} rustdoc`. \
                 Is the '{toolchain}' toolchain installed? Try: rustup toolchain install {toolchain}"
            )
        })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            if stderr.contains("toolchain") && stderr.contains("is not installed") {
                bail!(
                    "The '{toolchain}' toolchain is not installed.\n\
                     Install it with: rustup toolchain install {toolchain}"
                );
            }
            if stderr.contains("is ambiguous") {
                // Auto-retry: parse candidate specs, pick highest version
                if !crate_name.contains('@') {
                    let specs: Vec<&str> = stderr
                        .lines()
                        .filter_map(|l| {
                            let trimmed = l.trim();
                            if trimmed.contains('@') && !trimmed.contains(' ') {
                                Some(trimmed)
                            } else {
                                None
                            }
                        })
                        .collect();

                    if let Some(best) = pick_highest_version_spec(&specs) {
                        return generate_rustdoc_json(
                            best,
                            toolchain,
                            manifest_path,
                            document_private_items,
                            target_dir,
                            verbose,
                            use_cache,
                        );
                    }
                }

                // Fallback: bail with user-facing message
                bail!(
                    "Multiple versions of '{crate_name}' exist and auto-resolution failed. \
                     Use `<name>@<version>` to disambiguate (e.g. `{crate_name}@1.0.0`)."
                );
            }
            if stderr.contains("did not match any packages")
                || stderr.contains("package(s) `")
                || stderr.contains("no packages match")
            {
                bail!(
                    "Package '{crate_name}' not found in the workspace.\n\
                     Check the package name and ensure it exists in the workspace.\n\
                     TIP: If it's an optional or unresolved dependency, try:\n\
                       cargo brief --crates {crate_name} --features <features>\n\
                     Original error:\n{stderr}"
                );
            }
            bail!("cargo rustdoc failed:\n{stderr}");
        }
    }

    // Find the generated JSON file. Strip `@version` suffix first — cargo uses it
    // for disambiguation but the output file is named by the lib target name.
    // Use find_lib_json_path to handle crates where [lib] name != package name.
    let base_name = crate_name.split('@').next().unwrap_or(crate_name);
    let doc_dir = target_dir.join("doc");
    find_lib_json_path(crate_name, manifest_path, &doc_dir).with_context(|| {
        let expected_name = base_name.replace('-', "_");
        let fallback = describe_lib_json_fallback(crate_name, manifest_path, &doc_dir);
        format!(
            "Expected rustdoc JSON at {} but file not found; {fallback}",
            doc_dir.join(format!("{expected_name}.json")).display(),
        )
    })
}

/// Parse rustdoc JSON with bincode caching. If a `.bin` file exists and is
/// newer than the `.json`, deserialize from bincode (5-10x faster). Otherwise
/// parse JSON and write the `.bin` cache for next time.
pub fn parse_rustdoc_json_cached(path: &Path) -> Result<rustdoc_types::Crate> {
    let bin_path = path.with_extension("bin");

    if bin_path.exists()
        && let (Ok(bin_meta), Ok(json_meta)) = (bin_path.metadata(), path.metadata())
        && bin_meta.modified()? >= json_meta.modified()?
    {
        let bytes = std::fs::read(&bin_path)?;
        if let Ok(krate) = bincode::deserialize(&bytes) {
            return Ok(krate);
        }
        // Corrupted .bin — fall through to JSON parse
    }

    let krate = parse_rustdoc_json(path)?;

    // Best-effort write of bincode cache
    if let Ok(bytes) = bincode::serialize(&krate) {
        let _ = std::fs::write(&bin_path, bytes);
    }

    Ok(krate)
}

/// Parse a rustdoc JSON file into the `rustdoc_types::Crate` structure.
pub fn parse_rustdoc_json(path: &Path) -> Result<rustdoc_types::Crate> {
    let content = std::fs::read_to_string(path)
        .with_context(|| format!("Failed to read {}", path.display()))?;
    let krate: rustdoc_types::Crate =
        serde_json::from_str(&content).context("Failed to parse rustdoc JSON")?;
    Ok(krate)
}

/// Parse Cargo.lock to extract all resolved package names and versions.
///
/// Returns a `LockfilePackages` with hyphenated package names (as they appear
/// in Cargo.lock) and multi-version tracking for disambiguation.
/// Returns an empty struct on any error (missing file, malformed content).
pub fn load_lockfile_packages(manifest_path: Option<&str>) -> LockfilePackages {
    let lockfile_path = if let Some(manifest) = manifest_path {
        Path::new(manifest)
            .parent()
            .map(|p| p.join("Cargo.lock"))
            .unwrap_or_else(|| PathBuf::from("Cargo.lock"))
    } else {
        PathBuf::from("Cargo.lock")
    };

    let content = match std::fs::read_to_string(&lockfile_path) {
        Ok(c) => c,
        Err(_) => {
            return LockfilePackages {
                names: HashSet::new(),
                multi_versions: HashMap::new(),
            };
        }
    };

    let mut names = HashSet::new();
    let mut all_versions: HashMap<String, Vec<String>> = HashMap::new();
    let mut current_name: Option<String> = None;

    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed == "[[package]]" {
            current_name = None;
            continue;
        }
        if trimmed.starts_with("name = \"") {
            current_name = trimmed
                .strip_prefix("name = \"")
                .and_then(|s| s.strip_suffix('"'))
                .map(|s| s.to_string());
            if let Some(ref name) = current_name {
                names.insert(name.clone());
            }
        } else if trimmed.starts_with("version = \"")
            && let Some(ref name) = current_name
            && let Some(ver) = trimmed
                .strip_prefix("version = \"")
                .and_then(|s| s.strip_suffix('"'))
        {
            all_versions
                .entry(name.clone())
                .or_default()
                .push(ver.to_string());
        } else if trimmed.starts_with('[') {
            current_name = None;
        }
    }

    // Only retain entries with 2+ versions
    let multi_versions: HashMap<String, Vec<String>> = all_versions
        .into_iter()
        .filter(|(_, v)| v.len() > 1)
        .map(|(name, mut versions)| {
            // Sort by semver ascending; fall back to string sort
            versions.sort_by(
                |a, b| match (semver::Version::parse(a), semver::Version::parse(b)) {
                    (Ok(va), Ok(vb)) => va.cmp(&vb),
                    _ => a.cmp(b),
                },
            );
            (name, versions)
        })
        .collect();

    LockfilePackages {
        names,
        multi_versions,
    }
}

/// Batch-generate rustdoc JSON for multiple crates via single `cargo doc`.
///
/// Returns names that succeeded (cached or newly generated). Crates whose
/// JSON already exists are counted as cached successes without invoking cargo.
pub fn batch_generate_rustdoc_json(
    crate_names: &[&str],
    toolchain: &str,
    manifest_path: Option<&str>,
    target_dir: &Path,
    verbose: bool,
) -> Vec<String> {
    let mut succeeded = Vec::new();
    let mut to_generate = Vec::new();

    for &name in crate_names {
        let doc_dir = target_dir.join("doc");
        if find_lib_json_path(name, manifest_path, &doc_dir).is_some() {
            succeeded.push(name.to_string());
        } else {
            to_generate.push(name);
        }
    }

    if to_generate.is_empty() {
        return succeeded;
    }

    if verbose {
        eprintln!(
            "[cargo-brief] Batch generating rustdoc JSON for {} crate(s): {}",
            to_generate.len(),
            to_generate.join(", ")
        );
    }

    let mut cmd = Command::new("cargo");
    cmd.arg(format!("+{toolchain}"));
    cmd.args(["doc", "--no-deps", "--lib"]);

    for name in &to_generate {
        cmd.args(["-p", name]);
    }

    if let Some(manifest) = manifest_path {
        cmd.args(["--manifest-path", manifest]);
    }

    cmd.env(
        "RUSTDOCFLAGS",
        "--output-format json -Z unstable-options --document-private-items",
    );

    if verbose {
        cmd.stderr(Stdio::inherit());
        let status = cmd.status();
        if let Err(e) = &status {
            eprintln!("warning: batch cargo doc failed to execute: {e}");
            return succeeded;
        }
        // Even on non-zero exit, some crates may have succeeded — check below
    } else {
        let output = cmd.output();
        match output {
            Err(e) => {
                eprintln!("warning: batch cargo doc failed to execute: {e}");
                return succeeded;
            }
            Ok(o) if !o.status.success() => {
                // Non-fatal: some crates may still have generated JSON
            }
            Ok(_) => {}
        }
    }

    // Check which JSONs got created
    for name in &to_generate {
        let doc_dir = target_dir.join("doc");
        if find_lib_json_path(name, manifest_path, &doc_dir).is_some() {
            succeeded.push(name.to_string());
        } else if verbose {
            eprintln!("warning: batch cargo doc did not produce JSON for '{name}'");
        }
    }

    succeeded
}

/// Pick the spec with the highest semver version from a list like `["foo@1.0.0", "foo@2.0.0"]`.
fn pick_highest_version_spec<'a>(specs: &[&'a str]) -> Option<&'a str> {
    specs
        .iter()
        .filter_map(|&s| {
            let ver_str = s.split_once('@')?.1;
            let ver = semver::Version::parse(ver_str).ok()?;
            Some((ver, s))
        })
        .max_by(|a, b| a.0.cmp(&b.0))
        .map(|(_, s)| s)
}

#[cfg(test)]
mod tests {
    use super::*;

    static LIB_TARGET_TEST_LOCK: Mutex<()> = Mutex::new(());

    fn clear_lib_target_name_cache() {
        if let Some(cache) = LIB_TARGET_NAME_CACHE.get()
            && let Ok(mut guard) = cache.lock()
        {
            guard.clear();
        }
        LIB_TARGET_METADATA_LOADS.store(0, Ordering::Relaxed);
    }

    #[test]
    fn lib_target_cache_lookup_key_preserves_version() {
        assert_eq!(
            lib_target_cache_lookup_key("renamed-lib-package"),
            "renamed_lib_package"
        );
        assert_eq!(
            lib_target_cache_lookup_key("renamed-lib-package@0.1.0"),
            "renamed_lib_package@0.1.0"
        );
    }

    #[test]
    fn lib_target_name_lookup_reuses_manifest_metadata() {
        let _guard = LIB_TARGET_TEST_LOCK.lock().unwrap();
        clear_lib_target_name_cache();

        match query_lib_target_name("renamed-lib-package", Some("test_fixture/Cargo.toml")) {
            LibTargetNameLookup::Target(name) => assert_eq!(name, "renamed_lib_actual"),
            LibTargetNameLookup::Ambiguous | LibTargetNameLookup::Unknown => {
                panic!("renamed-lib-package should resolve to its lib target")
            }
        }
        match query_lib_target_name("renamed-lib-package", Some("test_fixture/Cargo.toml")) {
            LibTargetNameLookup::Target(name) => assert_eq!(name, "renamed_lib_actual"),
            LibTargetNameLookup::Ambiguous | LibTargetNameLookup::Unknown => {
                panic!("cached renamed-lib-package should resolve to its lib target")
            }
        }

        assert_eq!(LIB_TARGET_METADATA_LOADS.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn ambiguous_unversioned_lookup_does_not_accept_package_stem_json() {
        let _guard = LIB_TARGET_TEST_LOCK.lock().unwrap();
        clear_lib_target_name_cache();
        let manifest_path = "ambiguous-fixture/Cargo.toml";
        let cache_key = manifest_cache_key(Some(manifest_path));
        let cache = LIB_TARGET_NAME_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
        let mut names = HashMap::new();
        names.insert("ambiguous_pkg".to_string(), LibTargetName::Ambiguous);
        cache.lock().unwrap().insert(cache_key, names);

        let temp_dir = tempfile::tempdir().expect("create temp dir");
        let doc_dir = temp_dir.path();
        std::fs::write(doc_dir.join("ambiguous_pkg.json"), "{}").expect("write stale json");

        assert!(find_lib_json_path("ambiguous-pkg", Some(manifest_path), doc_dir).is_none());
    }
}