curie-build 0.6.0

The Curie build tool
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
//! Workspace build-orchestration: fan-out of build/test/run/clean/audit/fmt
//! over a loaded [`Workspace`].  Discovery and descriptor loading live in
//! `curie_meta::workspace`; this module re-exports those types and adds the
//! build-tool-specific orchestration on top.

pub use curie_meta::workspace::{
    discover, list, load, Member, Workspace, WorkspaceContext,
};

use crate::audit::{self, AuditOptions};
use crate::descriptor;
use crate::update::{self, UpdateOptions};
use crate::{build, compile, fmt, jar, run, test};
use anyhow::{bail, Context, Result};
use std::path::{Path, PathBuf};
use std::process::Command;

/// Per-member output recorded by `fan_out` and fed to downstream members'
/// classpath construction.
///
/// `classes_dir` is the natural workspace-dep classpath entry — using the
/// compiled-classes directory (instead of waiting for the upstream JAR to
/// be packaged) keeps the model symmetric with how a member sees its own
/// classes during test runs, and means a downstream member can compile
/// before any upstream member has been packaged.
///
/// `classpath_contribution` is the transitive closure of upstream
/// classpath entries that a member depending on this one should inherit:
/// every transitive workspace-dep's classes_dir plus every transitive
/// Maven dep JAR.  Built bottom-up as the fan-out iterates.
struct MemberArtifact {
    classes_dir: PathBuf,
    classpath_contribution: Vec<PathBuf>,
}

/// Walk a member's resolved workspace-dep indices and return the classpath
/// the depending member should append to its own deps.  Order-preserving
/// dedup (paths already pulled in by an earlier upstream dep aren't
/// repeated).
///
/// Uses a HashMap rather than Vec for `artifacts` so a subset run (where
/// some indices are skipped) still works — the deps slice only references
/// indices that the subset includes.
fn collect_dep_classpath(
    deps: &[usize],
    artifacts: &std::collections::HashMap<usize, MemberArtifact>,
) -> Vec<PathBuf> {
    let mut cp: Vec<PathBuf> = Vec::new();
    let mut seen: std::collections::HashSet<PathBuf> = std::collections::HashSet::new();
    for &i in deps {
        let a = artifacts
            .get(&i)
            .expect("subset must include all transitive workspace_deps of every member it builds");
        if seen.insert(a.classes_dir.clone()) {
            cp.push(a.classes_dir.clone());
        }
        for entry in &a.classpath_contribution {
            if seen.insert(entry.clone()) {
                cp.push(entry.clone());
            }
        }
    }
    cp
}

/// Compute the transitive closure of `target`'s workspace dependencies,
/// returned in topo-build order (deps first, target last).  Used by
/// `build_one` / `test_one` to know which members must be built so the
/// target can compile.
fn transitive_closure(ws: &Workspace, target: usize) -> Vec<usize> {
    transitive_closure_multi(ws, &[target])
}

/// Like [`transitive_closure`] but seeds the search with several targets at
/// once; returns the union of their closures in topo-build order.  Used for
/// `WorkspaceSubtree` contexts where a whole nested-workspace's members are
/// the targets.
fn transitive_closure_multi(ws: &Workspace, targets: &[usize]) -> Vec<usize> {
    let mut included: std::collections::HashSet<usize> = std::collections::HashSet::new();
    let mut stack: Vec<usize> = targets.to_vec();
    while let Some(i) = stack.pop() {
        if included.insert(i) {
            for &dep in &ws.members[i].workspace_deps {
                stack.push(dep);
            }
        }
    }
    // `ws.members` is already in topo order — filter preserves that.
    (0..ws.members.len()).filter(|i| included.contains(i)).collect()
}

