alef-cli 0.16.35

CLI for the alef polyglot binding generator
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
use alef_core::config::{Language, ResolvedCrateConfig};
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use tracing::{debug, warn};

/// One formatter invocation (command + args).
struct FormatterCommand {
    command: String,
    args: Vec<String>,
}

/// Post-generation formatter configuration.
/// Each language may run a sequence of formatter commands in a working directory.
struct FormatterSpec {
    /// Commands to run in sequence; on first failure the rest are skipped (warning logged).
    commands: Vec<FormatterCommand>,
    /// Working directory relative to project root; empty string = project root.
    work_dir: String,
}

/// Get the default formatter spec for a language.
///
/// Notes on Rust formatting: backends that emit Rust code (FFI, PyO3, NAPI, Magnus,
/// ext-php-rs, Rustler, wasm-bindgen) all live inside the consumer workspace, so a
/// single `cargo fmt --all` from the project root covers every generated `.rs` file.
/// We attach this to `Language::Ffi` because FFI is always present when any of the
/// C-FFI-bridged languages (Go/Java/C#) are enabled, and harmless when only WASM
/// or pure-Rust bindings are used.
fn get_default_formatter(config: &ResolvedCrateConfig, lang: Language) -> Option<FormatterSpec> {
    match lang {
        // ruff check --fix runs lint autofixes (unused imports, missing TypeAlias
        // annotations, import sorting); ruff format applies whitespace formatting.
        // Both must run — `format` alone leaves I001/F401/TC008 issues that fail CI.
        Language::Python => {
            let package_path = config
                .python
                .as_ref()
                .and_then(|python| python.stubs.as_ref())
                .map(|stubs| stubs.output.to_string_lossy().into_owned())
                .unwrap_or_else(|| "packages/python/".to_owned());
            Some(FormatterSpec {
                commands: vec![
                    FormatterCommand {
                        command: "ruff".to_owned(),
                        args: vec!["check".to_owned(), "--fix".to_owned(), package_path.clone()],
                    },
                    FormatterCommand {
                        command: "ruff".to_owned(),
                        args: vec!["format".to_owned(), package_path],
                    },
                ],
                work_dir: String::new(),
            })
        }
        Language::Node => Some(FormatterSpec {
            // Run the Oxc formatter and linter from the repo root so package,
            // e2e, and registry-mode test app output are normalized consistently.
            commands: vec![
                FormatterCommand {
                    command: "pnpm".to_owned(),
                    args: vec!["dlx".to_owned(), "oxfmt".to_owned(), ".".to_owned()],
                },
                FormatterCommand {
                    command: "pnpm".to_owned(),
                    args: vec![
                        "dlx".to_owned(),
                        "oxlint".to_owned(),
                        "--fix".to_owned(),
                        ".".to_owned(),
                    ],
                },
            ],
            work_dir: ".".to_owned(),
        }),
        Language::Ruby => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "rubocop".to_owned(),
                args: vec!["-A".to_owned(), "--no-server".to_owned()],
            }],
            work_dir: "packages/ruby/".to_owned(),
        }),
        Language::Php => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "php-cs-fixer".to_owned(),
                args: vec!["fix".to_owned()],
            }],
            work_dir: "packages/php/".to_owned(),
        }),
        Language::Elixir => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "mix".to_owned(),
                args: vec!["format".to_owned()],
            }],
            work_dir: "packages/elixir/".to_owned(),
        }),
        Language::Go => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "gofmt".to_owned(),
                args: vec!["-w".to_owned(), ".".to_owned()],
            }],
            work_dir: "packages/go/".to_owned(),
        }),
        // google-java-format requires explicit file paths — no recursive flag.
        // We collect *.java files from the work_dir at runtime and pass them as args.
        // The command is built dynamically in `format_generated`; this spec carries
        // only the base args (the file list is appended before invocation).
        Language::Java => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "google-java-format".to_owned(),
                args: vec!["-i".to_owned()],
            }],
            work_dir: "packages/java/src/".to_owned(),
        }),
        // Bug fix: when both a .csproj and a .slnx exist in packages/csharp/, `dotnet
        // format` without a workspace argument aborts. Use the project_file from config
        // when available so the correct project is targeted unambiguously.
        Language::Csharp => {
            let mut args = vec!["format".to_owned()];
            let work_dir = "packages/csharp/".to_owned();
            if let Some(project_file) = config.project_file_for_language(Language::Csharp) {
                // project_file is a path relative to the project root (e.g.
                // "packages/csharp/LiterLlm.csproj"). Strip the work_dir prefix so the
                // argument is relative to work_dir where the command runs.
                let relative = Path::new(project_file)
                    .strip_prefix(&work_dir)
                    .unwrap_or(Path::new(project_file));
                args.push(relative.to_string_lossy().into_owned());
            }
            Some(FormatterSpec {
                commands: vec![FormatterCommand {
                    command: "dotnet".to_owned(),
                    args,
                }],
                work_dir,
            })
        }
        // Format the resolved wasm binding crate directly. The crate may be excluded
        // from the root workspace, so `cargo fmt -p <pkg>` is not reliable.
        // `cargo sort` normalises the generated Cargo.toml so prek's cargo-sort hook
        // is a no-op; without it, cargo-sort reformats feature indentation after the
        // hash is finalised, making alef verify report the file as stale.
        //
        // No oxfmt step: oxfmt's default TOML style (2-space indent, collapsed
        // multi-line arrays) collides with cargo-sort's preserved 4-space
        // indent, producing an infinite format/regen loop on the embedded hash.
        // cargo-sort alone is enough to canonicalise the wasm Cargo.toml.
        Language::Wasm => {
            let crate_dir = config
                .output_for("wasm")
                .map(resolve_crate_dir)
                .unwrap_or_else(|| Path::new("crates").join(format!("{}-wasm", config.name)));
            let manifest_path = crate_dir.join("Cargo.toml").to_string_lossy().into_owned();
            let crate_dir_str = crate_dir.to_string_lossy().into_owned();
            Some(FormatterSpec {
                commands: vec![
                    FormatterCommand {
                        command: "cargo".to_owned(),
                        args: vec!["fmt".to_owned(), "--manifest-path".to_owned(), manifest_path],
                    },
                    FormatterCommand {
                        command: "cargo".to_owned(),
                        args: vec!["sort".to_owned(), crate_dir_str],
                    },
                ],
                work_dir: String::new(),
            })
        }
        // FFI runs `cargo fmt --all` from project root: this formats every generated
        // Rust crate in the consumer workspace (FFI, PyO3, NAPI-RS, Magnus, ext-php-rs,
        // Rustler, wasm-bindgen). Was previously `cargo fmt` in `packages/ffi/` —
        // which has no Cargo.toml, so it silently no-op'd and CI's `cargo fmt --check`
        // would fail on the unformatted FFI lib.rs.
        // `cargo sort -w` normalises all workspace Cargo.toml files so prek's
        // cargo-sort hook is a no-op; without it the hook reformats feature
        // indentation after finalize_hashes, making alef verify report stale files.
        //
        // No oxfmt step here. The shared Kreuzberg pre-commit `oxfmt` hook is scoped
        // to `[javascript, jsx, ts, tsx, json, css]` only (see pre-commit-hooks
        // `.pre-commit-hooks.yaml`), so any JS/TS/JSON files that need oxfmt-shape
        // formatting are picked up by the per-language scaffold + the consumer's
        // own oxfmt hook on next commit. Running `pnpm dlx oxfmt .` from here would
        // additionally reformat every TOML in the workspace — oxfmt's default
        // settings differ from `cargo-sort` / hand-maintained Cargo.toml styles
        // (collapses multi-line arrays, 2-space indent), which produced an
        // infinite format/regen loop for any consumer whose hand-maintained
        // Cargo.toml didn't already match oxfmt's TOML defaults.
        Language::Ffi => Some(FormatterSpec {
            commands: vec![
                FormatterCommand {
                    command: "cargo".to_owned(),
                    args: vec!["fmt".to_owned(), "--all".to_owned()],
                },
                FormatterCommand {
                    command: "cargo".to_owned(),
                    args: vec!["sort".to_owned(), "-w".to_owned()],
                },
            ],
            work_dir: String::new(),
        }),
        Language::R => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "Rscript".to_owned(),
                args: vec!["-e".to_owned(), "styler::style_pkg('packages/r')".to_owned()],
            }],
            work_dir: String::new(),
        }),
        Language::Kotlin => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "ktlint".to_owned(),
                args: vec!["--format".to_owned()],
            }],
            work_dir: "packages/kotlin/src/".to_owned(),
        }),
        Language::KotlinAndroid => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "ktlint".to_owned(),
                args: vec!["--format".to_owned()],
            }],
            work_dir: "packages/kotlin-android/src/".to_owned(),
        }),
        Language::Swift => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "swift".to_owned(),
                args: vec![
                    "format".to_owned(),
                    "--in-place".to_owned(),
                    "--recursive".to_owned(),
                    "Sources".to_owned(),
                ],
            }],
            work_dir: "packages/swift/".to_owned(),
        }),
        Language::Dart => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "dart".to_owned(),
                args: vec!["format".to_owned(), ".".to_owned()],
            }],
            work_dir: "packages/dart/".to_owned(),
        }),
        Language::Gleam => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "gleam".to_owned(),
                args: vec!["format".to_owned()],
            }],
            work_dir: "packages/gleam/".to_owned(),
        }),
        Language::Zig => Some(FormatterSpec {
            commands: vec![FormatterCommand {
                command: "zig".to_owned(),
                args: vec!["fmt".to_owned(), "src".to_owned()],
            }],
            work_dir: "packages/zig/".to_owned(),
        }),
        // C is an e2e test consumer of the FFI layer — no generated files to format.
        // Jni output is Rust source formatted by `cargo fmt --all` (triggered by the Ffi formatter).
        Language::Rust | Language::C | Language::Jni => None,
    }
}

