1use htl::cache;
10mod junit;
11pub mod report;
12mod scaffold;
13
14#[derive(Clone, Copy, PartialEq, Eq, Debug, clap::ValueEnum)]
20enum Format {
21 Text,
23 Json,
26}
27
28#[derive(Clone, Copy, PartialEq, Eq, Debug, clap::ValueEnum)]
31enum CacheModeArg {
32 PerModule,
34 WholeRun,
36}
37
38#[derive(Subcommand)]
39enum TypesCmd {
40 Add {
43 library: String,
45 #[arg(long)]
47 from: Option<PathBuf>,
48 #[arg(long)]
50 force: bool,
51 },
52}
53
54#[derive(Subcommand)]
55enum PkgCmd {
56 Install,
58 Add {
60 name: String,
62 git: String,
64 #[arg(long)]
66 tag: Option<String>,
67 #[arg(long)]
69 rev: Option<String>,
70 #[arg(long)]
72 branch: Option<String>,
73 #[arg(long)]
76 entry: Option<PathBuf>,
77 #[arg(long)]
80 target_dir: Option<PathBuf>,
81 },
82 Update {
84 name: Option<String>,
86 #[arg(long)]
88 dry_run: bool,
89 #[arg(long)]
91 force: bool,
92 },
93 Clean {
95 #[arg(long)]
97 all: bool,
98 },
99 Patch {
102 dep: String,
104 #[arg(long)]
106 force: bool,
107 },
108}
109
110#[derive(Subcommand)]
111enum CacheCmd {
112 Clear {
114 path: Option<PathBuf>,
117 },
118 Status {
120 path: Option<PathBuf>,
123 #[arg(long, value_enum, default_value_t = Format::Text)]
125 format: Format,
126 #[arg(long)]
128 entries: bool,
129 },
130}
131
132#[derive(Subcommand)]
133enum BundleCmd {
134 Info {
137 file: PathBuf,
139 #[arg(long, value_enum, default_value_t = Format::Text)]
141 format: Format,
142 },
143}
144
145impl From<CacheModeArg> for cache::Mode {
146 fn from(m: CacheModeArg) -> Self {
147 match m {
148 CacheModeArg::PerModule => cache::Mode::PerModule,
149 CacheModeArg::WholeRun => cache::Mode::WholeRun,
150 }
151 }
152}
153
154use anyhow::{Context, Result, bail};
155use clap::{Parser, Subcommand};
156use htl::BuildTarget;
157use htl::Htl;
158use htl::bundle::Bundle;
159use htl::project;
163use std::fs;
164use std::path::{Path, PathBuf};
165use std::process::ExitCode;
166
167#[derive(Parser)]
168#[command(
169 name = "htl",
170 version,
171 about = "Teal, hidden: run / check / build .tl on mlua",
172 long_about = "\
173Teal, hidden: run / check / build .tl on mlua.
174
175htl is a toolchain for Teal, and a way to embed one. It type-checks `.tl` sources with \
176Teal's own checker, runs and tests them on an embedded mlua state, formats them, and \
177links them into a single stripped-bytecode bundle. A Rust host embeds the same engine, \
178so the scripts it ships are checked when the host is built rather than when its user \
179first runs one.
180
181The guide, with every command and what a project looks like:
182 https://github.com/ynishi/htl
183
184The API a Rust host holds — `include_tl!`, `#[host_module]`, the run cache:
185 https://docs.rs/htl"
186)]
187struct Cli {
188 #[command(subcommand)]
189 cmd: Cmd,
190}
191
192#[derive(Subcommand)]
193enum Cmd {
194 #[command(after_long_help = "\
200Examples:
201 htl check src every .tl under src/
202 htl check src --strict every warn counts as deny: warnings and lints fail
203 htl check src --lint nil-index=deny,no-any=warn
204 one rule fails the run, another is advice
205 htl check src --lint -tl:hint a warning kind of the Teal compiler silenced
206 htl check --list-lints every rule with its default level, then exit
207
208The store is .htl/cache at the project root, the nearest htl.toml or mlua-pkg.toml above
209the paths; --no-cache skips it, --explain-cache says why a lookup missed.
210Caching: https://github.com/ynishi/htl#caching
211")]
212 Check {
213 paths: Vec<PathBuf>,
214 #[arg(long)]
216 strict: bool,
217 #[arg(long, allow_hyphen_values = true)]
223 lint: Option<String>,
224 #[arg(long)]
226 list_lints: bool,
227 #[arg(long, value_enum, default_value_t = Format::Text)]
229 format: Format,
230 #[arg(long)]
232 no_cache: bool,
233 #[arg(long, value_enum)]
235 cache_mode: Option<CacheModeArg>,
236 #[arg(long)]
238 explain_cache: bool,
239 },
240 #[command(after_long_help = "\
244Examples:
245 htl test every .tl that requires htl.test
246 htl test tests --filter parser only tests whose \"suite > name\" contains it
247 htl test --coverage --coverage-lines
248 which lines of each module the suite never reached
249 htl test --junit report.xml also write the run as a JUnit report, for a CI
250")]
251 Test {
252 paths: Vec<PathBuf>,
253 #[arg(long)]
255 filter: Option<String>,
256 #[arg(long, default_value = htl::testing::DEFAULT_LIB)]
258 lib: String,
259 #[arg(long, allow_hyphen_values = true)]
261 lint: Option<String>,
262 #[arg(long)]
264 fail_fast: bool,
265 #[arg(short, long)]
267 verbose: bool,
268 #[arg(short, long, conflicts_with = "verbose")]
270 quiet: bool,
271 #[arg(long, value_name = "MS")]
273 slow: Option<f64>,
274 #[arg(long)]
276 update: bool,
277 #[arg(long)]
279 coverage: bool,
280 #[arg(long, requires = "coverage")]
282 coverage_lines: bool,
283 #[arg(long, value_name = "FILE")]
285 lcov: Option<PathBuf>,
286 #[arg(long, value_name = "FILE")]
288 junit: Option<PathBuf>,
289 #[arg(long)]
291 seed: Option<u64>,
292 #[arg(long, value_enum, default_value_t = Format::Text)]
294 format: Format,
295 #[arg(long)]
297 no_cache: bool,
298 #[arg(long)]
300 explain_cache: bool,
301 },
302 #[command(after_long_help = "\
315Examples:
316 htl fix src apply the safe fixes to every .tl under src/
317 htl fix src --diff --dry-run what would change, as a unified diff per file
318 htl fix src --rule forward-ref only that rule's fixes
319 htl fix src --unsafe also the fixes that may change what the program does
320")]
321 Fix {
322 paths: Vec<PathBuf>,
323 #[arg(long, value_delimiter = ',')]
326 rule: Vec<String>,
327 #[arg(long = "unsafe")]
329 unsafe_fixes: bool,
330 #[arg(long)]
332 dry_run: bool,
333 #[arg(long)]
335 diff: bool,
336 #[arg(long)]
338 allow_dirty: bool,
339 #[arg(long)]
341 allow_no_vcs: bool,
342 #[arg(long)]
344 exit_non_zero_on_fix: bool,
345 #[arg(long, value_enum, default_value_t = Format::Text)]
347 format: Format,
348 },
349 Dts {
364 dir: Option<PathBuf>,
366 },
367 #[command(after_long_help = "\
371Examples:
372 htl new hello src/main.tl, a module, tests/, mlua-pkg.toml
373 htl new hello --lib a library: no entry script
374 htl new hello --embed the same, plus a Rust host (--target bin)
375 htl new hello --target cdylib --lib
376 a library behind a C ABI, for a caller that is not Rust
377 htl new hello --embed --htl main
378 built against the unreleased htl (a git pin on main)
379 htl new hello --embed --htl path:../htl
380 built against a local checkout of this repository
381 htl new hello --no-x without the htlx dependency the manifest gets by default
382
383Build targets: https://docs.rs/htl/latest/htl/build_target/enum.BuildTarget.html
384Layout of a project: https://github.com/ynishi/htl#layout-of-a-project-htl-new
385")]
386 New {
387 name: String,
388 #[arg(long)]
390 lib: bool,
391 #[arg(long)]
393 embed: bool,
394 #[arg(long, value_name = "NAME", value_parser = clap::builder::PossibleValuesParser::new(scaffold::target_names()))]
398 target: Option<String>,
399 #[arg(long, value_name = "REQ")]
403 htl: Option<String>,
404 #[arg(long)]
406 no_x: bool,
407 },
408 Init {
410 dir: Option<PathBuf>,
411 #[arg(long)]
412 lib: bool,
413 #[arg(long)]
415 embed: bool,
416 #[arg(long, value_name = "NAME", value_parser = clap::builder::PossibleValuesParser::new(scaffold::target_names()))]
418 target: Option<String>,
419 #[arg(long, value_name = "REQ")]
423 htl: Option<String>,
424 #[arg(long)]
426 no_x: bool,
427 },
428 Pkg {
431 #[command(subcommand)]
432 cmd: PkgCmd,
433 },
434 Types {
436 #[command(subcommand)]
437 cmd: TypesCmd,
438 },
439 Cache {
443 #[command(subcommand)]
444 cmd: CacheCmd,
445 },
446 Fmt {
448 paths: Vec<PathBuf>,
449 #[arg(long)]
451 check: bool,
452 #[arg(long)]
454 indent: Option<usize>,
455 },
456 Gen {
458 file: PathBuf,
459 #[arg(short, long)]
460 out: Option<PathBuf>,
461 },
462 Run {
464 file: PathBuf,
465 #[arg(trailing_var_arg = true)]
466 args: Vec<String>,
467 },
468 #[command(after_long_help = "\
475Examples:
476 htl build src/main.tl -o app.hb
477 the entry's require closure, as one bundle
478 htl build src/main.tl -o app.hb --debug
479 keep line numbers and local names in the bytecode
480 htl build src/main.tl -o app.hb --host mymod
481 leave a module the host provides out of the bundle
482 htl run app.hb run what was built
483
484Caching: https://github.com/ynishi/htl#caching
485")]
486 Build {
493 entry: PathBuf,
497 #[arg(short, long, default_value = "app.hb")]
498 out: PathBuf,
499 #[arg(short, long, default_value = "main")]
501 main: String,
502 #[arg(long)]
504 debug: bool,
505 #[arg(long)]
508 source: bool,
509 #[arg(long, value_delimiter = ',')]
511 extra: Vec<String>,
512 #[arg(long, value_delimiter = ',')]
514 host: Vec<String>,
515 #[arg(long)]
518 no_cache: bool,
519 #[arg(long)]
521 explain_cache: bool,
522 },
523 #[command(after_long_help = "\
528Examples:
529 htl unused modules and dependencies no entry reaches
530 htl unused --exit-non-zero-on-unused
531 the same report, and a non-zero exit for a CI
532 htl unused --format json the same report as one JSON document
533")]
534 Unused {
535 paths: Vec<PathBuf>,
538 #[arg(long, value_enum, default_value_t = Format::Text)]
540 format: Format,
541 #[arg(long)]
543 exit_non_zero_on_unused: bool,
544 #[arg(long)]
547 no_cache: bool,
548 #[arg(long)]
550 explain_cache: bool,
551 },
552 #[command(after_long_help = "\
559Examples:
560 htl resolve mq the chain for require(\"mq\")
561 htl resolve socket.http a name with dots, as a require spells it
562 htl resolve mq --format json the same rows as one JSON document
563
564Exits 1 when the name resolves to nothing, when two files implement it, or when a file of
565the project implements a name the host provides, so a script can ask.
566
567`htl.test` is answered like a host-provided name: no file of the project implements it
568(`htl test` preloads it into the state it runs), and its one row is the declaration the
569binary carries, `provided by the environment`.
570")]
571 Resolve {
572 module: String,
574 path: Option<PathBuf>,
576 #[arg(long, value_enum, default_value_t = Format::Text)]
578 format: Format,
579 },
580 Bundle {
584 #[command(subcommand)]
585 cmd: BundleCmd,
586 },
587}
588
589pub fn command() -> clap::Command {
595 <Cli as clap::CommandFactory>::command()
596}
597
598pub fn run() -> ExitCode {
606 let mut argv: Vec<String> = std::env::args().collect();
608 let is_cargo = Path::new(&argv[0])
609 .file_stem()
610 .and_then(|s| s.to_str())
611 .map(|s| s == "cargo-htl")
612 .unwrap_or(false);
613 if is_cargo && argv.get(1).map(String::as_str) == Some("htl") {
614 argv.remove(1);
615 }
616 match real_main(Cli::parse_from(argv)) {
617 Ok(code) => code,
618 Err(e) => {
619 eprintln!("htl: {e:#}");
620 ExitCode::from(2)
621 }
622 }
623}
624
625fn real_main(cli: Cli) -> Result<ExitCode> {
626 match cli.cmd {
627 Cmd::Check {
628 paths,
629 strict,
630 lint,
631 list_lints,
632 format,
633 no_cache,
634 cache_mode,
635 explain_cache,
636 } => cmd_check(
637 &paths,
638 lint.as_deref(),
639 CheckFlags {
640 strict,
641 list_lints,
642 json: format == Format::Json,
643 use_cache: !no_cache,
644 cache_mode: cache_mode.map(Into::into),
645 explain: explain_cache,
646 },
647 ),
648 Cmd::Fmt {
649 paths,
650 check,
651 indent,
652 } => cmd_fmt(&paths, check, indent),
653 Cmd::Test {
654 paths,
655 filter,
656 lib,
657 lint,
658 fail_fast,
659 verbose,
660 quiet,
661 slow,
662 update,
663 coverage,
664 coverage_lines,
665 lcov,
666 junit,
667 seed,
668 format,
669 no_cache,
670 explain_cache,
671 } => cmd_test(
672 &paths,
673 filter.as_deref(),
674 &lib,
675 lint.as_deref(),
676 TestFlags {
677 fail_fast,
678 verbose,
679 quiet,
680 slow,
681 update,
682 coverage: coverage || lcov.is_some(),
683 coverage_lines,
684 lcov,
685 junit,
686 json: format == Format::Json,
687 no_cache,
688 explain: explain_cache,
689 seed,
690 },
691 ),
692 Cmd::Pkg { cmd } => match cmd {
693 PkgCmd::Install => cmd_pkg_install(),
694 PkgCmd::Add {
695 name,
696 git,
697 tag,
698 rev,
699 branch,
700 entry,
701 target_dir,
702 } => cmd_pkg_add(htl::pkg::mlua_pkg::ops::AddSpec {
703 name,
704 git,
705 tag,
706 rev,
707 branch,
708 entry,
709 target_dir,
710 }),
711 PkgCmd::Update {
712 name,
713 dry_run,
714 force,
715 } => cmd_pkg_update(htl::pkg::mlua_pkg::ops::UpdateOpts {
716 name,
717 dry_run,
718 force,
719 }),
720 PkgCmd::Clean { all } => cmd_pkg_clean(all),
721 PkgCmd::Patch { dep, force } => cmd_pkg_patch(&dep, force),
722 },
723 Cmd::Types { cmd } => match cmd {
724 TypesCmd::Add {
725 library,
726 from,
727 force,
728 } => cmd_types_add(&library, from.as_deref(), force),
729 },
730 Cmd::Cache { cmd } => match cmd {
731 CacheCmd::Clear { path } => cmd_cache_clear(path.as_deref()),
732 CacheCmd::Status {
733 path,
734 format,
735 entries,
736 } => cmd_cache_status(path.as_deref(), format == Format::Json, entries),
737 },
738 Cmd::Dts { dir } => cmd_dts(dir.as_deref()),
739 Cmd::New {
740 name,
741 lib,
742 embed,
743 target,
744 htl,
745 no_x,
746 } => cmd_new(&name, lib, embed, target.as_deref(), htl.as_deref(), no_x),
747 Cmd::Init {
748 dir,
749 lib,
750 embed,
751 target,
752 htl,
753 no_x,
754 } => cmd_init(
755 dir.as_deref(),
756 lib,
757 embed,
758 target.as_deref(),
759 htl.as_deref(),
760 no_x,
761 ),
762 Cmd::Gen { file, out } => cmd_gen(&file, out.as_deref()),
763 Cmd::Run { file, args } => cmd_run(&file, &args),
764 Cmd::Fix {
765 paths,
766 rule,
767 unsafe_fixes,
768 dry_run,
769 diff,
770 allow_dirty,
771 allow_no_vcs,
772 exit_non_zero_on_fix,
773 format,
774 } => cmd_fix(
775 &paths,
776 FixFlags {
777 rule,
778 unsafe_fixes,
779 dry_run: dry_run || diff,
780 diff,
781 allow_dirty,
782 allow_no_vcs,
783 exit_non_zero_on_fix,
784 json: format == Format::Json,
785 },
786 ),
787 Cmd::Build {
788 entry,
789 out,
790 main,
791 debug,
792 source,
793 extra,
794 host,
795 no_cache,
796 explain_cache,
797 } => cmd_build(
798 &entry,
799 &out,
800 &main,
801 htl::link::LinkOptions {
802 debug,
803 source,
804 extra,
805 host,
806 entry_name: None,
807 },
808 BuildCache {
809 use_cache: !no_cache,
810 explain: explain_cache,
811 },
812 ),
813 Cmd::Unused {
814 paths,
815 format,
816 exit_non_zero_on_unused,
817 no_cache,
818 explain_cache,
819 } => cmd_unused(
820 &paths,
821 UnusedFlags {
822 json: format == Format::Json,
823 fail_on_unused: exit_non_zero_on_unused,
824 use_cache: !no_cache,
825 explain: explain_cache,
826 },
827 ),
828 Cmd::Resolve {
829 module,
830 path,
831 format,
832 } => cmd_resolve(&module, path.as_deref(), format == Format::Json),
833 Cmd::Bundle { cmd } => match cmd {
834 BundleCmd::Info { file, format } => cmd_bundle_info(&file, format == Format::Json),
835 },
836 }
837}
838
839fn text_sink() -> project::Sink<report::Out> {
850 project::Sink::new(report::Out::new(false))
851}
852
853fn judged(found: &htl::verdict::Findings, policy: &htl::verdict::Policy) -> String {
857 let mut out = String::new();
858 if policy.capped {
859 return out;
860 }
861 if found.denied > 0 {
862 out.push_str(&format!(", {} at deny", found.denied));
863 }
864 if policy.strict && found.warnings + found.lints > 0 {
865 out.push_str(&format!(
866 ", {} warning(s) and {} lint(s) under strict",
867 found.warnings, found.lints
868 ));
869 }
870 out
871}
872
873fn config_lang(h: &Htl, cfg: &project::Config) -> Result<()> {
879 h.set_lang(&project::lang_of(cfg))
880}
881
882fn config_lints(h: &Htl, cfg: &project::Config) -> Result<htl::lint::Lints> {
883 let spec = cfg
884 .as_ref()
885 .map(|(_, _, c)| c.lint_spec())
886 .unwrap_or_default();
887 let lints = htl::lint::Lints::parse(&spec)?;
888 h.select_lints(lints.selection())?;
889 Ok(lints)
890}
891
892fn auto_dts(start: &Path) -> Result<()> {
903 auto_dts_to(start, true)
904}
905
906fn auto_dts_to(start: &Path, write: bool) -> Result<()> {
910 let mut project = None;
911 if let Some((root, _, cfg)) = load_config(start)? {
912 let model = htl::model::Project::load(&root, cfg)?;
914 let contracts: Vec<_> = model.contracts.iter().map(|c| c.terms.clone()).collect();
915 let (results, problems) = htl::contract::publish_to(&root, &contracts, write);
916 announce_dts_to(&results, &root, write);
917 for p in &problems {
918 eprintln!("dts: {p}");
919 }
920 project = Some(root);
921 }
922 let Some(root) = htl::dts::find_cargo_package_root(start) else {
923 return Ok(());
924 };
925 let results =
926 htl::dts::generate_crate_to(&root, write).map_err(|e| anyhow::anyhow!("htl dts: {e}"))?;
927 announce_dts_to(&results, &root, write);
928 let types_root = project.unwrap_or_else(|| root.clone());
929 let report = dep_dts_to(&root, &types_root, write);
930 announce_dts_to(&report.written, &types_root, write);
931 announce_dep_report(&report, "dts: ");
932 Ok(())
933}
934
935#[derive(Default)]
942struct DepReport {
943 written: Vec<(PathBuf, bool)>,
945 not_written: Vec<String>,
947 left_in_place: Vec<String>,
950 unresolved: Option<String>,
955}
956
957fn dep_dts(cargo_root: &Path, types_root: &Path) -> DepReport {
964 dep_dts_to(cargo_root, types_root, true)
965}
966
967fn dep_dts_to(cargo_root: &Path, types_root: &Path, write: bool) -> DepReport {
969 let decls = match htl::dep_dts::resolve(cargo_root) {
970 Ok(d) => d,
971 Err(e) => {
972 return DepReport {
973 unresolved: Some(e),
974 ..DepReport::default()
975 };
976 }
977 };
978 let types = match decl_root(types_root) {
981 Ok(t) => t,
982 Err(e) => {
983 return DepReport {
984 unresolved: Some(format!("{e:#}")),
985 ..DepReport::default()
986 };
987 }
988 };
989 let left_in_place = htl::dep_dts::orphans(&types, &decls);
990 let (written, not_written) = htl::dep_dts::materialise_to(&types, &decls, write);
991 DepReport {
992 written,
993 not_written,
994 left_in_place,
995 unresolved: None,
996 }
997}
998
999fn announce_dep_report(report: &DepReport, prefix: &str) {
1004 for p in &report.not_written {
1005 eprintln!("{prefix}not written: {p}");
1006 }
1007 for p in &report.left_in_place {
1008 eprintln!("{prefix}left in place: {p}");
1009 }
1010 if let Some(e) = &report.unresolved {
1011 eprintln!("{prefix}{e}");
1012 }
1013}
1014
1015fn announce_dts_to(results: &[(PathBuf, bool)], root: &Path, wrote: bool) {
1018 for (target, changed) in results {
1019 if *changed {
1020 let target = target.strip_prefix(root).unwrap_or(target).display();
1021 if wrote {
1022 eprintln!("dts: wrote {target}");
1023 } else {
1024 eprintln!(
1025 "dts: would write {target} (dry run: not written; checked against the \
1026 file on disk)"
1027 );
1028 }
1029 }
1030 }
1031}
1032
1033fn cmd_dts(dir: Option<&Path>) -> Result<ExitCode> {
1034 let start = match dir {
1035 Some(d) => d.to_path_buf(),
1036 None => std::env::current_dir()?,
1037 };
1038 let mut results = Vec::new();
1041 let mut failed = false;
1042 let mut project = None;
1043 if let Some((croot, _, cfg)) = load_config(&start)? {
1044 let model = htl::model::Project::load(&croot, cfg)?;
1045 let contracts: Vec<_> = model.contracts.iter().map(|c| c.terms.clone()).collect();
1046 let (published, problems) = htl::contract::publish(&croot, &contracts);
1047 for p in &problems {
1048 eprintln!(" {p}");
1049 }
1050 failed = !problems.is_empty();
1053 results.extend(published);
1054 project = Some(croot);
1055 }
1056 let code = |failed: bool| {
1057 if failed {
1058 ExitCode::FAILURE
1059 } else {
1060 ExitCode::SUCCESS
1061 }
1062 };
1063 let Some(root) = htl::dts::find_cargo_package_root(&start) else {
1064 if results.is_empty() && !failed {
1065 bail!(
1066 "no Cargo.toml with a [package] section, and no ---@contract type to publish, \
1067 at or above {}",
1068 start.display()
1069 );
1070 }
1071 report_dts(&results, &start);
1072 return Ok(code(failed));
1073 };
1074 results.extend(htl::dts::generate_crate(&root).map_err(|e| anyhow::anyhow!("{e}"))?);
1075 let types_root = project.unwrap_or_else(|| root.clone());
1076 let report = dep_dts(&root, &types_root);
1077 announce_dep_report(&report, " ");
1078 failed = failed || !report.not_written.is_empty();
1081 results.extend(report.written);
1082 report_dts(&results, &root);
1083 Ok(code(failed))
1084}
1085
1086fn report_dts(results: &[(PathBuf, bool)], root: &Path) {
1087 let mut written = 0usize;
1088 for (target, w) in results {
1089 let rel = target.strip_prefix(root).unwrap_or(target).display();
1090 eprintln!(" {} {}", if *w { "wrote " } else { "unchanged" }, rel);
1091 written += usize::from(*w);
1092 }
1093 eprintln!(
1094 "htl dts: {} declaration(s) from {}, {} written",
1095 results.len(),
1096 root.display(),
1097 written
1098 );
1099}
1100
1101fn apply_model(
1108 h: &Htl,
1109 cfg: &project::Config,
1110 start: &Path,
1111) -> Result<Option<htl::model::Project>> {
1112 let model = project::model_of(cfg, start)?;
1113 if let Some(m) = &model {
1114 h.apply_model(m, htl::model::View::Source)?;
1115 }
1116 Ok(model)
1117}
1118
1119fn report_scaffold(dir: &Path, written: &[PathBuf], kept: &[PathBuf]) {
1123 let rel = |p: &PathBuf| p.strip_prefix(dir).unwrap_or(p).display().to_string();
1124 for p in written {
1125 eprintln!(" created {}", rel(p));
1126 }
1127 for p in kept {
1128 eprintln!(" kept {} (already there)", rel(p));
1129 }
1130 if kept.is_empty() {
1131 eprintln!(
1132 "htl: {} file(s) written under {}",
1133 written.len(),
1134 dir.display()
1135 );
1136 } else {
1137 eprintln!(
1138 "htl: {} file(s) written, {} kept under {}",
1139 written.len(),
1140 kept.len(),
1141 dir.display()
1142 );
1143 }
1144}
1145
1146fn cmd_new(
1147 name: &str,
1148 lib: bool,
1149 embed: bool,
1150 target: Option<&str>,
1151 htl: Option<&str>,
1152 no_x: bool,
1153) -> Result<ExitCode> {
1154 let dir = PathBuf::from(name);
1155 let pkg_name = dir
1156 .file_name()
1157 .and_then(|s| s.to_str())
1158 .ok_or_else(|| anyhow::anyhow!("invalid project name: {name}"))?
1159 .to_string();
1160 let target = scaffold::resolve_target(target, embed, lib)?;
1164 let htl = scaffold::HtlPin::parse(htl)?;
1165 let opts = scaffold::Options {
1166 lib,
1167 target,
1168 htl,
1169 no_x,
1170 };
1171 let done = scaffold::scaffold(&dir, &pkg_name, &opts, true)?;
1172 report_scaffold(&dir, &done.written, &[]);
1173 let fetch = if opts.writes_htlx() {
1176 "htl pkg install && "
1177 } else {
1178 ""
1179 };
1180 let then = match opts.target.map(|t| t.target) {
1186 Some(BuildTarget::Window) => "htl check . && cargo run",
1187 _ => "htl test",
1188 };
1189 eprintln!("next: cd {} && {fetch}{then}", dir.display());
1190 Ok(ExitCode::SUCCESS)
1191}
1192
1193fn cmd_init(
1194 dir: Option<&Path>,
1195 lib: bool,
1196 embed: bool,
1197 target: Option<&str>,
1198 htl: Option<&str>,
1199 no_x: bool,
1200) -> Result<ExitCode> {
1201 let dir = match dir {
1202 Some(d) => d.to_path_buf(),
1203 None => std::env::current_dir()?,
1204 };
1205 let abs = std::fs::canonicalize(&dir).unwrap_or(dir.clone());
1206 let name = abs
1207 .file_name()
1208 .and_then(|s| s.to_str())
1209 .ok_or_else(|| anyhow::anyhow!("cannot derive a project name from {}", abs.display()))?
1210 .to_string();
1211 let asked_for_a_target = target.is_some() || embed;
1212 let target = scaffold::resolve_target(target, embed, lib)?;
1213 let htl = scaffold::HtlPin::parse(htl)?;
1214 let opts = scaffold::Options {
1215 lib,
1216 target,
1217 htl,
1218 no_x,
1219 };
1220 let done = scaffold::scaffold(&dir, &name, &opts, false)?;
1221 let kept: &[PathBuf] = if asked_for_a_target { &done.kept } else { &[] };
1224 if done.written.is_empty() && kept.is_empty() {
1225 eprintln!("htl init: nothing to do, all scaffold files already exist");
1226 } else {
1227 report_scaffold(&dir, &done.written, kept);
1228 }
1229 Ok(ExitCode::SUCCESS)
1230}
1231
1232fn pkg_project() -> Result<htl::pkg::MluaProject> {
1238 let cwd = std::env::current_dir()?;
1239 let root = htl::model::Project::find_root(&cwd)?.map(|r| r.root);
1240 root.filter(|r| r.join(htl::pkg::MANIFEST_NAME).is_file())
1241 .map(|r| htl::pkg::MluaProject::at(&r))
1242 .with_context(|| {
1243 format!(
1244 "no {} above {}: `htl pkg` runs in a project",
1245 htl::pkg::MANIFEST_NAME,
1246 cwd.display()
1247 )
1248 })
1249}
1250
1251fn decl_root(root: &Path) -> Result<PathBuf> {
1255 let cfg = load_config(root)?;
1256 Ok(project::model_of(&cfg, root)?
1257 .and_then(|m| m.own().roots.decl.clone())
1258 .unwrap_or_else(|| root.join("types")))
1259}
1260
1261fn cmd_pkg_install() -> Result<ExitCode> {
1263 let project = pkg_project()?;
1264 let report = project.install()?;
1265 report_install(&report, &project);
1266 let project = htl::pkg::MluaProject::at(&project.root);
1270 report_types_sync(
1271 &project.sync_types(&decl_root(&project.root)?)?,
1272 &project.root,
1273 );
1274 report_patch_drift(&project);
1275 Ok(ExitCode::SUCCESS)
1276}
1277
1278fn report_install(
1280 report: &htl::pkg::mlua_pkg::ops::InstallReport,
1281 project: &htl::pkg::MluaProject,
1282) {
1283 use htl::pkg::mlua_pkg::ops::Placement;
1284 for w in &report.warnings {
1285 if w.contains("patch_dir") && w.contains("not used") {
1289 continue;
1290 }
1291 eprintln!("warning: {w}");
1292 }
1293 let rel = |p: &Path| {
1294 p.strip_prefix(&project.root)
1295 .unwrap_or(p)
1296 .display()
1297 .to_string()
1298 };
1299 for p in &report.packages {
1300 let sha: String = p.sha.chars().take(7).collect();
1301 let where_it_is = match (&p.placement, p.patched) {
1302 (_, true) => format!("from {}", rel(p.root())),
1303 (Placement::Copied(dest), _) => format!("copied to {}", rel(dest)),
1304 (Placement::Symlink(_), _) => p.entry.display().to_string(),
1305 };
1306 eprintln!(" install {} {sha} ({where_it_is})", p.name);
1307 }
1308 let n = report.packages.len();
1309 if report.transitive > 0 {
1310 eprintln!(
1311 "htl pkg install: {n} package(s), {} of them transitive",
1312 report.transitive
1313 );
1314 } else {
1315 eprintln!("htl pkg install: {n} package(s)");
1316 }
1317}
1318
1319fn cmd_pkg_add(spec: htl::pkg::mlua_pkg::ops::AddSpec) -> Result<ExitCode> {
1327 use htl::pkg::mlua_pkg::ops::AddOutcome;
1328 let cwd = std::env::current_dir()?;
1329 let root = htl::model::Project::find_root(&cwd)?
1330 .map(|r| r.root)
1331 .unwrap_or(cwd);
1332 let project = htl::pkg::MluaProject::at(&root);
1333 let name = spec.name.clone();
1334 let done = project.add(spec)?;
1335 let manifest = project
1336 .manifest
1337 .strip_prefix(&project.root)
1338 .unwrap_or(&project.manifest)
1339 .display();
1340 if done.report.manifest_created {
1341 eprintln!(" created {manifest}");
1342 }
1343 let verb = match done.report.outcome {
1344 AddOutcome::Added => "added",
1345 AddOutcome::Replaced => "updated",
1346 };
1347 eprintln!(" {verb} {name} in {manifest}");
1348 if let Some(dir) = &done.kept_patch_dir {
1349 eprintln!(
1353 " kept patch_dir = {} ({name} is patched)",
1354 dir.display()
1355 );
1356 }
1357 eprintln!("htl pkg add: run `htl pkg install` to fetch it");
1358 Ok(ExitCode::SUCCESS)
1359}
1360
1361fn cmd_pkg_update(opts: htl::pkg::mlua_pkg::ops::UpdateOpts) -> Result<ExitCode> {
1363 use htl::pkg::mlua_pkg::ops::UpdateOutcome;
1364 let project = pkg_project()?;
1365 let dry_run = opts.dry_run;
1366 let report = project.update(opts)?;
1367 if report.entries.is_empty() {
1368 eprintln!("htl pkg update: no dependency selected");
1369 return Ok(ExitCode::SUCCESS);
1370 }
1371 for (name, outcome) in &report.entries {
1372 match outcome {
1373 UpdateOutcome::TagBumped { old, new } => {
1374 eprintln!(" update {name}: tag {old} -> {new}")
1375 }
1376 UpdateOutcome::PrefixResolved { pin, resolved } => eprintln!(
1377 " update {name}: prefix '{pin}' is {resolved} (the manifest keeps the prefix)"
1378 ),
1379 UpdateOutcome::Refresh => eprintln!(" update {name}: refresh (branch or unpinned)"),
1380 UpdateOutcome::Skipped(why) => eprintln!(" skip {name}: {why}"),
1381 }
1382 }
1383 if dry_run {
1384 eprintln!("htl pkg update: dry run; nothing was written");
1385 return Ok(ExitCode::SUCCESS);
1386 }
1387 if let Some(install) = &report.install {
1388 report_install(install, &project);
1389 let project = htl::pkg::MluaProject::at(&project.root);
1390 report_types_sync(
1391 &project.sync_types(&decl_root(&project.root)?)?,
1392 &project.root,
1393 );
1394 report_patch_drift(&project);
1395 }
1396 Ok(ExitCode::SUCCESS)
1397}
1398
1399fn cmd_pkg_clean(all: bool) -> Result<ExitCode> {
1401 use htl::pkg::mlua_pkg::ops::CleanReport;
1402 let project = pkg_project()?;
1403 match project.clean(all)? {
1404 CleanReport::CacheRemoved => eprintln!("htl pkg clean: removed every cached package"),
1405 CleanReport::NoLockfile => {
1406 eprintln!("htl pkg clean: no lockfile, so nothing was ever cached")
1407 }
1408 CleanReport::StaleRemoved { removed: 0 } => eprintln!("htl pkg clean: nothing to remove"),
1409 CleanReport::StaleRemoved { removed } => eprintln!(
1410 "htl pkg clean: removed {removed} cache entr{} the lockfile no longer refers to",
1411 if removed == 1 { "y" } else { "ies" }
1412 ),
1413 }
1414 Ok(ExitCode::SUCCESS)
1415}
1416
1417fn report_patch_drift(project: &htl::pkg::MluaProject) {
1426 let short = |s: &str| s.chars().take(7).collect::<String>();
1427 for s in project.patch_status() {
1428 if s.in_use {
1429 continue;
1430 }
1431 let rel = s
1432 .dir
1433 .strip_prefix(&project.root)
1434 .unwrap_or(&s.dir)
1435 .display();
1436 let why = match (&s.base, &s.locked) {
1437 _ if !s.dir.is_dir() => "the copy is not there".to_string(),
1438 (None, _) => "the lockfile records no revision it was taken from".to_string(),
1439 (Some(b), Some(l)) => {
1440 format!("taken from {}, {} is now at {}", short(b), s.name, short(l))
1441 }
1442 (Some(b), None) => format!("taken from {}, and nothing is locked", short(b)),
1443 };
1444 eprintln!(" patch {rel} is not in use ({why})");
1445 eprintln!(
1446 " carry the change forward: commit it, then `htl pkg patch {}`",
1447 s.name
1448 );
1449 eprintln!(" drop it: remove patch_dir from mlua-pkg.toml and delete {rel}");
1450 }
1451}
1452
1453fn cmd_pkg_patch(dep: &str, force: bool) -> Result<ExitCode> {
1458 let project = pkg_project().context("a patch belongs to a project, so this runs in one")?;
1459 let done = project.patch(dep, force)?;
1460 let report = &done.report;
1461 let rel = report
1462 .patch_dir
1463 .strip_prefix(&project.root)
1464 .unwrap_or(&report.patch_dir);
1465 let verb = if report.created { "patched" } else { "rebuilt" };
1466 let base: String = report.base.chars().take(7).collect();
1467 eprintln!(" {verb} {} ({dep} at {base})", rel.display());
1468 if !done.dropped.is_empty() {
1472 eprintln!(
1473 " dropped {} (the repository's, not the package's)",
1474 done.dropped.join(", ")
1475 );
1476 }
1477 eprintln!("htl: it is the project's code now — edit it, commit it, then `htl pkg install`.");
1478 Ok(ExitCode::SUCCESS)
1479}
1480
1481fn report_types_sync(sync: &htl::pkg::TypesSync, root: &Path) {
1485 let rel = |p: &Path| p.strip_prefix(root).unwrap_or(p).display().to_string();
1486 for (path, dep) in &sync.written {
1487 eprintln!(" types {} (from {dep})", rel(path));
1488 }
1489 for (path, dep) in &sync.taken {
1490 eprintln!(" kept {} ({dep} publishes one too)", rel(path));
1491 }
1492}
1493
1494fn cmd_types_add(library: &str, from: Option<&Path>, force: bool) -> Result<ExitCode> {
1498 let project = pkg_project().context("`types/` is a project's, so this runs in one")?;
1499 let types = decl_root(&project.root)?;
1500 let sync = match from {
1501 Some(dir) => project.add_types_from(dir, library, "local", force, &types)?,
1502 None => project.add_types(library, force, &types)?,
1503 };
1504 report_types_sync(&sync, &project.root);
1505 if sync.written.is_empty() && !sync.taken.is_empty() {
1506 eprintln!("htl: nothing written; --force replaces what is already there");
1507 }
1508 Ok(ExitCode::SUCCESS)
1509}
1510
1511struct UnusedFlags {
1513 json: bool,
1514 fail_on_unused: bool,
1517 use_cache: bool,
1518 explain: bool,
1519}
1520
1521struct CheckFlags {
1523 strict: bool,
1524 list_lints: bool,
1525 json: bool,
1526 use_cache: bool,
1527 cache_mode: Option<cache::Mode>,
1528 explain: bool,
1529}
1530
1531struct TestFlags {
1532 fail_fast: bool,
1533 verbose: bool,
1534 quiet: bool,
1535 slow: Option<f64>,
1536 update: bool,
1537 coverage: bool,
1538 coverage_lines: bool,
1539 lcov: Option<PathBuf>,
1541 junit: Option<PathBuf>,
1543 json: bool,
1544 no_cache: bool,
1545 explain: bool,
1546 seed: Option<u64>,
1547}
1548
1549fn print_coverage(cov: &report::CoverageReport, with_lines: bool) {
1550 let width = cov
1551 .modules
1552 .iter()
1553 .map(|m| m.path.len())
1554 .max()
1555 .unwrap_or(4)
1556 .max(5);
1557 for m in &cov.modules {
1558 eprintln!(
1559 "coverage: {:<width$} {:>5}/{:<5} {:5.1}%",
1560 m.path,
1561 m.executed,
1562 m.total,
1563 100.0 * m.executed as f64 / m.total as f64
1564 );
1565 if !m.never_ran.is_empty() {
1566 let names: Vec<String> = m
1567 .never_ran
1568 .iter()
1569 .map(|f| format!("{} ({})", f.name, f.line))
1570 .collect();
1571 eprintln!(" never ran: {}", names.join(", "));
1572 }
1573 if with_lines && !m.unexecuted.is_empty() {
1574 let spans: Vec<String> = m
1575 .unexecuted
1576 .iter()
1577 .map(|(a, b)| {
1578 if a == b {
1579 a.to_string()
1580 } else {
1581 format!("{a}-{b}")
1582 }
1583 })
1584 .collect();
1585 eprintln!(" unexecuted: {}", spans.join(", "));
1586 }
1587 }
1588 if cov.total > 0 {
1589 eprintln!(
1590 "coverage: {:<width$} {:>5}/{:<5} {:5.1}% (statements)",
1591 "total",
1592 cov.executed,
1593 cov.total,
1594 100.0 * cov.executed as f64 / cov.total as f64
1595 );
1596 } else {
1597 eprintln!("coverage: no .tl module reached");
1598 }
1599}
1600
1601fn junit_suite(rep: &htl::testing::FileReport) -> junit::Suite {
1609 let mut left: Vec<&str> = rep.failures.iter().map(String::as_str).collect();
1610 let cases = rep
1611 .tests
1612 .iter()
1613 .map(|t| {
1614 let failure = (!t.ok)
1615 .then(|| {
1616 let head = format!("{}: ", t.name);
1617 let at = left.iter().position(|m| m.starts_with(&head))?;
1618 Some(left.remove(at)[head.len()..].to_string())
1619 })
1620 .flatten();
1621 junit::Case {
1622 name: t.name.clone(),
1623 ms: t.ms,
1624 failure: failure.or_else(|| (!t.ok).then(|| "failed".to_string())),
1627 }
1628 })
1629 .collect();
1630 let error = if !rep.check.ok() {
1631 Some(junit::Error {
1632 message: "type check failed".to_string(),
1633 kind: "check",
1634 body: rep
1635 .check
1636 .error_items
1637 .iter()
1638 .map(|d| d.clone().spelled().to_string())
1639 .collect::<Vec<_>>()
1640 .join("\n"),
1641 })
1642 } else {
1643 rep.error.as_ref().map(|e| junit::Error {
1644 message: e.clone(),
1645 kind: "error",
1646 body: e.clone(),
1647 })
1648 };
1649 junit::Suite {
1650 file: htl::diagnostic::display_path(&rep.path),
1651 duration_ms: rep.duration_ms,
1652 error,
1653 cases,
1654 }
1655}
1656
1657fn cmd_test(
1658 paths: &[PathBuf],
1659 filter: Option<&str>,
1660 lib: &str,
1661 lint: Option<&str>,
1662 flags: TestFlags,
1663) -> Result<ExitCode> {
1664 let paths = if paths.is_empty() {
1665 vec![PathBuf::from(".")]
1666 } else {
1667 paths.to_vec()
1668 };
1669 if let Some(first) = paths.first() {
1670 auto_dts(first)?;
1671 }
1672 let cfg = load_config(&paths[0])?;
1676 let model = project::model_of(&cfg, &paths[0])?;
1677 let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Test);
1678 let files = htl::testing::discover_tests_for(&paths, &skip, lib)?;
1679 project::refuse_flat_root(model.as_ref(), &paths, &files)?;
1682 if files.is_empty() {
1683 eprintln!("htl test: no test files found (looked for .tl files that require(\"{lib}\"))");
1684 return Ok(ExitCode::FAILURE);
1685 }
1686 let opts = project::TestOptions {
1687 config: &cfg,
1688 model: model.as_ref(),
1689 lint,
1690 lib,
1691 filter,
1692 run: htl::testing::RunOptions {
1693 fail_fast: flags.fail_fast,
1694 update_snapshots: flags.update,
1695 coverage: flags.coverage,
1696 seed: flags.seed,
1698 async_: Default::default(),
1700 lang: Default::default(),
1701 },
1702 cache: project::cache_options(
1703 !flags.no_cache,
1704 Some(cache::Mode::PerModule),
1705 &cfg,
1706 flags.explain,
1707 ),
1708 };
1709
1710 let mut sink = project::Sink::new(report::Out::new(flags.json));
1711 let mut json_files: Vec<report::TestFile> = Vec::new();
1712 let mut junit_suites: Vec<junit::Suite> = Vec::new();
1713 let rep = project::test(&mut sink, &files, &opts, &mut |rep, sink| {
1717 if flags.json {
1718 json_files.push(report::TestFile::from_report(rep, sink.out().take()));
1719 }
1720 if flags.junit.is_some() {
1721 junit_suites.push(junit_suite(rep));
1722 }
1723 let f = htl::diagnostic::display_path(&rep.path);
1725 let tag = if rep.ok() { "ok " } else { "FAIL" };
1726 let detail = if !rep.check.ok() {
1727 "type check failed".to_string()
1728 } else if let Some(e) = &rep.error {
1729 format!("error: {}", e.lines().next().unwrap_or_default())
1732 } else if rep.file_level {
1733 "ran to completion (no test library used)".to_string()
1734 } else {
1735 format!("{} passed, {} failed", rep.passed, rep.failed)
1736 };
1737 let show_file = !flags.json && (!flags.quiet || !rep.ok());
1740 if show_file {
1741 eprintln!("{tag} {f} ({detail}, {:.0} ms)", rep.duration_ms);
1742 if let Some(e) = &rep.error {
1743 for line in e.lines().skip(1) {
1744 eprintln!(" {line}");
1745 }
1746 }
1747 }
1748 for tr in &rep.tests {
1749 let slow = flags.slow.is_some_and(|ms| tr.ms >= ms);
1750 if !flags.json && (flags.verbose || slow) {
1751 if flags.quiet && !show_file {
1752 eprintln!("slow {f} {} ({:.1} ms)", tr.name, tr.ms);
1754 continue;
1755 }
1756 let mark = if tr.ok { "ok " } else { "FAIL" };
1757 let note = if slow && !flags.verbose {
1758 " [slow]"
1759 } else {
1760 ""
1761 };
1762 eprintln!(" {mark} {} ({:.1} ms){note}", tr.name, tr.ms);
1763 }
1764 }
1765 if !flags.json {
1766 for m in &rep.failures {
1767 eprintln!(" - {m}");
1768 }
1769 for p in &rep.snapshots_written {
1771 eprintln!(
1772 "snapshot written: {}",
1773 htl::diagnostic::display_path(Path::new(p))
1774 );
1775 }
1776 for p in &rep.snapshots_updated {
1777 eprintln!(
1778 "snapshot updated: {}",
1779 htl::diagnostic::display_path(Path::new(p))
1780 );
1781 }
1782 }
1783 })?;
1784
1785 if let Some(out) = &flags.junit {
1786 std::fs::write(out, junit::document(&junit_suites, rep.duration_ms))
1789 .with_context(|| format!("writing {}", out.display()))?;
1790 }
1791 if let (Some(out), Some(cov)) = (&flags.lcov, &rep.coverage) {
1792 let root = match &cfg {
1795 Some((dir, _, _)) => dir.clone(),
1796 None => std::env::current_dir()?,
1797 };
1798 let root = std::fs::canonicalize(&root).unwrap_or(root);
1799 std::fs::write(out, cov.lcov(&root))
1800 .with_context(|| format!("writing {}", out.display()))?;
1801 }
1802 let ok = rep.ok();
1803 if flags.json {
1804 let summary = report::TestSummary {
1805 files: rep.files.len(),
1806 files_run: rep.ran,
1807 passed: rep.passed,
1808 failed: rep.failed,
1809 files_with_errors: rep.files_with_errors,
1810 replayed: rep.replayed,
1811 duration_ms: rep.duration_ms,
1812 ok,
1813 seed: rep.seed,
1814 };
1815 report::emit(&report::TestReport {
1816 files: json_files,
1817 summary,
1818 coverage: rep.coverage,
1819 })?;
1820 } else {
1821 if let Some(cov) = &rep.coverage {
1822 print_coverage(cov, flags.coverage_lines);
1823 if let Some(out) = &flags.lcov {
1824 eprintln!("coverage: lcov written to {}", out.display());
1825 }
1826 }
1827 eprintln!(
1828 "htl test: {} file(s), {} passed, {} failed, {} file(s) with errors{}{} ({:.0} ms)",
1829 rep.ran,
1830 rep.passed,
1831 rep.failed,
1832 rep.files_with_errors,
1833 if rep.skipped() > 0 {
1834 format!(", {} file(s) not run (--fail-fast)", rep.skipped())
1835 } else {
1836 String::new()
1837 },
1838 if rep.replayed > 0 {
1840 format!(", {} checked from cache", rep.replayed)
1841 } else {
1842 String::new()
1843 },
1844 rep.duration_ms
1845 );
1846 eprintln!(
1849 "htl test: seed {} (repeat with --seed {})",
1850 rep.seed, rep.seed
1851 );
1852 }
1853 Ok(if ok {
1854 ExitCode::SUCCESS
1855 } else {
1856 ExitCode::FAILURE
1857 })
1858}
1859
1860fn report_patched(paths: &[PathBuf]) {
1866 let roots: Vec<PathBuf> = paths
1867 .iter()
1868 .filter_map(|p| std::fs::canonicalize(p).ok())
1869 .collect();
1870 let cwd = std::env::current_dir().unwrap_or_default();
1871 let Some(project) = paths
1874 .first()
1875 .and_then(|p| htl::model::Project::find_root(p).ok().flatten())
1876 .map(|r| r.root)
1877 .filter(|r| r.join(htl::pkg::MANIFEST_NAME).is_file())
1878 .map(|r| htl::pkg::MluaProject::at(&r))
1879 else {
1880 return;
1881 };
1882 for p in &project.patches {
1883 let canon = std::fs::canonicalize(&p.dir).unwrap_or_else(|_| p.dir.clone());
1884 if !roots.iter().any(|r| canon.starts_with(r)) {
1885 continue;
1886 }
1887 let shown = p.dir.strip_prefix(&cwd).unwrap_or(&p.dir);
1888 eprintln!(" patched {} ({})", shown.display(), p.name);
1889 }
1890}
1891
1892fn load_config(first: &Path) -> Result<project::Config> {
1904 let cfg = project::config_of(first)?;
1905 if let Some((_, path, c)) = &cfg {
1906 htl::config::check_toolchain(c, path, env!("CARGO_PKG_VERSION"))?;
1907 }
1908 Ok(cfg)
1909}
1910
1911fn warn_cargo_htl_mismatch(first: &Path) {
1938 let Some(root) = htl::dts::find_cargo_package_root(first) else {
1939 return;
1940 };
1941 let Ok(text) = fs::read_to_string(root.join("Cargo.toml")) else {
1942 return;
1943 };
1944 let Ok(manifest) = text.parse::<toml::Table>() else {
1945 return;
1946 };
1947 let Some(dep) = manifest.get("dependencies").and_then(|d| d.get("htl")) else {
1948 return;
1949 };
1950 let req_text = match dep {
1951 toml::Value::String(s) => s.as_str(),
1952 toml::Value::Table(t) => match t.get("version").and_then(toml::Value::as_str) {
1953 Some(s) => s,
1954 None => return,
1955 },
1956 _ => return,
1957 };
1958 let running = env!("CARGO_PKG_VERSION");
1959 let (Ok(req), Ok(version)) = (
1960 semver::VersionReq::parse(req_text),
1961 semver::Version::parse(running),
1962 ) else {
1963 return;
1964 };
1965 if req.matches(&version) {
1966 return;
1967 }
1968 eprintln!(
1971 "htl {running}; Cargo.toml asks for htl {req_text} — the crate and the CLI are \
1972 meant to move together (cargo install htl-cli --version {req_text}, or bump the \
1973 dependency)"
1974 );
1975}
1976
1977fn cmd_fmt(paths: &[PathBuf], check: bool, indent: Option<usize>) -> Result<ExitCode> {
1978 let paths = if paths.is_empty() {
1979 vec![PathBuf::from(".")]
1980 } else {
1981 paths.to_vec()
1982 };
1983 let cfg = load_config(&paths[0])?;
1984 let indent = indent
1985 .or_else(|| cfg.as_ref().and_then(|(_, _, c)| c.fmt.indent))
1986 .unwrap_or(3);
1987 let h = Htl::new()?;
1988 config_lang(&h, &cfg)?;
1989 let model = project::model_of(&cfg, &paths[0])?;
1993 let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Own);
1994 let files = htl::collect_tl_skipping(&paths, &skip)?;
1995 let (mut changed, mut failed) = (0usize, 0usize);
1996 for f in &files {
1997 let before = fs::read_to_string(f).with_context(|| format!("reading {}", f.display()))?;
1998 let after = match h.format_file(f, indent) {
1999 Ok(s) => s,
2000 Err(e) => {
2001 eprintln!("error: {e}");
2002 failed += 1;
2003 continue;
2004 }
2005 };
2006 if after != before {
2007 changed += 1;
2008 if check {
2009 eprintln!("would reformat: {}", f.display());
2010 } else {
2011 fs::write(f, after).with_context(|| format!("writing {}", f.display()))?;
2012 eprintln!("reformatted: {}", f.display());
2013 }
2014 }
2015 }
2016 eprintln!(
2017 "htl fmt: {} file(s), {} {}, {} failed",
2018 files.len(),
2019 changed,
2020 if check { "would change" } else { "reformatted" },
2021 failed
2022 );
2023 let fail = failed > 0 || (check && changed > 0);
2024 Ok(if fail {
2025 ExitCode::FAILURE
2026 } else {
2027 ExitCode::SUCCESS
2028 })
2029}
2030
2031struct FixFlags {
2032 rule: Vec<String>,
2033 unsafe_fixes: bool,
2034 dry_run: bool,
2035 diff: bool,
2036 allow_dirty: bool,
2037 allow_no_vcs: bool,
2038 exit_non_zero_on_fix: bool,
2039 json: bool,
2040}
2041
2042fn cmd_fix(paths: &[PathBuf], flags: FixFlags) -> Result<ExitCode> {
2043 use htl::fix::{FixOptions, fix_file, git_dirty, unified_diff};
2044 let paths = if paths.is_empty() {
2045 vec![PathBuf::from(".")]
2046 } else {
2047 paths.to_vec()
2048 };
2049 let cfg = load_config(&paths[0])?;
2050 auto_dts_to(&paths[0], !flags.dry_run)?;
2052 let model = project::model_of(&cfg, &paths[0])?;
2053 let scope = project::Scope::new(&cfg, model.as_ref(), &paths[0], None)?;
2056 let h = project::checker(
2057 model.as_ref(),
2058 scope.lints.selection(),
2059 &project::lang_of(&cfg),
2060 )?;
2061 let opts = FixOptions {
2062 unsafe_fixes: flags.unsafe_fixes,
2063 promoted: cfg
2064 .as_ref()
2065 .map(|(_, _, c)| c.fix.unsafe_.clone())
2066 .unwrap_or_default(),
2067 disabled: cfg
2068 .as_ref()
2069 .map(|(_, _, c)| c.fix.disable.clone())
2070 .unwrap_or_default(),
2071 only: flags.rule.clone(),
2072 dry_run: flags.dry_run,
2073 };
2074 opts.validate()?;
2077 let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Own);
2081 let (files, outside) = project::held_by_modules(
2083 model.as_ref(),
2084 &paths,
2085 htl::collect_tl_skipping(&paths, &skip)?,
2086 )?;
2087 if let Some(note) = project::outside_modules_note(model.as_ref(), &outside, "fixed") {
2088 eprintln!("htl fix: {note}");
2089 }
2090 let check_skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Check);
2094 let checked_only: Vec<PathBuf> = project::held_by_modules(
2095 model.as_ref(),
2096 &paths,
2097 htl::collect_tl_skipping(&paths, &check_skip)?,
2098 )?
2099 .0
2100 .into_iter()
2101 .filter(|f| !files.contains(f))
2102 .collect();
2103
2104 if !flags.dry_run {
2106 let mut dirty = Vec::new();
2107 let mut no_vcs = Vec::new();
2108 for f in &files {
2109 match git_dirty(f)? {
2110 Some(true) => dirty.push(f.clone()),
2111 Some(false) => {}
2112 None => no_vcs.push(f.clone()),
2113 }
2114 }
2115 if !dirty.is_empty() && !flags.allow_dirty {
2116 bail!(
2117 "htl fix rewrites files in place and relies on git to undo it; these have uncommitted changes:\n {}\ncommit or stash them, or pass --allow-dirty",
2118 dirty
2119 .iter()
2120 .map(|p| p.display().to_string())
2121 .collect::<Vec<_>>()
2122 .join("\n ")
2123 );
2124 }
2125 if !no_vcs.is_empty() && !flags.allow_no_vcs {
2126 bail!(
2127 "htl fix rewrites files in place and relies on git to undo it; these are not in a git repository:\n {}\npass --allow-no-vcs to proceed without that safety net",
2128 no_vcs
2129 .iter()
2130 .map(|p| p.display().to_string())
2131 .collect::<Vec<_>>()
2132 .join("\n ")
2133 );
2134 }
2135 }
2136
2137 let mut sink = project::Sink::new(report::Out::new(flags.json));
2138 sink.judge_by(scope.lints.selection());
2140 let origins = project::Origins::new(model.as_ref());
2144 let walk = scope.walk(&cfg, model.as_ref(), &origins);
2145 let walked: Vec<PathBuf> = files.iter().chain(&checked_only).cloned().collect();
2146 sink.walking(&walked);
2147 let (mut applied, mut skipped, mut json_files) = (Vec::new(), Vec::new(), Vec::new());
2148 let (mut changed, mut deferred, mut reverted) = (0usize, 0usize, 0usize);
2149 let mut found = htl::verdict::Findings::default();
2150 let mut infos: Vec<(PathBuf, htl::CheckInfo)> = Vec::with_capacity(walked.len());
2151 for f in &files {
2152 let saved = h.search_path()?;
2155 project::file_view(&h, model.as_ref(), f)?;
2156 let before = if flags.diff {
2157 std::fs::read_to_string(f).ok()
2158 } else {
2159 None
2160 };
2161 let out = fix_file(&h, f, &opts)?;
2162 if out.contents.is_some() {
2163 changed += 1;
2164 }
2165 deferred += out.deferred;
2166 if out.reverted.is_some() {
2167 reverted += 1;
2168 }
2169 if !flags.json {
2170 for a in &out.applied {
2171 eprintln!(
2172 "fixed: {}:{}: {} ({}{})",
2173 project::display_path(f),
2174 a.line,
2175 a.rule,
2176 a.applicability.as_str(),
2177 if flags.dry_run { ", not written" } else { "" }
2178 );
2179 }
2180 for s in &out.skipped {
2181 eprintln!(
2182 "skipped: {}:{}: {} — {}",
2183 project::display_path(f),
2184 s.line,
2185 s.rule,
2186 s.reason
2187 );
2188 }
2189 if let Some(r) = &out.reverted {
2190 eprintln!("reverted: {}: {r}", project::display_path(f));
2191 }
2192 if let Some(o) = &out.oscillation {
2193 eprintln!(
2194 "stopped: {}: fixes of {o} undo each other",
2195 project::display_path(f)
2196 );
2197 }
2198 if out.deferred > 0 {
2199 eprintln!(
2200 "deferred: {}: {} edit(s) overlapped applied ones; run htl fix again",
2201 project::display_path(f),
2202 out.deferred
2203 );
2204 }
2205 if flags.diff
2206 && let (Some(b), Some(a)) = (&before, &out.contents)
2207 {
2208 print!("{}", unified_diff(&project::display_path(f), b, a));
2209 }
2210 if flags.diff
2213 && let (Some(b), Some(s)) = (
2214 out.contents.as_ref().or(before.as_ref()),
2215 out.suggested.as_ref(),
2216 )
2217 {
2218 print!(
2219 "{}",
2220 unified_diff(&format!("{} (suggested)", project::display_path(f)), b, s)
2221 );
2222 }
2223 }
2224 found.lints += project::file_findings(&h, &mut sink, f, &out.check, &walk)?;
2227 h.set_search_path(&saved)?;
2228 found.errors += out.check.errors.len();
2229 found.warnings += out.check.warnings.len();
2230 infos.push((f.clone(), out.check.clone()));
2231 if flags.json {
2232 applied.extend(out.applied.iter().map(|a| report::FixApplied {
2233 file: project::display_path(f),
2234 line: a.line,
2235 rule: a.rule.clone(),
2236 applicability: a.applicability.as_str(),
2237 pass: a.pass,
2238 }));
2239 skipped.extend(out.skipped.iter().map(|s| report::FixSkipped {
2240 file: project::display_path(f),
2241 line: s.line,
2242 rule: s.rule.clone(),
2243 reason: s.reason.clone(),
2244 }));
2245 json_files.push(report::FixFile {
2246 path: project::display_path(f),
2247 changed: out.contents.is_some(),
2248 deferred: out.deferred,
2249 reverted: out.reverted.clone(),
2250 oscillation: out.oscillation.clone(),
2251 diagnostics: sink.out().take(),
2252 });
2253 }
2254 }
2255 for f in &checked_only {
2257 let m = project::check_one(&h, &mut sink, f, &walk)?;
2258 found.errors += m.errors;
2259 found.warnings += m.warnings;
2260 found.lints += m.lints;
2261 infos.push((f.clone(), m.requires_only()));
2262 if flags.json {
2263 json_files.push(report::FixFile {
2264 path: project::display_path(f),
2265 changed: false,
2266 deferred: 0,
2267 reverted: None,
2268 oscillation: None,
2269 diagnostics: sink.out().take(),
2270 });
2271 }
2272 }
2273 let mut whole = scope.whole(&cfg, model.as_ref());
2276 whole.publish = !flags.dry_run;
2277 let w = project::project_findings(&mut sink, &whole, &infos);
2278 found.errors += w.errors;
2279 found.lints += w.lints;
2280 let project_diagnostics = if flags.json {
2281 sink.out().take()
2282 } else {
2283 Vec::new()
2284 };
2285 found.errors += sink.dependency_error_count();
2288 found.denied = sink.denied();
2289 let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
2291 let errors_remaining = found.errors;
2292 let n_applied = if flags.json { applied.len() } else { 0 };
2293 let fail =
2294 htl::verdict::verdict(&found, &policy) || (flags.exit_non_zero_on_fix && changed > 0);
2295 if flags.json {
2296 let n_skipped = skipped.len();
2297 report::emit(&report::FixReport {
2298 dry_run: flags.dry_run,
2299 applied,
2300 skipped,
2301 files: json_files,
2302 summary: report::FixSummary {
2303 files: files.len(),
2304 files_changed: changed,
2305 applied: n_applied,
2306 skipped: n_skipped,
2307 deferred,
2308 reverted,
2309 errors_remaining,
2310 warnings: found.warnings,
2311 lints: found.lints,
2312 denied: found.denied,
2313 strict: policy.strict,
2314 ok: !fail,
2315 },
2316 diagnostics: project_diagnostics,
2317 })?;
2318 } else {
2319 let judged = judged(&found, &policy);
2323 eprintln!(
2324 "htl fix: {} file(s){}, {} changed{}, {} error(s) remaining{}{}",
2325 files.len(),
2326 if checked_only.is_empty() {
2327 String::new()
2328 } else {
2329 format!(" + {} checked in patched dependencies", checked_only.len())
2330 },
2331 changed,
2332 if flags.dry_run {
2333 " (dry run, nothing written)"
2334 } else {
2335 ""
2336 },
2337 errors_remaining,
2338 judged,
2339 if deferred > 0 {
2340 format!(", {deferred} deferred")
2341 } else {
2342 String::new()
2343 }
2344 );
2345 }
2346 Ok(if fail {
2347 ExitCode::FAILURE
2348 } else {
2349 ExitCode::SUCCESS
2350 })
2351}
2352
2353fn cmd_check(paths: &[PathBuf], lint: Option<&str>, flags: CheckFlags) -> Result<ExitCode> {
2365 let CheckFlags {
2366 list_lints,
2367 json,
2368 use_cache,
2369 cache_mode,
2370 explain,
2371 ..
2372 } = flags;
2373 let mut sink = project::Sink::new(report::Out::new(json));
2374 let paths = if paths.is_empty() {
2375 vec![PathBuf::from(".")]
2376 } else {
2377 paths.to_vec()
2378 };
2379 if list_lints {
2384 let width = htl::lint::rule_names()
2389 .iter()
2390 .map(|r| r.len())
2391 .max()
2392 .unwrap_or(0);
2393 for (name, level) in htl::lint::rule_defaults() {
2394 println!("{name:width$} {level}");
2395 }
2396 return Ok(ExitCode::SUCCESS);
2397 }
2398 let cfg = load_config(&paths[0])?;
2400 warn_cargo_htl_mismatch(&paths[0]);
2401 let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), flags.strict);
2402 if let Some(first) = paths.first() {
2406 auto_dts(first)?;
2407 }
2408 let walk_model = project::model_of(&cfg, &paths[0])?;
2413 if walk_model.is_none()
2418 && let Some(dir) = paths.iter().find(|p| p.is_dir())
2419 {
2420 bail!(
2421 "no {} or {} in {} or any directory above it: a directory is checked as a \
2422 project. Run `htl init` there to make it one, or name the .tl files to check \
2423 each on its own",
2424 htl::config::CONFIG_NAME,
2425 htl::pkg::MANIFEST_NAME,
2426 fs::canonicalize(dir)
2429 .unwrap_or_else(|_| dir.clone())
2430 .display()
2431 );
2432 }
2433 let skip = project::not_walked(walk_model.as_ref(), &paths, htl::model::Purpose::Check);
2434 let (files, outside) = project::held_by_modules(
2435 walk_model.as_ref(),
2436 &paths,
2437 htl::collect_tl_skipping(&paths, &skip)?,
2438 )?;
2439 if let Some(note) = project::outside_modules_note(walk_model.as_ref(), &outside, "checked") {
2440 eprintln!("htl check: {note}");
2441 }
2442 if !json {
2443 report_patched(&paths);
2444 }
2445
2446 let rep = project::check(
2450 &mut sink,
2451 &files,
2452 &project::Options {
2453 paths: &paths,
2454 config: &cfg,
2455 model: walk_model.as_ref(),
2456 lint,
2457 cache: project::cache_options(use_cache, cache_mode, &cfg, explain),
2458 },
2459 )?;
2460 let fail = report_check(
2461 sink.out(),
2462 json,
2463 &rep,
2464 &policy,
2465 patched_files(walk_model.as_ref(), &rep.files),
2466 )?;
2467 Ok(if fail {
2468 ExitCode::FAILURE
2469 } else {
2470 ExitCode::SUCCESS
2471 })
2472}
2473
2474fn cmd_unused(paths: &[PathBuf], flags: UnusedFlags) -> Result<ExitCode> {
2481 let UnusedFlags {
2482 json,
2483 fail_on_unused,
2484 use_cache,
2485 explain,
2486 } = flags;
2487 let paths = if paths.is_empty() {
2488 vec![PathBuf::from(".")]
2489 } else {
2490 paths.to_vec()
2491 };
2492 let cfg = load_config(&paths[0])?;
2493 auto_dts(&paths[0])?;
2496 let model = project::model_of(&cfg, &paths[0])?;
2497 let rep = htl::unused::unused(&htl::unused::Options {
2498 paths: &paths,
2499 config: &cfg,
2500 model: model.as_ref(),
2501 cache: project::cache_options(use_cache, None, &cfg, explain),
2502 })?;
2503 let s = &rep.summary;
2504 if json {
2505 report::emit(&rep)?;
2506 } else if s.no_entry {
2507 eprintln!(
2508 "htl unused: nothing to start from, so nothing is reported. An entry is \
2509 main.tl in the source root ([layout] source) or beside the manifest, a test file, a module under a [[contract]] directory, or a name \
2510 in [build] extra / host"
2511 );
2512 } else {
2513 if s.check_errors > 0 {
2514 eprintln!(
2515 "htl unused: {} error(s) in the check this graph comes from — what those \
2516 files require is missing from it (htl check)",
2517 s.check_errors
2518 );
2519 }
2520 for m in &rep.modules {
2521 match &m.module {
2522 Some(n) => eprintln!("module: {} ({n})", m.path),
2523 None => eprintln!("module: {}", m.path),
2524 }
2525 }
2526 for d in &rep.dependencies {
2527 eprintln!("dependency: {}", d.name);
2528 }
2529 eprintln!(
2530 "htl unused: {}, {} [{} considered, {} reached, {}]",
2531 count(s.modules, "module", "modules"),
2532 count(s.dependencies, "dependency", "dependencies"),
2533 s.considered,
2534 s.reached,
2535 count(s.entries, "entry", "entries"),
2536 );
2537 }
2538 Ok(if fail_on_unused && !s.ok {
2539 ExitCode::FAILURE
2540 } else {
2541 ExitCode::SUCCESS
2542 })
2543}
2544
2545fn cmd_resolve(module: &str, path: Option<&Path>, json: bool) -> Result<ExitCode> {
2554 let start = path.unwrap_or(Path::new("."));
2555 let cfg = load_config(start)?;
2556 auto_dts(start)?;
2560 let h = Htl::new()?;
2561 h.install_std()?;
2564 let model = apply_model(&h, &cfg, start)?;
2565 let rep = htl::resolve::resolve(
2566 &h,
2567 module,
2568 cfg.as_ref().map(|(r, _, _)| r.as_path()),
2569 model.as_ref(),
2570 )?;
2571 if json {
2572 report::emit(&rep)?;
2573 } else {
2574 print_resolution(&rep);
2575 }
2576 Ok(if rep.summary.ok {
2577 ExitCode::SUCCESS
2578 } else {
2579 ExitCode::FAILURE
2580 })
2581}
2582
2583fn print_resolution(r: &htl::resolve::Resolution) {
2587 let ambiguous = r
2588 .candidates
2589 .iter()
2590 .any(|c| c.status == htl::resolve::Status::Ambiguous);
2591 let typed_by = r
2595 .read
2596 .as_ref()
2597 .map(|d| format!(", typed by {d}"))
2598 .unwrap_or_default();
2599 match (&r.read, &r.error, &r.provided_by) {
2600 (_, Some(e), _) => println!("htl resolve {}: error: {e}", r.module),
2601 (Some(read), None, Some(by)) if r.provider == Some(htl::model::Provider::Declared) => {
2605 println!(
2606 "htl resolve {}: {read}, provided by the environment ({by})",
2607 r.module
2608 )
2609 }
2610 (_, None, Some(by)) => {
2611 println!(
2612 "htl resolve {}: provided by the host ({by}){typed_by}",
2613 r.module
2614 )
2615 }
2616 (Some(read), None, None) => println!("htl resolve {}: {read}", r.module),
2617 (None, None, None) if ambiguous => println!(
2618 "htl resolve {}: more than one module implements it, and no order picks one",
2619 r.module
2620 ),
2621 (None, None, None) => println!(
2622 "htl resolve {}: nothing in the project or on the search path answers require(\"{}\")",
2623 r.module, r.module
2624 ),
2625 }
2626 if !r.candidates.is_empty() {
2627 let w = |f: fn(&htl::resolve::Candidate) -> usize, head: usize| {
2628 r.candidates.iter().map(f).max().unwrap_or(0).max(head)
2629 };
2630 let file = w(|c| c.path.len(), 4);
2631 let kind = w(|c| c.kind.as_str().len(), 4);
2632 let read_at = r
2633 .candidates
2634 .iter()
2635 .find(|c| c.status == htl::resolve::Status::Read)
2636 .map(|c| c.order);
2637 println!();
2638 println!(" order {:file$} {:kind$} status", "file", "kind");
2639 for c in &r.candidates {
2640 let status = match (c.status, c.shadowed_by, read_at) {
2641 (htl::resolve::Status::Shadowed, Some(by), _) => format!("shadowed by {by}"),
2642 (htl::resolve::Status::Runtime, _, Some(by)) => format!("runtime, typed by {by}"),
2644 (s, _, _) => s.as_str().to_string(),
2645 };
2646 let origin = match &c.origin {
2647 Some(o) => format!(" ({})", o.describe()),
2648 None => String::new(),
2649 };
2650 println!(
2651 " {:<5} {:file$} {:kind$} {status}{origin}",
2652 c.order,
2653 c.path,
2654 c.kind.as_str()
2655 );
2656 }
2657 }
2658 println!();
2659 match r.answered_by {
2660 htl::resolve::AnsweredBy::Model => println!(" answered by the project model"),
2661 htl::resolve::AnsweredBy::Path => {
2664 println!(" searched, in order: {}", r.searched.join(", "))
2665 }
2666 }
2667}
2668
2669fn count(n: usize, one: &str, many: &str) -> String {
2672 format!("{n} {}", if n == 1 { one } else { many })
2673}
2674
2675fn cmd_cache_clear(path: Option<&Path>) -> Result<ExitCode> {
2685 let root = cache_root(path)?;
2686 let dir = root.join(".htl").join("cache");
2687 if !dir.is_dir() {
2688 eprintln!("htl cache: nothing stored at {}", dir.display());
2689 return Ok(ExitCode::SUCCESS);
2690 }
2691 let n = fs::read_dir(&dir)
2692 .map(|rd| {
2693 rd.flatten()
2694 .filter(|e| e.path().extension().is_some_and(|x| x == "json"))
2695 .count()
2696 })
2697 .unwrap_or(0);
2698 fs::remove_dir_all(&dir).with_context(|| format!("removing {}", dir.display()))?;
2699 eprintln!(
2700 "htl cache: removed {n} {} from {}",
2701 if n == 1 { "entry" } else { "entries" },
2702 dir.display()
2703 );
2704 Ok(ExitCode::SUCCESS)
2705}
2706
2707fn cache_root(path: Option<&Path>) -> Result<PathBuf> {
2710 let start = path.unwrap_or(Path::new("."));
2711 let model = htl::model::Project::find_root(start)?.map(|r| r.root);
2712 Ok(model.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))))
2713}
2714
2715fn cmd_cache_status(path: Option<&Path>, json: bool, list_entries: bool) -> Result<ExitCode> {
2721 let c = cache::describe(&cache_root(path)?);
2722 if json {
2723 report::emit(&c)?;
2724 return Ok(ExitCode::SUCCESS);
2725 }
2726
2727 if c.entries.is_empty() {
2728 println!("htl cache: empty ({})", c.dir);
2729 return Ok(ExitCode::SUCCESS);
2730 }
2731 let mut by_kind: std::collections::BTreeMap<&str, (usize, u64)> = Default::default();
2732 for e in &c.entries {
2733 let slot = by_kind.entry(e.kind.as_str()).or_default();
2734 slot.0 += 1;
2735 slot.1 += e.bytes;
2736 }
2737 println!(
2738 "htl cache: {} entries, {} at {}",
2739 c.entries.len(),
2740 human_bytes(c.bytes),
2741 c.dir
2742 );
2743 for (kind, (n, bytes)) in &by_kind {
2744 let what = match *kind {
2745 cache::CHECK => "checked modules",
2746 cache::GEN => "checked and generated test files (htl test)",
2747 cache::MODULE => "generated modules (htl test / build, include_bundle!)",
2748 cache::RUN => "whole runs",
2749 _ => "entries this build cannot read",
2750 };
2751 println!(" {n:>5} {what} ({})", human_bytes(*bytes));
2752 }
2753 if let (Some(min), Some(max)) = (
2754 c.entries.iter().map(|e| e.age_secs).min(),
2755 c.entries.iter().map(|e| e.age_secs).max(),
2756 ) {
2757 println!(
2758 " last used between {} and {} ago",
2759 human_age(min),
2760 human_age(max)
2761 );
2762 }
2763 if list_entries {
2764 println!();
2765 for e in &c.entries {
2766 println!(
2767 " {:<6} {:>8} {}",
2768 e.kind,
2769 human_bytes(e.bytes),
2770 e.subjects.join(", ")
2771 );
2772 }
2773 }
2774 Ok(ExitCode::SUCCESS)
2775}
2776
2777fn human_bytes(n: u64) -> String {
2778 if n >= 1 << 20 {
2779 format!("{:.1} MB", n as f64 / (1u64 << 20) as f64)
2780 } else if n >= 1 << 10 {
2781 format!("{:.0} KB", n as f64 / (1u64 << 10) as f64)
2782 } else {
2783 format!("{n} B")
2784 }
2785}
2786
2787fn human_age(secs: u64) -> String {
2788 match secs {
2789 s if s < 90 => format!("{s}s"),
2790 s if s < 5400 => format!("{}m", s / 60),
2791 s if s < 172_800 => format!("{}h", s / 3600),
2792 s => format!("{}d", s / 86_400),
2793 }
2794}
2795
2796fn patched_files(model: Option<&htl::model::Project>, files: &[PathBuf]) -> usize {
2802 let Some(model) = model else {
2803 return 0;
2804 };
2805 if !model
2806 .modules
2807 .iter()
2808 .any(|m| m.owner == htl::model::Owner::Patched)
2809 {
2810 return 0;
2811 }
2812 files
2813 .iter()
2814 .filter(|f| {
2815 model
2816 .home_of(f)
2817 .is_some_and(|m| m.owner == htl::model::Owner::Patched)
2818 })
2819 .count()
2820}
2821
2822fn report_check(
2831 out: &mut report::Out,
2832 json: bool,
2833 rep: &project::Report,
2834 policy: &htl::verdict::Policy,
2835 patched: usize,
2836) -> Result<bool> {
2837 let (files, replayed) = (rep.files.len(), rep.replayed);
2838 let (errors, warnings, lints) = (rep.errors, rep.warnings, rep.lints);
2839 let strict = policy.strict;
2840 let fail = htl::verdict::verdict(&rep.findings(), policy);
2841 let all_cached = rep.all_cached();
2842 if json {
2843 report::emit(&report::CheckReport {
2844 files,
2845 patched,
2846 diagnostics: out.take(),
2847 summary: report::CheckSummary {
2848 errors,
2849 warnings,
2850 lints,
2851 denied: rep.denied,
2852 strict,
2853 ok: !fail,
2854 cached: all_cached,
2855 replayed,
2856 },
2857 })?;
2858 } else {
2859 let cached = if all_cached {
2861 " [cached]".to_string()
2862 } else if replayed > 0 {
2863 format!(" [{replayed}/{files} cached]")
2864 } else {
2865 String::new()
2866 };
2867 let denied = match rep.denied {
2872 0 => String::new(),
2873 n => format!(", {n} at deny"),
2874 };
2875 let counted = match patched {
2879 0 => format!("{files} file(s)"),
2880 n => format!("{} file(s) + {n} in patched dependencies", files - n),
2881 };
2882 eprintln!(
2883 "htl check: {counted}, {errors} error(s), {warnings} warning(s), {lints} lint(s){denied}{}{cached}",
2884 if strict { " [strict]" } else { "" }
2885 );
2886 }
2887 Ok(fail)
2888}
2889
2890fn cmd_gen(file: &Path, out: Option<&Path>) -> Result<ExitCode> {
2891 let h = Htl::new()?;
2892 auto_dts(file)?;
2893 let cfg = load_config(file)?;
2897 config_lints(&h, &cfg)?;
2898 config_lang(&h, &cfg)?;
2899 let model = apply_model(&h, &cfg, file)?;
2900 project::file_view(&h, model.as_ref(), file)?;
2901 h.install_std()?;
2902 let (code, c) = h.gen_lua(file)?;
2903 text_sink().checkinfo(&c);
2904 let (Some(code), false) = (code, runs_fail(&c)) else {
2907 return Ok(ExitCode::FAILURE);
2908 };
2909 match out {
2913 Some(p) => fs::write(p, code).with_context(|| format!("writing {}", p.display()))?,
2914 None => print!("{code}"),
2915 }
2916 Ok(ExitCode::SUCCESS)
2917}
2918
2919fn cmd_run(file: &Path, args: &[String]) -> Result<ExitCode> {
2920 let bytes = fs::read(file).with_context(|| format!("reading {}", file.display()))?;
2921 let h = Htl::new()?;
2922 auto_dts(file)?;
2923 let cfg = load_config(file)?;
2928 config_lints(&h, &cfg)?;
2929 config_lang(&h, &cfg)?;
2930 h.configure_async(async_config(&cfg))?;
2931 let model = apply_model(&h, &cfg, file)?;
2932 h.install_test_lib()?;
2933 h.install_std()?;
2934 h.install_task_lib()?;
2935 if Bundle::is_bundle(&bytes) {
2936 let b = Bundle::decode(&bytes)?;
2937 let main = h.load_bundle(&b, args)?;
2940 return run_root(&h, Entry::Main(main), args);
2941 }
2942 project::file_view(&h, model.as_ref(), file)?;
2944 h.install_searcher()?;
2945 h.set_arg(&file.to_string_lossy(), args)?;
2946 let (code, c) = h.gen_lua(file)?;
2947 text_sink().checkinfo(&c);
2948 let (Some(code), false) = (code, runs_fail(&c)) else {
2950 return Ok(ExitCode::FAILURE);
2951 };
2952 run_root(
2953 &h,
2954 Entry::Chunk {
2955 code,
2956 name: format!("@{}", file.display()),
2957 },
2958 args,
2959 )
2960}
2961
2962enum Entry {
2964 Chunk { code: String, name: String },
2965 Main(htl::mlua::Function),
2966}
2967
2968fn async_config(cfg: &project::Config) -> &htl::config::AsyncConfig {
2970 static DEFAULT: htl::config::AsyncConfig = htl::config::AsyncConfig {
2971 grace_ms: None,
2972 preempt: None,
2973 };
2974 match cfg {
2975 Some((_, _, c)) => &c.async_,
2976 None => &DEFAULT,
2977 }
2978}
2979
2980fn run_root(h: &Htl, entry: Entry, args: &[String]) -> Result<ExitCode> {
2997 let token = htl::mlua_isle::runtime::CancelToken::new();
2998 {
2999 let token = token.clone();
3000 std::thread::Builder::new()
3001 .name("htl-ctrl-c".into())
3002 .spawn(move || {
3003 let Ok(rt) = tokio::runtime::Builder::new_current_thread()
3004 .enable_all()
3005 .build()
3006 else {
3007 return;
3008 };
3009 rt.block_on(async {
3010 if tokio::signal::ctrl_c().await.is_ok() {
3011 token.cancel();
3012 }
3013 if tokio::signal::ctrl_c().await.is_ok() {
3014 std::process::exit(130);
3015 }
3016 });
3017 })
3018 .context("starting the Ctrl-C watcher")?;
3019 }
3020 let rt = tokio::runtime::Builder::new_current_thread()
3021 .enable_all()
3022 .build()
3023 .context("building the executor's runtime")?;
3024 let local = tokio::task::LocalSet::new();
3025 let out = local.block_on(&rt, async {
3026 match entry {
3027 Entry::Chunk { code, name } => h.run_async(&code, &name, args, &token).await,
3028 Entry::Main(main) => {
3029 let va: htl::mlua::Variadic<String> = args.iter().cloned().collect();
3030 h.call_async(main, va, &token).await.map(|_| ())
3031 }
3032 }
3033 });
3034 Ok(match out {
3035 Ok(()) => ExitCode::SUCCESS,
3036 Err(e) if htl::is_cancelled(&e) => {
3037 eprintln!("htl run: interrupted");
3038 ExitCode::from(130)
3039 }
3040 Err(e) => {
3041 eprintln!("{}", htl::developer_message(&e));
3042 ExitCode::FAILURE
3043 }
3044 })
3045}
3046
3047fn runs_fail(c: &htl::CheckInfo) -> bool {
3051 htl::verdict::verdict(
3052 &htl::verdict::Findings {
3053 errors: c.errors.len(),
3054 warnings: c.warnings.len(),
3055 lints: c.lints.len(),
3056 denied: 0,
3057 },
3058 &htl::verdict::Policy::ERRORS_ONLY,
3059 )
3060}
3061
3062struct BuildCache {
3067 use_cache: bool,
3068 explain: bool,
3069}
3070
3071fn cmd_build(
3072 entry: &Path,
3073 out: &Path,
3074 main: &str,
3075 mut opts: htl::link::LinkOptions,
3076 cache_flags: BuildCache,
3077) -> Result<ExitCode> {
3078 let h = Htl::new()?;
3079 auto_dts(entry)?;
3080 h.install_std()?;
3084 let cfg = load_config(entry)?;
3085 let lints = config_lints(&h, &cfg)?;
3088 config_lang(&h, &cfg)?;
3089 let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
3090 let model = apply_model(&h, &cfg, entry)?;
3091 if let Some(m) = &model {
3095 opts.host.extend(m.provided().map(|(n, _)| n.to_string()));
3096 }
3097 if let Some((_, _, cfg)) = &cfg {
3098 opts.extra.extend(cfg.build.extra.iter().cloned());
3099 opts.host.extend(cfg.build.host.iter().cloned());
3100 if let Some(t) = cfg.build.target.filter(|t| *t != BuildTarget::Hb) {
3106 eprintln!(
3107 "htl build: this project's target is `{}` — what runs its output is {} — and `htl build` writes a `.hb` bundle, which is the `hb` target. Build it with `cargo build`; to bundle its scripts anyway, drop `[build] target` from htl.toml.",
3108 t.name(),
3109 t.runs_it()
3110 );
3111 return Ok(ExitCode::FAILURE);
3112 }
3113 }
3114 if entry.is_dir() {
3115 if cache_flags.explain {
3116 eprintln!("htl cache: the directory form of `htl build` is not cached");
3117 }
3118 let skip = project::not_walked(
3121 model.as_ref(),
3122 &[entry.to_path_buf()],
3123 htl::model::Purpose::Check,
3124 );
3125 return cmd_build_dir(&h, entry, out, main, &opts, &skip, &cfg);
3126 }
3127 project::file_view(&h, model.as_ref(), entry)?;
3128 opts.entry_name = model.as_ref().and_then(|m| m.locate(entry).map(|p| p.name));
3131 let root = project::store_root(model.as_ref());
3137 let spec = cfg
3138 .as_ref()
3139 .map(|(_, _, c)| c.lint_spec())
3140 .unwrap_or_default();
3141 let lint = (!spec.is_empty()).then_some(spec.as_str());
3142 let cache_opts = project::cache_options(
3143 cache_flags.use_cache,
3144 Some(cache::Mode::PerModule),
3145 &cfg,
3146 cache_flags.explain,
3147 );
3148 let store = project::with_model(
3149 project::store(&root, cache_opts, None, "htl build"),
3150 model.as_ref(),
3151 );
3152 let link_store = store.as_ref().map(|c| htl::link::LinkStore {
3153 cache: c,
3154 lint,
3155 root: &root,
3156 config: cfg.as_ref().map(|(_, p, c)| (p.as_path(), c)),
3157 });
3158 let linked = htl::link::link_with(&h, entry, &opts, link_store)?;
3159 project::explain_cache(store.as_ref(), cache_opts);
3160 let mut sink = text_sink();
3163 sink.judge_by(lints.selection());
3164 for (p, c) in &linked.checks {
3165 sink.checkinfo(&linked.reported(p, c));
3166 }
3167 let n_err = linked.errors.len();
3168 let found = htl::verdict::Findings {
3169 errors: n_err,
3170 warnings: linked.checks.iter().map(|(_, c)| c.warnings.len()).sum(),
3171 lints: linked.lints.len(),
3172 denied: sink.denied(),
3173 };
3174 for d in &linked.link_errors {
3177 sink.diagnostic(d);
3178 }
3179 if htl::verdict::verdict(&found, &policy) {
3180 eprintln!(
3181 "htl build: {n_err} error(s){}, bundle not written",
3182 judged(&found, &policy)
3183 );
3184 return Ok(ExitCode::FAILURE);
3185 }
3186 let buf = linked.bundle()?.encode();
3187 fs::write(out, &buf).with_context(|| format!("writing {}", out.display()))?;
3188 let typed = linked.modules.iter().filter(|m| m.typed).count();
3189 let cached = if typed > 0 && linked.cached == typed {
3192 " [cached]".to_string()
3193 } else if linked.cached > 0 {
3194 format!(" [{}/{typed} cached]", linked.cached)
3195 } else {
3196 String::new()
3197 };
3198 eprintln!(
3199 "htl build: {} module(s) ({typed} typed, {} lua) -> {} ({} bytes{}{}){cached}",
3200 linked.modules.len(),
3201 linked.modules.len() - typed,
3202 out.display(),
3203 buf.len(),
3204 if opts.source {
3205 ", source"
3206 } else {
3207 ", bytecode"
3208 },
3209 if opts.debug { " with debug info" } else { "" }
3210 );
3211 if !linked.host_modules.is_empty() {
3212 eprintln!(
3213 "htl build: host must provide: {}",
3214 linked.host_modules.join(", ")
3215 );
3216 }
3217 Ok(ExitCode::SUCCESS)
3218}
3219
3220fn cmd_build_dir(
3222 h: &Htl,
3223 dir: &Path,
3224 out: &Path,
3225 entry: &str,
3226 opts: &htl::link::LinkOptions,
3227 skip: &[PathBuf],
3228 cfg: &project::Config,
3229) -> Result<ExitCode> {
3230 let lints = config_lints(h, cfg)?;
3232 config_lang(h, cfg)?;
3233 let policy = &htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
3234 h.add_path(dir)?;
3235 let files = htl::collect_tl_skipping(&[dir.to_path_buf()], skip)?;
3236 let mut b = Bundle {
3237 entry: entry.to_string(),
3238 htl_version: env!("CARGO_PKG_VERSION").into(),
3239 ..Default::default()
3240 };
3241 let mut found = htl::verdict::Findings::default();
3242 let mut sink = text_sink();
3243 sink.judge_by(lints.selection());
3244 for f in &files {
3245 let rel = f.strip_prefix(dir).unwrap_or(f);
3247 let name = htl::naming::name_of("", rel)
3248 .with_context(|| format!("cannot derive a module name for {}", f.display()))?;
3249 let (code, c) = h.gen_lua(f)?;
3250 sink.checkinfo(&c);
3251 found.errors += c.errors.len();
3252 found.warnings += c.warnings.len();
3253 found.lints += c.lints.len();
3254 let Some(code) = code else { continue };
3255 let (kind, payload) = if opts.source {
3256 (htl::bundle::Kind::Source, code.into_bytes())
3257 } else {
3258 (
3259 htl::bundle::Kind::Bytecode,
3260 h.compile_with(&name, &code, !opts.debug)?,
3261 )
3262 };
3263 b.modules.push(htl::bundle::Module {
3264 name,
3265 kind,
3266 payload,
3267 });
3268 }
3269 found.denied = sink.denied();
3270 if htl::verdict::verdict(&found, policy) {
3271 eprintln!(
3272 "htl build: {} error(s){}, bundle not written",
3273 found.errors,
3274 judged(&found, policy)
3275 );
3276 return Ok(ExitCode::FAILURE);
3277 }
3278 if b.module(entry).is_none() {
3279 bail!(
3280 "entry module '{entry}' not found among {} module(s)",
3281 b.modules.len()
3282 );
3283 }
3284 if !opts.source {
3285 b.fingerprint = h.fingerprint()?;
3286 }
3287 let buf = b.encode();
3288 fs::write(out, &buf).with_context(|| format!("writing {}", out.display()))?;
3289 eprintln!(
3290 "htl build: {} module(s) -> {} ({} bytes)",
3291 b.modules.len(),
3292 out.display(),
3293 buf.len()
3294 );
3295 Ok(ExitCode::SUCCESS)
3296}
3297
3298#[derive(serde::Serialize)]
3305struct BundleInfo {
3306 file: String,
3307 format: u8,
3308 htl_version: Option<String>,
3309 payload: &'static str,
3311 lua: Option<htl::bundle::LuaHeader>,
3312 entry: String,
3313 modules: Vec<BundleModuleInfo>,
3314 host_modules: Vec<String>,
3315}
3316
3317#[derive(serde::Serialize)]
3318struct BundleModuleInfo {
3319 name: String,
3320 kind: &'static str,
3321 bytes: usize,
3322}
3323
3324fn cmd_bundle_info(file: &Path, json: bool) -> Result<ExitCode> {
3325 let bytes = fs::read(file).with_context(|| format!("reading {}", file.display()))?;
3326 let Some(format) = htl::bundle::format_version(&bytes) else {
3327 bail!("{} is not an htl bundle (bad magic)", file.display());
3328 };
3329 let b = Bundle::decode(&bytes)?;
3330 let kind_name = |k: htl::bundle::Kind| match k {
3331 htl::bundle::Kind::Bytecode => "bytecode",
3332 htl::bundle::Kind::Source => "source",
3333 };
3334 let payload = match (
3335 b.modules
3336 .iter()
3337 .any(|m| m.kind == htl::bundle::Kind::Bytecode),
3338 b.modules
3339 .iter()
3340 .any(|m| m.kind == htl::bundle::Kind::Source),
3341 ) {
3342 (true, true) => "mixed",
3343 (false, true) => "source",
3344 _ => "bytecode",
3346 };
3347 let info = BundleInfo {
3348 file: file.display().to_string(),
3349 format,
3350 htl_version: (!b.htl_version.is_empty()).then(|| b.htl_version.clone()),
3351 payload,
3352 lua: htl::bundle::LuaHeader::parse(&b.fingerprint),
3353 entry: b.entry.clone(),
3354 modules: b
3355 .modules
3356 .iter()
3357 .map(|m| BundleModuleInfo {
3358 name: m.name.clone(),
3359 kind: kind_name(m.kind),
3360 bytes: m.payload.len(),
3361 })
3362 .collect(),
3363 host_modules: b.host_modules.clone(),
3364 };
3365 if json {
3366 report::emit(&info)?;
3367 return Ok(ExitCode::SUCCESS);
3368 }
3369
3370 println!(
3371 "{}: htl bundle, format {}, built by {}",
3372 info.file,
3373 info.format,
3374 match &info.htl_version {
3375 Some(v) => format!("htl {v}"),
3376 None => "htl (version not recorded)".to_string(),
3377 }
3378 );
3379 println!(" payload: {}", info.payload);
3380 let lua = match &info.lua {
3381 Some(h) => h.to_string(),
3382 None if !b.fingerprint.is_empty() => {
3383 format!("unreadable fingerprint ({} bytes)", b.fingerprint.len())
3384 }
3385 None if info.format == 1 => {
3386 "not recorded (a format 1 bundle carries no fingerprint)".into()
3387 }
3388 None => "any (source payload, no bytecode to bind)".into(),
3389 };
3390 println!(" lua: {lua}");
3391 println!(" entry: {}", info.entry);
3392 let modules: Vec<String> = info
3393 .modules
3394 .iter()
3395 .map(|m| {
3396 if info.payload == "mixed" {
3397 format!("{} ({})", m.name, m.kind)
3398 } else {
3399 m.name.clone()
3400 }
3401 })
3402 .collect();
3403 println!(
3404 " modules: {}",
3405 if modules.is_empty() {
3406 "(none)".to_string()
3407 } else {
3408 modules.join(", ")
3409 }
3410 );
3411 println!(
3412 " host: {}",
3413 if info.host_modules.is_empty() {
3414 "(none)".to_string()
3415 } else {
3416 info.host_modules.join(", ")
3417 }
3418 );
3419 Ok(ExitCode::SUCCESS)
3420}