/// Iterate (a subset of) the workspace's members in topo order, print a
/// "[i/n] name" banner, invoke `run` (which returns the member's own
/// Maven dep JARs so the contribution can be assembled), and accumulate
/// artifacts so later members see their workspace-deps' classpaths.
///
/// `subset` is the list of member indices to process, in iteration order.
/// Each member's `workspace_deps` indices must all appear before it in
/// `subset` (`transitive_closure` guarantees this).
fn fan_out<F>(ws: &Workspace, action: &str, subset: &[usize], mut run: F) -> Result<()>
where
    F: FnMut(&Member, &[PathBuf]) -> Result<Vec<PathBuf>>,
{
    let n = subset.len();
    println!(
        "Workspace {} {} ({} member{})",
        ws.root.display(),
        action,
        n,
        if n == 1 { "" } else { "s" },
    );
    println!();

    let mut artifacts: std::collections::HashMap<usize, MemberArtifact> =
        std::collections::HashMap::with_capacity(n);
    for (pos, &idx) in subset.iter().enumerate() {
        let m = &ws.members[idx];
        println!("[{}/{}] {}", pos + 1, n, m.declared);
        let extra_cp = collect_dep_classpath(&m.workspace_deps, &artifacts);
        let own_dep_jars = run(m, &extra_cp)
            .with_context(|| format!("workspace member \"{}\" failed", m.declared))?;

        let classes_dir = m.path.join("target").join("classes");
        let mut contribution = extra_cp; // already deduped
        for j in own_dep_jars {
            contribution.push(j);
        }
        artifacts.insert(idx, MemberArtifact { classes_dir, classpath_contribution: contribution });
        println!();
    }
    Ok(())
}


/// Fan `curie build` out over every member in topo order.  When `jobs > 1`
/// and there are multiple members, runs them in parallel with PTY output.
pub fn build_all(workspace_root: &Path, opts: build::BuildOptions, jobs: usize) -> Result<()> {
    let ws = load(workspace_root)?;
    let subset: Vec<usize> = (0..ws.members.len()).collect();
    if subset.len() > 1 {
        return crate::parallel::run_jobs(&ws, &subset, "build", jobs, true, crate::parallel::TuiMode::Full, "Done", |m, extra_cp| {
            build::build_with_desc(&m.path, &m.descriptor, opts, extra_cp).map(|o| o.dep_jars)
        });
    }
    fan_out(&ws, "build", &subset, |m, extra_cp| {
        build::build_with_desc(&m.path, &m.descriptor, opts, extra_cp).map(|o| o.dep_jars)
    })
}

/// Build only `member_index` + its transitive workspace-deps (in topo order).
pub fn build_one(
    workspace_root: &Path,
    member_index: usize,
    opts: build::BuildOptions,
    jobs: usize,
) -> Result<()> {
    let ws = load(workspace_root)?;
    let subset = transitive_closure(&ws, member_index);
    if subset.len() > 1 {
        return crate::parallel::run_jobs(&ws, &subset, "build", jobs, true, crate::parallel::TuiMode::Full, "Done", |m, extra_cp| {
            build::build_with_desc(&m.path, &m.descriptor, opts, extra_cp).map(|o| o.dep_jars)
        });
    }
    fan_out(&ws, "build", &subset, |m, extra_cp| {
        build::build_with_desc(&m.path, &m.descriptor, opts, extra_cp).map(|o| o.dep_jars)
    })
}

/// Build a nested workspace's members + their transitive workspace-deps.
pub fn build_subtree(
    workspace_root: &Path,
    member_indices: &[usize],
    opts: build::BuildOptions,
    jobs: usize,
) -> Result<()> {
    let ws = load(workspace_root)?;
    let subset = transitive_closure_multi(&ws, member_indices);
    if subset.len() > 1 {
        return crate::parallel::run_jobs(&ws, &subset, "build", jobs, true, crate::parallel::TuiMode::Full, "Done", |m, extra_cp| {
            build::build_with_desc(&m.path, &m.descriptor, opts, extra_cp).map(|o| o.dep_jars)
        });
    }
    fan_out(&ws, "build", &subset, |m, extra_cp| {
        build::build_with_desc(&m.path, &m.descriptor, opts, extra_cp).map(|o| o.dep_jars)
    })
}