/// Detect whether the consumer's Java package configures Spotless via
/// `spotless-maven-plugin`. The walked pom is `<base>/<work_dir>/../pom.xml`
/// (one level up from `packages/java/src/`, which is `packages/java/pom.xml`).
///
/// Returns the path to the pom when Spotless is configured, otherwise `None`.
/// The check is conservative — a literal substring match on the plugin
/// artifactId — because parsing a full pom is wildly out of scope for a
/// formatter-selection heuristic.
fn detect_spotless_pom(base_dir: &Path, java_work_dir: &str) -> Option<PathBuf> {
    let pom = base_dir.join(java_work_dir).parent()?.join("pom.xml");
    if !pom.is_file() {
        return None;
    }
    let content = std::fs::read_to_string(&pom).ok()?;
    if content.contains("spotless-maven-plugin") {
        Some(pom)
    } else {
        None
    }
}

/// Collect all `.java` files under `dir` recursively (up to `limit` paths).
/// Returns an empty vec if the directory does not exist or cannot be read.
fn collect_java_files(dir: &Path, limit: usize) -> Vec<PathBuf> {
    let pattern = format!("{}/**/*.java", dir.display());
    let Ok(entries) = glob::glob(&pattern) else {
        return vec![];
    };
    entries.flatten().filter(|p| p.is_file()).take(limit).collect()
}

