Skip to main content

htl_cli/
lib.rs

1//! htl CLI. Also built as `cargo-htl` so `cargo htl <verb>` works.
2//!
3//! - `htl check <paths...>`   type-check files or directories
4//! - `htl gen <file.tl>`      emit readable Lua (escape hatch)
5//! - `htl run <file.tl|.hb>`  type-check then execute (strict: type errors abort)
6//! - `htl build <dir>`        compile a tree of `.tl` into one stripped-bytecode bundle
7//! - `htl bundle info <.hb>`  print what a bundle records, without running it
8
9use htl::cache;
10mod junit;
11pub mod report;
12mod scaffold;
13
14/// Output format of every command that has `--format`. One enum, so its help must be
15/// true of all of them: `check` / `test` / `fix` / `unused` print their JSON document on
16/// stdout and nothing on stderr, and their text form on stderr only, so the two never
17/// mix; `cache status` / `bundle info` / `resolve` are reports rather than runs, so both
18/// of their forms go to stdout. The exit code is the same in either form.
19#[derive(Clone, Copy, PartialEq, Eq, Debug, clap::ValueEnum)]
20enum Format {
21    /// Human-readable lines
22    Text,
23    /// One JSON document on stdout (README, "Machine-readable output":
24    /// https://github.com/ynishi/htl#machine-readable-output)
25    Json,
26}
27
28/// How `htl check` grains its cache. Separate from whether it caches at all, which is
29/// `--no-cache`.
30#[derive(Clone, Copy, PartialEq, Eq, Debug, clap::ValueEnum)]
31enum CacheModeArg {
32    /// One entry per module: an edit costs that module and what depends on it
33    PerModule,
34    /// One entry per run: any edit re-checks everything, a run with no edits reads one file
35    WholeRun,
36}
37
38#[derive(Subcommand)]
39enum TypesCmd {
40    /// Copy a library's declarations from teal-types into `types/`, and record which
41    /// commit they came from
42    Add {
43        /// The library as teal-types names it (`luasocket`, `lpeg`, …)
44        library: String,
45        /// Take them from a checkout of teal-types already on disk instead of fetching
46        #[arg(long)]
47        from: Option<PathBuf>,
48        /// Replace declarations that are already in `types/`
49        #[arg(long)]
50        force: bool,
51    },
52}
53
54#[derive(Subcommand)]
55enum PkgCmd {
56    /// Fetch every dependency `mlua-pkg.toml` declares and write `mlua-pkg.lock`
57    Install,
58    /// Write a dependency into `mlua-pkg.toml`; `install` is what fetches it
59    Add {
60        /// The name `require()` will use for it
61        name: String,
62        /// Remote git URL
63        git: String,
64        /// Pin to a tag: exact (`v1.0.0`), or a prefix that follows its patches (`v1.0`)
65        #[arg(long)]
66        tag: Option<String>,
67        /// Pin to a commit
68        #[arg(long)]
69        rev: Option<String>,
70        /// Track a branch (a build from it is not reproducible)
71        #[arg(long)]
72        branch: Option<String>,
73        /// The subdirectory `require()` resolves through, when it is not the one the
74        /// package declares
75        #[arg(long)]
76        entry: Option<PathBuf>,
77        /// Copy the package into this directory instead of linking it (manifest-relative,
78        /// rewritten by every install)
79        #[arg(long)]
80        target_dir: Option<PathBuf>,
81    },
82    /// Refresh dependencies, bump the pins that follow releases, then install
83    Update {
84        /// One dependency; every one when omitted
85        name: Option<String>,
86        /// Print the plan and write nothing
87        #[arg(long)]
88        dry_run: bool,
89        /// Bump exact tag pins too, to the highest release the remote has
90        #[arg(long)]
91        force: bool,
92    },
93    /// Remove cached packages the lockfile no longer refers to
94    Clean {
95        /// Remove the whole cache rather than the unreferenced entries
96        #[arg(long)]
97        all: bool,
98    },
99    /// Take a dependency's source into `patches/<dep>/`: a copy the project owns, edits
100    /// and commits, which install resolves the dependency from
101    Patch {
102        /// The dependency as `mlua-pkg.toml` names it
103        dep: String,
104        /// Refresh the copy even when it has uncommitted changes (they are discarded)
105        #[arg(long)]
106        force: bool,
107    },
108}
109
110#[derive(Subcommand)]
111enum CacheCmd {
112    /// Delete this project's stored check results
113    Clear {
114        /// A path inside the project; the store is .htl/cache at the project root, the
115        /// nearest htl.toml or mlua-pkg.toml above
116        path: Option<PathBuf>,
117    },
118    /// Report what the store holds
119    Status {
120        /// A path inside the project; the store is .htl/cache at the project root, the
121        /// nearest htl.toml or mlua-pkg.toml above
122        path: Option<PathBuf>,
123        /// Output format
124        #[arg(long, value_enum, default_value_t = Format::Text)]
125        format: Format,
126        /// List every entry rather than counting them by kind
127        #[arg(long)]
128        entries: bool,
129    },
130}
131
132#[derive(Subcommand)]
133enum BundleCmd {
134    /// Print what a bundle records: format, the htl that built it, payload kind, the Lua
135    /// its bytecode was compiled for, entry, modules, host-provided names
136    Info {
137        /// The `.hb` file
138        file: PathBuf,
139        /// Output format
140        #[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;
159// The project layer: which files a check walks, what it replays from the run cache and
160// what it keeps there, and which of a run's findings are said. Shared with `include_tl!`,
161// which asks the same of the same store.
162use 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    /// Type-check .tl files or directories (htl lints are reported as `lint:`)
195    ///
196    /// A module reached through `require` is checked with the file that required it, and
197    /// what has not changed is replayed from `.htl/`. README, "Lints":
198    /// https://github.com/ynishi/htl#lints-htl-check-include_tl
199    #[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        /// Treat every `warn` as `deny`: warnings and lints fail the run
215        #[arg(long)]
216        strict: bool,
217        /// Rule levels over the defaults, e.g. `nil-index=deny,no-any=warn` (`-rule` =
218        /// allow, `+rule` = warn)
219        // A spec that silences a rule starts with `-`, and without this clap reads
220        // `--lint -contract` as a cluster of short flags and answers `unexpected argument
221        // '-c'`: the documented way to turn one rule off never reached the spec parser.
222        #[arg(long, allow_hyphen_values = true)]
223        lint: Option<String>,
224        /// List every lint rule with its default level and exit
225        #[arg(long)]
226        list_lints: bool,
227        /// Output format
228        #[arg(long, value_enum, default_value_t = Format::Text)]
229        format: Format,
230        /// Check even if an identical run is cached, and do not store this one
231        #[arg(long)]
232        no_cache: bool,
233        /// How the cache is grained; overrides `[cache] mode` in htl.toml
234        #[arg(long, value_enum)]
235        cache_mode: Option<CacheModeArg>,
236        /// Say why the cache was not used, and what this run did with it
237        #[arg(long)]
238        explain_cache: bool,
239    },
240    /// Run tests: every `.tl` that requires the test library, one isolated state per file
241    ///
242    /// README, "Tests": https://github.com/ynishi/htl#tests
243    #[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        /// Only run tests whose "suite > name" contains this substring
254        #[arg(long)]
255        filter: Option<String>,
256        /// Assertion library module consulted for the verdict (must expose `run(filter)`)
257        #[arg(long, default_value = htl::testing::DEFAULT_LIB)]
258        lib: String,
259        /// Lint rules on top of the defaults, e.g. `+no-any`
260        #[arg(long, allow_hyphen_values = true)]
261        lint: Option<String>,
262        /// Stop at the first failure (within a file, and across files)
263        #[arg(long)]
264        fail_fast: bool,
265        /// Print every test with its time (default: only failures and files)
266        #[arg(short, long)]
267        verbose: bool,
268        /// Only failures (with details), errors, and the summary line; no per-file `ok` lines
269        #[arg(short, long, conflicts_with = "verbose")]
270        quiet: bool,
271        /// Also print tests slower than this many milliseconds
272        #[arg(long, value_name = "MS")]
273        slow: Option<f64>,
274        /// Rewrite snapshots (`to_match_snapshot`) that differ instead of failing
275        #[arg(long)]
276        update: bool,
277        /// Report executed statements per `.tl` module the tests reached (slower: a line hook)
278        #[arg(long)]
279        coverage: bool,
280        /// With --coverage, also list the unexecuted line ranges of each module
281        #[arg(long, requires = "coverage")]
282        coverage_lines: bool,
283        /// Also write the coverage as an lcov tracefile here (implies --coverage)
284        #[arg(long, value_name = "FILE")]
285        lcov: Option<PathBuf>,
286        /// Also write the run as a JUnit XML report here, for a CI that reads test results
287        #[arg(long, value_name = "FILE")]
288        junit: Option<PathBuf>,
289        /// Seed the random stream tests draw from; printed every run, so a failure repeats
290        #[arg(long)]
291        seed: Option<u64>,
292        /// Output format
293        #[arg(long, value_enum, default_value_t = Format::Text)]
294        format: Format,
295        /// Check and generate every file even if a cached form is available, and store none
296        #[arg(long)]
297        no_cache: bool,
298        /// Say why the cache was not used, and what this run did with it
299        #[arg(long)]
300        explain_cache: bool,
301    },
302    /// Apply the fixes diagnostics carry (safe ones by default)
303    ///
304    /// The working tree is the undo: a file git reports as modified or staged is refused
305    /// (`--allow-dirty`), and so is a file outside a repository (`--allow-no-vcs`), since a
306    /// rewrite git could not give back is one nobody can review. `--dry-run` reports
307    /// without writing; `--diff` prints a unified diff per file instead, and a `suggest`
308    /// fix — never applied — is listed as skipped and printed there as a second diff
309    /// headed `<file> (suggested)`. The `.d.tl` declarations the project publishes are
310    /// regenerated first, as `htl check` regenerates them; a dry run works them out and
311    /// writes none.
312    ///
313    /// README, "Fixing": https://github.com/ynishi/htl#fixing-htl-fix
314    #[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        /// Only fixes of these rules: any rule `htl check --list-lints` names, or
324        /// `forward-ref` / `tl:error`, the classes an error's fix is filed under
325        #[arg(long, value_delimiter = ',')]
326        rule: Vec<String>,
327        /// Also apply `unsafe` fixes (may change what the program does)
328        #[arg(long = "unsafe")]
329        unsafe_fixes: bool,
330        /// Compute and report, write nothing
331        #[arg(long)]
332        dry_run: bool,
333        /// Like --dry-run, and print a unified diff per file that would change
334        #[arg(long)]
335        diff: bool,
336        /// Fix files that git reports as modified or staged
337        #[arg(long)]
338        allow_dirty: bool,
339        /// Fix files that are not inside a git repository
340        #[arg(long)]
341        allow_no_vcs: bool,
342        /// Exit 1 when any file was changed (for CI)
343        #[arg(long)]
344        exit_non_zero_on_fix: bool,
345        /// Output format
346        #[arg(long, value_enum, default_value_t = Format::Text)]
347        format: Format,
348    },
349    /// Write the `.d.tl` files this project declares: those `#[host_module(dts = ..)]` /
350    /// `#[teal(dts = ..)]` ask for in a Rust crate, without building it, and the module
351    /// each `---@contract` type is declared in (check / run / test / build do both)
352    ///
353    /// Each declaration is reported as `wrote`, `unchanged`, or `not written`, one line
354    /// each. The commands that generate before they work (`check` / `run` / `test` /
355    /// `build` / `fix` / `unused` / `resolve` / `gen`) do the same job first and print only
356    /// what moved, prefixed `dts:` — `dts: wrote …`, `dts: not written: …`, `dts: left in
357    /// place: …` — and never an `unchanged` line; a dependency's declarations land under
358    /// `types/<crate>/` from every one of them. The exit code is about `not written` and
359    /// nothing else: non-zero when a declaration this command was asked to write could
360    /// not be written. A file already under `types/` that no dependency ships any more is
361    /// reported as `left in place` — nothing was asked for, nothing is deleted, and the
362    /// exit code does not move.
363    Dts {
364        /// Crate root or any path inside it (default: current directory)
365        dir: Option<PathBuf>,
366    },
367    /// Create a new Teal project directory
368    ///
369    /// README, "Layout of a project": https://github.com/ynishi/htl#layout-of-a-project-htl-new
370    #[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        /// Library only (no src/main.tl)
389        #[arg(long)]
390        lib: bool,
391        /// Also emit a Rust host: shorthand for --target bin
392        #[arg(long)]
393        embed: bool,
394        /// What will run this project's output: a target writes Cargo.toml + src/lib.rs
395        /// (the #[host_module], the embedded module, preload) and, when there is an entry
396        /// script, a thin src/main.rs
397        #[arg(long, value_name = "NAME", value_parser = clap::builder::PossibleValuesParser::new(scaffold::target_names()))]
398        target: Option<String>,
399        /// The htl the project depends on: `main`, or `path:<checkout>`. Without it, the
400        /// htl this binary was built with — its version on crates.io, or the checkout it
401        /// was built in
402        #[arg(long, value_name = "REQ")]
403        htl: Option<String>,
404        /// Leave the htlx (htl-x collections) dependency out of mlua-pkg.toml
405        #[arg(long)]
406        no_x: bool,
407    },
408    /// Fill in the scaffold files that are missing in an existing directory
409    Init {
410        dir: Option<PathBuf>,
411        #[arg(long)]
412        lib: bool,
413        /// Shorthand for --target bin
414        #[arg(long)]
415        embed: bool,
416        /// Fill in this target's files, and report the ones that were already there
417        #[arg(long, value_name = "NAME", value_parser = clap::builder::PossibleValuesParser::new(scaffold::target_names()))]
418        target: Option<String>,
419        /// The htl the project depends on: `main`, or `path:<checkout>`. Without it, the
420        /// htl this binary was built with — its version on crates.io, or the checkout it
421        /// was built in
422        #[arg(long, value_name = "REQ")]
423        htl: Option<String>,
424        /// Leave the htlx (htl-x collections) dependency out of mlua-pkg.toml
425        #[arg(long)]
426        no_x: bool,
427    },
428    /// Package management at the nearest `mlua-pkg.toml` project root: install / add /
429    /// update / clean / patch, through mlua-pkg's library rather than its binary
430    Pkg {
431        #[command(subcommand)]
432        cmd: PkgCmd,
433    },
434    /// Bring declarations for a library into `types/`
435    Types {
436        #[command(subcommand)]
437        cmd: TypesCmd,
438    },
439    /// Manage the check cache
440    ///
441    /// README, "Caching": https://github.com/ynishi/htl#caching
442    Cache {
443        #[command(subcommand)]
444        cmd: CacheCmd,
445    },
446    /// Format .tl files in place (indentation / blank lines / trailing whitespace)
447    Fmt {
448        paths: Vec<PathBuf>,
449        /// Do not write; exit 1 if any file would change
450        #[arg(long)]
451        check: bool,
452        /// Indent width in spaces (default: `[fmt] indent` in htl.toml, else 3)
453        #[arg(long)]
454        indent: Option<usize>,
455    },
456    /// Emit readable Lua for one .tl file (escape hatch)
457    Gen {
458        file: PathBuf,
459        #[arg(short, long)]
460        out: Option<PathBuf>,
461    },
462    /// Run a .tl script or a .hb bundle
463    Run {
464        file: PathBuf,
465        #[arg(trailing_var_arg = true)]
466        args: Vec<String>,
467    },
468    /// Compile a directory of .tl into a stripped-bytecode bundle
469    ///
470    /// A bundle is the `hb` target, so this refuses a project whose `[build] target` is
471    /// another one — a `bin` or a `cdylib` is built with `cargo build`.
472    ///
473    /// README, "Bundles": https://github.com/ynishi/htl#bundles-htl-build
474    #[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    /// The closure is judged by the project's `[lint.rules]` as `htl check` judges it — a
487    /// rule the project turned off is not reported here either — and no bundle is written
488    /// when it fails. A project whose `[build] target` is not `hb` is refused, naming the
489    /// command that does build it: the key records a decision the project made rather
490    /// than a note about itself, and dropping it from `htl.toml` is how a project with
491    /// Rust in it asks for a bundle anyway.
492    Build {
493        /// Entry `.tl` file: it and everything it requires are bundled, replaying from
494        /// the run cache what still holds (a directory bundles every `.tl` under it, the
495        /// older snapshot form, which is not cached)
496        entry: PathBuf,
497        #[arg(short, long, default_value = "app.hb")]
498        out: PathBuf,
499        /// Entry module name when `entry` is a directory
500        #[arg(short, long, default_value = "main")]
501        main: String,
502        /// Keep debug info (line numbers, local names) in the bytecode
503        #[arg(long)]
504        debug: bool,
505        /// Store generated Lua source instead of bytecode: for a big-endian target, a Lua
506        /// with non-default number types, or a bundle that must outlive a Lua upgrade
507        #[arg(long)]
508        source: bool,
509        /// Modules to bundle that only a dynamic require reaches (also `[build] extra`)
510        #[arg(long, value_delimiter = ',')]
511        extra: Vec<String>,
512        /// Modules the host provides, besides the project's own list (`#[host_module]`, `[build] host`, `std.*`; `.d.tl`-only modules are implied)
513        #[arg(long, value_delimiter = ',')]
514        host: Vec<String>,
515        /// Generate every module even if its cached form still holds, and do not store
516        /// (the directory form is never cached)
517        #[arg(long)]
518        no_cache: bool,
519        /// Say why the cache was not used, and what this run did with it
520        #[arg(long)]
521        explain_cache: bool,
522    },
523    /// Report what no entry's `require` closure reaches: modules, and `mlua-pkg.toml`
524    /// dependencies
525    ///
526    /// README, "Unused": https://github.com/ynishi/htl#unused-htl-unused
527    #[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        /// What to report on (default: the working directory). The walk is the project's
536        /// either way: a module reached only from `tests/` is reached
537        paths: Vec<PathBuf>,
538        /// Output format
539        #[arg(long, value_enum, default_value_t = Format::Text)]
540        format: Format,
541        /// Exit 1 when anything is reported (for CI)
542        #[arg(long)]
543        exit_non_zero_on_unused: bool,
544        /// Build the graph from a fresh check even if a cached one is available, and
545        /// store none
546        #[arg(long)]
547        no_cache: bool,
548        /// Say why the cache was not used, and what this run did with it
549        #[arg(long)]
550        explain_cache: bool,
551    },
552    /// Which file `require("<module>")` resolves to, and what that hides
553    ///
554    /// The whole chain in search order: what is read, what is reachable and not read, and
555    /// which crate or dependency each one came from. An override on the search path is the
556    /// mechanism working, and this is how to see it. README, "Which file a name resolves
557    /// to": https://github.com/ynishi/htl#which-file-a-name-resolves-to-htl-resolve
558    #[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        /// The module name a `require` would spell (`mq`, `socket.http`)
573        module: String,
574        /// A path inside the project (default: the working directory)
575        path: Option<PathBuf>,
576        /// Output format
577        #[arg(long, value_enum, default_value_t = Format::Text)]
578        format: Format,
579    },
580    /// Read a `.hb` bundle without running it
581    ///
582    /// README, "Bundles": https://github.com/ynishi/htl#bundles-htl-build
583    Bundle {
584        #[command(subcommand)]
585        cmd: BundleCmd,
586    },
587}
588
589/// The command line as clap holds it, before any argument is parsed.
590///
591/// The help is documentation, and the tests that keep it from rotting read it from here
592/// rather than from a copy kept beside them: they walk this tree and ask each command,
593/// argument and value for the strings it would print.
594pub fn command() -> clap::Command {
595    <Cli as clap::CommandFactory>::command()
596}
597
598/// Parse the command line and run the command; the exit code.
599///
600/// An exit of 1 is a verdict — an error in a file, a finding at `deny`, a failing test, a
601/// name that resolves to nothing ([`htl::verdict`]). A command that could not get as far
602/// as a verdict — a directory in no project, an `htl.toml` that does not parse, a file it
603/// cannot read, a flag it does not take — says why and exits 2, whichever command it was
604/// (clap exits 2 on a bad flag, and an `Err` from any command lands on the same arm).
605pub fn run() -> ExitCode {
606    // Invoked as `cargo htl ...` -> argv = ["cargo-htl", "htl", ...]; drop the "htl".
607    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
839/// The reporting layer, for the commands that report without a `--format` of their own.
840///
841/// `htl check` and `htl test` hand their findings to a [`project::Sink`] over a
842/// [`report::Out`]; `gen` / `run` / `build` used to loop `eprintln!` over the three
843/// vectors themselves, so the same finding read differently depending on which verb was
844/// typed. They come through here instead, and there is one renderer again.
845///
846/// One sink per command run, not per file: what the layer decides once per run — a
847/// dependency's error said once, and never on behalf of a file the run checks itself —
848/// is then once per *command*, which is what `htl build` over a closure of modules wants.
849fn text_sink() -> project::Sink<report::Out> {
850    project::Sink::new(report::Out::new(false))
851}
852
853/// What else a verdict counted besides errors, for a summary line: a finding at `deny`,
854/// and under `strict` every warning and lint. Empty when neither. Without it a run that
855/// failed on a lint would end in `0 error(s)`.
856fn 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
873/// The lint selection `htl.toml` puts in force, applied to `h`: the rules `htl check`
874/// reports under, at the levels it judges them by. A command that checks without it
875/// reports what the project turned off and judges nothing.
876/// Hand the checker the project's `[lang]`: whether `async` / `await` are keywords in the
877/// files it is about to read. Beside [`config_lints`] on every command that parses.
878fn 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
892/// (Re)generate the `.d.tl` files this project declares: the ones a Rust crate's
893/// `#[host_module]` / `#[derive(TealRecord)]` ask for, so the checker sees Rust-side
894/// modules before any `cargo build`, and the module each `---@contract` type is declared
895/// in, which is what an outside author writes their modules against. Quiet unless
896/// something was written.
897///
898/// A contract that could not be published is said here, on every command that generates:
899/// `htl check` reports it again as a lint (which `strict` makes fatal) and `htl dts`
900/// exits non-zero on it, but `run` / `test` / `build` have neither, and a declaration
901/// that is quietly not written is one an outside author finds missing later.
902fn auto_dts(start: &Path) -> Result<()> {
903    auto_dts_to(start, true)
904}
905
906/// [`auto_dts`], writing only when `write` is set. A dry run (`htl fix --dry-run` /
907/// `--diff`) works out every declaration the same way and says which it would write, and
908/// what it then checks is the declarations as they are on disk — which is why it says so.
909fn auto_dts_to(start: &Path, write: bool) -> Result<()> {
910    let mut project = None;
911    if let Some((root, _, cfg)) = load_config(start)? {
912        // The contracts the project's model holds, published from there.
913        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/// What `htl dts` has to say about the declarations its dependencies ship. Three kinds,
936/// kept apart because they mean three different things to the reader and to the exit code
937/// — which is the distinction the single `[htl <rule>]` suffix they all used to carry was
938/// hiding. None of them is a lint: no rule name, nothing to configure or silence, and
939/// nothing that reaches the `Sink`, so `htl check` neither counts one nor carries one in
940/// `--format json` — it prints them, prefixed `dts:`, exactly as this command does.
941#[derive(Default)]
942struct DepReport {
943    /// `(target, written)` pairs, in the shape the rest of the report prints.
944    written: Vec<(PathBuf, bool)>,
945    /// Asked for and not written. This, and only this, is what `htl dts` exits non-zero on.
946    not_written: Vec<String>,
947    /// Under `types/` and no longer shipped by anything. Reported, never deleted, and it
948    /// fails nothing: what the file is for is the project's to say.
949    left_in_place: Vec<String>,
950    /// The crate graph would not resolve, so nothing was asked for — cargo could not be
951    /// run, or the project's lockfile does not cover its manifest and the graph is read
952    /// with `--locked`. Either way the committed declarations stand and the check that
953    /// follows names the module if one is missing, which is why this fails nothing.
954    unresolved: Option<String>,
955}
956
957/// Materialise the declarations this project's dependencies ship, under
958/// `<declaration root>/<crate>/` — the declaration root of the project at `types_root`,
959/// `types/` unless its `htl.toml` says otherwise — and report on what happened to each.
960///
961/// The graph comes from `cargo metadata`, so this costs a subprocess on every command that
962/// generates. Nothing is built, and nothing is downloaded for dependencies already fetched.
963fn dep_dts(cargo_root: &Path, types_root: &Path) -> DepReport {
964    dep_dts_to(cargo_root, types_root, true)
965}
966
967/// [`dep_dts`], writing only when `write` is set.
968fn 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    // Orphans before materialising: the note beside a crate's declarations is what says
979    // which crate they came from, and the write below rewrites it.
980    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
999/// The three report lists, each said as what it is. `wrote` / `unchanged` name what
1000/// happened to a declaration, and so do these: a reader who has never heard the words
1001/// `shipped-declaration` or `orphaned-declaration` can still tell which line failed the
1002/// command and which one is a file sitting there to decide about.
1003fn 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
1015/// What generation wrote, or for a run that did not write (`wrote` unset) what it would
1016/// have.
1017fn 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    // The contract types first: a project may publish one without having a Rust host at
1039    // all, and the `bail!` below would then be wrong about there being nothing to do.
1040    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        // Asked to generate and did not: the command says so in its exit code, or a CI
1051        // step that regenerates declarations would pass having written nothing.
1052        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    // An orphan, and a graph that would not resolve, are reports. A declaration asked for
1079    // and not written is a failure, the same as a contract that could not be published.
1080    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
1101/// The project `start` belongs to, with `h` set up for its sources the way `htl check`
1102/// sets up its checker: the model's directories on the search path, its installed
1103/// dependencies made reachable. `None`, with nothing on the path, outside a project.
1104///
1105/// What one file may read beyond that — the test root, for a file under it; its own
1106/// directory, for a file in no project — is [`project::file_view`], asked per file.
1107fn 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
1119/// What the run wrote, and — when a target was asked for by name — what it left alone. The
1120/// kept list is what makes `htl init --target bin` on an older project say which of that
1121/// target's files were already there instead of skipping them in silence.
1122fn 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    // Before the directory is touched: an unknown target, or one that disagrees with
1161    // --lib, fails here and leaves nothing behind. So does an htl this scaffold does not
1162    // write for — a release by number — so it is settled first too.
1163    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    // The manifest names a dependency: the fetch comes before the first test, or the
1174    // first test is a `module not found`.
1175    let fetch = if opts.writes_htlx() {
1176        "htl pkg install && "
1177    } else {
1178        ""
1179    };
1180    // The window target's first build needs two files nothing has written yet: `htl check`
1181    // materialises the dependency's declaration as `types/htl-mq/mq.d.tl` (from its
1182    // `[package.metadata.htl] dts`) and writes `src/fx.d.tl` from the host module, and
1183    // `cargo build` reads the first of them when `include_bundle!` links `mq`. So the
1184    // check comes before the run rather than after it.
1185    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    // A target was named: say what it would have written and found already there. Without
1222    // one the old one-liner stands, so a plain re-run does not list the whole tree.
1223    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
1232/// The project `htl pkg` acts on: the one above the working directory, found the way
1233/// every command finds it ([`htl::model::Project::find_root`]), with its `mlua-pkg.toml`.
1234///
1235/// mlua-pkg reports a missing manifest as an I/O error that does not name the file, and the
1236/// path htl looked for is the whole of the answer, so it is checked here.
1237fn 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
1251/// The declaration root of the project at `root`, from its model: `[layout] types`, which
1252/// is `types/` unless `htl.toml` says otherwise — where the declarations htl brings into a
1253/// project are written.
1254fn 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
1261/// `htl pkg install`: fetch what the manifest declares, then bring in what the deps publish.
1262fn cmd_pkg_install() -> Result<ExitCode> {
1263    let project = pkg_project()?;
1264    let report = project.install()?;
1265    report_install(&report, &project);
1266    // Re-read the project: install wrote the lockfile the two reports below are read from.
1267    // A dep publishes its declarations at `types/` in its package root, which is not where
1268    // `require` looks, so they are copied in for the checker to see.
1269    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
1278/// What install did, in the shape the other reports use. The library prints nothing.
1279fn 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        // A patch that was not used is reported below in htl's own verbs, where both ways
1286        // out are named. The library's line points at `mlua-pkg patch --force`, which
1287        // rebuilds the copy without asking git whether what is in it was committed.
1288        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
1319/// `htl pkg add <name> <git>`: the manifest entry, without fetching anything.
1320///
1321/// This is the one verb that may run outside a project: mlua-pkg writes a manifest when
1322/// there is none. It writes it at the project's root — beside `htl.toml`, when the working
1323/// directory is in a project that has one — and in the working directory only outside any
1324/// project. Written in a subdirectory, it would give the project a second root, which
1325/// every command then refuses.
1326fn 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        // `add` rewrites the entry, and its spec has no room for a patch. Putting the key
1350        // back is the difference between the project resolving that dependency from its own
1351        // copy and resolving it from upstream with the copy left unread in the tree.
1352        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
1361/// `htl pkg update [name]`: what each pin does next, then the install that follows.
1362fn 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
1399/// `htl pkg clean`: the cache, which is machine-local and rebuilt by the next install.
1400fn 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
1417/// A patched copy the dependency is no longer resolved from, said after every install
1418/// until it is refreshed or removed.
1419///
1420/// A patch is bound to the revision it was taken from, and an upgrade moves the pin off it.
1421/// Install does not fail over that — the project builds against the new upstream — so the
1422/// only thing that keeps the copy from being forgotten in the tree is being told about it
1423/// each time. Both ways out are named, in htl's verbs: mlua-pkg's own warning points at
1424/// `mlua-pkg patch --force`, which skips the question htl asks git before overwriting.
1425fn 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
1453/// `htl pkg patch <dep>`: the dependency's source, taken into `patches/<dep>/` where the
1454/// project owns it. Not a passthrough — htl decides the directory, writes `patch_dir` into
1455/// the manifest and answers the "may this be overwritten" question against git; the copy
1456/// and the `patch_base` bookkeeping are mlua-pkg's. See `Project::patch`.
1457fn 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    // Only when there was something to drop: on a dependency whose repository holds
1469    // nothing but the package this line would be noise, and the reader would learn the
1470    // rule from a line that says nothing happened.
1471    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
1481/// What the deps published, in the shape `htl init` reports its scaffold. A name that was
1482/// already taken is said out loud: nothing was overwritten, and the project is the one
1483/// that decides which declaration it wants.
1484fn 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
1494/// `htl types add <library>`: the declarations a library did not ship, from the collection
1495/// that has them. The revision is recorded because nothing else in that ecosystem does —
1496/// see `Project::add_types`.
1497fn 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
1511/// What `htl unused` was asked for, beyond the paths.
1512struct UnusedFlags {
1513    json: bool,
1514    /// Exit 1 when anything was reported. Off by default: "unused" is a question about
1515    /// intent, so CI opts in rather than out.
1516    fail_on_unused: bool,
1517    use_cache: bool,
1518    explain: bool,
1519}
1520
1521/// What `htl check` was asked for, beyond the paths and the lint selection.
1522struct 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    /// Where to write the lcov tracefile, if asked for.
1540    lcov: Option<PathBuf>,
1541    /// Where to write the JUnit XML report, if asked for.
1542    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
1601/// One file's report as a JUnit suite.
1602///
1603/// The assertion library reports a failure as `"<suite > test>: <message>"` and its tests
1604/// by the same composed name, so a failing case takes back the message printed under the
1605/// file by matching that prefix — including the traceback a runtime error carries, which
1606/// belongs in the report for the same reason it belongs on the terminal: it is the part
1607/// that says where.
1608fn 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                // A failing test whose message could not be matched still failed: say so
1625                // rather than reporting it as passed.
1626                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    // A patched dependency's tests are its suite, not this project's: `htl pkg patch`
1673    // takes the whole package root, tests included, and running them here would report a
1674    // library's own failures as the project's.
1675    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    // A test at the root of a project laid out flat without saying so would only fail on
1680    // its first `require`; say what is wrong instead, as `check` does.
1681    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            // Given, or drawn for the run and printed below, so that it can be given back.
1697            seed: flags.seed,
1698            // `project::test` fills it from the config it is handed.
1699            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    // What is left of the run here: how a file reads on a terminal, and what the document
1714    // says about it. Which files run, in what isolation, from what store — and what they
1715    // draw — is `project::test`'s.
1716    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        // Spelled as `htl check` spells the same file, whatever path the walk was handed.
1724        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            // The cause on the file's own line, its frames under it: a traceback inside
1730            // the parenthesised summary would push `12 ms` past a screen of stack.
1731            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        // Quiet: a passing file is silence; failures, errors and slow tests still show.
1738        // JSON: nothing on stderr, the document carries it all.
1739        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                    // The file line was skipped: name the file with the slow test.
1753                    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            // A snapshot written or rewritten is a change on disk: always say so, even in -q.
1770            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        // The run's own total, not the sum of the files: it is the number the summary
1787        // line prints, and the report is there to agree with the summary line.
1788        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        // Against the project root rather than the working directory: a tracefile is
1793        // uploaded from wherever CI ran the command and resolved against the repository.
1794        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            // Says the checking was reused, not the run: every one of these files ran.
1839            if rep.replayed > 0 {
1840                format!(", {} checked from cache", rep.replayed)
1841            } else {
1842                String::new()
1843            },
1844            rep.duration_ms
1845        );
1846        // Always, not only on failure: the seed of a run that passed is what reproduces
1847        // the run that passes, and a failure two commits later is compared against it.
1848        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
1860/// Name the patched dependencies this walk enters, in the shape the other reports use.
1861///
1862/// Only the ones actually below a given root: `htl check src` walks the project's own
1863/// sources and nothing under `patches/`, and saying otherwise would be a claim about files
1864/// that were not read.
1865fn 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    // The project found the way every command finds it; a failure to find one was said by
1872    // the command itself, which ran first.
1873    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
1892/// Nearest `htl.toml` above the first path: `(dir holding it, path, config)`.
1893///
1894/// Where `[toolchain] htl` is answered, and the only place: every command that reads the
1895/// config comes through here, and the pin has to be settled before the command reads a
1896/// source file — which is a property of this function, not something each command could
1897/// be trusted to repeat. `htl new` / `htl init` do not call it, so a mismatch never stops
1898/// anyone from creating a project.
1899///
1900/// The version compared is this binary's, which is the one the pin is about. The `htl`
1901/// crate a Rust host builds against is Cargo's, pinned in `Cargo.toml` — a separate
1902/// question, and the one [`warn_cargo_htl_mismatch`] answers.
1903fn 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
1911/// Say so, once, when the `Cargo.toml` around this project asks for an `htl` crate this
1912/// command is not.
1913///
1914/// The two halves of htl are released together and a project uses both: the crate the host
1915/// links, and the command that checks the Teal beside it. A project left at `htl = "0.5.1"`
1916/// while the installed CLI had moved to 0.6.0 was checked by one and built against the
1917/// other, and nothing in either output mentioned the other one — which is the whole of the
1918/// failure, since either version alone was fine.
1919///
1920/// **A warning, not an error, and not one of the summary's `warning(s)`** — that count is
1921/// the checker's, and this is not about the sources. A project goes to a hard error over a
1922/// version skew for one of two reasons, and htl has neither: the format between the halves
1923/// is unstable, so a mismatched pair miscompiles rather than failing (wasm-bindgen refuses
1924/// a JS glue built by another version); or the diagnostic downstream is opaque, so the
1925/// person would otherwise read a type error from deep inside a dependency (cargo's
1926/// `rust-version`). Here both halves worked — the run that produced this report is the
1927/// evidence — and the line is worth printing only because the person has no other way to
1928/// notice. npm's `engines` is the same shape and warns by default; Prisma says nothing at
1929/// all, and its silence is filed as a bug.
1930///
1931/// Read from the `Cargo.toml` of the crate the project sits in, `[dependencies] htl` only:
1932/// a string, or a table with `version`. A table without one — `path`, `git`, or a
1933/// `workspace = true` inheritance — states no requirement here and is passed over without
1934/// a word, which is exactly what a scaffold written by a checkout-built CLI produces. No
1935/// `Cargo.toml`, no `[dependencies]`, no `htl` key, or a requirement cargo itself would
1936/// reject: nothing is printed, because there is nothing this can be surer about than cargo.
1937fn 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    // Both ways out are named because either is the right one: the project may be the
1969    // thing that is current, in which case the command is what moves.
1970    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    // Not a dependency's files, a patched one included: formatting the copy would turn
1990    // every one of its files into a diff against the revision it was taken from, and bury
1991    // the project's own change somewhere inside that.
1992    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    // A dry run writes nothing, the declarations generated before the check included.
2051    auto_dts_to(&paths[0], !flags.dry_run)?;
2052    let model = project::model_of(&cfg, &paths[0])?;
2053    // What `htl check` holds a file to, resolved the same way and read by the same checker,
2054    // so a file this run leaves reports what a check of it would.
2055    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    // Here as well as inside `fix_file`, so a misspelt rule is answered even when the
2075    // paths hold no `.tl` at all — the request is wrong either way.
2076    opts.validate()?;
2077    // Written as `htl fmt` walks, not as `htl check` does: this command rewrites files, so a
2078    // patched dependency is left alone whatever the fixes would be. An edit there belongs
2079    // in the diff the project makes against the revision it took, written by a person.
2080    let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Own);
2081    // Only what a module of the project holds, as `htl check` walks it.
2082    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    // But checked as `htl check` walks: the copy is still the project's code, and what is
2091    // wrong in it is what the two commands both judge the tree by. Only checked, never
2092    // handed to `fix_file`.
2093    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    // The working tree is the undo: refuse to rewrite what git could not give back.
2105    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    // Judged by the levels `htl check` judges by: a finding under a rule at `deny` fails.
2139    sink.judge_by(scope.lints.selection());
2140    // Dependencies are reported as `htl check` reports them and never rewritten: a fix
2141    // under `.htl/` goes at the next install, one under `[check] paths` is not this
2142    // project's. `fix_file` only ever writes the file it was given.
2143    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        // Put back after each file, as `htl check` does, so a file is fixed against what
2153        // it may read and not also against the directories of the files before it.
2154        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            // A suggestion is never written, so the diff is the only place it is shown.
2211            // Against what was applied, when something was: the two are one edit session.
2212            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        // What the file says as it is left, the way `htl check` would say it: the checker's
2225        // result and this layer's lints, while the path it was checked under is in place.
2226        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    // A patched copy: checked as `htl check` checks it, and not fixed.
2256    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    // What the project says about itself as a whole.
2274    // A dry run publishes nothing either.
2275    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    // A broken dependency is an error `htl fix` cannot remove; it remains, as `htl check`
2286    // would count it.
2287    found.errors += sink.dependency_error_count();
2288    found.denied = sink.denied();
2289    // Judged as `htl check` judges the same tree: the same findings, the same policy.
2290    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        // What else the verdict counted, said only when it did: a finding at `deny`, and
2320        // under `strict` every warning and lint. Without it a run that failed on a lint
2321        // would end in `0 error(s) remaining`.
2322        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
2353/// Type-check a tree, replaying the modules whose inputs have not moved.
2354///
2355/// The order here is load-bearing. Generating `.d.tl` and collecting the file list come
2356/// first, because both produce inputs the keys are built from. Every module is looked up
2357/// before any is checked, so that a run where nothing moved never builds a checker at all —
2358/// that costs about 13.5 ms (`crates/htl-core/benches/check.rs`), which is most of what a
2359/// fully replayed run has to save on a small project.
2360///
2361/// A module that misses re-checks its dependencies as part of its own check, since the
2362/// checker's store starts empty. So an edit costs the edited module, whatever depends on
2363/// it, and whatever those pull in — less than the project, more than the minimum.
2364fn 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    // Listing the rules reports nothing about the project and shares nothing with a run —
2380    // and now not even a Lua state: the registry is Rust (`htl_core::lint::RULES`), which
2381    // is what lets the rules the project layer reports under be listed beside the ones
2382    // `lint.lua` implements.
2383    if list_lints {
2384        // The name and the level a project that says nothing gets. Two columns rather than
2385        // one because the default is a level now, and the five rules at `allow` are
2386        // otherwise invisible: a reader would have to turn one on to find out it was off.
2387        // Still one rule per line, name first, so it reads and greps as it always did.
2388        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    // htl.toml first, then --lint, so the flag wins; `strict` from the file unless flagged.
2399    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    // `.d.tl` written from Rust source is an input to the check, so it is regenerated
2403    // before anything hashes the tree: a hit computed over stale declarations would be a
2404    // hit on a different question from the one being asked.
2405    if let Some(first) = paths.first() {
2406        auto_dts(first)?;
2407    }
2408    // A patched dependency is walked with the project's own sources — it is the project's
2409    // code, and its errors are the project's to fix. Which dependency each directory
2410    // stands in for is said here, so that an error under it is read as that dependency's
2411    // without the reader having to know the manifest.
2412    let walk_model = project::model_of(&cfg, &paths[0])?;
2413    // A directory is checked as a project: which of its files is which module, and what
2414    // they may read, is the project's to say. With none there is no answer to give, and
2415    // checking each file as a thing on its own would answer a different question without
2416    // saying so. A file named on its own is that question, asked outright, and is checked.
2417    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            // Absolute: where the search started is the whole of the answer, and `.`
2427            // relative to itself would say nothing.
2428            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    // The walk, the store and every decision about them are the library's: `include_tl!`
2447    // asks the same questions of the same store, and the two answering them separately is
2448    // what let them disagree. What is left here is the flags, the summary and the exit code.
2449    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
2474/// Report what no entry's `require` closure reaches.
2475///
2476/// The graph is the check's — `htl check` resolves every `require` already, and the
2477/// `require-cycle` lint reads the same edges — so this is that walk plus the entries the
2478/// project declares (`htl::unused`). What is left here is the flags, the printing and the
2479/// exit code.
2480fn 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    // A `.d.tl` written from Rust source is an input to the check the graph comes from,
2494    // exactly as it is for `htl check`.
2495    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
2545/// Say which file a module name resolves to, and what that hides.
2546///
2547/// The chain comes from the library ([`htl::resolve`]), over a checker set up exactly as a
2548/// check of this project sets one up — the installed deps, then the config's search paths,
2549/// in that order, because `add_path` prepends and the order is the whole answer. Asking
2550/// any other way would report a path nothing else uses.
2551///
2552/// A report, so both forms go to stdout (README, "Machine-readable output").
2553fn 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    // The declarations a check generates before it reads anything are inputs to the
2557    // answer — `types/<crate>/` is written by `htl dts` — and a report without them would
2558    // describe a project nobody checks.
2559    auto_dts(start)?;
2560    let h = Htl::new()?;
2561    // First, so that the project's own directories go in front of it as they do under
2562    // `htl run`: `add_path` prepends, and the report prints the order it searched.
2563    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
2583/// The text form of a resolution: a table in search order, then who answered — the project
2584/// model, or the search path and the directories it consulted. The order column is the reason one row is read and the others are not, which
2585/// is why it is printed rather than left to be looked up.
2586fn print_resolution(r: &htl::resolve::Resolution) {
2587    let ambiguous = r
2588        .candidates
2589        .iter()
2590        .any(|c| c.status == htl::resolve::Status::Ambiguous);
2591    // A name the host provides: what answers it at run time is the host, and a row read
2592    // for it is only the declaration it is typed from. Refused when a file implements it
2593    // too, and then the header is the error, as a check reports it.
2594    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        // Declared and nothing else: the environment provides it — a library installed on
2602        // the machine, a module registered where the model cannot read it. The declaration
2603        // is the file read, and comes first, as any file the name resolves to does.
2604        (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                // Not hidden by the row that is read: typed by it, and loaded by the run.
2643                (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        // The name is not the project's: where the search looked is half the answer, and
2662        // all of it when nothing was found.
2663        htl::resolve::AnsweredBy::Path => {
2664            println!("  searched, in order: {}", r.searched.join(", "))
2665        }
2666    }
2667}
2668
2669/// `1 module` / `2 modules`: a count whose noun agrees with it, for a line short enough
2670/// that `module(s)` would be the loudest thing on it.
2671fn count(n: usize, one: &str, many: &str) -> String {
2672    format!("{n} {}", if n == 1 { one } else { many })
2673}
2674
2675/// Delete this project's cache store.
2676///
2677/// The store sits beside `htl.toml`, which is not necessarily where anyone is standing:
2678/// `htl check src` run from anywhere in a repo writes to the project root. That is the
2679/// reason this exists rather than leaving people to work out which directory to remove.
2680///
2681/// Only `.htl/cache` goes. `.htl/` itself is left in place for whatever else may come to
2682/// live there, and the path is built here rather than taken from an argument, so there is
2683/// no spelling of the command that removes anything else.
2684fn 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
2707/// The directory a cache command works on: the project's root, as every command keeps its
2708/// store there ([`project::store_root`]), or the working directory outside a project.
2709fn 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
2715/// Say what the store holds.
2716///
2717/// Answers "what is stored", not "what would be reused": an entry listed here can still miss
2718/// on the next run because a file it recorded has changed. The two are different questions
2719/// and conflating them would make this command lie in the more dangerous direction.
2720fn 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
2796/// How many of the files a walk visited came out of a patched dependency: the files in the
2797/// home of a module the project owns as a patch ([`htl::model::Project::home_of`]) — its
2798/// tests included, which no name reaches. Zero outside a project and for a
2799/// project with no patch, which is what keeps the summary line below unchanged for
2800/// everyone who has never run `htl pkg patch`.
2801fn 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
2822/// The one place a check reports its totals, so a replayed module and a checked one cannot
2823/// drift apart in how they are summarized. Returns whether the run counts as a failure.
2824///
2825/// `patched` is how many of those files are a patched dependency's ([`patched_files`]),
2826/// split out of the count rather than added to it. One `htl pkg patch` turned a project's
2827/// `15 file(s)` into `25 file(s)` with nothing said, and a file count that moves for a
2828/// reason the line does not give is a file count a reader cannot use: the dependency's
2829/// `tests/` are now in the walk, they are not the project's, and the line has to say so.
2830fn 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        // Say nothing when nothing was replayed; say how much when it was only some.
2860        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        // The three counts say what kinds of thing the run said; `at deny` says how much
2868        // of that it failed on, and is left out when it is none of it — its being there at
2869        // all is the news. A run with two warnings and one denied lint reads as
2870        // `0 error(s), 2 warning(s), 1 lint(s), 1 at deny`.
2871        let denied = match rep.denied {
2872            0 => String::new(),
2873            n => format!(", {n} at deny"),
2874        };
2875        // A project with no patch reads exactly as it read before the split existed: the
2876        // second number appears only when there is something on the other side of it, and
2877        // the two always add up to the one number that was there.
2878        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    // The search path `htl check` gives this file, so `htl gen` resolves what `htl check`
2894    // resolved and a file the checker accepts is one this command can emit. Same calls, in
2895    // the same order, as `cmd_run` and `cmd_build`.
2896    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    // Judged on its errors alone (`Policy::ERRORS_ONLY`): the warnings and lints above are
2905    // reported, and `htl check` is where they are judged.
2906    let (Some(code), false) = (code, runs_fail(&c)) else {
2907        return Ok(ExitCode::FAILURE);
2908    };
2909    // Written as it came back. This command used to append the final newline itself, which
2910    // made it the one consumer of `gen_lua` whose output was a whole file; the generator
2911    // does it for all of them now (`H.gen` in `prelude.lua` says why).
2912    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    // The search path `htl check` gives this file, so `htl run` resolves what `htl check`
2924    // resolved rather than failing on a `require` the checker was happy with. Same calls
2925    // as `cmd_gen` and `cmd_build`; before the bundle branch because a bundle carries its
2926    // own modules and gains nothing from it either way.
2927    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        // A bundle's frames are as good as its payload: stripped bytecode has no lines to
2938        // show, and `htl build --debug` is what keeps them.
2939        let main = h.load_bundle(&b, args)?;
2940        return run_root(&h, Entry::Main(main), args);
2941    }
2942    // Check first so lints/warnings are visible before the script runs.
2943    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    // As `htl gen`: errors stop it, the rest is reported (`Policy::ERRORS_ONLY`).
2949    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
2962/// What `htl run` runs as the program's root: a generated chunk, or a bundle's entry.
2963enum Entry {
2964    Chunk { code: String, name: String },
2965    Main(htl::mlua::Function),
2966}
2967
2968/// The `[async]` of a loaded `htl.toml`, or the default when there is no file.
2969fn 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
2980/// Run the program as a root coroutine on the executor, with Ctrl-C cancelling it.
2981///
2982/// The runtime is a current-thread tokio runtime with its time driver (the grace is a
2983/// tokio timeout) and a `LocalSet`, built for this run; `Htl::run_async` is awaited to
2984/// the end, so when it resolves nothing the program started is alive.
2985///
2986/// Ctrl-C is watched from a thread of its own rather than a task on this runtime: a
2987/// program in a CPU loop never yields to the runtime, so a task here would never get to
2988/// run, while the cancel hook reads the token from any thread at its next check. The
2989/// first Ctrl-C cancels the root — the program gets its grace to clean up — and a second
2990/// one ends the process, for a program that does not.
2991///
2992/// A cancelled run prints `htl run: interrupted` and exits 130, the shell's own code for
2993/// it; a program that raised prints the innermost cause and the frames that reached it
2994/// (`htl run` is a development command, so the frames are the default; `htl::user_message`
2995/// is what an embedding host shows people who did not write the Teal).
2996fn 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
3047/// Whether `htl run` / `htl gen` stop at the check `c`: [`htl::verdict::verdict`] under
3048/// [`Policy::ERRORS_ONLY`](htl::verdict::Policy::ERRORS_ONLY), so an error does and
3049/// nothing else.
3050fn 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
3062/// The run cache as `htl build` was asked to use it: `--no-cache` and `--explain-cache`,
3063/// the two switches that mean something for one closure. There is no `--cache-mode`: a
3064/// build's entries are per module, as `htl test`'s are, since a build is many modules and
3065/// an edit to one should cost that one and its dependents rather than the closure.
3066struct 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    // `std.*` resolves to its declaration and nothing else, so the linker files it under
3081    // the host's modules — which is what it is: the binary that runs the bundle preloads
3082    // it, as `htl run` does before `run_bundle`.
3083    h.install_std()?;
3084    let cfg = load_config(entry)?;
3085    // The rules and levels `htl check` uses: a bundle is judged as a check of its closure
3086    // would be, and a rule the project turned off is not reported here either.
3087    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    // The names the host provides are the model's — `#[host_module]`s in the crate around
3092    // the project, `[build] host`, `std.*` — so the bundle leaves out what the host
3093    // registers without anyone restating it; `--host` adds to them.
3094    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        // The one thing `[build] target` changes: this command produces the `hb` target and
3101        // nothing else, so a project that records another is told which command does build
3102        // it rather than handed a bundle nothing in it was going to load. Here rather than
3103        // further down because both forms of `htl build` pass through — the directory form
3104        // returns a few lines below — and because nothing has been written yet.
3105        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        // Not into an installed or vendored copy — a dependency's files, which the model
3119        // knows — and into a patch, which is the project's code.
3120        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    // The name the entry is served under: the one the project's model gives the file,
3129    // which is what a `require` of it elsewhere in the project writes.
3130    opts.entry_name = model.as_ref().and_then(|m| m.locate(entry).map(|p| p.name));
3131    // The store: `module` entries keyed the way `htl test` and the macros key them — the
3132    // file the module is and the lint selection `htl.toml` puts in force — so a module any
3133    // of them generated is one the build replays, and the reverse. Nothing is swept here:
3134    // a build sees one closure, and only `htl check`, which sees the project, bounds the
3135    // store (`cache.rs`).
3136    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    // One sink for the whole closure: a build is many modules, and what the layer says
3161    // once per run is said once for the build rather than once per module.
3162    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    // The checks' own errors went through the sink above; the linker's are its own list,
3175    // said through the same sink so they read as the checks' do.
3176    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    // The same suffix `htl check` prints: everything from the store, some of it, or
3190    // nothing said when none was.
3191    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
3220/// The older form: every `.tl` under a directory, module names from their paths.
3221fn 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    // Judged as the file form is: `htl.toml`'s rules and levels, its `strict`.
3231    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        // Named against the directory the bundle is built from, by the naming rule.
3246        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/// What `htl bundle info` reports: everything the file records and nothing it does not
3299/// (no Lua state is created, nothing is loaded), which is what a build step checks in and
3300/// a bug report pastes; `--format json` is the same fields. `lua` is the header the
3301/// bytecode was compiled for, in the fields the mismatch message names; `None` when the
3302/// bundle carries no fingerprint, which `payload` explains (`source`: the text says `any`)
3303/// or `format` does (`1`, before the fingerprint: `not recorded`).
3304#[derive(serde::Serialize)]
3305struct BundleInfo {
3306    file: String,
3307    format: u8,
3308    htl_version: Option<String>,
3309    /// `bytecode`, `source`, or `mixed` when modules disagree.
3310    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, or no modules at all: what the loader would treat it as.
3345        _ => "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}