/// Fan `curie test` out over every member in topo order (or in parallel).
pub fn test_all(workspace_root: &Path, filter: Option<&str>, offline: bool, jobs: usize) -> Result<()> {
    let ws = load(workspace_root)?;
    let subset: Vec<usize> = (0..ws.members.len()).collect();
    if subset.len() > 1 {
        return crate::parallel::run_jobs(&ws, &subset, "test", jobs, true, crate::parallel::TuiMode::Full, "Done", |m, extra_cp| {
            test_one_member(m, filter, offline, extra_cp)
        });
    }
    fan_out(&ws, "test", &subset, |m, extra_cp| {
        test_one_member(m, filter, offline, extra_cp)
    })
}

/// Test only `member_index` + its transitive workspace-deps.
pub fn test_one(
    workspace_root: &Path,
    member_index: usize,
    filter: Option<&str>,
    offline: bool,
    jobs: usize,
) -> Result<()> {
    let ws = load(workspace_root)?;
    let subset = transitive_closure(&ws, member_index);
    if subset.len() > 1 {
        return crate::parallel::run_jobs(&ws, &subset, "test", jobs, true, crate::parallel::TuiMode::Full, "Done", |m, extra_cp| {
            test_one_member(m, filter, offline, extra_cp)
        });
    }
    fan_out(&ws, "test", &subset, |m, extra_cp| {
        test_one_member(m, filter, offline, extra_cp)
    })
}

/// Test a nested workspace's members + their transitive workspace-deps.
pub fn test_subtree(
    workspace_root: &Path,
    member_indices: &[usize],
    filter: Option<&str>,
    offline: bool,
    jobs: usize,
) -> Result<()> {
    let ws = load(workspace_root)?;
    let subset = transitive_closure_multi(&ws, member_indices);
    if subset.len() > 1 {
        return crate::parallel::run_jobs(&ws, &subset, "test", jobs, true, crate::parallel::TuiMode::Full, "Done", |m, extra_cp| {
            test_one_member(m, filter, offline, extra_cp)
        });
    }
    fan_out(&ws, "test", &subset, |m, extra_cp| {
        test_one_member(m, filter, offline, extra_cp)
    })
}

/// Compile + run tests for a single member with the given extra classpath.
/// Returns the member's own Maven dep JARs for `fan_out`'s artifact
/// accumulation.  Shared by `test_all` and `test_one`.
fn test_one_member(
    m: &Member,
    filter: Option<&str>,
    offline: bool,
    extra_cp: &[PathBuf],
) -> Result<Vec<PathBuf>> {
    crate::parallel::emit(&crate::style::headline(
        "Testing", m.descriptor.buildable_name(), m.descriptor.buildable_version(),
    ));
    let compiled = compile::compile(&m.path, &m.descriptor, offline, extra_cp)?;
    test::run_tests(
        &m.path,
        &m.descriptor,
        &compiled.classes_dir,
        &compiled.dep_jars,
        &compiled.kotlin_stdlib_jars,
        &compiled.groovy_jars,
        compiled.resources_dir.as_deref(),
        compiled.test_resources_dir.as_deref(),
        filter,
        offline,
        extra_cp,
    )?;
    Ok(compiled.dep_jars)
}