/// Run language-native formatters on emitted packages after generation.
/// For each language in the output, if formatting is enabled and the formatter binary
/// is available, run the formatter in the package directory.
/// Formatter errors are logged as warnings and do not fail the generate command.
pub fn format_generated(
    files: &[(Language, Vec<alef_core::backend::GeneratedFile>)],
    config: &ResolvedCrateConfig,
    base_dir: &Path,
    only_languages: Option<&std::collections::HashSet<Language>>,
) {
    let mut formatted_langs = std::collections::HashSet::new();

    for (lang, _) in files {
        // Skip if already formatted in this batch
        if formatted_langs.contains(lang) {
            continue;
        }

        // Resolve format config for this language (check overrides first)
        let lang_str = lang.to_string().to_lowercase();
        let format_cfg = config
            .format_overrides
            .get(&lang_str)
            .cloned()
            .unwrap_or_else(|| config.format.clone());

        // Languages with a custom format_override command bypass the only_languages
        // filter. A custom command is an explicit declaration that this formatter
        // must run whenever the language's files are present in the output — even
        // when the generated content is identical to on-disk content (i.e., files
        // were not re-written this run). Skipping in that case would leave the
        // embedded alef:hash: computed over pre-formatter content, causing prek's
        // own formatter hook to rewrite the files post-hash and break alef verify.
        // Default formatters still respect only_languages so that warming the cache
        // (no file writes) avoids unnecessary ruff/mix-format/etc. invocations.
        let has_custom_command = format_cfg.command.is_some();
        if !has_custom_command {
            if let Some(filter) = only_languages {
                if !filter.contains(lang) {
                    continue;
                }
            }
        }

        if !format_cfg.enabled {
            debug!("  [{lang_str}] formatting disabled, skipping");
            continue;
        }

        // Get the formatter command (custom or default)
        let formatter_cmd = if let Some(custom) = format_cfg.command {
            // Custom command: run as-is in the package directory
            if let Err(e) = run_custom_formatter(&custom, base_dir) {
                warn!("[{lang_str}] custom formatter failed: {e}");
            }
            formatted_langs.insert(*lang);
            continue;
        } else if let Some(spec) = get_default_formatter(config, *lang) {
            // For Java, prefer the project's Spotless pipeline when configured in
            // packages/java/pom.xml — running the same tool the project's prek
            // hook would run keeps `alef-verify` stable. Falling back to
            // google-java-format would produce different bytes than Spotless,
            // and the embedded `alef:hash:` value would invalidate as soon as
            // prek's `mvn spotless:apply` ran. See issue tslp/v1.8.0-rc.14.
            if *lang == Language::Java
                && let Some(pom) = detect_spotless_pom(base_dir, &spec.work_dir)
            {
                debug!(
                    "  [java] spotless detected at {}, using mvn spotless:apply",
                    pom.display()
                );
                FormatterSpec {
                    commands: vec![FormatterCommand {
                        command: "mvn".to_owned(),
                        args: vec![
                            "-f".to_owned(),
                            pom.to_string_lossy().into_owned(),
                            "spotless:apply".to_owned(),
                            "-q".to_owned(),
                        ],
                    }],
                    work_dir: spec.work_dir,
                }
            } else {
                spec
            }
        } else {
            // No formatter for this language (e.g., Rust is formatted in-memory before writing)
            debug!("  [{lang_str}] no default formatter configured");
            continue;
        };

        // Resolve work dir (empty string = project root)
        let work_dir = if formatter_cmd.work_dir.is_empty() {
            base_dir.to_path_buf()
        } else {
            base_dir.join(&formatter_cmd.work_dir)
        };
        if !work_dir.exists() {
            debug!(
                "  [{lang_str}] package directory does not exist: {}, skipping",
                work_dir.display()
            );
            continue;
        }

        // Run each command in sequence; stop on first failure (warning logged)
        for step in &formatter_cmd.commands {
            if !is_tool_available(&step.command) {
                warn!("[{lang_str}] formatter not found: {} (skipping format)", step.command);
                break;
            }

            // For Java, google-java-format requires explicit file paths: collect them now.
            // Spotless and other Maven-driven formatters operate on the pom and don't
            // take per-file arguments, so the collection is skipped for them.
            let extra_args: Vec<String> = if *lang == Language::Java && step.command == "google-java-format" {
                const JAVA_FILE_BATCH_LIMIT: usize = 200;
                let java_files = collect_java_files(&work_dir, JAVA_FILE_BATCH_LIMIT);
                if java_files.is_empty() {
                    debug!(
                        "  [{lang_str}] no .java files found in {}, skipping",
                        work_dir.display()
                    );
                    break;
                }
                java_files
                    .into_iter()
                    .map(|p| p.to_string_lossy().into_owned())
                    .collect()
            } else {
                vec![]
            };

            let mut all_args: Vec<&str> = step.args.iter().map(String::as_str).collect();
            let extra_refs: Vec<&str> = extra_args.iter().map(String::as_str).collect();
            all_args.extend_from_slice(&extra_refs);

            match run_formatter(&step.command, &all_args, &work_dir) {
                Ok(()) => {
                    debug!("  [{lang_str}] {} {:?} ok", step.command, all_args);
                }
                Err(e) => {
                    warn!("[{lang_str}] {} {:?} failed: {}", step.command, all_args, e);
                    break;
                }
            }
        }

        formatted_langs.insert(*lang);
    }
}