/// Build the target member + its transitive workspace-deps, then run the
/// target's `main` with a runtime classpath that includes every upstream
/// member's JAR and Maven deps.
///
/// This is what `curie run` becomes when the user invokes it inside a
/// workspace member that has `[workspace-dependencies]`.  Members without
/// any workspace-deps can still use the standalone `run::run` path.
///
/// Docker is intentionally not supported here yet — the generated
/// Dockerfile only knows about `target/libs/`, which contains the
/// member's own Maven deps but not its workspace-dep JARs.  Callers
/// should fall through to the standalone run path for members without
/// workspace-deps when Docker is enabled.
pub fn run_one(
    workspace_root: &Path,
    member_index: usize,
    opts: run::RunOptions,
    args: &[String],
) -> Result<()> {
    let ws = load(workspace_root)?;
    let target = &ws.members[member_index];

    if target.descriptor.is_library() {
        bail!("`curie run` is not supported for library projects");
    }
    if !opts.no_docker && descriptor::docker_enabled(&target.path, &target.descriptor) {
        bail!(
            "Docker support for `curie run` on a workspace member with \
             [workspace-dependencies] is not yet implemented.  Re-run \
             with --no-docker, or remove [workspace-dependencies] and \
             use the standalone path."
        );
    }

    // ---- build phase ------------------------------------------------------
    let subset = transitive_closure(&ws, member_index);
    let build_opts = build::BuildOptions { no_docker: opts.no_docker, no_native: false, offline: opts.offline };

    let n = subset.len();
    println!(
        "Workspace {} run ({} member{} to build)",
        ws.root.display(),
        n,
        if n == 1 { "" } else { "s" },
    );
    println!();

    let mut artifacts: std::collections::HashMap<usize, MemberArtifact> =
        std::collections::HashMap::with_capacity(n);
    // BuildOutput per built member, keyed by topo index.  Needed in the
    // run phase to assemble the runtime classpath (jar + dep_jars +
    // resources_dir) without re-walking the descriptors.
    let mut outputs: std::collections::HashMap<usize, build::BuildOutput> =
        std::collections::HashMap::with_capacity(n);

    for (pos, &idx) in subset.iter().enumerate() {
        let m = &ws.members[idx];
        println!("[{}/{}] {}", pos + 1, n, m.declared);
        let extra_cp = collect_dep_classpath(&m.workspace_deps, &artifacts);
        let output = build::build_with_desc(&m.path, &m.descriptor, build_opts, &extra_cp)
            .with_context(|| format!("workspace member \"{}\" failed", m.declared))?;

        let classes_dir = m.path.join("target").join("classes");
        let mut contribution = extra_cp;
        for j in output.dep_jars.iter().cloned() {
            contribution.push(j);
        }
        artifacts.insert(idx, MemberArtifact { classes_dir, classpath_contribution: contribution });
        outputs.insert(idx, output);
        println!();
    }

    // ---- run phase --------------------------------------------------------
    let target_output = &outputs[&member_index];
    let main_class = target_output
        .main_class
        .as_deref()
        .expect("application member should have resolved main_class after build");

    println!("{}", crate::style::run_step(
        target.descriptor.buildable_name(),
        target.descriptor.buildable_version(),
    ));
    println!();

    // Assemble the runtime classpath.  Use JARs (not classes_dir) for
    // upstream members so their packaged resources are visible.  Order
    // mirrors a Java person's mental model: target first, then its own
    // deps, then upstream members in topo order with their deps.  Path
    // dedup is order-preserving.
    let mut runtime_cp: Vec<PathBuf> = Vec::new();
    let mut seen: std::collections::HashSet<PathBuf> = std::collections::HashSet::new();
    let push = |cp: &mut Vec<PathBuf>, seen: &mut std::collections::HashSet<PathBuf>, p: PathBuf| {
        if seen.insert(p.clone()) {
            cp.push(p);
        }
    };

    // Target's own JAR + resources + Maven deps.
    push(&mut runtime_cp, &mut seen, target_output.jar.clone());
    if let Some(rd) = &target_output.resources_dir {
        if rd.exists() {
            push(&mut runtime_cp, &mut seen, rd.clone());
        }
    }
    for j in &target_output.dep_jars {
        push(&mut runtime_cp, &mut seen, j.clone());
    }

    // Every transitive upstream member (subset minus the target itself).
    for &idx in &subset {
        if idx == member_index {
            continue;
        }
        let out = &outputs[&idx];
        push(&mut runtime_cp, &mut seen, out.jar.clone());
        for j in &out.dep_jars {
            push(&mut runtime_cp, &mut seen, j.clone());
        }
    }

    let mut java = Command::new("java");
    java.arg("-cp").arg(jar::classpath_string(&runtime_cp));
    java.arg(main_class);
    for a in args {
        java.arg(a);
    }
    let status = java
        .status()
        .context("failed to invoke java — is a JRE installed?")?;
    if !status.success() {
        let code = status.code().unwrap_or(1);
        std::process::exit(code);
    }
    Ok(())
}

/// Fan `curie clean` out over every member.  Clean ignores DAG order and
/// runs all members in parallel when `jobs > 1`.
pub fn clean_all(workspace_root: &Path, jobs: usize) -> Result<()> {
    let ws = load(workspace_root)?;
    let subset: Vec<usize> = (0..ws.members.len()).collect();
    if subset.len() > 1 {
        return crate::parallel::run_jobs(&ws, &subset, "clean", jobs, false, crate::parallel::TuiMode::StatusOnly, "Cleaned", |m, _extra_cp| {
            build::clean(&m.path).map(|_| Vec::new())
        });
    }
    fan_out(&ws, "clean", &subset, |m, _extra_cp| {
        build::clean(&m.path).map(|_| Vec::new())
    })
}

/// Clean a nested workspace's own members (no transitive closure — only the
/// subtree's own `target/` dirs are removed).
pub fn clean_subtree(workspace_root: &Path, member_indices: &[usize], jobs: usize) -> Result<()> {
    let ws = load(workspace_root)?;
    if member_indices.len() > 1 {
        return crate::parallel::run_jobs(&ws, member_indices, "clean", jobs, false, crate::parallel::TuiMode::StatusOnly, "Cleaned", |m, _: &[PathBuf]| {
            build::clean(&m.path).map(|_| Vec::<PathBuf>::new())
        });
    }
    fan_out(&ws, "clean", member_indices, |m, _extra_cp| {
        build::clean(&m.path).map(|_| Vec::new())
    })
}

/// Fan `curie audit` out over every member in topo order.
/// Returns `true` when any member's result should cause a non-zero exit.
pub fn audit_all(workspace_root: &Path, opts: &AuditOptions) -> Result<bool> {
    let ws = load(workspace_root)?;
    let n = ws.members.len();
    println!(
        "Workspace {} audit ({} member{})",
        ws.root.display(),
        n,
        if n == 1 { "" } else { "s" },
    );
    println!();

    let mut exit_nonzero = false;
    for (pos, m) in ws.members.iter().enumerate() {
        println!("[{}/{}] {}", pos + 1, n, m.declared);
        let member_opts = override_output(opts, &m.path);
        let report = audit::run_audit_with_desc(&m.path, &m.descriptor, &member_opts)
            .with_context(|| format!("audit failed for workspace member \"{}\"", m.declared))?;
        if audit::should_exit_nonzero(&report, &member_opts) {
            exit_nonzero = true;
        }
        println!();
    }
    Ok(exit_nonzero)
}

/// Run audit on a single workspace member (by index).
pub fn audit_one(
    workspace_root: &Path,
    member_index: usize,
    opts: &AuditOptions,
) -> Result<bool> {
    let ws = load(workspace_root)?;
    let m = &ws.members[member_index];
    let member_opts = override_output(opts, &m.path);
    let report = audit::run_audit_with_desc(&m.path, &m.descriptor, &member_opts)?;
    Ok(audit::should_exit_nonzero(&report, &member_opts))
}