/// Check if a tool is available on PATH.
fn is_tool_available(tool: &str) -> bool {
    Command::new("which")
        .arg(tool)
        .output()
        .map(|output| output.status.success())
        .unwrap_or(false)
}

/// Run a formatter command with arguments in a specific directory.
fn run_formatter(command: &str, args: &[&str], work_dir: &Path) -> anyhow::Result<()> {
    let output = Command::new(command).args(args).current_dir(work_dir).output()?;

    if !output.status.success() {
        return Err(anyhow::anyhow!(
            "formatter exited with code {:?}: {}",
            output.status.code(),
            format_command_output(&output)
        ));
    }

    Ok(())
}

/// Run a custom formatter command (shell-style string) in a directory.
fn run_custom_formatter(cmd: &str, work_dir: &Path) -> anyhow::Result<()> {
    let output = Command::new("sh").arg("-c").arg(cmd).current_dir(work_dir).output()?;

    if !output.status.success() {
        debug!("custom formatter output: {}", format_command_output(&output));
        return Err(anyhow::anyhow!(
            "formatter exited with code {:?}: {}",
            output.status.code(),
            format_command_output(&output)
        ));
    }

    Ok(())
}

fn format_command_output(output: &Output) -> String {
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = stdout.trim();
    let stderr = stderr.trim();

    match (stdout.is_empty(), stderr.is_empty()) {
        (false, false) => format!("stdout:\n{stdout}\nstderr:\n{stderr}"),
        (false, true) => format!("stdout:\n{stdout}"),
        (true, false) => format!("stderr:\n{stderr}"),
        (true, true) => "<no output>".to_string(),
    }
}