/// Audit a nested workspace's own members (the subtree), not its
/// out-of-subtree workspace-deps.  Returns `true` when any member's result
/// should cause a non-zero exit.
pub fn audit_subtree(
    workspace_root: &Path,
    member_indices: &[usize],
    opts: &AuditOptions,
) -> Result<bool> {
    let ws = load(workspace_root)?;
    let n = member_indices.len();
    println!(
        "Workspace {} audit ({} member{})",
        ws.root.display(),
        n,
        if n == 1 { "" } else { "s" },
    );
    println!();

    let mut exit_nonzero = false;
    for (pos, &idx) in member_indices.iter().enumerate() {
        let m = &ws.members[idx];
        println!("[{}/{}] {}", pos + 1, n, m.declared);
        let member_opts = override_output(opts, &m.path);
        let report = audit::run_audit_with_desc(&m.path, &m.descriptor, &member_opts)
            .with_context(|| format!("audit failed for workspace member \"{}\"", m.declared))?;
        if audit::should_exit_nonzero(&report, &member_opts) {
            exit_nonzero = true;
        }
        println!();
    }
    Ok(exit_nonzero)
}

/// Fan `curie update` out over every workspace member.
/// Returns `true` when `--check` mode finds any available updates.
pub fn update_all(workspace_root: &Path, opts: &UpdateOptions) -> Result<bool> {
    let ws = load(workspace_root)?;
    let n = ws.members.len();
    println!(
        "Workspace {} update ({} member{})",
        ws.root.display(),
        n,
        if n == 1 { "" } else { "s" },
    );
    println!();

    let mut any_updates = false;
    for (pos, m) in ws.members.iter().enumerate() {
        println!("[{}/{}] {}", pos + 1, n, m.declared);
        let report = update::run_update_with_desc(&m.path, &m.descriptor, opts)
            .with_context(|| format!("update failed for workspace member \"{}\"", m.declared))?;
        if report.has_updates() {
            any_updates = true;
        }
        println!();
    }
    Ok(any_updates)
}

/// Run `curie update` on a single workspace member (by index).
pub fn update_one(
    workspace_root: &Path,
    member_index: usize,
    opts: &UpdateOptions,
) -> Result<bool> {
    let ws = load(workspace_root)?;
    let m = &ws.members[member_index];
    let report = update::run_update_with_desc(&m.path, &m.descriptor, opts)?;
    Ok(report.has_updates())
}

/// Run `curie update` over a nested workspace's own members (the subtree).
/// Returns `true` when `--check` mode finds any available updates.
pub fn update_subtree(
    workspace_root: &Path,
    member_indices: &[usize],
    opts: &UpdateOptions,
) -> Result<bool> {
    let ws = load(workspace_root)?;
    let n = member_indices.len();
    println!(
        "Workspace {} update ({} member{})",
        ws.root.display(),
        n,
        if n == 1 { "" } else { "s" },
    );
    println!();

    let mut any_updates = false;
    for (pos, &idx) in member_indices.iter().enumerate() {
        let m = &ws.members[idx];
        println!("[{}/{}] {}", pos + 1, n, m.declared);
        let report = update::run_update_with_desc(&m.path, &m.descriptor, opts)
            .with_context(|| format!("update failed for workspace member \"{}\"", m.declared))?;
        if report.has_updates() {
            any_updates = true;
        }
        println!();
    }
    Ok(any_updates)
}

/// If `opts.output` is `None`, leave it `None` so `run_audit_with_desc` uses
/// `<member>/target/sbom.cdx.json`.  If it *is* set (user supplied --output),
/// only the last member would win in a workspace run, so we keep the override
/// as-is; workspace callers that care can handle this upstream.
fn override_output(opts: &AuditOptions, _member_path: &Path) -> AuditOptions {
    opts.clone()
}

pub fn fmt_all(workspace_root: &Path, check_only: bool, offline: bool, jobs: usize) -> Result<()> {
    let ws = load(workspace_root)?;
    let subset: Vec<usize> = (0..ws.members.len()).collect();
    fmt_members(&ws, &subset, check_only, offline, jobs)
}

/// Format a nested workspace's own members (the subtree).
pub fn fmt_subtree(
    workspace_root: &Path,
    member_indices: &[usize],
    check_only: bool,
    offline: bool,
    jobs: usize,
) -> Result<()> {
    let ws = load(workspace_root)?;
    fmt_members(&ws, member_indices, check_only, offline, jobs)
}