fn resolve_crate_dir(output_path: &Path) -> PathBuf {
    output_path
        .parent()
        .map(Path::to_path_buf)
        .unwrap_or_else(|| output_path.to_path_buf())
}

#[cfg(test)]
mod tests {
    use super::*;
    use alef_core::config::{Language, NewAlefConfig, ResolvedCrateConfig};

    fn make_config(crate_name: &str) -> ResolvedCrateConfig {
        let cfg: NewAlefConfig = toml::from_str(&format!(
            r#"
[workspace]
languages = ["rust"]
[[crates]]
name = "{crate_name}"
sources = ["src/lib.rs"]
"#
        ))
        .expect("valid config");
        cfg.resolve().unwrap().remove(0)
    }

    fn make_config_with_csharp_project(crate_name: &str, project_file: &str) -> ResolvedCrateConfig {
        let cfg: NewAlefConfig = toml::from_str(&format!(
            r#"
[workspace]
languages = ["csharp"]
[[crates]]
name = "{crate_name}"
sources = ["src/lib.rs"]
[crates.csharp]
project_file = "{project_file}"
"#
        ))
        .expect("valid config");
        cfg.resolve().unwrap().remove(0)
    }

    #[test]
    fn formatter_error_includes_stdout_and_stderr() {
        let err = run_formatter(
            "sh",
            &["-c", "printf 'stdout text'; printf 'stderr text' >&2; exit 7"],
            Path::new("."),
        )
        .expect_err("formatter should fail");
        let msg = err.to_string();
        assert!(msg.contains("stdout text"), "missing stdout in error: {msg}");
        assert!(msg.contains("stderr text"), "missing stderr in error: {msg}");
    }

    #[test]
    fn test_wasm_formatter_uses_manifest_path() {
        let config = make_config("liter-llm");
        let spec = get_default_formatter(&config, Language::Wasm).expect("should have formatter");
        // Two commands: cargo fmt (rs files), cargo sort (Cargo.toml table order).
        // No oxfmt step — oxfmt's default TOML style fights cargo-sort's preserved
        // indent and produces an infinite format/regen loop on the embedded hash.
        assert_eq!(spec.commands.len(), 2, "WASM must have cargo fmt + cargo sort steps");
        let fmt_cmd = &spec.commands[0];
        assert_eq!(fmt_cmd.command, "cargo");
        assert_eq!(
            fmt_cmd.args,
            vec!["fmt", "--manifest-path", "crates/liter-llm-wasm/Cargo.toml"]
        );
        let sort_cmd = &spec.commands[1];
        assert_eq!(sort_cmd.command, "cargo");
        assert_eq!(
            sort_cmd.args,
            vec!["sort", "crates/liter-llm-wasm"],
            "cargo sort arg must be the crate directory, not the manifest path"
        );
        assert!(spec.work_dir.is_empty(), "WASM formatter must run at workspace root");
    }

    #[test]
    fn test_wasm_formatter_uses_configured_output_path() {
        let cfg: NewAlefConfig = toml::from_str(
            r#"
[workspace]
languages = ["wasm"]
[[crates]]
name = "tree-sitter-language-pack"
sources = ["crates/ts-pack-core/src/lib.rs"]
[crates.output]
wasm = "crates/ts-pack-core-wasm/src/"
"#,
        )
        .expect("valid config");
        let config = cfg.resolve().unwrap().remove(0);
        let spec = get_default_formatter(&config, Language::Wasm).expect("should have formatter");
        let fmt_cmd = &spec.commands[0];
        assert_eq!(
            fmt_cmd.args,
            vec!["fmt", "--manifest-path", "crates/ts-pack-core-wasm/Cargo.toml"]
        );
        let sort_cmd = &spec.commands[1];
        assert_eq!(
            sort_cmd.args,
            vec!["sort", "crates/ts-pack-core-wasm"],
            "cargo sort arg must match the crate dir derived from the configured output path"
        );
    }

    #[test]
    fn test_ffi_formatter_includes_cargo_sort() {
        let config = make_config("liter-llm");
        let spec = get_default_formatter(&config, Language::Ffi).expect("should have formatter");
        // Two commands: cargo fmt --all (rs files) + cargo sort -w (Cargo.toml table
        // order across the workspace). No oxfmt step here — the shared Kreuzberg
        // pre-commit `oxfmt` hook is JS/TS/JSON/CSS only, and running oxfmt on `.`
        // additionally reformats every workspace TOML (including hand-maintained
        // Cargo.toml files) into oxfmt's 2-space style, fighting cargo-sort's
        // preserved indent and breaking the embedded hash.
        assert_eq!(spec.commands.len(), 2, "FFI must have cargo fmt + cargo sort steps");
        let fmt_cmd = &spec.commands[0];
        assert_eq!(fmt_cmd.command, "cargo");
        assert_eq!(fmt_cmd.args, vec!["fmt", "--all"]);
        let sort_cmd = &spec.commands[1];
        assert_eq!(sort_cmd.command, "cargo");
        assert_eq!(
            sort_cmd.args,
            vec!["sort", "-w"],
            "cargo sort must run workspace-wide so all binding crate Cargo.toml files are normalised"
        );
        assert!(spec.work_dir.is_empty(), "FFI formatter must run at workspace root");
    }

    // Bug 2: C# formatter must include project_file when configured to avoid workspace ambiguity.
    #[test]
    fn test_csharp_formatter_with_project_file() {
        let config = make_config_with_csharp_project("liter-llm", "packages/csharp/LiterLlm.csproj");
        let spec = get_default_formatter(&config, Language::Csharp).expect("should have formatter");
        assert_eq!(spec.commands.len(), 1);
        let cmd = &spec.commands[0];
        assert_eq!(cmd.command, "dotnet");
        assert!(cmd.args.contains(&"format".to_owned()), "args must contain 'format'");
        assert!(
            cmd.args.contains(&"LiterLlm.csproj".to_owned()),
            "args must contain the relative project file, got: {:?}",
            cmd.args
        );
        assert_eq!(spec.work_dir, "packages/csharp/");
    }

    #[test]
    fn test_csharp_formatter_without_project_file() {
        let config = make_config("liter-llm");
        let spec = get_default_formatter(&config, Language::Csharp).expect("should have formatter");
        let cmd = &spec.commands[0];
        assert_eq!(cmd.command, "dotnet");
        assert_eq!(
            cmd.args,
            vec!["format"],
            "without project_file, args must be just ['format']"
        );
    }

    // Bug 3: Java file collection — only .java files are returned, non-.java files are excluded.
    #[test]
    fn test_collect_java_files_returns_only_java_files() {
        let dir = tempfile::tempdir().expect("tempdir");
        let root = dir.path();

        // Create a nested structure with .java and other files
        std::fs::create_dir_all(root.join("com/example")).unwrap();
        std::fs::write(root.join("com/example/Foo.java"), "class Foo {}").unwrap();
        std::fs::write(root.join("com/example/Bar.java"), "class Bar {}").unwrap();
        std::fs::write(root.join("com/example/readme.txt"), "ignore me").unwrap();
        std::fs::write(root.join("com/example/Baz.class"), "ignore me").unwrap();

        let files = collect_java_files(root, 200);
        assert_eq!(files.len(), 2, "expected 2 .java files, got: {:?}", files);
        assert!(files.iter().all(|p| p.extension().is_some_and(|e| e == "java")));
    }