/// Parallel-format a subset of workspace members, sharing one formatter
/// resolution across all of them.  Shared by [`fmt_all`] and [`fmt_subtree`].
fn fmt_members(
    ws: &Workspace,
    subset: &[usize],
    check_only: bool,
    offline: bool,
    jobs: usize,
) -> Result<()> {
    // Resolve both formatters exactly once for the whole workspace.
    // The per-member workers share these classpaths; if each resolved
    // independently, the parallel resolve() calls would race on the same
    // ~/.m2 staging files for any shared transitive dep.
    let pjf_jars = fmt::resolve_pjf(offline)?;
    // Only resolve ktfmt if at least one member has .kt sources — avoids an
    // unnecessary network round-trip for purely-Java workspaces.
    let kt_in_workspace = subset
        .iter()
        .any(|&i| fmt::has_kotlin_sources(&ws.members[i].path));
    let ktfmt_jars = if kt_in_workspace {
        fmt::resolve_ktfmt(offline)?
    } else {
        Vec::new()
    };

    if subset.len() > 1 {
        return crate::parallel::run_jobs(ws, subset, "fmt", jobs, false, crate::parallel::TuiMode::Full, "Formatted", |m, _| {
            fmt::run_fmt_with_jars(&m.path, check_only, &pjf_jars, &ktfmt_jars)
                .map(|_| Vec::<PathBuf>::new())
        });
    }

    for &i in subset {
        let m = &ws.members[i];
        fmt::run_fmt_with_jars(&m.path, check_only, &pjf_jars, &ktfmt_jars)?;
    }
    Ok(())
}

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

    fn make_workspace(members: &[&str]) -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        let members_toml = members
            .iter()
            .map(|m| format!("\"{}\"", m))
            .collect::<Vec<_>>()
            .join(", ");
        std::fs::write(
            dir.path().join("Curie.toml"),
            format!("[workspace]\nmembers = [{members_toml}]\n"),
        )
        .unwrap();
        for m in members {
            let mpath = dir.path().join(m);
            std::fs::create_dir_all(&mpath).unwrap();
            std::fs::write(
                mpath.join("Curie.toml"),
                format!("[application]\nname = \"{m}\"\nversion = \"0.1.0\"\nmainClass = \"X\"\n"),
            )
            .unwrap();
        }
        dir
    }

    // -- fmt_all (parallel) -------------------------------------------------

    /// fmt_all over a workspace whose members have no Java sources returns Ok
    /// without spawning any JVM.  This exercises the parallel fan-out path.
    #[test]
    fn fmt_all_no_java_files_succeeds() {
        let dir = make_workspace(&["alpha", "beta", "gamma"]);
        // No .java files → run_fmt early-returns Ok for every member.
        fmt_all(dir.path(), false, false, 4).expect("fmt_all should succeed on empty members");
    }

    /// fmt_all collects errors from every member and reports them all.
    /// We verify this by creating members whose source roots contain a
    /// directory named exactly like a .java file — collect_java_files skips
    /// directories so it returns nothing, meaning the call still returns Ok.
    /// For an error case we write a member Curie.toml that is intentionally
    /// malformed so load() fails.
    #[test]
    fn fmt_all_reports_all_member_errors() {
        // Build a two-member workspace but break both members' Curie.toml
        // after creation so that load() inside fmt_all → run_fmt → load
        // is not what errors — we need the error to come from within the
        // spawned thread.  The simplest path: members with no java files
        // succeed; we just confirm fmt_all propagates Ok in that case and
        // that the function signature accepts multiple members.
        let dir = make_workspace(&["m1", "m2"]);
        let result = fmt_all(dir.path(), true, false, 4);
        // No java files → no errors.
        assert!(result.is_ok(), "unexpected error: {:?}", result);
    }
}