    #[test]
    fn test_collect_java_files_empty_dir() {
        let dir = tempfile::tempdir().expect("tempdir");
        let files = collect_java_files(dir.path(), 200);
        assert!(files.is_empty());
    }

    #[test]
    fn test_collect_java_files_nonexistent_dir() {
        let files = collect_java_files(Path::new("/nonexistent/path/to/src"), 200);
        assert!(files.is_empty());
    }

    #[test]
    fn test_collect_java_files_respects_limit() {
        let dir = tempfile::tempdir().expect("tempdir");
        let root = dir.path();
        for i in 0..10 {
            std::fs::write(root.join(format!("File{i}.java")), "class Foo {}").unwrap();
        }
        let files = collect_java_files(root, 5);
        assert_eq!(files.len(), 5);
    }

    // Regression: custom format_override commands must run even when the language
    // is absent from the only_languages filter (i.e., files were not re-written
    // this run). The only_languages filter is an optimization for default formatters
    // (skip when nothing changed), but a custom command must always run to ensure
    // the embedded alef:hash: is computed over formatter-normalized content.
    // Without this, adding [workspace.format_overrides.php] and running
    // `alef all --format` on an already-generated repo would skip php-cs-fixer,
    // leaving hashes computed over raw (pre-formatter) content; prek's own
    // php-cs-fixer hook would then reformat and break alef verify.
    #[test]
    fn format_generated_custom_override_runs_when_lang_absent_from_only_languages_filter() {
        let dir = tempfile::tempdir().expect("tempdir");
        let sentinel = dir.path().join("was_run.txt");
        let sentinel_str = sentinel.to_string_lossy().replace('\\', "/");

        // Config with a custom format_override for php that writes a sentinel file.
        let cfg: NewAlefConfig = toml::from_str(&format!(
            r#"
[workspace]
languages = ["php"]

[workspace.format_overrides.php]
command = "touch {sentinel_str}"

[[crates]]
name = "my-lib"
sources = ["src/lib.rs"]
"#
        ))
        .expect("valid config");
        let config = cfg.resolve().expect("resolve").remove(0);

        // Simulate bindings for php — language appears in files but is NOT in only_languages.
        let files: Vec<(Language, Vec<alef_core::backend::GeneratedFile>)> = vec![(Language::Php, vec![])];

        // only_languages is empty — simulates "nothing was written this run".
        let only_languages: std::collections::HashSet<Language> = std::collections::HashSet::new();

        assert!(!sentinel.exists(), "sentinel must not exist before format_generated");

        format_generated(&files, &config, dir.path(), Some(&only_languages));

        assert!(
            sentinel.exists(),
            "custom format_override command must run even when php is absent from only_languages"
        );
    }

    // Complement: default formatters must still respect the only_languages filter
    // so that a warm cache (no file writes) skips unnecessary ruff/mix-format/etc.
    // invocations for default formatters.
    #[test]
    fn format_generated_default_formatter_skipped_when_lang_absent_from_only_languages() {
        let dir = tempfile::tempdir().expect("tempdir");
        // Config with no format_overrides — python uses the default ruff formatter.
        let config = make_config("my-lib");

        let files: Vec<(Language, Vec<alef_core::backend::GeneratedFile>)> = vec![(Language::Python, vec![])];

        // only_languages is empty — simulates "nothing was written this run".
        let only_languages: std::collections::HashSet<Language> = std::collections::HashSet::new();

        // This should complete without error (ruff not present on the test box is fine —
        // the point is that format_generated skips python entirely without reaching the
        // is_tool_available check, so no warning is emitted and no external process runs).
        // We verify by ensuring format_generated returns without calling any tool.
        // Since python has a default formatter (ruff), skipping means the tool is never
        // looked up — we can't assert negatively on tool invocation, but the test
        // documents the intent: no-op when only_languages filter excludes the language.
        format_generated(&files, &config, dir.path(), Some(&only_languages));
        // If we reach here without error the skip path worked correctly.
    }
}