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//! - `htl lsp`                a language server on stdio; its diagnostics are `htl check`'s
9
10use htl::cache;
11mod junit;
12mod lsp;
13mod pin;
14pub mod report;
15mod scaffold;
16
17/// Output format of every command that has `--format`. One enum, so its help must be
18/// true of all of them: `check` / `test` / `fix` / `unused` / `adopt` print their JSON
19/// document on stdout and nothing on stderr, and their text form on stderr only, so the
20/// two never mix; `cache status` / `bundle info` / `resolve` are reports rather than runs,
21/// so both of their forms go to stdout. The exit code is the same in either form.
22#[derive(Clone, Copy, PartialEq, Eq, Debug, clap::ValueEnum)]
23enum Format {
24    /// Human-readable lines
25    Text,
26    /// One JSON document on stdout (README, "Machine-readable output":
27    /// https://github.com/ynishi/htl#machine-readable-output)
28    Json,
29}
30
31/// How `htl check` grains its cache. Separate from whether it caches at all, which is
32/// `--no-cache`.
33#[derive(Clone, Copy, PartialEq, Eq, Debug, clap::ValueEnum)]
34enum CacheModeArg {
35    /// One entry per module: an edit costs that module and what depends on it
36    PerModule,
37    /// One entry per run: any edit re-checks everything, a run with no edits reads one file
38    WholeRun,
39}
40
41#[derive(Subcommand)]
42enum TypesCmd {
43    /// Copy a library's declarations from teal-types into `types/`, and record which
44    /// commit they came from
45    Add {
46        /// The library as teal-types names it (`luasocket`, `lpeg`, …)
47        library: String,
48        /// Take them from a checkout of teal-types already on disk instead of fetching
49        #[arg(long)]
50        from: Option<PathBuf>,
51        /// Replace declarations that are already in `types/`
52        #[arg(long)]
53        force: bool,
54    },
55}
56
57#[derive(Subcommand)]
58enum PkgCmd {
59    /// Fetch every dependency `mlua-pkg.toml` declares and write `mlua-pkg.lock`
60    Install,
61    /// Write a dependency into `mlua-pkg.toml`; `install` is what fetches it
62    Add {
63        /// The name `require()` will use for it
64        name: String,
65        /// Remote git URL
66        git: String,
67        /// Pin to a tag: exact (`v1.0.0`), or a prefix that follows its patches (`v1.0`)
68        #[arg(long)]
69        tag: Option<String>,
70        /// Pin to a commit
71        #[arg(long)]
72        rev: Option<String>,
73        /// Track a branch (a build from it is not reproducible)
74        #[arg(long)]
75        branch: Option<String>,
76        /// The subdirectory `require()` resolves through, when it is not the one the
77        /// package declares
78        #[arg(long)]
79        entry: Option<PathBuf>,
80        /// Copy the package into this directory instead of linking it (manifest-relative,
81        /// rewritten by every install)
82        #[arg(long)]
83        target_dir: Option<PathBuf>,
84    },
85    /// Refresh dependencies, bump the pins that follow releases, then install
86    Update {
87        /// One dependency; every one when omitted
88        name: Option<String>,
89        /// Print the plan and write nothing
90        #[arg(long)]
91        dry_run: bool,
92        /// Bump exact tag pins too, to the highest release the remote has
93        #[arg(long)]
94        force: bool,
95    },
96    /// Remove cached packages the lockfile no longer refers to
97    Clean {
98        /// Remove the whole cache rather than the unreferenced entries
99        #[arg(long)]
100        all: bool,
101    },
102    /// Take a dependency's source into `patches/<dep>/`: a copy the project owns, edits
103    /// and commits, which install resolves the dependency from
104    Patch {
105        /// The dependency as `mlua-pkg.toml` names it
106        dep: String,
107        /// Refresh the copy even when it has uncommitted changes (they are discarded)
108        #[arg(long)]
109        force: bool,
110    },
111}
112
113#[derive(Subcommand)]
114enum CacheCmd {
115    /// Delete this project's stored check results
116    Clear {
117        /// A path inside the project; the store is .htl/cache at the project root, the
118        /// nearest htl.toml or mlua-pkg.toml above
119        path: Option<PathBuf>,
120    },
121    /// Report what the store holds
122    Status {
123        /// A path inside the project; the store is .htl/cache at the project root, the
124        /// nearest htl.toml or mlua-pkg.toml above
125        path: Option<PathBuf>,
126        /// Output format
127        #[arg(long, value_enum, default_value_t = Format::Text)]
128        format: Format,
129        /// List every entry rather than counting them by kind
130        #[arg(long)]
131        entries: bool,
132    },
133}
134
135#[derive(Subcommand)]
136enum BundleCmd {
137    /// Print what a bundle records: format, the htl that built it, payload kind, the Lua
138    /// its bytecode was compiled for, entry, modules, host-provided names
139    Info {
140        /// The `.hb` file
141        file: PathBuf,
142        /// Output format
143        #[arg(long, value_enum, default_value_t = Format::Text)]
144        format: Format,
145    },
146}
147
148impl From<CacheModeArg> for cache::Mode {
149    fn from(m: CacheModeArg) -> Self {
150        match m {
151            CacheModeArg::PerModule => cache::Mode::PerModule,
152            CacheModeArg::WholeRun => cache::Mode::WholeRun,
153        }
154    }
155}
156
157use anyhow::{Context, Result, bail};
158use clap::{Parser, Subcommand};
159use htl::BuildTarget;
160use htl::Htl;
161use htl::bundle::Bundle;
162// The project layer: which files a check walks, what it replays from the run cache and
163// what it keeps there, and which of a run's findings are said. Shared with `include_tl!`,
164// which asks the same of the same store.
165use htl::project;
166use std::fs;
167use std::path::{Path, PathBuf};
168use std::process::ExitCode;
169
170#[derive(Parser)]
171#[command(
172    name = "htl",
173    version,
174    about = "Teal, hidden: run / check / build .tl on mlua",
175    long_about = "\
176Teal, hidden: run / check / build .tl on mlua.
177
178htl is a toolchain for Teal, and a way to embed one. It type-checks `.tl` sources with \
179Teal's own checker, runs and tests them on an embedded mlua state, formats them, and \
180links them into a single stripped-bytecode bundle. A Rust host embeds the same engine, \
181so the scripts it ships are checked when the host is built rather than when its user \
182first runs one.
183
184The guide, with every command and what a project looks like:
185  https://github.com/ynishi/htl
186
187The API a Rust host holds — `include_tl!`, `#[host_module]`, the run cache:
188  https://docs.rs/htl"
189)]
190struct Cli {
191    #[command(subcommand)]
192    cmd: Cmd,
193}
194
195#[derive(Subcommand)]
196enum Cmd {
197    /// Type-check .tl files or directories (htl lints are reported as `lint:`)
198    ///
199    /// A module reached through `require` is checked with the file that required it, and
200    /// what has not changed is replayed from `.htl/`. README, "Lints":
201    /// https://github.com/ynishi/htl#lints-htl-check-include_tl
202    #[command(after_long_help = "\
203Examples:
204  htl check src                  every .tl under src/
205  htl check src --strict         every warn counts as deny: warnings and lints fail
206  htl check src --lint nil-index=deny,no-any=warn
207                                 one rule fails the run, another is advice
208  htl check src --lint -tl:hint  a warning kind of the Teal compiler silenced
209  htl check --list-lints         every rule with its default level, then exit
210  htl check --explain union-exhaustive
211                                 why the rule exists and how to decide its cases
212  htl check src --statistics     how many findings each rule produced, not the findings
213
214The store is .htl/cache at the project root, the nearest htl.toml or mlua-pkg.toml above
215the paths; --no-cache skips it, --explain-cache says why a lookup missed.
216Caching: https://github.com/ynishi/htl#caching
217")]
218    Check {
219        paths: Vec<PathBuf>,
220        /// Treat every `warn` as `deny`: warnings and lints fail the run
221        #[arg(long)]
222        strict: bool,
223        /// Rule levels over the defaults, e.g. `nil-index=deny,no-any=warn` (`-rule` =
224        /// allow, `+rule` = warn)
225        // A spec that silences a rule starts with `-`, and without this clap reads
226        // `--lint -contract` as a cluster of short flags and answers `unexpected argument
227        // '-c'`: the documented way to turn one rule off never reached the spec parser.
228        #[arg(long, allow_hyphen_values = true)]
229        lint: Option<String>,
230        /// List every lint rule with its default level and exit
231        #[arg(long)]
232        list_lints: bool,
233        /// Print a rule's explanation — what it catches, why, the fix, the judgment call —
234        /// and exit; the name is the one a finding prints in `[htl <rule>]`
235        #[arg(long, value_name = "RULE")]
236        explain: Option<String>,
237        /// Output format
238        #[arg(long, value_enum, default_value_t = Format::Text)]
239        format: Format,
240        /// Check even if an identical run is cached, and do not store this one
241        #[arg(long)]
242        no_cache: bool,
243        /// How the cache is grained; overrides `[cache] mode` in htl.toml
244        #[arg(long, value_enum)]
245        cache_mode: Option<CacheModeArg>,
246        /// Say why the cache was not used, and what this run did with it
247        #[arg(long)]
248        explain_cache: bool,
249        /// Print how many findings each rule produced, instead of the findings
250        /// themselves: one row per rule with at least one, count first, `[*]` when every
251        /// finding of it has a fix `htl fix` applies, `[-]` when only some do or only
252        /// with `--unsafe`, `[ ]` when none has one; rules with no finding are not
253        /// listed. The summary line and the exit code are unchanged. With `--format
254        /// json`, `statistics` carries the same rows and `diagnostics` is empty
255        #[arg(long)]
256        statistics: bool,
257    },
258    /// Run tests: every `.tl` that requires the test library, one isolated state per file
259    ///
260    /// README, "Tests": https://github.com/ynishi/htl#tests
261    #[command(after_long_help = "\
262Examples:
263  htl test                       every .tl that requires htl.test
264  htl test tests --filter parser only tests whose \"suite > name\" contains it
265  htl test --coverage --coverage-lines
266                                 which lines of each module the suite never reached
267  htl test --junit report.xml    also write the run as a JUnit report, for a CI
268")]
269    Test {
270        paths: Vec<PathBuf>,
271        /// Only run tests whose "suite > name" contains this substring
272        #[arg(long)]
273        filter: Option<String>,
274        /// Assertion library module consulted for the verdict (must expose `run(filter)`)
275        #[arg(long, default_value = htl::testing::DEFAULT_LIB)]
276        lib: String,
277        /// Lint rules on top of the defaults, e.g. `+no-any`
278        #[arg(long, allow_hyphen_values = true)]
279        lint: Option<String>,
280        /// Stop at the first failure (within a file, and across files)
281        #[arg(long)]
282        fail_fast: bool,
283        /// Pass when no test file is found (the summary still says 0 files)
284        ///
285        /// Without it, finding nothing exits 1: a test file is found by what it loads, not
286        /// by its name, so a wrong `--lib` or a wrong directory finds nothing and would
287        /// otherwise pass silently. For a project whose gate runs `htl test` before its
288        /// first test exists; remove the flag when that test arrives. With test files
289        /// present it changes nothing.
290        #[arg(long)]
291        allow_empty: bool,
292        /// Print every test with its time (default: only failures and files)
293        #[arg(short, long)]
294        verbose: bool,
295        /// Only failures (with details), errors, and the summary line; no per-file `ok` lines
296        #[arg(short, long, conflicts_with = "verbose")]
297        quiet: bool,
298        /// Also print tests slower than this many milliseconds
299        #[arg(long, value_name = "MS")]
300        slow: Option<f64>,
301        /// Rewrite snapshots (`to_match_snapshot`) that differ instead of failing
302        #[arg(long)]
303        update: bool,
304        /// Report executed statements per `.tl` module the tests reached (slower: a line hook)
305        #[arg(long)]
306        coverage: bool,
307        /// With --coverage, also list the unexecuted line ranges of each module
308        #[arg(long, requires = "coverage")]
309        coverage_lines: bool,
310        /// Also write the coverage as an lcov tracefile here (implies --coverage)
311        #[arg(long, value_name = "FILE")]
312        lcov: Option<PathBuf>,
313        /// Also write the run as a JUnit XML report here, for a CI that reads test results
314        #[arg(long, value_name = "FILE")]
315        junit: Option<PathBuf>,
316        /// Seed the random stream tests draw from; printed every run, so a failure repeats
317        #[arg(long)]
318        seed: Option<u64>,
319        /// Output format
320        #[arg(long, value_enum, default_value_t = Format::Text)]
321        format: Format,
322        /// Check and generate every file even if a cached form is available, and store none
323        #[arg(long)]
324        no_cache: bool,
325        /// Say why the cache was not used, and what this run did with it
326        #[arg(long)]
327        explain_cache: bool,
328    },
329    /// Apply the fixes diagnostics carry (safe ones by default)
330    ///
331    /// The working tree is the undo: a file git reports as modified or staged is refused
332    /// (`--allow-dirty`), and so is a file outside a repository (`--allow-no-vcs`), since a
333    /// rewrite git could not give back is one nobody can review. `--dry-run` reports
334    /// without writing; `--diff` prints a unified diff per file instead, and a `suggest`
335    /// fix — never applied — is listed as skipped and printed there as a second diff
336    /// headed `<file> (suggested)`. The `.d.tl` declarations the project publishes are
337    /// regenerated first, as `htl check` regenerates them; a dry run works them out and
338    /// writes none.
339    ///
340    /// README, "Fixing": https://github.com/ynishi/htl#fixing-htl-fix
341    #[command(after_long_help = "\
342Examples:
343  htl fix src                    apply the safe fixes to every .tl under src/
344  htl fix src --diff --dry-run   what would change, as a unified diff per file
345  htl fix src --rule forward-ref only that rule's fixes
346  htl fix src --unsafe           also the fixes that may change what the program does
347")]
348    Fix {
349        paths: Vec<PathBuf>,
350        /// Only fixes of these rules: any rule `htl check --list-lints` names, or
351        /// `forward-ref` / `tl:error`, the classes an error's fix is filed under
352        #[arg(long, value_delimiter = ',')]
353        rule: Vec<String>,
354        /// Also apply `unsafe` fixes (may change what the program does)
355        #[arg(long = "unsafe")]
356        unsafe_fixes: bool,
357        /// Compute and report, write nothing
358        #[arg(long)]
359        dry_run: bool,
360        /// Like --dry-run, and print a unified diff per file that would change
361        #[arg(long)]
362        diff: bool,
363        /// Fix files that git reports as modified or staged
364        #[arg(long)]
365        allow_dirty: bool,
366        /// Fix files that are not inside a git repository
367        #[arg(long)]
368        allow_no_vcs: bool,
369        /// Exit 1 when any file was changed (for CI)
370        #[arg(long)]
371        exit_non_zero_on_fix: bool,
372        /// Output format
373        #[arg(long, value_enum, default_value_t = Format::Text)]
374        format: Format,
375    },
376    /// Write the `.d.tl` files this project declares: those `#[host_module(dts = ..)]` /
377    /// `#[teal(dts = ..)]` ask for in a Rust crate, without building it, and the module
378    /// each `---@contract` type is declared in (check / run / test / build do both)
379    ///
380    /// Each declaration is reported as `wrote`, `unchanged`, or `not written`, one line
381    /// each. The commands that generate before they work (`check` / `run` / `test` /
382    /// `build` / `fix` / `unused` / `adopt` / `resolve` / `gen`) do the same job first and print only
383    /// what moved, prefixed `dts:` — `dts: wrote …`, `dts: not written: …`, `dts: left in
384    /// place: …` — and never an `unchanged` line; a dependency's declarations land under
385    /// `types/<crate>/` from every one of them. The exit code is about `not written` and
386    /// nothing else: non-zero when a declaration this command was asked to write could
387    /// not be written. A file already under `types/` that no dependency ships any more is
388    /// reported as `left in place` — nothing was asked for, nothing is deleted, and the
389    /// exit code does not move.
390    Dts {
391        /// Crate root or any path inside it (default: current directory)
392        dir: Option<PathBuf>,
393    },
394    /// Write a run-time shape (an lshape schema) for each record and enum this project
395    /// declares, and the `.d.tl` that types it
396    ///
397    /// Reads the project's own `.tl` files under its source directory (a `.d.tl`, the test
398    /// root and `types/` are not read) and writes, per module that declares a record or an
399    /// enum, `<source>/shapes/<module>.lua` — a Lua module returning one shape per
400    /// declaration, written against `require("lshape")` — and `<types>/shapes/<module>.d.tl`,
401    /// which declares each as `lshape.Schema<module.Record>`; and once
402    /// `<types>/lshape.d.tl`, the declaration of lshape those are written against. A value
403    /// that arrives from outside is then narrowed once, at the boundary, with
404    /// `lshape.check.assert(v, S.Record)`, and read without a cast after it. The record is
405    /// the source: nothing generated is to be edited. What is lowered, how, and what is
406    /// not (the record a file returns, which is the module; `record R is I`; a union of
407    /// records) is the module doc of `htl::shapes`.
408    ///
409    /// Reports `shapes: wrote <path>` or `shapes: current <path>` for each file, on stderr;
410    /// a file whose text would not change is not touched. A target that exists and does
411    /// not begin with the line `htl shapes` writes is not one of its own and is never
412    /// overwritten or removed. A module that declares nothing to lower any more has the
413    /// files an earlier run wrote for it removed (`shapes: removed <path>`); one that is
414    /// refused keeps them, and each is reported as `left as it was and is stale`. The exit code is non-zero when a file fails its check (it is lowered
415    /// and written all the same, so a file that requires a shapes module before the first
416    /// run wrote it is clean on the second), when a declaration has no shape (named at its field, and nothing is written for that
417    /// module), when two modules need each other's shapes, when a module is named `shapes`
418    /// (the namespace the generated modules sit in), when a target is not its own to
419    /// overwrite, and when `require("lshape")` resolves to no Lua or Teal module — lshape is
420    /// not vendored or installed — in which case nothing is written.
421    Shapes {
422        /// Project root or any path inside it (default: current directory)
423        dir: Option<PathBuf>,
424    },
425    /// Create a new Teal project directory
426    ///
427    /// README, "Layout of a project": https://github.com/ynishi/htl#layout-of-a-project-htl-new
428    #[command(after_long_help = "\
429Examples:
430  htl new hello                    src/main.tl, a module, tests/, mlua-pkg.toml
431  htl new hello --lib              a library: no entry script
432  htl new hello --embed            the same, plus a Rust host (--target bin)
433  htl new hello --target cdylib --lib
434                                   a library behind a C ABI, for a caller that is not Rust
435  htl new hello --embed --htl main
436                                   built against the unreleased htl (a git pin on main)
437  htl new hello --embed --htl path:../htl
438                                   built against a local checkout of this repository
439  htl new hello --no-x             without the htlx dependency the manifest gets by default
440
441Build targets: https://docs.rs/htl/latest/htl/build_target/enum.BuildTarget.html
442Layout of a project: https://github.com/ynishi/htl#layout-of-a-project-htl-new
443")]
444    New {
445        name: String,
446        /// Library only (no src/main.tl)
447        #[arg(long)]
448        lib: bool,
449        /// Also emit a Rust host: shorthand for --target bin
450        #[arg(long)]
451        embed: bool,
452        /// What will run this project's output: a target writes Cargo.toml + src/lib.rs
453        /// (the #[host_module], the embedded module, preload) and, when there is an entry
454        /// script, a thin src/main.rs
455        #[arg(long, value_name = "NAME", value_parser = clap::builder::PossibleValuesParser::new(scaffold::target_names()))]
456        target: Option<String>,
457        /// The htl the project depends on: `release`, `main`, or `path:<checkout>`.
458        /// Without it, the htl this binary was built with — its version on crates.io, or
459        /// the checkout it was built in
460        #[arg(long, value_name = "REQ")]
461        htl: Option<String>,
462        /// Leave the htlx (htl-x collections) dependency out of mlua-pkg.toml
463        #[arg(long)]
464        no_x: bool,
465    },
466    /// Fill in the scaffold files that are missing in an existing directory
467    Init {
468        dir: Option<PathBuf>,
469        #[arg(long)]
470        lib: bool,
471        /// Shorthand for --target bin
472        #[arg(long)]
473        embed: bool,
474        /// Fill in this target's files, and report the ones that were already there
475        #[arg(long, value_name = "NAME", value_parser = clap::builder::PossibleValuesParser::new(scaffold::target_names()))]
476        target: Option<String>,
477        /// The htl the project depends on: `release`, `main`, or `path:<checkout>`.
478        /// Without it, the htl this binary was built with — its version on crates.io, or
479        /// the checkout it was built in
480        #[arg(long, value_name = "REQ")]
481        htl: Option<String>,
482        /// Leave the htlx (htl-x collections) dependency out of mlua-pkg.toml
483        #[arg(long)]
484        no_x: bool,
485    },
486    /// Move the htl a project `htl new` already wrote depends on
487    ///
488    /// Rewrites `Cargo.toml`'s `htl` line (and `htl-mq`'s, under `--target window`),
489    /// `mise.toml`, and `[toolchain] htl` in `htl.toml` when the project has one, then runs
490    /// `cargo update` so `Cargo.lock` agrees — the same four things `--htl` decides for a
491    /// project that does not exist yet (see `htl new`). `release` is this CLI's own
492    /// version, `main` its repository's branch, `path:<checkout>` a local clone — a
493    /// relative one resolved against the current directory, the way the shell reads it,
494    /// not against `dir`, and refused when there is no `crates/htl/Cargo.toml` under it;
495    /// a number is refused, the way `htl new --htl <number>` is, naming the release's own
496    /// CLI instead. `--no-update` writes the three files and prints the `cargo update` to
497    /// run by hand rather than running it.
498    ///
499    /// A `Cargo.toml` whose `htl` (or `htl-mq`) line this command does not recognise —
500    /// anything but a bare version, or a table naming exactly one of `version` / `git` +
501    /// `branch` / `path` plus an optional `features` list — is left untouched: the fallback
502    /// is the `[patch.crates-io]` block this refusal prints. README, "Running against an
503    /// unpublished htl": https://github.com/ynishi/htl#running-against-an-unpublished-htl
504    ///
505    /// Exit 0 once everything above is moved; 2 when `--htl` is not one of the three (or
506    /// names no checkout), there is no `Cargo.toml` to move, the line is not recognised,
507    /// or `cargo update` fails (the files are rewritten either way — only the lock is left
508    /// unresolved).
509    #[command(after_long_help = "\
510Examples:
511  htl pin path:../htl            from this project's own directory, move it onto a sibling
512                                  checkout, for a change that has to be built against it
513  htl pin release                move it back once that checkout's change is released
514")]
515    Pin {
516        /// `release`, `main`, or `path:<checkout>` — the same names `htl new --htl` takes
517        htl: String,
518        /// Project root or any path inside it (default: current directory)
519        dir: Option<PathBuf>,
520        /// Write the three files and print the `cargo update` to run, rather than running it
521        #[arg(long)]
522        no_update: bool,
523    },
524    /// Package management at the nearest `mlua-pkg.toml` project root: install / add /
525    /// update / clean / patch, through mlua-pkg's library rather than its binary
526    Pkg {
527        #[command(subcommand)]
528        cmd: PkgCmd,
529    },
530    /// Bring declarations for a library into `types/`
531    Types {
532        #[command(subcommand)]
533        cmd: TypesCmd,
534    },
535    /// Manage the check cache
536    ///
537    /// README, "Caching": https://github.com/ynishi/htl#caching
538    Cache {
539        #[command(subcommand)]
540        cmd: CacheCmd,
541    },
542    /// Format .tl files in place (indentation / blank lines / trailing whitespace)
543    Fmt {
544        paths: Vec<PathBuf>,
545        /// Do not write; exit 1 if any file would change
546        #[arg(long)]
547        check: bool,
548        /// Indent width in spaces (default: `[fmt] indent` in htl.toml, else 3)
549        #[arg(long)]
550        indent: Option<usize>,
551    },
552    /// Emit readable Lua for one .tl file (escape hatch)
553    ///
554    /// Line n of the output is line n of the .tl, so a run-time error names the .tl line.
555    /// A comment-only line (`--` or `---` after indentation, not a `--[[` opener) survives
556    /// verbatim at its line, `---` doc included, and so do the comment lines after the last
557    /// line of code (the `---` after a module's `return`): the output ends with the last of
558    /// them. A trailing comment (after code) and a block comment (`--[[ ... ]]`) do not
559    /// survive: the trailing one is dropped from its line, and every line of the block comes
560    /// out empty, a `--` line inside it included.
561    Gen {
562        file: PathBuf,
563        #[arg(short, long)]
564        out: Option<PathBuf>,
565    },
566    /// Run a .tl script or a .hb bundle
567    Run {
568        file: PathBuf,
569        #[arg(trailing_var_arg = true)]
570        args: Vec<String>,
571    },
572    /// Compile a directory of .tl into a stripped-bytecode bundle
573    ///
574    /// A bundle is the `hb` target, so this refuses a project whose `[build] target` is
575    /// another one — a `bin` or a `cdylib` is built with `cargo build`.
576    ///
577    /// README, "Bundles": https://github.com/ynishi/htl#bundles-htl-build
578    #[command(after_long_help = "\
579Examples:
580  htl build src/main.tl -o app.hb
581                                 the entry's require closure, as one bundle
582  htl build src/main.tl -o app.hb --debug
583                                 keep line numbers and local names in the bytecode
584  htl build src/main.tl -o app.hb --host mymod
585                                 leave a module the host provides out of the bundle
586  htl run app.hb                 run what was built
587
588Caching: https://github.com/ynishi/htl#caching
589")]
590    /// The closure is judged by the project's `[lint.rules]` as `htl check` judges it — a
591    /// rule the project turned off is not reported here either — and no bundle is written
592    /// when it fails. A project whose `[build] target` is not `hb` is refused, naming the
593    /// command that does build it: the key records a decision the project made rather
594    /// than a note about itself, and dropping it from `htl.toml` is how a project with
595    /// Rust in it asks for a bundle anyway.
596    Build {
597        /// Entry `.tl` file: it and everything it requires are bundled, replaying from
598        /// the run cache what still holds (a directory bundles every `.tl` under it, the
599        /// older snapshot form, which is not cached)
600        entry: PathBuf,
601        #[arg(short, long, default_value = "app.hb")]
602        out: PathBuf,
603        /// Entry module name when `entry` is a directory
604        #[arg(short, long, default_value = "main")]
605        main: String,
606        /// Keep debug info (line numbers, local names) in the bytecode
607        #[arg(long)]
608        debug: bool,
609        /// Store generated Lua source instead of bytecode: for a big-endian target, a Lua
610        /// with non-default number types, or a bundle that must outlive a Lua upgrade
611        #[arg(long)]
612        source: bool,
613        /// Modules to bundle that only a dynamic require reaches (also `[build] extra`)
614        #[arg(long, value_delimiter = ',')]
615        extra: Vec<String>,
616        /// Modules the host provides, besides the project's own list (`#[host_module]`, `[build] host`, `std.*`; `.d.tl`-only modules are implied)
617        #[arg(long, value_delimiter = ',')]
618        host: Vec<String>,
619        /// Generate every module even if its cached form still holds, and do not store
620        /// (the directory form is never cached)
621        #[arg(long)]
622        no_cache: bool,
623        /// Say why the cache was not used, and what this run did with it
624        #[arg(long)]
625        explain_cache: bool,
626    },
627    /// Report what no entry's `require` closure reaches: modules, and `mlua-pkg.toml`
628    /// dependencies
629    ///
630    /// README, "Unused": https://github.com/ynishi/htl#unused-htl-unused
631    #[command(after_long_help = "\
632Examples:
633  htl unused                     modules and dependencies no entry reaches
634  htl unused --exit-non-zero-on-unused
635                                 the same report, and a non-zero exit for a CI
636  htl unused --format json       the same report as one JSON document
637")]
638    Unused {
639        /// What to report on (default: the working directory). The walk is the project's
640        /// either way: a module reached only from `tests/` is reached
641        paths: Vec<PathBuf>,
642        /// Output format
643        #[arg(long, value_enum, default_value_t = Format::Text)]
644        format: Format,
645        /// Exit 1 when anything is reported (for CI)
646        #[arg(long)]
647        exit_non_zero_on_unused: bool,
648        /// Build the graph from a fresh check even if a cached one is available, and
649        /// store none
650        #[arg(long)]
651        no_cache: bool,
652        /// Say why the cache was not used, and what this run did with it
653        #[arg(long)]
654        explain_cache: bool,
655    },
656    /// Report how many declarations carry each of htl's nine markers, and where
657    ///
658    /// `applicable` is how many declarations a lint has found the evidence to call
659    /// candidates for a marker. Two rows have one so far: `---@struct`'s is what
660    /// `unmarked-struct` would report, a record built whole at every one of its
661    /// construction sites and left unmarked; `---@sealed`'s is what `unmarked-sealed`
662    /// would report, a record built and cast only in the file that declares it and left
663    /// unmarked. Every other row is empty (`-`), not `0`: a feature with no evidence
664    /// counted is a different claim from a feature nothing is applicable to, and this
665    /// command does not guess between them. `--detail` lists each candidate after the
666    /// row's marked declarations, with `(applicable: <reason>, N site(s))`. It is a
667    /// report, not a gate: the exit code is always 0.
668    /// README, "Adoption": https://github.com/ynishi/htl#adoption-htl-adopt
669    #[command(after_long_help = "\
670Examples:
671  htl adopt                      which of htl's nine markers this project writes, and how much
672  htl adopt --detail             the same, plus every marked declaration's file, line and name
673  htl adopt --format json        the same report as one JSON document
674")]
675    Adopt {
676        /// What to report on (default: the working directory). The walk behind the
677        /// census is always the whole project, so the check resolves and replays as
678        /// usual; the counts are of the files under the paths given here.
679        paths: Vec<PathBuf>,
680        /// Output format
681        #[arg(long, value_enum, default_value_t = Format::Text)]
682        format: Format,
683        /// List every marked declaration under the table, file:line and name, grouped by
684        /// feature, and each `applicable` candidate after them; the plain form prints
685        /// the table alone
686        #[arg(long)]
687        detail: bool,
688        /// Build the census from a fresh check even if a cached one is available, and
689        /// store none
690        #[arg(long)]
691        no_cache: bool,
692        /// Say why the cache was not used, and what this run did with it
693        #[arg(long)]
694        explain_cache: bool,
695    },
696    /// Which file `require("<module>")` resolves to, and what that hides
697    ///
698    /// The whole chain in search order: what is read, what is reachable and not read, and
699    /// which crate or dependency each one came from. An override on the search path is the
700    /// mechanism working, and this is how to see it. README, "Which file a name resolves
701    /// to": https://github.com/ynishi/htl#which-file-a-name-resolves-to-htl-resolve
702    #[command(after_long_help = "\
703Examples:
704  htl resolve mq                 the chain for require(\"mq\")
705  htl resolve socket.http        a name with dots, as a require spells it
706  htl resolve mq --format json   the same rows as one JSON document
707
708Exits 1 when the name resolves to nothing, when two files implement it, or when a file of
709the project implements a name the host provides, so a script can ask.
710
711`htl.test` is answered like a host-provided name: no file of the project implements it
712(`htl test` preloads it into the state it runs), and its one row is the declaration the
713binary carries, `provided by the environment`.
714")]
715    Resolve {
716        /// The module name a `require` would spell (`mq`, `socket.http`)
717        module: String,
718        /// A path inside the project (default: the working directory)
719        path: Option<PathBuf>,
720        /// Output format
721        #[arg(long, value_enum, default_value_t = Format::Text)]
722        format: Format,
723    },
724    /// Read a `.hb` bundle without running it
725    ///
726    /// README, "Bundles": https://github.com/ynishi/htl#bundles-htl-build
727    Bundle {
728        #[command(subcommand)]
729        cmd: BundleCmd,
730    },
731    /// A language server on stdio whose diagnostics are `htl check`'s
732    ///
733    /// Speaks the Language Server Protocol on stdin and stdout; an editor starts it as a
734    /// child process. A file is checked when it is opened and when it is saved, from disk,
735    /// under the project it belongs to: the same resolution rules, lint levels,
736    /// `-- htl: allow(..)` comments and `.d.tl` declarations `htl check` applies, and the
737    /// same messages and lines. Saving a file also re-checks the other open files that
738    /// require it. A change to `htl.toml`, `mlua-pkg.toml`, `mlua-pkg.lock`, a `.d.tl` under
739    /// `types/` or the Rust `htl dts` reads re-checks the open files of that project when the
740    /// client watches files (`didChangeWatchedFiles`); for any client, the three
741    /// manifests are noticed at the next open or save in the project. Diagnostics are not computed
742    /// while typing. Anything the server has to say about itself goes to stderr.
743    Lsp,
744}
745
746/// The command line as clap holds it, before any argument is parsed.
747///
748/// The help is documentation, and the tests that keep it from rotting read it from here
749/// rather than from a copy kept beside them: they walk this tree and ask each command,
750/// argument and value for the strings it would print.
751pub fn command() -> clap::Command {
752    <Cli as clap::CommandFactory>::command()
753}
754
755/// `htl::registry::register_installer("htl-std", htl_std::install, htl_std::PROVIDES)`,
756/// once.
757///
758/// This binary links both `htl-core` and `htl-std` at once, which is exactly the seam
759/// `htl_core::registry`'s own doc describes ("Why this exists"): `htl-core` cannot depend
760/// on `htl-std` back (`htl-std -> htl -> htl-core -> htl-std` would be a cycle), so only a
761/// binary that sits above both can hand `htl-core` the function pointer. Doing it here —
762/// in `run()`, before anything parses a command — rather than in `htl-core` itself is the
763/// whole reason the registry module exists; see that module's doc for the rest. The call
764/// goes through `htl::registry` rather than naming `htl-core` directly: `htl` already
765/// re-exports it (`pub use htl_core::*`), and this crate has no other reason to depend on
766/// `htl-core` itself.
767///
768/// Registering the same name twice is refused (`register_installer`'s own doc, "Order and
769/// duplicates"), so this is behind a `std::sync::Once`: `run()` is the entry both `htl` and
770/// `cargo-htl` call, once each per process, which is enough on its own, but an in-process
771/// caller could call it more than once without the guard.
772fn register_libraries() {
773    static ONCE: std::sync::Once = std::sync::Once::new();
774    ONCE.call_once(|| {
775        htl::registry::register_installer("htl-std", htl_std::install, htl_std::PROVIDES)
776            .expect("\"htl-std\" registered twice");
777    });
778}
779
780/// Parse the command line and run the command; the exit code.
781///
782/// An exit of 1 is a verdict — an error in a file, a finding at `deny`, a failing test, a
783/// name that resolves to nothing ([`htl::verdict`]). A command that could not get as far
784/// as a verdict — a directory in no project, an `htl.toml` that does not parse, a file it
785/// cannot read, a flag it does not take — says why and exits 2, whichever command it was
786/// (clap exits 2 on a bad flag, and an `Err` from any command lands on the same arm).
787pub fn run() -> ExitCode {
788    register_libraries();
789    // Invoked as `cargo htl ...` -> argv = ["cargo-htl", "htl", ...]; drop the "htl".
790    let mut argv: Vec<String> = std::env::args().collect();
791    let is_cargo = Path::new(&argv[0])
792        .file_stem()
793        .and_then(|s| s.to_str())
794        .map(|s| s == "cargo-htl")
795        .unwrap_or(false);
796    if is_cargo && argv.get(1).map(String::as_str) == Some("htl") {
797        argv.remove(1);
798    }
799    match real_main(Cli::parse_from(argv)) {
800        Ok(code) => code,
801        Err(e) => {
802            eprintln!("htl: {e:#}");
803            ExitCode::from(2)
804        }
805    }
806}
807
808fn real_main(cli: Cli) -> Result<ExitCode> {
809    match cli.cmd {
810        Cmd::Check {
811            paths,
812            strict,
813            lint,
814            list_lints,
815            explain,
816            format,
817            no_cache,
818            cache_mode,
819            explain_cache,
820            statistics,
821        } => cmd_check(
822            &paths,
823            lint.as_deref(),
824            CheckFlags {
825                strict,
826                list_lints,
827                explain_rule: explain,
828                json: format == Format::Json,
829                use_cache: !no_cache,
830                cache_mode: cache_mode.map(Into::into),
831                explain: explain_cache,
832                statistics,
833            },
834        ),
835        Cmd::Fmt {
836            paths,
837            check,
838            indent,
839        } => cmd_fmt(&paths, check, indent),
840        Cmd::Test {
841            paths,
842            filter,
843            lib,
844            lint,
845            fail_fast,
846            allow_empty,
847            verbose,
848            quiet,
849            slow,
850            update,
851            coverage,
852            coverage_lines,
853            lcov,
854            junit,
855            seed,
856            format,
857            no_cache,
858            explain_cache,
859        } => cmd_test(
860            &paths,
861            filter.as_deref(),
862            &lib,
863            lint.as_deref(),
864            TestFlags {
865                fail_fast,
866                allow_empty,
867                verbose,
868                quiet,
869                slow,
870                update,
871                coverage: coverage || lcov.is_some(),
872                coverage_lines,
873                lcov,
874                junit,
875                json: format == Format::Json,
876                no_cache,
877                explain: explain_cache,
878                seed,
879            },
880        ),
881        Cmd::Pkg { cmd } => match cmd {
882            PkgCmd::Install => cmd_pkg_install(),
883            PkgCmd::Add {
884                name,
885                git,
886                tag,
887                rev,
888                branch,
889                entry,
890                target_dir,
891            } => cmd_pkg_add(htl::pkg::mlua_pkg::ops::AddSpec {
892                name,
893                git,
894                tag,
895                rev,
896                branch,
897                entry,
898                target_dir,
899            }),
900            PkgCmd::Update {
901                name,
902                dry_run,
903                force,
904            } => cmd_pkg_update(htl::pkg::mlua_pkg::ops::UpdateOpts {
905                name,
906                dry_run,
907                force,
908            }),
909            PkgCmd::Clean { all } => cmd_pkg_clean(all),
910            PkgCmd::Patch { dep, force } => cmd_pkg_patch(&dep, force),
911        },
912        Cmd::Types { cmd } => match cmd {
913            TypesCmd::Add {
914                library,
915                from,
916                force,
917            } => cmd_types_add(&library, from.as_deref(), force),
918        },
919        Cmd::Cache { cmd } => match cmd {
920            CacheCmd::Clear { path } => cmd_cache_clear(path.as_deref()),
921            CacheCmd::Status {
922                path,
923                format,
924                entries,
925            } => cmd_cache_status(path.as_deref(), format == Format::Json, entries),
926        },
927        Cmd::Dts { dir } => cmd_dts(dir.as_deref()),
928        Cmd::Shapes { dir } => cmd_shapes(dir.as_deref()),
929        Cmd::New {
930            name,
931            lib,
932            embed,
933            target,
934            htl,
935            no_x,
936        } => cmd_new(&name, lib, embed, target.as_deref(), htl.as_deref(), no_x),
937        Cmd::Init {
938            dir,
939            lib,
940            embed,
941            target,
942            htl,
943            no_x,
944        } => cmd_init(
945            dir.as_deref(),
946            lib,
947            embed,
948            target.as_deref(),
949            htl.as_deref(),
950            no_x,
951        ),
952        Cmd::Pin {
953            htl,
954            dir,
955            no_update,
956        } => pin::cmd_pin(&htl, dir, no_update),
957        Cmd::Gen { file, out } => cmd_gen(&file, out.as_deref()),
958        Cmd::Run { file, args } => cmd_run(&file, &args),
959        Cmd::Fix {
960            paths,
961            rule,
962            unsafe_fixes,
963            dry_run,
964            diff,
965            allow_dirty,
966            allow_no_vcs,
967            exit_non_zero_on_fix,
968            format,
969        } => cmd_fix(
970            &paths,
971            FixFlags {
972                rule,
973                unsafe_fixes,
974                dry_run: dry_run || diff,
975                diff,
976                allow_dirty,
977                allow_no_vcs,
978                exit_non_zero_on_fix,
979                json: format == Format::Json,
980            },
981        ),
982        Cmd::Build {
983            entry,
984            out,
985            main,
986            debug,
987            source,
988            extra,
989            host,
990            no_cache,
991            explain_cache,
992        } => cmd_build(
993            &entry,
994            &out,
995            &main,
996            htl::link::LinkOptions {
997                debug,
998                source,
999                extra,
1000                host,
1001                entry_name: None,
1002            },
1003            BuildCache {
1004                use_cache: !no_cache,
1005                explain: explain_cache,
1006            },
1007        ),
1008        Cmd::Unused {
1009            paths,
1010            format,
1011            exit_non_zero_on_unused,
1012            no_cache,
1013            explain_cache,
1014        } => cmd_unused(
1015            &paths,
1016            UnusedFlags {
1017                json: format == Format::Json,
1018                fail_on_unused: exit_non_zero_on_unused,
1019                use_cache: !no_cache,
1020                explain: explain_cache,
1021            },
1022        ),
1023        Cmd::Adopt {
1024            paths,
1025            format,
1026            detail,
1027            no_cache,
1028            explain_cache,
1029        } => cmd_adopt(
1030            &paths,
1031            AdoptFlags {
1032                json: format == Format::Json,
1033                detail,
1034                use_cache: !no_cache,
1035                explain: explain_cache,
1036            },
1037        ),
1038        Cmd::Resolve {
1039            module,
1040            path,
1041            format,
1042        } => cmd_resolve(&module, path.as_deref(), format == Format::Json),
1043        Cmd::Bundle { cmd } => match cmd {
1044            BundleCmd::Info { file, format } => cmd_bundle_info(&file, format == Format::Json),
1045        },
1046        Cmd::Lsp => lsp::serve(),
1047    }
1048}
1049
1050/// The reporting layer, for the commands that report without a `--format` of their own.
1051///
1052/// `htl check` and `htl test` hand their findings to a [`project::Sink`] over a
1053/// [`report::Out`]; `gen` / `run` / `build` used to loop `eprintln!` over the three
1054/// vectors themselves, so the same finding read differently depending on which verb was
1055/// typed. They come through here instead, and there is one renderer again.
1056///
1057/// One sink per command run, not per file: what the layer decides once per run — a
1058/// dependency's error said once, and never on behalf of a file the run checks itself —
1059/// is then once per *command*, which is what `htl build` over a closure of modules wants.
1060fn text_sink() -> project::Sink<report::Out> {
1061    project::Sink::new(report::Out::new(false, false))
1062}
1063
1064/// What else a verdict counted besides errors, for a summary line: a finding at `deny`,
1065/// and under `strict` every warning and lint. Empty when neither. Without it a run that
1066/// failed on a lint would end in `0 error(s)`.
1067fn judged(found: &htl::verdict::Findings, policy: &htl::verdict::Policy) -> String {
1068    let mut out = String::new();
1069    if policy.capped {
1070        return out;
1071    }
1072    if found.denied > 0 {
1073        out.push_str(&format!(", {} at deny", found.denied));
1074    }
1075    if policy.strict && found.warnings + found.lints > 0 {
1076        out.push_str(&format!(
1077            ", {} warning(s) and {} lint(s) under strict",
1078            found.warnings, found.lints
1079        ));
1080    }
1081    out
1082}
1083
1084/// Hand the checker the project's `[lang]`: whether `async` / `await` are keywords in the
1085/// files it is about to read. For a command that parses without a checker of the project's
1086/// ([`checker_for`] sets this itself).
1087fn config_lang(h: &Htl, cfg: &project::Config) -> Result<()> {
1088    h.set_lang(&project::lang_of(cfg))
1089}
1090
1091/// (Re)generate the `.d.tl` files this project declares: the ones a Rust crate's
1092/// `#[host_module]` / `#[derive(TealRecord)]` ask for, so the checker sees Rust-side
1093/// modules before any `cargo build`, and the module each `---@contract` type is declared
1094/// in, which is what an outside author writes their modules against. Quiet unless
1095/// something was written.
1096///
1097/// A contract that could not be published is said here, on every command that generates:
1098/// `htl check` reports it again as a lint (which `strict` makes fatal) and `htl dts`
1099/// exits non-zero on it, but `run` / `test` / `build` have neither, and a declaration
1100/// that is quietly not written is one an outside author finds missing later.
1101fn auto_dts(start: &Path) -> Result<()> {
1102    auto_dts_to(start, true)
1103}
1104
1105/// [`auto_dts`], writing only when `write` is set. A dry run (`htl fix --dry-run` /
1106/// `--diff`) works out every declaration the same way and says which it would write, and
1107/// what it then checks is the declarations as they are on disk — which is why it says so.
1108fn auto_dts_to(start: &Path, write: bool) -> Result<()> {
1109    let mut project = None;
1110    if let Some((root, _, cfg)) = load_config(start)? {
1111        // The contracts the project's model holds, published from there.
1112        let model = htl::model::Project::load(&root, cfg)?;
1113        let contracts: Vec<_> = model.contracts.iter().map(|c| c.terms.clone()).collect();
1114        let (results, problems) = htl::contract::publish_to(&root, &contracts, write);
1115        announce_dts_to(&results, &root, write);
1116        for p in &problems {
1117            eprintln!("dts: {p}");
1118        }
1119        project = Some(root);
1120    }
1121    let Some(root) = htl::dts::find_cargo_package_root(start) else {
1122        return Ok(());
1123    };
1124    let results =
1125        htl::dts::generate_crate_to(&root, write).map_err(|e| anyhow::anyhow!("htl dts: {e}"))?;
1126    announce_dts_to(&results, &root, write);
1127    let types_root = project.unwrap_or_else(|| root.clone());
1128    let report = dep_dts_to(&root, &types_root, write);
1129    announce_dts_to(&report.written, &types_root, write);
1130    announce_dep_report(&report, "dts: ");
1131    Ok(())
1132}
1133
1134/// What `htl dts` has to say about the declarations its dependencies ship. Three kinds,
1135/// kept apart because they mean three different things to the reader and to the exit code
1136/// — which is the distinction the single `[htl <rule>]` suffix they all used to carry was
1137/// hiding. None of them is a lint: no rule name, nothing to configure or silence, and
1138/// nothing that reaches the `Sink`, so `htl check` neither counts one nor carries one in
1139/// `--format json` — it prints them, prefixed `dts:`, exactly as this command does.
1140#[derive(Default)]
1141struct DepReport {
1142    /// `(target, written)` pairs, in the shape the rest of the report prints.
1143    written: Vec<(PathBuf, bool)>,
1144    /// Asked for and not written. This, and only this, is what `htl dts` exits non-zero on.
1145    not_written: Vec<String>,
1146    /// Under `types/` and no longer shipped by anything. Reported, never deleted, and it
1147    /// fails nothing: what the file is for is the project's to say.
1148    left_in_place: Vec<String>,
1149    /// The crate graph would not resolve, so nothing was asked for — cargo could not be
1150    /// run, or the project's lockfile does not cover its manifest and the graph is read
1151    /// with `--locked`. Either way the committed declarations stand and the check that
1152    /// follows names the module if one is missing, which is why this fails nothing.
1153    unresolved: Option<String>,
1154}
1155
1156/// Materialise the declarations this project's dependencies ship, under
1157/// `<declaration root>/<crate>/` — the declaration root of the project at `types_root`,
1158/// `types/` unless its `htl.toml` says otherwise — and report on what happened to each.
1159///
1160/// The graph comes from `cargo metadata`, so this costs a subprocess on every command that
1161/// generates. Nothing is built, and nothing is downloaded for dependencies already fetched.
1162fn dep_dts(cargo_root: &Path, types_root: &Path) -> DepReport {
1163    dep_dts_to(cargo_root, types_root, true)
1164}
1165
1166/// [`dep_dts`], writing only when `write` is set.
1167fn dep_dts_to(cargo_root: &Path, types_root: &Path, write: bool) -> DepReport {
1168    let decls = match htl::dep_dts::resolve(cargo_root) {
1169        Ok(d) => d,
1170        Err(e) => {
1171            return DepReport {
1172                unresolved: Some(e),
1173                ..DepReport::default()
1174            };
1175        }
1176    };
1177    // Orphans before materialising: the note beside a crate's declarations is what says
1178    // which crate they came from, and the write below rewrites it.
1179    let types = match decl_root(types_root) {
1180        Ok(t) => t,
1181        Err(e) => {
1182            return DepReport {
1183                unresolved: Some(format!("{e:#}")),
1184                ..DepReport::default()
1185            };
1186        }
1187    };
1188    let left_in_place = htl::dep_dts::orphans(&types, &decls);
1189    let (written, not_written) = htl::dep_dts::materialise_to(&types, &decls, write);
1190    DepReport {
1191        written,
1192        not_written,
1193        left_in_place,
1194        unresolved: None,
1195    }
1196}
1197
1198/// The three report lists, each said as what it is. `wrote` / `unchanged` name what
1199/// happened to a declaration, and so do these: a reader who has never heard the words
1200/// `shipped-declaration` or `orphaned-declaration` can still tell which line failed the
1201/// command and which one is a file sitting there to decide about.
1202fn announce_dep_report(report: &DepReport, prefix: &str) {
1203    for p in &report.not_written {
1204        eprintln!("{prefix}not written: {p}");
1205    }
1206    for p in &report.left_in_place {
1207        eprintln!("{prefix}left in place: {p}");
1208    }
1209    if let Some(e) = &report.unresolved {
1210        eprintln!("{prefix}{e}");
1211    }
1212}
1213
1214/// What generation wrote, or for a run that did not write (`wrote` unset) what it would
1215/// have.
1216fn announce_dts_to(results: &[(PathBuf, bool)], root: &Path, wrote: bool) {
1217    for (target, changed) in results {
1218        if *changed {
1219            let target = target.strip_prefix(root).unwrap_or(target).display();
1220            if wrote {
1221                eprintln!("dts: wrote {target}");
1222            } else {
1223                eprintln!(
1224                    "dts: would write {target} (dry run: not written; checked against the \
1225                     file on disk)"
1226                );
1227            }
1228        }
1229    }
1230}
1231
1232fn cmd_dts(dir: Option<&Path>) -> Result<ExitCode> {
1233    let start = match dir {
1234        Some(d) => d.to_path_buf(),
1235        None => std::env::current_dir()?,
1236    };
1237    // The contract types first: a project may publish one without having a Rust host at
1238    // all, and the `bail!` below would then be wrong about there being nothing to do.
1239    let mut results = Vec::new();
1240    let mut failed = false;
1241    let mut project = None;
1242    if let Some((croot, _, cfg)) = load_config(&start)? {
1243        let model = htl::model::Project::load(&croot, cfg)?;
1244        let contracts: Vec<_> = model.contracts.iter().map(|c| c.terms.clone()).collect();
1245        let (published, problems) = htl::contract::publish(&croot, &contracts);
1246        for p in &problems {
1247            eprintln!("  {p}");
1248        }
1249        // Asked to generate and did not: the command says so in its exit code, or a CI
1250        // step that regenerates declarations would pass having written nothing.
1251        failed = !problems.is_empty();
1252        results.extend(published);
1253        project = Some(croot);
1254    }
1255    let code = |failed: bool| {
1256        if failed {
1257            ExitCode::FAILURE
1258        } else {
1259            ExitCode::SUCCESS
1260        }
1261    };
1262    let Some(root) = htl::dts::find_cargo_package_root(&start) else {
1263        if results.is_empty() && !failed {
1264            bail!(
1265                "no Cargo.toml with a [package] section, and no ---@contract type to publish, \
1266                 at or above {}",
1267                start.display()
1268            );
1269        }
1270        report_dts(&results, &start);
1271        return Ok(code(failed));
1272    };
1273    results.extend(htl::dts::generate_crate(&root).map_err(|e| anyhow::anyhow!("{e}"))?);
1274    let types_root = project.unwrap_or_else(|| root.clone());
1275    let report = dep_dts(&root, &types_root);
1276    announce_dep_report(&report, "  ");
1277    // An orphan, and a graph that would not resolve, are reports. A declaration asked for
1278    // and not written is a failure, the same as a contract that could not be published.
1279    failed = failed || !report.not_written.is_empty();
1280    results.extend(report.written);
1281    report_dts(&results, &root);
1282    Ok(code(failed))
1283}
1284
1285fn report_dts(results: &[(PathBuf, bool)], root: &Path) {
1286    let mut written = 0usize;
1287    for (target, w) in results {
1288        let rel = target.strip_prefix(root).unwrap_or(target).display();
1289        eprintln!("  {} {}", if *w { "wrote    " } else { "unchanged" }, rel);
1290        written += usize::from(*w);
1291    }
1292    eprintln!(
1293        "htl dts: {} declaration(s) from {}, {} written",
1294        results.len(),
1295        root.display(),
1296        written
1297    );
1298}
1299
1300/// One source file `htl shapes` read, with the check that was kept for it.
1301struct ShapeFile {
1302    /// The project-relative path, for the header and the diagnostics.
1303    rel: PathBuf,
1304    /// The module name the file answers to.
1305    module: String,
1306    info: htl::CheckInfo,
1307}
1308
1309/// What became of one file `htl shapes` writes.
1310enum Generated {
1311    Wrote,
1312    Current,
1313    /// It was its own and the module it was generated for lowers to nothing now.
1314    Removed,
1315    /// It exists and does not start with the header `htl shapes` writes: not its own.
1316    Foreign,
1317}
1318
1319/// Whether the file at `path` exists and starts with the header `htl shapes` writes.
1320fn written_by_shapes(path: &Path) -> bool {
1321    fs::read_to_string(path)
1322        .map(|cur| cur.starts_with(htl::shapes::HEADER))
1323        .unwrap_or(false)
1324}
1325
1326/// The two files a module lowers to: `<source>/shapes/<module>.lua` and
1327/// `<types>/shapes/<module>.d.tl`.
1328fn shape_targets(source: &Path, types: &Path, module: &str) -> [PathBuf; 2] {
1329    let name = module.replace('.', "/");
1330    [
1331        source.join("shapes").join(format!("{name}.lua")),
1332        types.join("shapes").join(format!("{name}.d.tl")),
1333    ]
1334}
1335
1336/// Remove a file `htl shapes` wrote for a module that lowers to nothing now. `None` when
1337/// there is no such file; a file that is not its own is left (`Foreign`).
1338fn remove_generated(path: &Path) -> Result<Option<Generated>> {
1339    if !path.exists() {
1340        return Ok(None);
1341    }
1342    if !written_by_shapes(path) {
1343        return Ok(Some(Generated::Foreign));
1344    }
1345    fs::remove_file(path).with_context(|| format!("removing {}", path.display()))?;
1346    Ok(Some(Generated::Removed))
1347}
1348
1349/// `path` as the table of shapes spells it: symlinks and `..` resolved.
1350fn canonical_path(p: &Path) -> PathBuf {
1351    fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf())
1352}
1353
1354/// Write `text` to `path` when it would change, unless the file is somebody's: a target
1355/// that exists and whose first line is not the one `htl shapes` writes is left as it is.
1356fn write_generated(path: &Path, text: &str) -> Result<Generated> {
1357    if path.exists() && !written_by_shapes(path) {
1358        return Ok(Generated::Foreign);
1359    }
1360    let changed = htl::write_if_changed_when(path, text, true)
1361        .with_context(|| format!("writing {}", path.display()))?;
1362    Ok(if changed {
1363        Generated::Wrote
1364    } else {
1365        Generated::Current
1366    })
1367}
1368
1369/// `htl shapes`: the module doc of `htl::shapes` says what is lowered and how; this is the
1370/// part that reads and writes files.
1371///
1372/// Two passes over the project's sources, because a shape names the shapes of other files:
1373/// the first checks every file and collects where each declaration is ([`htl::shapes::RefTable`]),
1374/// the second lowers each file against that table and writes what came out. A file with a
1375/// problem is not written, the others are, and the exit code says so.
1376fn cmd_shapes(dir: Option<&Path>) -> Result<ExitCode> {
1377    let start = match dir {
1378        Some(d) => d.to_path_buf(),
1379        None => std::env::current_dir()?,
1380    };
1381    let ProjectChecker { h, cfg, model, .. } = checker_for(&start, true)?;
1382    let Some((root, _, _)) = cfg else {
1383        bail!(
1384            "no htl.toml at or above {}: `htl shapes` writes into a project",
1385            start.display()
1386        );
1387    };
1388    let Some(model) = model else {
1389        bail!("no project at or above {}", start.display());
1390    };
1391    // The generated shapes are written against lshape, which this project supplies. An
1392    // implementation, not a declaration: after a first run `types/lshape.d.tl` is a row of
1393    // its own, and says nothing about whether a Lua module is there to load.
1394    let lshape = htl::resolve::resolve(&h, "lshape", Some(&root), Some(&model))?;
1395    if htl::shapes::lshape_runtime(&lshape).is_none() {
1396        eprintln!(
1397            "shapes: lshape is not on the path (no Lua or Teal module answers require(\"lshape\")); vendor or install it, then rerun"
1398        );
1399        return Ok(ExitCode::FAILURE);
1400    }
1401    let own = model.own();
1402    let Some(source) = own.roots.source.clone() else {
1403        bail!("the project at {} has no source directory", root.display());
1404    };
1405    let types = own.roots.decl.clone().unwrap_or_else(|| root.join("types"));
1406    let shown = |p: &Path| p.strip_prefix(&root).unwrap_or(p).to_path_buf();
1407
1408    // The files, and the modules they answer to. A module called `shapes` is in the
1409    // namespace the generated modules are written under.
1410    let paths = [source.clone()];
1411    let skip = project::not_walked(Some(&model), &paths, htl::model::Purpose::Own);
1412    let mut modules = Vec::new();
1413    let mut collides = false;
1414    for file in htl::collect_tl_skipping(&paths, &skip)? {
1415        let Some(place) = model.locate(&file) else {
1416            continue;
1417        };
1418        if place.module.owner != htl::model::Owner::Own {
1419            continue;
1420        }
1421        if place.name == "shapes" || place.name.starts_with("shapes.") {
1422            eprintln!(
1423                "shapes: module `{}` ({}) is in the namespace the generated modules are written under (shapes.*); rename it, then rerun",
1424                place.name,
1425                shown(&file).display()
1426            );
1427            collides = true;
1428        }
1429        modules.push((file, place.name));
1430    }
1431    if collides {
1432        return Ok(ExitCode::FAILURE);
1433    }
1434
1435    // Pass 1: check every file, keeping the index.
1436    let mut sink = text_sink();
1437    let mut failed = false;
1438    let mut checked = Vec::new();
1439    for (file, module) in modules {
1440        let info = h.check_indexed(&file)?;
1441        // A file whose check fails is reported, fails the run, and is lowered all the same:
1442        // the index is kept for any file that parsed, and a field whose type did not
1443        // resolve is refused at that field by the lowering itself. What this buys is the
1444        // first run. A file that requires `shapes.<m>` before that module exists fails its
1445        // check on that line and nothing else; written anyway, the second run is clean.
1446        // Skipped, a file that asserts its own records could never be written at all.
1447        if runs_fail(&info) {
1448            sink.checkinfo(&info);
1449            failed = true;
1450        }
1451        checked.push(ShapeFile {
1452            rel: shown(&file),
1453            module,
1454            info,
1455        });
1456    }
1457    let mut refs = htl::shapes::RefTable::new(canonical_path);
1458    for f in &checked {
1459        if let Some(decls) = f.info.types.declarations() {
1460            refs.extend(htl::shapes::declared_shapes(&f.module, decls));
1461        }
1462    }
1463
1464    // Pass 2: lower each file against the table.
1465    let lowered: Vec<htl::shapes::Lowered> = checked
1466        .iter()
1467        .map(|f| {
1468            htl::shapes::lower(
1469                &htl::shapes::Source {
1470                    module: &f.module,
1471                    rel_path: &f.rel,
1472                    info: &f.info,
1473                },
1474                &refs,
1475            )
1476        })
1477        .collect();
1478    // Shapes that need each other's cannot be loaded: Lua's `require` would loop.
1479    let edges: Vec<(String, Vec<String>)> = checked
1480        .iter()
1481        .zip(&lowered)
1482        .filter_map(|(f, l)| match l {
1483            htl::shapes::Lowered::Written { requires, .. } => {
1484                Some((f.module.clone(), requires.clone()))
1485            }
1486            _ => None,
1487        })
1488        .collect();
1489    let cycles = htl::shapes::module_cycles(&edges);
1490    let in_cycle = |module: &str| cycles.iter().find(|g| g.iter().any(|m| m == module));
1491
1492    // A module that is refused keeps the files an earlier run wrote for it, which still
1493    // validate against the old shape: said, not left standing in silence.
1494    let say_stale = |module: &str| {
1495        for target in shape_targets(&source, &types, module) {
1496            if written_by_shapes(&target) {
1497                eprintln!(
1498                    "shapes: {} left as it was and is stale: the module is refused above",
1499                    shown(&target).display()
1500                );
1501            }
1502        }
1503    };
1504
1505    for (f, l) in checked.iter().zip(&lowered) {
1506        match l {
1507            htl::shapes::Lowered::Nothing => {
1508                // Its records are gone: so are the files that were written for them. Only
1509                // when the check kept an index; a file that did not parse says nothing about
1510                // its records, and its files stay.
1511                if f.info.types.declarations().is_some() {
1512                    for target in shape_targets(&source, &types, &f.module) {
1513                        if let Some(g) = remove_generated(&target)? {
1514                            failed |= announce_shape(&shown(&target), g);
1515                        }
1516                    }
1517                }
1518            }
1519            htl::shapes::Lowered::Refused { problems, notes } => {
1520                notes
1521                    .iter()
1522                    .chain(problems)
1523                    .for_each(|d| sink.diagnostic(d));
1524                say_stale(&f.module);
1525                failed = true;
1526            }
1527            htl::shapes::Lowered::Written { output, notes, .. } => {
1528                notes.iter().for_each(|d| sink.diagnostic(d));
1529                if let Some(group) = in_cycle(&f.module) {
1530                    let named: Vec<String> =
1531                        group.iter().map(|m| format!("`shapes.{m}`")).collect();
1532                    let (last, init) = named.split_last().expect("a cycle has members");
1533                    let all = if init.is_empty() {
1534                        last.clone()
1535                    } else {
1536                        format!("{} and {last}", init.join(", "))
1537                    };
1538                    sink.diagnostic(&htl::Diagnostic::new(
1539                        htl::Severity::Error,
1540                        f.rel.display().to_string(),
1541                        1,
1542                        1,
1543                        format!(
1544                            "{all} require each other, a `require` loop Lua cannot load; move the records they share into one module"
1545                        ),
1546                        None,
1547                    ));
1548                    say_stale(&f.module);
1549                    failed = true;
1550                    continue;
1551                }
1552                let [lua, dtl] = shape_targets(&source, &types, &f.module);
1553                failed |= announce_shape(&shown(&lua), write_generated(&lua, &output.lua)?);
1554                failed |= announce_shape(&shown(&dtl), write_generated(&dtl, &output.dtl)?);
1555            }
1556        }
1557    }
1558    let lshape_dtl = types.join("lshape.d.tl");
1559    failed |= announce_shape(
1560        &shown(&lshape_dtl),
1561        write_generated(&lshape_dtl, htl::shapes::LSHAPE_DTL)?,
1562    );
1563    Ok(if failed {
1564        ExitCode::FAILURE
1565    } else {
1566        ExitCode::SUCCESS
1567    })
1568}
1569
1570/// Say what became of one generated file, one line on stderr. `true` when the file was not
1571/// its to write, which fails the run.
1572fn announce_shape(rel: &Path, g: Generated) -> bool {
1573    match g {
1574        Generated::Wrote => eprintln!("shapes: wrote {}", rel.display()),
1575        Generated::Current => eprintln!("shapes: current {}", rel.display()),
1576        Generated::Removed => eprintln!("shapes: removed {}", rel.display()),
1577        Generated::Foreign => {
1578            eprintln!(
1579                "shapes: {} exists and was not written by htl shapes; move it aside",
1580                rel.display()
1581            );
1582            return true;
1583        }
1584    }
1585    false
1586}
1587
1588/// What the run wrote, and — when a target was asked for by name — what it left alone. The
1589/// kept list is what makes `htl init --target bin` on an older project say which of that
1590/// target's files were already there instead of skipping them in silence.
1591fn report_scaffold(dir: &Path, written: &[PathBuf], kept: &[PathBuf]) {
1592    let rel = |p: &PathBuf| p.strip_prefix(dir).unwrap_or(p).display().to_string();
1593    for p in written {
1594        eprintln!("  created {}", rel(p));
1595    }
1596    for p in kept {
1597        eprintln!("  kept    {} (already there)", rel(p));
1598    }
1599    if kept.is_empty() {
1600        eprintln!(
1601            "htl: {} file(s) written under {}",
1602            written.len(),
1603            dir.display()
1604        );
1605    } else {
1606        eprintln!(
1607            "htl: {} file(s) written, {} kept under {}",
1608            written.len(),
1609            kept.len(),
1610            dir.display()
1611        );
1612    }
1613}
1614
1615fn cmd_new(
1616    name: &str,
1617    lib: bool,
1618    embed: bool,
1619    target: Option<&str>,
1620    htl: Option<&str>,
1621    no_x: bool,
1622) -> Result<ExitCode> {
1623    let dir = PathBuf::from(name);
1624    let pkg_name = dir
1625        .file_name()
1626        .and_then(|s| s.to_str())
1627        .ok_or_else(|| anyhow::anyhow!("invalid project name: {name}"))?
1628        .to_string();
1629    // Before the directory is touched: an unknown target, or one that disagrees with
1630    // --lib, fails here and leaves nothing behind. So does an htl this scaffold does not
1631    // write for — a release by number — so it is settled first too.
1632    let target = scaffold::resolve_target(target, embed, lib)?;
1633    let htl = scaffold::HtlPin::parse(htl)?;
1634    let opts = scaffold::Options {
1635        lib,
1636        target,
1637        htl,
1638        no_x,
1639    };
1640    let done = scaffold::scaffold(&dir, &pkg_name, &opts, true)?;
1641    report_scaffold(&dir, &done.written, &[]);
1642    // The manifest names a dependency: the fetch comes before the first test, or the
1643    // first test is a `module not found`.
1644    let fetch = if opts.writes_htlx() {
1645        "htl pkg install && "
1646    } else {
1647        ""
1648    };
1649    // The window target's first build needs two files nothing has written yet: `htl check`
1650    // materialises the dependency's declaration as `types/htl-mq/mq.d.tl` (from its
1651    // `[package.metadata.htl] dts`) and writes `src/fx.d.tl` from the host module, and
1652    // `cargo build` reads the first of them when `include_bundle!` links `mq`. So the
1653    // check comes before the run rather than after it.
1654    let then = match opts.target.map(|t| t.target) {
1655        Some(BuildTarget::Window) => "htl check . && cargo run",
1656        _ => "htl test",
1657    };
1658    eprintln!("next: cd {} && {fetch}{then}", dir.display());
1659    Ok(ExitCode::SUCCESS)
1660}
1661
1662fn cmd_init(
1663    dir: Option<&Path>,
1664    lib: bool,
1665    embed: bool,
1666    target: Option<&str>,
1667    htl: Option<&str>,
1668    no_x: bool,
1669) -> Result<ExitCode> {
1670    let dir = match dir {
1671        Some(d) => d.to_path_buf(),
1672        None => std::env::current_dir()?,
1673    };
1674    let abs = std::fs::canonicalize(&dir).unwrap_or(dir.clone());
1675    let name = abs
1676        .file_name()
1677        .and_then(|s| s.to_str())
1678        .ok_or_else(|| anyhow::anyhow!("cannot derive a project name from {}", abs.display()))?
1679        .to_string();
1680    let asked_for_a_target = target.is_some() || embed;
1681    let target = scaffold::resolve_target(target, embed, lib)?;
1682    let htl = scaffold::HtlPin::parse(htl)?;
1683    let opts = scaffold::Options {
1684        lib,
1685        target,
1686        htl,
1687        no_x,
1688    };
1689    let done = scaffold::scaffold(&dir, &name, &opts, false)?;
1690    // A target was named: say what it would have written and found already there. Without
1691    // one the old one-liner stands, so a plain re-run does not list the whole tree.
1692    let kept: &[PathBuf] = if asked_for_a_target { &done.kept } else { &[] };
1693    if done.written.is_empty() && kept.is_empty() {
1694        eprintln!("htl init: nothing to do, all scaffold files already exist");
1695    } else {
1696        report_scaffold(&dir, &done.written, kept);
1697    }
1698    Ok(ExitCode::SUCCESS)
1699}
1700
1701/// The project `htl pkg` acts on: the one above the working directory, found the way
1702/// every command finds it ([`htl::model::Project::find_root`]), with its `mlua-pkg.toml`.
1703///
1704/// mlua-pkg reports a missing manifest as an I/O error that does not name the file, and the
1705/// path htl looked for is the whole of the answer, so it is checked here.
1706fn pkg_project() -> Result<htl::pkg::MluaProject> {
1707    let cwd = std::env::current_dir()?;
1708    let root = htl::model::Project::find_root(&cwd)?.map(|r| r.root);
1709    root.filter(|r| r.join(htl::pkg::MANIFEST_NAME).is_file())
1710        .map(|r| htl::pkg::MluaProject::at(&r))
1711        .with_context(|| {
1712            format!(
1713                "no {} above {}: `htl pkg` runs in a project",
1714                htl::pkg::MANIFEST_NAME,
1715                cwd.display()
1716            )
1717        })
1718}
1719
1720/// The declaration root of the project at `root`, from its model: `[layout] types`, which
1721/// is `types/` unless `htl.toml` says otherwise — where the declarations htl brings into a
1722/// project are written.
1723fn decl_root(root: &Path) -> Result<PathBuf> {
1724    let cfg = load_config(root)?;
1725    Ok(project::model_of(&cfg, root)?
1726        .and_then(|m| m.own().roots.decl.clone())
1727        .unwrap_or_else(|| root.join("types")))
1728}
1729
1730/// `htl pkg install`: fetch what the manifest declares, then bring in what the deps publish.
1731fn cmd_pkg_install() -> Result<ExitCode> {
1732    let project = pkg_project()?;
1733    let report = project.install()?;
1734    report_install(&report, &project);
1735    // Re-read the project: install wrote the lockfile the two reports below are read from.
1736    // A dep publishes its declarations at `types/` in its package root, which is not where
1737    // `require` looks, so they are copied in for the checker to see.
1738    let project = htl::pkg::MluaProject::at(&project.root);
1739    report_types_sync(
1740        &project.sync_types(&decl_root(&project.root)?)?,
1741        &project.root,
1742    );
1743    report_patch_drift(&project);
1744    Ok(ExitCode::SUCCESS)
1745}
1746
1747/// What install did, in the shape the other reports use. The library prints nothing.
1748fn report_install(
1749    report: &htl::pkg::mlua_pkg::ops::InstallReport,
1750    project: &htl::pkg::MluaProject,
1751) {
1752    use htl::pkg::mlua_pkg::ops::Placement;
1753    for w in &report.warnings {
1754        // A patch that was not used is reported below in htl's own verbs, where both ways
1755        // out are named. The library's line points at `mlua-pkg patch --force`, which
1756        // rebuilds the copy without asking git whether what is in it was committed.
1757        if w.contains("patch_dir") && w.contains("not used") {
1758            continue;
1759        }
1760        eprintln!("warning: {w}");
1761    }
1762    let rel = |p: &Path| {
1763        p.strip_prefix(&project.root)
1764            .unwrap_or(p)
1765            .display()
1766            .to_string()
1767    };
1768    for p in &report.packages {
1769        let sha: String = p.sha.chars().take(7).collect();
1770        let where_it_is = match (&p.placement, p.patched) {
1771            (_, true) => format!("from {}", rel(p.root())),
1772            (Placement::Copied(dest), _) => format!("copied to {}", rel(dest)),
1773            (Placement::Symlink(_), _) => p.entry.display().to_string(),
1774        };
1775        eprintln!("  install {} {sha} ({where_it_is})", p.name);
1776    }
1777    let n = report.packages.len();
1778    if report.transitive > 0 {
1779        eprintln!(
1780            "htl pkg install: {n} package(s), {} of them transitive",
1781            report.transitive
1782        );
1783    } else {
1784        eprintln!("htl pkg install: {n} package(s)");
1785    }
1786}
1787
1788/// `htl pkg add <name> <git>`: the manifest entry, without fetching anything.
1789///
1790/// This is the one verb that may run outside a project: mlua-pkg writes a manifest when
1791/// there is none. It writes it at the project's root — beside `htl.toml`, when the working
1792/// directory is in a project that has one — and in the working directory only outside any
1793/// project. Written in a subdirectory, it would give the project a second root, which
1794/// every command then refuses.
1795fn cmd_pkg_add(spec: htl::pkg::mlua_pkg::ops::AddSpec) -> Result<ExitCode> {
1796    use htl::pkg::mlua_pkg::ops::AddOutcome;
1797    let cwd = std::env::current_dir()?;
1798    let root = htl::model::Project::find_root(&cwd)?
1799        .map(|r| r.root)
1800        .unwrap_or(cwd);
1801    let project = htl::pkg::MluaProject::at(&root);
1802    let name = spec.name.clone();
1803    let done = project.add(spec)?;
1804    let manifest = project
1805        .manifest
1806        .strip_prefix(&project.root)
1807        .unwrap_or(&project.manifest)
1808        .display();
1809    if done.report.manifest_created {
1810        eprintln!("  created {manifest}");
1811    }
1812    let verb = match done.report.outcome {
1813        AddOutcome::Added => "added",
1814        AddOutcome::Replaced => "updated",
1815    };
1816    eprintln!("  {verb}   {name} in {manifest}");
1817    if let Some(dir) = &done.kept_patch_dir {
1818        // `add` rewrites the entry, and its spec has no room for a patch. Putting the key
1819        // back is the difference between the project resolving that dependency from its own
1820        // copy and resolving it from upstream with the copy left unread in the tree.
1821        eprintln!(
1822            "  kept    patch_dir = {} ({name} is patched)",
1823            dir.display()
1824        );
1825    }
1826    eprintln!("htl pkg add: run `htl pkg install` to fetch it");
1827    Ok(ExitCode::SUCCESS)
1828}
1829
1830/// `htl pkg update [name]`: what each pin does next, then the install that follows.
1831fn cmd_pkg_update(opts: htl::pkg::mlua_pkg::ops::UpdateOpts) -> Result<ExitCode> {
1832    use htl::pkg::mlua_pkg::ops::UpdateOutcome;
1833    let project = pkg_project()?;
1834    let dry_run = opts.dry_run;
1835    let report = project.update(opts)?;
1836    if report.entries.is_empty() {
1837        eprintln!("htl pkg update: no dependency selected");
1838        return Ok(ExitCode::SUCCESS);
1839    }
1840    for (name, outcome) in &report.entries {
1841        match outcome {
1842            UpdateOutcome::TagBumped { old, new } => {
1843                eprintln!("  update  {name}: tag {old} -> {new}")
1844            }
1845            UpdateOutcome::PrefixResolved { pin, resolved } => eprintln!(
1846                "  update  {name}: prefix '{pin}' is {resolved} (the manifest keeps the prefix)"
1847            ),
1848            UpdateOutcome::Refresh => eprintln!("  update  {name}: refresh (branch or unpinned)"),
1849            UpdateOutcome::Skipped(why) => eprintln!("  skip    {name}: {why}"),
1850        }
1851    }
1852    if dry_run {
1853        eprintln!("htl pkg update: dry run; nothing was written");
1854        return Ok(ExitCode::SUCCESS);
1855    }
1856    if let Some(install) = &report.install {
1857        report_install(install, &project);
1858        let project = htl::pkg::MluaProject::at(&project.root);
1859        report_types_sync(
1860            &project.sync_types(&decl_root(&project.root)?)?,
1861            &project.root,
1862        );
1863        report_patch_drift(&project);
1864    }
1865    Ok(ExitCode::SUCCESS)
1866}
1867
1868/// `htl pkg clean`: the cache, which is machine-local and rebuilt by the next install.
1869fn cmd_pkg_clean(all: bool) -> Result<ExitCode> {
1870    use htl::pkg::mlua_pkg::ops::CleanReport;
1871    let project = pkg_project()?;
1872    match project.clean(all)? {
1873        CleanReport::CacheRemoved => eprintln!("htl pkg clean: removed every cached package"),
1874        CleanReport::NoLockfile => {
1875            eprintln!("htl pkg clean: no lockfile, so nothing was ever cached")
1876        }
1877        CleanReport::StaleRemoved { removed: 0 } => eprintln!("htl pkg clean: nothing to remove"),
1878        CleanReport::StaleRemoved { removed } => eprintln!(
1879            "htl pkg clean: removed {removed} cache entr{} the lockfile no longer refers to",
1880            if removed == 1 { "y" } else { "ies" }
1881        ),
1882    }
1883    Ok(ExitCode::SUCCESS)
1884}
1885
1886/// A patched copy the dependency is no longer resolved from, said after every install
1887/// until it is refreshed or removed.
1888///
1889/// A patch is bound to the revision it was taken from, and an upgrade moves the pin off it.
1890/// Install does not fail over that — the project builds against the new upstream — so the
1891/// only thing that keeps the copy from being forgotten in the tree is being told about it
1892/// each time. Both ways out are named, in htl's verbs: mlua-pkg's own warning points at
1893/// `mlua-pkg patch --force`, which skips the question htl asks git before overwriting.
1894fn report_patch_drift(project: &htl::pkg::MluaProject) {
1895    let short = |s: &str| s.chars().take(7).collect::<String>();
1896    for s in project.patch_status() {
1897        if s.in_use {
1898            continue;
1899        }
1900        let rel = s
1901            .dir
1902            .strip_prefix(&project.root)
1903            .unwrap_or(&s.dir)
1904            .display();
1905        let why = match (&s.base, &s.locked) {
1906            _ if !s.dir.is_dir() => "the copy is not there".to_string(),
1907            (None, _) => "the lockfile records no revision it was taken from".to_string(),
1908            (Some(b), Some(l)) => {
1909                format!("taken from {}, {} is now at {}", short(b), s.name, short(l))
1910            }
1911            (Some(b), None) => format!("taken from {}, and nothing is locked", short(b)),
1912        };
1913        eprintln!("  patch   {rel} is not in use ({why})");
1914        eprintln!(
1915            "          carry the change forward: commit it, then `htl pkg patch {}`",
1916            s.name
1917        );
1918        eprintln!("          drop it: remove patch_dir from mlua-pkg.toml and delete {rel}");
1919    }
1920}
1921
1922/// `htl pkg patch <dep>`: the dependency's source, taken into `patches/<dep>/` where the
1923/// project owns it. Not a passthrough — htl decides the directory, writes `patch_dir` into
1924/// the manifest and answers the "may this be overwritten" question against git; the copy
1925/// and the `patch_base` bookkeeping are mlua-pkg's. See `Project::patch`.
1926fn cmd_pkg_patch(dep: &str, force: bool) -> Result<ExitCode> {
1927    let project = pkg_project().context("a patch belongs to a project, so this runs in one")?;
1928    let done = project.patch(dep, force)?;
1929    let report = &done.report;
1930    let rel = report
1931        .patch_dir
1932        .strip_prefix(&project.root)
1933        .unwrap_or(&report.patch_dir);
1934    let verb = if report.created { "patched" } else { "rebuilt" };
1935    let base: String = report.base.chars().take(7).collect();
1936    eprintln!("  {verb} {} ({dep} at {base})", rel.display());
1937    // Only when there was something to drop: on a dependency whose repository holds
1938    // nothing but the package this line would be noise, and the reader would learn the
1939    // rule from a line that says nothing happened.
1940    if !done.dropped.is_empty() {
1941        eprintln!(
1942            "  dropped {} (the repository's, not the package's)",
1943            done.dropped.join(", ")
1944        );
1945    }
1946    eprintln!("htl: it is the project's code now — edit it, commit it, then `htl pkg install`.");
1947    Ok(ExitCode::SUCCESS)
1948}
1949
1950/// What the deps published, in the shape `htl init` reports its scaffold. A name that was
1951/// already taken is said out loud: nothing was overwritten, and the project is the one
1952/// that decides which declaration it wants.
1953fn report_types_sync(sync: &htl::pkg::TypesSync, root: &Path) {
1954    let rel = |p: &Path| p.strip_prefix(root).unwrap_or(p).display().to_string();
1955    for (path, dep) in &sync.written {
1956        eprintln!("  types   {} (from {dep})", rel(path));
1957    }
1958    for (path, dep) in &sync.taken {
1959        eprintln!("  kept    {} ({dep} publishes one too)", rel(path));
1960    }
1961}
1962
1963/// `htl types add <library>`: the declarations a library did not ship, from the collection
1964/// that has them. The revision is recorded because nothing else in that ecosystem does —
1965/// see `Project::add_types`.
1966fn cmd_types_add(library: &str, from: Option<&Path>, force: bool) -> Result<ExitCode> {
1967    let project = pkg_project().context("`types/` is a project's, so this runs in one")?;
1968    let types = decl_root(&project.root)?;
1969    let sync = match from {
1970        Some(dir) => project.add_types_from(dir, library, "local", force, &types)?,
1971        None => project.add_types(library, force, &types)?,
1972    };
1973    report_types_sync(&sync, &project.root);
1974    if sync.written.is_empty() && !sync.taken.is_empty() {
1975        eprintln!("htl: nothing written; --force replaces what is already there");
1976    }
1977    Ok(ExitCode::SUCCESS)
1978}
1979
1980/// What `htl unused` was asked for, beyond the paths.
1981struct UnusedFlags {
1982    json: bool,
1983    /// Exit 1 when anything was reported. Off by default: "unused" is a question about
1984    /// intent, so CI opts in rather than out.
1985    fail_on_unused: bool,
1986    use_cache: bool,
1987    explain: bool,
1988}
1989
1990/// What `htl adopt` was asked for, beyond the paths.
1991struct AdoptFlags {
1992    json: bool,
1993    /// List every marked declaration under the table, and each `applicable` candidate
1994    /// after them; the plain form prints the table alone.
1995    detail: bool,
1996    use_cache: bool,
1997    explain: bool,
1998}
1999
2000/// What `htl check` was asked for, beyond the paths and the lint selection.
2001struct CheckFlags {
2002    strict: bool,
2003    list_lints: bool,
2004    /// `--explain <rule>`: print the rule's explanation and exit.
2005    explain_rule: Option<String>,
2006    json: bool,
2007    use_cache: bool,
2008    cache_mode: Option<cache::Mode>,
2009    explain: bool,
2010    /// `--statistics`: the per-rule table in place of the findings.
2011    statistics: bool,
2012}
2013
2014struct TestFlags {
2015    fail_fast: bool,
2016    /// Finding no test file is a pass rather than a failure.
2017    allow_empty: bool,
2018    verbose: bool,
2019    quiet: bool,
2020    slow: Option<f64>,
2021    update: bool,
2022    coverage: bool,
2023    coverage_lines: bool,
2024    /// Where to write the lcov tracefile, if asked for.
2025    lcov: Option<PathBuf>,
2026    /// Where to write the JUnit XML report, if asked for.
2027    junit: Option<PathBuf>,
2028    json: bool,
2029    no_cache: bool,
2030    explain: bool,
2031    seed: Option<u64>,
2032}
2033
2034fn print_coverage(cov: &report::CoverageReport, with_lines: bool) {
2035    let width = cov
2036        .modules
2037        .iter()
2038        .map(|m| m.path.len())
2039        .max()
2040        .unwrap_or(4)
2041        .max(5);
2042    for m in &cov.modules {
2043        eprintln!(
2044            "coverage: {:<width$}  {:>5}/{:<5} {:5.1}%",
2045            m.path,
2046            m.executed,
2047            m.total,
2048            100.0 * m.executed as f64 / m.total as f64
2049        );
2050        if !m.never_ran.is_empty() {
2051            let names: Vec<String> = m
2052                .never_ran
2053                .iter()
2054                .map(|f| format!("{} ({})", f.name, f.line))
2055                .collect();
2056            eprintln!("          never ran: {}", names.join(", "));
2057        }
2058        if with_lines && !m.unexecuted.is_empty() {
2059            let spans: Vec<String> = m
2060                .unexecuted
2061                .iter()
2062                .map(|(a, b)| {
2063                    if a == b {
2064                        a.to_string()
2065                    } else {
2066                        format!("{a}-{b}")
2067                    }
2068                })
2069                .collect();
2070            eprintln!("          unexecuted: {}", spans.join(", "));
2071        }
2072    }
2073    if cov.total > 0 {
2074        eprintln!(
2075            "coverage: {:<width$}  {:>5}/{:<5} {:5.1}%  (statements)",
2076            "total",
2077            cov.executed,
2078            cov.total,
2079            100.0 * cov.executed as f64 / cov.total as f64
2080        );
2081    } else {
2082        eprintln!("coverage: no .tl module reached");
2083    }
2084}
2085
2086/// One file's report as a JUnit suite.
2087///
2088/// The assertion library reports a failure as `"<suite > test>: <message>"` and its tests
2089/// by the same composed name, so a failing case takes back the message printed under the
2090/// file by matching that prefix — including the traceback a runtime error carries, which
2091/// belongs in the report for the same reason it belongs on the terminal: it is the part
2092/// that says where.
2093fn junit_suite(rep: &htl::testing::FileReport) -> junit::Suite {
2094    let mut left: Vec<&str> = rep.failures.iter().map(String::as_str).collect();
2095    let cases = rep
2096        .tests
2097        .iter()
2098        .map(|t| {
2099            let failure = (!t.ok)
2100                .then(|| {
2101                    let head = format!("{}: ", t.name);
2102                    let at = left.iter().position(|m| m.starts_with(&head))?;
2103                    Some(left.remove(at)[head.len()..].to_string())
2104                })
2105                .flatten();
2106            junit::Case {
2107                name: t.name.clone(),
2108                ms: t.ms,
2109                // A failing test whose message could not be matched still failed: say so
2110                // rather than reporting it as passed.
2111                failure: failure.or_else(|| (!t.ok).then(|| "failed".to_string())),
2112            }
2113        })
2114        .collect();
2115    let error = if !rep.check.ok() {
2116        Some(junit::Error {
2117            message: "type check failed".to_string(),
2118            kind: "check",
2119            body: rep
2120                .check
2121                .error_items
2122                .iter()
2123                .map(|d| d.clone().spelled().to_string())
2124                .collect::<Vec<_>>()
2125                .join("\n"),
2126        })
2127    } else {
2128        rep.error.as_ref().map(|e| junit::Error {
2129            message: e.clone(),
2130            kind: "error",
2131            body: e.clone(),
2132        })
2133    };
2134    junit::Suite {
2135        file: htl::diagnostic::display_path(&rep.path),
2136        duration_ms: rep.duration_ms,
2137        error,
2138        cases,
2139    }
2140}
2141
2142fn cmd_test(
2143    paths: &[PathBuf],
2144    filter: Option<&str>,
2145    lib: &str,
2146    lint: Option<&str>,
2147    flags: TestFlags,
2148) -> Result<ExitCode> {
2149    let paths = if paths.is_empty() {
2150        vec![PathBuf::from(".")]
2151    } else {
2152        paths.to_vec()
2153    };
2154    if let Some(first) = paths.first() {
2155        auto_dts(first)?;
2156    }
2157    // A patched dependency's tests are its suite, not this project's: `htl pkg patch`
2158    // takes the whole package root, tests included, and running them here would report a
2159    // library's own failures as the project's.
2160    let cfg = load_config(&paths[0])?;
2161    let model = project::model_of(&cfg, &paths[0])?;
2162    let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Test);
2163    let files = htl::testing::discover_tests_for(&paths, &skip, lib)?;
2164    // A test at the root of a project laid out flat without saying so would only fail on
2165    // its first `require`; say what is wrong instead, as `check` does.
2166    project::refuse_flat_root(model.as_ref(), &paths, &files)?;
2167    // Finding nothing goes through the same report as any run: under `--allow-empty` its
2168    // summary line, and under `--format json` its document whether allowed or not, so a
2169    // reader of stdout always gets one.
2170    let empty = files.is_empty();
2171    if empty {
2172        eprintln!("htl test: no test files found (looked for .tl files that require(\"{lib}\"))");
2173    }
2174    let opts = project::TestOptions {
2175        config: &cfg,
2176        model: model.as_ref(),
2177        lint,
2178        lib,
2179        filter,
2180        run: htl::testing::RunOptions {
2181            fail_fast: flags.fail_fast,
2182            update_snapshots: flags.update,
2183            coverage: flags.coverage,
2184            // Given, or drawn for the run and printed below, so that it can be given back.
2185            seed: flags.seed,
2186            // `project::test` fills it from the config it is handed.
2187            async_: Default::default(),
2188            lang: Default::default(),
2189        },
2190        cache: project::cache_options(
2191            !flags.no_cache,
2192            Some(cache::Mode::PerModule),
2193            &cfg,
2194            flags.explain,
2195        ),
2196    };
2197
2198    let mut sink = project::Sink::new(report::Out::new(flags.json, false));
2199    let mut json_files: Vec<report::TestFile> = Vec::new();
2200    let mut junit_suites: Vec<junit::Suite> = Vec::new();
2201    // What is left of the run here: how a file reads on a terminal, and what the document
2202    // says about it. Which files run, in what isolation, from what store — and what they
2203    // draw — is `project::test`'s.
2204    let rep = if empty {
2205        // Nothing to run, so no session to open and no store to touch: the run's report
2206        // with every count at zero, and no file having drawn from the seed.
2207        project::TestReport {
2208            files: Vec::new(),
2209            ran: 0,
2210            passed: 0,
2211            failed: 0,
2212            files_with_errors: 0,
2213            replayed: 0,
2214            seed: flags.seed.unwrap_or(0),
2215            duration_ms: 0.0,
2216            coverage: None,
2217        }
2218    } else {
2219        project::test(&mut sink, &files, &opts, &mut |rep, sink| {
2220            if flags.json {
2221                json_files.push(report::TestFile::from_report(rep, sink.out().take()));
2222            }
2223            if flags.junit.is_some() {
2224                junit_suites.push(junit_suite(rep));
2225            }
2226            // Spelled as `htl check` spells the same file, whatever path the walk was handed.
2227            let f = htl::diagnostic::display_path(&rep.path);
2228            let tag = if rep.ok() { "ok  " } else { "FAIL" };
2229            let detail = if !rep.check.ok() {
2230                "type check failed".to_string()
2231            } else if let Some(e) = &rep.error {
2232                // The cause on the file's own line, its frames under it: a traceback inside
2233                // the parenthesised summary would push `12 ms` past a screen of stack.
2234                format!("error: {}", e.lines().next().unwrap_or_default())
2235            } else if rep.file_level {
2236                "ran to completion (no test library used)".to_string()
2237            } else {
2238                format!("{} passed, {} failed", rep.passed, rep.failed)
2239            };
2240            // Quiet: a passing file is silence; failures, errors and slow tests still show.
2241            // JSON: nothing on stderr, the document carries it all.
2242            let show_file = !flags.json && (!flags.quiet || !rep.ok());
2243            if show_file {
2244                eprintln!("{tag} {f}  ({detail}, {:.0} ms)", rep.duration_ms);
2245                if let Some(e) = &rep.error {
2246                    for line in e.lines().skip(1) {
2247                        eprintln!("      {line}");
2248                    }
2249                }
2250            }
2251            for tr in &rep.tests {
2252                let slow = flags.slow.is_some_and(|ms| tr.ms >= ms);
2253                if !flags.json && (flags.verbose || slow) {
2254                    if flags.quiet && !show_file {
2255                        // The file line was skipped: name the file with the slow test.
2256                        eprintln!("slow {f}  {}  ({:.1} ms)", tr.name, tr.ms);
2257                        continue;
2258                    }
2259                    let mark = if tr.ok { "ok  " } else { "FAIL" };
2260                    let note = if slow && !flags.verbose {
2261                        "  [slow]"
2262                    } else {
2263                        ""
2264                    };
2265                    eprintln!("      {mark} {}  ({:.1} ms){note}", tr.name, tr.ms);
2266                }
2267            }
2268            if !flags.json {
2269                for m in &rep.failures {
2270                    eprintln!("      - {m}");
2271                }
2272                // A snapshot written or rewritten is a change on disk: always say so, even in -q.
2273                for p in &rep.snapshots_written {
2274                    eprintln!(
2275                        "snapshot written: {}",
2276                        htl::diagnostic::display_path(Path::new(p))
2277                    );
2278                }
2279                for p in &rep.snapshots_updated {
2280                    eprintln!(
2281                        "snapshot updated: {}",
2282                        htl::diagnostic::display_path(Path::new(p))
2283                    );
2284                }
2285            }
2286        })?
2287    };
2288
2289    if let Some(out) = &flags.junit {
2290        // The run's own total, not the sum of the files: it is the number the summary
2291        // line prints, and the report is there to agree with the summary line.
2292        std::fs::write(out, junit::document(&junit_suites, rep.duration_ms))
2293            .with_context(|| format!("writing {}", out.display()))?;
2294    }
2295    if let (Some(out), Some(cov)) = (&flags.lcov, &rep.coverage) {
2296        // Against the project root rather than the working directory: a tracefile is
2297        // uploaded from wherever CI ran the command and resolved against the repository.
2298        let root = match &cfg {
2299            Some((dir, _, _)) => dir.clone(),
2300            None => std::env::current_dir()?,
2301        };
2302        let root = std::fs::canonicalize(&root).unwrap_or(root);
2303        std::fs::write(out, cov.lcov(&root))
2304            .with_context(|| format!("writing {}", out.display()))?;
2305    }
2306    // An empty run is a pass only when asked for; the document's `ok` says what the exit
2307    // code says, so a reader of either sees the same refusal.
2308    let ok = rep.ok() && (!empty || flags.allow_empty);
2309    if flags.json {
2310        let summary = report::TestSummary {
2311            files: rep.files.len(),
2312            files_run: rep.ran,
2313            passed: rep.passed,
2314            failed: rep.failed,
2315            files_with_errors: rep.files_with_errors,
2316            replayed: rep.replayed,
2317            duration_ms: rep.duration_ms,
2318            ok,
2319            seed: rep.seed,
2320        };
2321        report::emit(&report::TestReport {
2322            files: json_files,
2323            summary,
2324            coverage: rep.coverage,
2325        })?;
2326    } else if !empty || flags.allow_empty {
2327        // A refused empty run says so with the notice alone, as it always has.
2328        if let Some(cov) = &rep.coverage {
2329            print_coverage(cov, flags.coverage_lines);
2330            if let Some(out) = &flags.lcov {
2331                eprintln!("coverage: lcov written to {}", out.display());
2332            }
2333        }
2334        eprintln!(
2335            "htl test: {} file(s), {} passed, {} failed, {} file(s) with errors{}{} ({:.0} ms)",
2336            rep.ran,
2337            rep.passed,
2338            rep.failed,
2339            rep.files_with_errors,
2340            if rep.skipped() > 0 {
2341                format!(", {} file(s) not run (--fail-fast)", rep.skipped())
2342            } else {
2343                String::new()
2344            },
2345            // Says the checking was reused, not the run: every one of these files ran.
2346            if rep.replayed > 0 {
2347                format!(", {} checked from cache", rep.replayed)
2348            } else {
2349                String::new()
2350            },
2351            rep.duration_ms
2352        );
2353        // Always, not only on failure: the seed of a run that passed is what reproduces
2354        // the run that passes, and a failure two commits later is compared against it.
2355        // A run of no files drew nothing, so there is nothing to repeat.
2356        if !empty {
2357            eprintln!(
2358                "htl test: seed {} (repeat with --seed {})",
2359                rep.seed, rep.seed
2360            );
2361        }
2362    }
2363    Ok(if ok {
2364        ExitCode::SUCCESS
2365    } else {
2366        ExitCode::FAILURE
2367    })
2368}
2369
2370/// Name the patched dependencies this walk enters, in the shape the other reports use.
2371///
2372/// Only the ones actually below a given root: `htl check src` walks the project's own
2373/// sources and nothing under `patches/`, and saying otherwise would be a claim about files
2374/// that were not read.
2375fn report_patched(paths: &[PathBuf]) {
2376    let roots: Vec<PathBuf> = paths
2377        .iter()
2378        .filter_map(|p| std::fs::canonicalize(p).ok())
2379        .collect();
2380    let cwd = std::env::current_dir().unwrap_or_default();
2381    // The project found the way every command finds it; a failure to find one was said by
2382    // the command itself, which ran first.
2383    let Some(project) = paths
2384        .first()
2385        .and_then(|p| htl::model::Project::find_root(p).ok().flatten())
2386        .map(|r| r.root)
2387        .filter(|r| r.join(htl::pkg::MANIFEST_NAME).is_file())
2388        .map(|r| htl::pkg::MluaProject::at(&r))
2389    else {
2390        return;
2391    };
2392    for p in &project.patches {
2393        let canon = std::fs::canonicalize(&p.dir).unwrap_or_else(|_| p.dir.clone());
2394        if !roots.iter().any(|r| canon.starts_with(r)) {
2395            continue;
2396        }
2397        let shown = p.dir.strip_prefix(&cwd).unwrap_or(&p.dir);
2398        eprintln!("  patched {} ({})", shown.display(), p.name);
2399    }
2400}
2401
2402/// Nearest `htl.toml` above the first path: `(dir holding it, path, config)`.
2403///
2404/// Where `[toolchain] htl` is answered, and the only place: every command that reads the
2405/// config comes through here, and the pin has to be settled before the command reads a
2406/// source file — which is a property of this function, not something each command could
2407/// be trusted to repeat. `htl new` / `htl init` do not call it, so a mismatch never stops
2408/// anyone from creating a project.
2409///
2410/// The version compared is this binary's, which is the one the pin is about. The `htl`
2411/// crate a Rust host builds against is Cargo's, pinned in `Cargo.toml` — a separate
2412/// question, and the one [`warn_cargo_htl_mismatch`] answers.
2413fn load_config(first: &Path) -> Result<project::Config> {
2414    let cfg = project::config_of(first)?;
2415    if let Some((_, path, c)) = &cfg {
2416        htl::config::check_toolchain(c, path, env!("CARGO_PKG_VERSION"))?;
2417    }
2418    Ok(cfg)
2419}
2420
2421/// Say so, once, when the `Cargo.toml` around this project asks for an `htl` crate this
2422/// command is not.
2423///
2424/// The two halves of htl are released together and a project uses both: the crate the host
2425/// links, and the command that checks the Teal beside it. A project left at `htl = "0.5.1"`
2426/// while the installed CLI had moved to 0.6.0 was checked by one and built against the
2427/// other, and nothing in either output mentioned the other one — which is the whole of the
2428/// failure, since either version alone was fine.
2429///
2430/// **A warning, not an error, and not one of the summary's `warning(s)`** — that count is
2431/// the checker's, and this is not about the sources. A project goes to a hard error over a
2432/// version skew for one of two reasons, and htl has neither: the format between the halves
2433/// is unstable, so a mismatched pair miscompiles rather than failing (wasm-bindgen refuses
2434/// a JS glue built by another version); or the diagnostic downstream is opaque, so the
2435/// person would otherwise read a type error from deep inside a dependency (cargo's
2436/// `rust-version`). Here both halves worked — the run that produced this report is the
2437/// evidence — and the line is worth printing only because the person has no other way to
2438/// notice. npm's `engines` is the same shape and warns by default; Prisma says nothing at
2439/// all, and its silence is filed as a bug.
2440///
2441/// Read from the `Cargo.toml` of the crate the project sits in, `[dependencies] htl` only:
2442/// a string, or a table with `version`. A table without one — `path`, `git`, or a
2443/// `workspace = true` inheritance — states no requirement here and is passed over without
2444/// a word, which is exactly what a scaffold written by a checkout-built CLI produces. No
2445/// `Cargo.toml`, no `[dependencies]`, no `htl` key, or a requirement cargo itself would
2446/// reject: nothing is printed, because there is nothing this can be surer about than cargo.
2447fn warn_cargo_htl_mismatch(first: &Path) {
2448    let Some(root) = htl::dts::find_cargo_package_root(first) else {
2449        return;
2450    };
2451    let Ok(text) = fs::read_to_string(root.join("Cargo.toml")) else {
2452        return;
2453    };
2454    let Ok(manifest) = text.parse::<toml::Table>() else {
2455        return;
2456    };
2457    let Some(dep) = manifest.get("dependencies").and_then(|d| d.get("htl")) else {
2458        return;
2459    };
2460    let req_text = match dep {
2461        toml::Value::String(s) => s.as_str(),
2462        toml::Value::Table(t) => match t.get("version").and_then(toml::Value::as_str) {
2463            Some(s) => s,
2464            None => return,
2465        },
2466        _ => return,
2467    };
2468    let running = env!("CARGO_PKG_VERSION");
2469    let (Ok(req), Ok(version)) = (
2470        semver::VersionReq::parse(req_text),
2471        semver::Version::parse(running),
2472    ) else {
2473        return;
2474    };
2475    if req.matches(&version) {
2476        return;
2477    }
2478    // Both ways out are named because either is the right one: the project may be the
2479    // thing that is current, in which case the command is what moves.
2480    eprintln!(
2481        "htl {running}; Cargo.toml asks for htl {req_text} — the crate and the CLI are \
2482         meant to move together (cargo install htl-cli --version {req_text}, or bump the \
2483         dependency)"
2484    );
2485}
2486
2487fn cmd_fmt(paths: &[PathBuf], check: bool, indent: Option<usize>) -> Result<ExitCode> {
2488    let paths = if paths.is_empty() {
2489        vec![PathBuf::from(".")]
2490    } else {
2491        paths.to_vec()
2492    };
2493    let cfg = load_config(&paths[0])?;
2494    let indent = indent
2495        .or_else(|| cfg.as_ref().and_then(|(_, _, c)| c.fmt.indent))
2496        .unwrap_or(3);
2497    let h = Htl::new()?;
2498    config_lang(&h, &cfg)?;
2499    // Not a dependency's files, a patched one included: formatting the copy would turn
2500    // every one of its files into a diff against the revision it was taken from, and bury
2501    // the project's own change somewhere inside that.
2502    let model = project::model_of(&cfg, &paths[0])?;
2503    let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Own);
2504    let files = htl::collect_tl_skipping(&paths, &skip)?;
2505    let (mut changed, mut failed) = (0usize, 0usize);
2506    for f in &files {
2507        let before = fs::read_to_string(f).with_context(|| format!("reading {}", f.display()))?;
2508        let after = match h.format_file(f, indent) {
2509            Ok(s) => s,
2510            Err(e) => {
2511                eprintln!("error: {e}");
2512                failed += 1;
2513                continue;
2514            }
2515        };
2516        if after != before {
2517            changed += 1;
2518            if check {
2519                eprintln!("would reformat: {}", f.display());
2520            } else {
2521                fs::write(f, after).with_context(|| format!("writing {}", f.display()))?;
2522                eprintln!("reformatted: {}", f.display());
2523            }
2524        }
2525    }
2526    eprintln!(
2527        "htl fmt: {} file(s), {} {}, {} failed",
2528        files.len(),
2529        changed,
2530        if check { "would change" } else { "reformatted" },
2531        failed
2532    );
2533    let fail = failed > 0 || (check && changed > 0);
2534    Ok(if fail {
2535        ExitCode::FAILURE
2536    } else {
2537        ExitCode::SUCCESS
2538    })
2539}
2540
2541struct FixFlags {
2542    rule: Vec<String>,
2543    unsafe_fixes: bool,
2544    dry_run: bool,
2545    diff: bool,
2546    allow_dirty: bool,
2547    allow_no_vcs: bool,
2548    exit_non_zero_on_fix: bool,
2549    json: bool,
2550}
2551
2552fn cmd_fix(paths: &[PathBuf], flags: FixFlags) -> Result<ExitCode> {
2553    use htl::fix::{FixOptions, fix_file, git_dirty, unified_diff};
2554    let paths = if paths.is_empty() {
2555        vec![PathBuf::from(".")]
2556    } else {
2557        paths.to_vec()
2558    };
2559    // What `htl check` holds a file to, resolved the same way and read by the same checker,
2560    // so a file this run leaves reports what a check of it would. A dry run writes nothing,
2561    // the declarations generated before the check included.
2562    let ProjectChecker {
2563        h,
2564        cfg,
2565        model,
2566        scope,
2567    } = checker_for(&paths[0], !flags.dry_run)?;
2568    let opts = FixOptions {
2569        unsafe_fixes: flags.unsafe_fixes,
2570        promoted: cfg
2571            .as_ref()
2572            .map(|(_, _, c)| c.fix.unsafe_.clone())
2573            .unwrap_or_default(),
2574        disabled: cfg
2575            .as_ref()
2576            .map(|(_, _, c)| c.fix.disable.clone())
2577            .unwrap_or_default(),
2578        only: flags.rule.clone(),
2579        dry_run: flags.dry_run,
2580    };
2581    // Here as well as inside `fix_file`, so a misspelt rule is answered even when the
2582    // paths hold no `.tl` at all — the request is wrong either way.
2583    opts.validate()?;
2584    // Written as `htl fmt` walks, not as `htl check` does: this command rewrites files, so a
2585    // patched dependency is left alone whatever the fixes would be. An edit there belongs
2586    // in the diff the project makes against the revision it took, written by a person.
2587    let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Own);
2588    // Only what a module of the project holds, as `htl check` walks it.
2589    let (files, outside) = project::held_by_modules(
2590        model.as_ref(),
2591        &paths,
2592        htl::collect_tl_skipping(&paths, &skip)?,
2593    )?;
2594    if let Some(note) = project::outside_modules_note(model.as_ref(), &outside, "fixed") {
2595        eprintln!("htl fix: {note}");
2596    }
2597    // But checked as `htl check` walks: the copy is still the project's code, and what is
2598    // wrong in it is what the two commands both judge the tree by. Only checked, never
2599    // handed to `fix_file`.
2600    let check_skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Check);
2601    let checked_only: Vec<PathBuf> = project::held_by_modules(
2602        model.as_ref(),
2603        &paths,
2604        htl::collect_tl_skipping(&paths, &check_skip)?,
2605    )?
2606    .0
2607    .into_iter()
2608    .filter(|f| !files.contains(f))
2609    .collect();
2610
2611    // The working tree is the undo: refuse to rewrite what git could not give back.
2612    if !flags.dry_run {
2613        let mut dirty = Vec::new();
2614        let mut no_vcs = Vec::new();
2615        for f in &files {
2616            match git_dirty(f)? {
2617                Some(true) => dirty.push(f.clone()),
2618                Some(false) => {}
2619                None => no_vcs.push(f.clone()),
2620            }
2621        }
2622        if !dirty.is_empty() && !flags.allow_dirty {
2623            bail!(
2624                "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",
2625                dirty
2626                    .iter()
2627                    .map(|p| p.display().to_string())
2628                    .collect::<Vec<_>>()
2629                    .join("\n  ")
2630            );
2631        }
2632        if !no_vcs.is_empty() && !flags.allow_no_vcs {
2633            bail!(
2634                "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",
2635                no_vcs
2636                    .iter()
2637                    .map(|p| p.display().to_string())
2638                    .collect::<Vec<_>>()
2639                    .join("\n  ")
2640            );
2641        }
2642    }
2643
2644    let mut sink = project::Sink::new(report::Out::new(flags.json, false));
2645    // Judged by the levels `htl check` judges by: a finding under a rule at `deny` fails.
2646    sink.judge_by(scope.lints.selection());
2647    // Dependencies are reported as `htl check` reports them and never rewritten: a fix
2648    // under `.htl/` goes at the next install, one under `[check] paths` is not this
2649    // project's. `fix_file` only ever writes the file it was given.
2650    let origins = project::Origins::new(model.as_ref());
2651    let walk = scope.walk(&cfg, model.as_ref(), &origins);
2652    let walked: Vec<PathBuf> = files.iter().chain(&checked_only).cloned().collect();
2653    sink.walking(&walked);
2654    let (mut applied, mut skipped, mut json_files) = (Vec::new(), Vec::new(), Vec::new());
2655    let (mut changed, mut deferred, mut reverted) = (0usize, 0usize, 0usize);
2656    let mut found = htl::verdict::Findings::default();
2657    let mut infos: Vec<(PathBuf, htl::CheckInfo)> = Vec::with_capacity(walked.len());
2658    for f in &files {
2659        // Put back after each file, as `htl check` does, so a file is fixed against what
2660        // it may read and not also against the directories of the files before it.
2661        let saved = h.search_path()?;
2662        project::file_view(&h, model.as_ref(), f)?;
2663        let before = if flags.diff {
2664            std::fs::read_to_string(f).ok()
2665        } else {
2666            None
2667        };
2668        let out = fix_file(&h, f, &opts)?;
2669        if out.contents.is_some() {
2670            changed += 1;
2671        }
2672        deferred += out.deferred;
2673        if out.reverted.is_some() {
2674            reverted += 1;
2675        }
2676        if !flags.json {
2677            for a in &out.applied {
2678                eprintln!(
2679                    "fixed: {}:{}: {} ({}{})",
2680                    project::display_path(f),
2681                    a.line,
2682                    a.rule,
2683                    a.applicability.as_str(),
2684                    if flags.dry_run { ", not written" } else { "" }
2685                );
2686            }
2687            for s in &out.skipped {
2688                eprintln!(
2689                    "skipped: {}:{}: {} — {}",
2690                    project::display_path(f),
2691                    s.line,
2692                    s.rule,
2693                    s.reason
2694                );
2695            }
2696            if let Some(r) = &out.reverted {
2697                eprintln!("reverted: {}: {r}", project::display_path(f));
2698            }
2699            if let Some(o) = &out.oscillation {
2700                eprintln!(
2701                    "stopped: {}: fixes of {o} undo each other",
2702                    project::display_path(f)
2703                );
2704            }
2705            if out.deferred > 0 {
2706                eprintln!(
2707                    "deferred: {}: {} edit(s) overlapped applied ones; run htl fix again",
2708                    project::display_path(f),
2709                    out.deferred
2710                );
2711            }
2712            if flags.diff
2713                && let (Some(b), Some(a)) = (&before, &out.contents)
2714            {
2715                print!("{}", unified_diff(&project::display_path(f), b, a));
2716            }
2717            // A suggestion is never written, so the diff is the only place it is shown.
2718            // Against what was applied, when something was: the two are one edit session.
2719            if flags.diff
2720                && let (Some(b), Some(s)) = (
2721                    out.contents.as_ref().or(before.as_ref()),
2722                    out.suggested.as_ref(),
2723                )
2724            {
2725                print!(
2726                    "{}",
2727                    unified_diff(&format!("{} (suggested)", project::display_path(f)), b, s)
2728                );
2729            }
2730        }
2731        // What the file says as it is left, the way `htl check` would say it: the checker's
2732        // result and this layer's lints, while the path it was checked under is in place.
2733        found.lints += project::file_findings(&h, &mut sink, f, &out.check, &walk)?;
2734        h.set_search_path(&saved)?;
2735        found.errors += out.check.errors.len();
2736        found.warnings += out.check.warnings.len();
2737        infos.push((f.clone(), out.check.clone()));
2738        if flags.json {
2739            applied.extend(out.applied.iter().map(|a| report::FixApplied {
2740                file: project::display_path(f),
2741                line: a.line,
2742                rule: a.rule.clone(),
2743                applicability: a.applicability.as_str(),
2744                pass: a.pass,
2745            }));
2746            skipped.extend(out.skipped.iter().map(|s| report::FixSkipped {
2747                file: project::display_path(f),
2748                line: s.line,
2749                rule: s.rule.clone(),
2750                reason: s.reason.clone(),
2751            }));
2752            json_files.push(report::FixFile {
2753                path: project::display_path(f),
2754                changed: out.contents.is_some(),
2755                deferred: out.deferred,
2756                reverted: out.reverted.clone(),
2757                oscillation: out.oscillation.clone(),
2758                diagnostics: sink.out().take(),
2759            });
2760        }
2761    }
2762    // A patched copy: checked as `htl check` checks it, and not fixed.
2763    for f in &checked_only {
2764        let m = project::check_one(&h, &mut sink, f, &walk)?;
2765        found.errors += m.errors;
2766        found.warnings += m.warnings;
2767        found.lints += m.lints;
2768        infos.push((f.clone(), m.requires_only()));
2769        if flags.json {
2770            json_files.push(report::FixFile {
2771                path: project::display_path(f),
2772                changed: false,
2773                deferred: 0,
2774                reverted: None,
2775                oscillation: None,
2776                diagnostics: sink.out().take(),
2777            });
2778        }
2779    }
2780    // What the project says about itself as a whole.
2781    // A dry run publishes nothing either.
2782    let mut whole = scope.whole(&cfg, model.as_ref());
2783    whole.publish = !flags.dry_run;
2784    whole.checker = Some(&h);
2785    let w = project::project_findings(&mut sink, &whole, &infos);
2786    found.errors += w.errors;
2787    found.lints += w.lints;
2788    let project_diagnostics = if flags.json {
2789        sink.out().take()
2790    } else {
2791        Vec::new()
2792    };
2793    // A broken dependency is an error `htl fix` cannot remove; it remains, as `htl check`
2794    // would count it.
2795    found.errors += sink.dependency_error_count();
2796    found.denied = sink.denied();
2797    // Judged as `htl check` judges the same tree: the same findings, the same policy.
2798    let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
2799    let errors_remaining = found.errors;
2800    let n_applied = if flags.json { applied.len() } else { 0 };
2801    let fail =
2802        htl::verdict::verdict(&found, &policy) || (flags.exit_non_zero_on_fix && changed > 0);
2803    if flags.json {
2804        let n_skipped = skipped.len();
2805        report::emit(&report::FixReport {
2806            dry_run: flags.dry_run,
2807            applied,
2808            skipped,
2809            files: json_files,
2810            summary: report::FixSummary {
2811                files: files.len(),
2812                files_changed: changed,
2813                applied: n_applied,
2814                skipped: n_skipped,
2815                deferred,
2816                reverted,
2817                errors_remaining,
2818                warnings: found.warnings,
2819                lints: found.lints,
2820                denied: found.denied,
2821                strict: policy.strict,
2822                ok: !fail,
2823            },
2824            diagnostics: project_diagnostics,
2825        })?;
2826    } else {
2827        // What else the verdict counted, said only when it did: a finding at `deny`, and
2828        // under `strict` every warning and lint. Without it a run that failed on a lint
2829        // would end in `0 error(s) remaining`.
2830        let judged = judged(&found, &policy);
2831        eprintln!(
2832            "htl fix: {} file(s){}, {} changed{}, {} error(s) remaining{}{}",
2833            files.len(),
2834            if checked_only.is_empty() {
2835                String::new()
2836            } else {
2837                format!(" + {} checked in patched dependencies", checked_only.len())
2838            },
2839            changed,
2840            if flags.dry_run {
2841                " (dry run, nothing written)"
2842            } else {
2843                ""
2844            },
2845            errors_remaining,
2846            judged,
2847            if deferred > 0 {
2848                format!(", {deferred} deferred")
2849            } else {
2850                String::new()
2851            }
2852        );
2853    }
2854    Ok(if fail {
2855        ExitCode::FAILURE
2856    } else {
2857        ExitCode::SUCCESS
2858    })
2859}
2860
2861/// Type-check a tree, replaying the modules whose inputs have not moved.
2862///
2863/// The order here is load-bearing. Generating `.d.tl` and collecting the file list come
2864/// first, because both produce inputs the keys are built from. Every module is looked up
2865/// before any is checked, so that a run where nothing moved never builds a checker at all —
2866/// that costs about 13.5 ms (`crates/htl-core/benches/check.rs`), which is most of what a
2867/// fully replayed run has to save on a small project.
2868///
2869/// A module that misses re-checks its dependencies as part of its own check, since the
2870/// checker's store starts empty. So an edit costs the edited module, whatever depends on
2871/// it, and whatever those pull in — less than the project, more than the minimum.
2872fn cmd_check(paths: &[PathBuf], lint: Option<&str>, flags: CheckFlags) -> Result<ExitCode> {
2873    let CheckFlags {
2874        list_lints,
2875        json,
2876        use_cache,
2877        cache_mode,
2878        explain,
2879        statistics,
2880        ..
2881    } = flags;
2882    let mut sink = project::Sink::new(report::Out::new(json, statistics));
2883    let paths = if paths.is_empty() {
2884        vec![PathBuf::from(".")]
2885    } else {
2886        paths.to_vec()
2887    };
2888    // Listing the rules reports nothing about the project and shares nothing with a run —
2889    // and now not even a Lua state: the registry is Rust (`htl_core::lint::RULES`), which
2890    // is what lets the rules the project layer reports under be listed beside the ones
2891    // `lint.lua` implements.
2892    // `--explain <rule>`: the rule's explanation, and nothing else. The name is the one a
2893    // finding prints in its `[htl <rule>]` suffix, so a reader hands the flag what they
2894    // were shown; a name no check reports under — a typo, or a fix class — is refused the
2895    // way `--lint` refuses it, on stderr, with the exit code an unusable argument gets.
2896    if let Some(rule) = flags.explain_rule.as_deref() {
2897        let Some(r) = htl::lint::explained(rule) else {
2898            eprintln!("unknown lint rule: {rule}");
2899            return Ok(ExitCode::from(2));
2900        };
2901        println!("{}  (default: {})\n\n{}", r.name, r.default, r.explain);
2902        return Ok(ExitCode::SUCCESS);
2903    }
2904    if list_lints {
2905        // The name and the level a project that says nothing gets. Two columns rather than
2906        // one because the default is a level now, and the five rules at `allow` are
2907        // otherwise invisible: a reader would have to turn one on to find out it was off.
2908        // Still one rule per line, name first, so it reads and greps as it always did.
2909        let width = htl::lint::rule_names()
2910            .iter()
2911            .map(|r| r.len())
2912            .max()
2913            .unwrap_or(0);
2914        for (name, level) in htl::lint::rule_defaults() {
2915            println!("{name:width$}  {level}");
2916        }
2917        // No pointer to `--explain` here: every rule listed has an explanation (the
2918        // registry's test holds it to that), and the listing is name-level pairs that
2919        // tests and greps read line by line. `--help` and a finding's own hint carry it.
2920        return Ok(ExitCode::SUCCESS);
2921    }
2922    // htl.toml first, then --lint, so the flag wins; `strict` from the file unless flagged.
2923    let cfg = load_config(&paths[0])?;
2924    warn_cargo_htl_mismatch(&paths[0]);
2925    let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), flags.strict);
2926    // `.d.tl` written from Rust source is an input to the check, so it is regenerated
2927    // before anything hashes the tree: a hit computed over stale declarations would be a
2928    // hit on a different question from the one being asked.
2929    if let Some(first) = paths.first() {
2930        auto_dts(first)?;
2931    }
2932    // A patched dependency is walked with the project's own sources — it is the project's
2933    // code, and its errors are the project's to fix. Which dependency each directory
2934    // stands in for is said here, so that an error under it is read as that dependency's
2935    // without the reader having to know the manifest.
2936    let walk_model = project::model_of(&cfg, &paths[0])?;
2937    // A directory is checked as a project: which of its files is which module, and what
2938    // they may read, is the project's to say. With none there is no answer to give, and
2939    // checking each file as a thing on its own would answer a different question without
2940    // saying so. A file named on its own is that question, asked outright, and is checked.
2941    if walk_model.is_none()
2942        && let Some(dir) = paths.iter().find(|p| p.is_dir())
2943    {
2944        bail!(
2945            "no {} or {} in {} or any directory above it: a directory is checked as a \
2946             project. Run `htl init` there to make it one, or name the .tl files to check \
2947             each on its own",
2948            htl::config::CONFIG_NAME,
2949            htl::pkg::MANIFEST_NAME,
2950            // Absolute: where the search started is the whole of the answer, and `.`
2951            // relative to itself would say nothing.
2952            fs::canonicalize(dir)
2953                .unwrap_or_else(|_| dir.clone())
2954                .display()
2955        );
2956    }
2957    let skip = project::not_walked(walk_model.as_ref(), &paths, htl::model::Purpose::Check);
2958    let (files, outside) = project::held_by_modules(
2959        walk_model.as_ref(),
2960        &paths,
2961        htl::collect_tl_skipping(&paths, &skip)?,
2962    )?;
2963    if let Some(note) = project::outside_modules_note(walk_model.as_ref(), &outside, "checked") {
2964        eprintln!("htl check: {note}");
2965    }
2966    if !json {
2967        report_patched(&paths);
2968    }
2969
2970    // The walk, the store and every decision about them are the library's: `include_tl!`
2971    // asks the same questions of the same store, and the two answering them separately is
2972    // what let them disagree. What is left here is the flags, the summary and the exit code.
2973    let rep = project::check(
2974        &mut sink,
2975        &files,
2976        &project::Options {
2977            paths: &paths,
2978            config: &cfg,
2979            model: walk_model.as_ref(),
2980            lint,
2981            cache: project::cache_options(use_cache, cache_mode, &cfg, explain),
2982        },
2983    )?;
2984    let fail = report_check(
2985        sink.out(),
2986        json,
2987        statistics,
2988        &rep,
2989        &policy,
2990        patched_files(walk_model.as_ref(), &rep.files),
2991    )?;
2992    Ok(if fail {
2993        ExitCode::FAILURE
2994    } else {
2995        ExitCode::SUCCESS
2996    })
2997}
2998
2999/// Report what no entry's `require` closure reaches.
3000///
3001/// The graph is the check's — `htl check` resolves every `require` already, and the
3002/// `require-cycle` lint reads the same edges — so this is that walk plus the entries the
3003/// project declares (`htl::unused`). What is left here is the flags, the printing and the
3004/// exit code.
3005fn cmd_unused(paths: &[PathBuf], flags: UnusedFlags) -> Result<ExitCode> {
3006    let UnusedFlags {
3007        json,
3008        fail_on_unused,
3009        use_cache,
3010        explain,
3011    } = flags;
3012    let paths = if paths.is_empty() {
3013        vec![PathBuf::from(".")]
3014    } else {
3015        paths.to_vec()
3016    };
3017    let cfg = load_config(&paths[0])?;
3018    // A `.d.tl` written from Rust source is an input to the check the graph comes from,
3019    // exactly as it is for `htl check`.
3020    auto_dts(&paths[0])?;
3021    let model = project::model_of(&cfg, &paths[0])?;
3022    let rep = htl::unused::unused(&htl::unused::Options {
3023        paths: &paths,
3024        config: &cfg,
3025        model: model.as_ref(),
3026        cache: project::cache_options(use_cache, None, &cfg, explain),
3027    })?;
3028    let s = &rep.summary;
3029    if json {
3030        report::emit(&rep)?;
3031    } else if s.no_entry {
3032        eprintln!(
3033            "htl unused: nothing to start from, so nothing is reported. An entry is \
3034             main.tl in the source root ([layout] source) or beside the manifest, a test file, a module under a [[contract]] directory, or a name \
3035             in [build] extra / host"
3036        );
3037    } else {
3038        if s.check_errors > 0 {
3039            eprintln!(
3040                "htl unused: {} error(s) in the check this graph comes from — what those \
3041                 files require is missing from it (htl check)",
3042                s.check_errors
3043            );
3044        }
3045        for m in &rep.modules {
3046            match &m.module {
3047                Some(n) => eprintln!("module: {} ({n})", m.path),
3048                None => eprintln!("module: {}", m.path),
3049            }
3050        }
3051        for d in &rep.dependencies {
3052            eprintln!("dependency: {}", d.name);
3053        }
3054        eprintln!(
3055            "htl unused: {}, {} [{} considered, {} reached, {}]",
3056            count(s.modules, "module", "modules"),
3057            count(s.dependencies, "dependency", "dependencies"),
3058            s.considered,
3059            s.reached,
3060            count(s.entries, "entry", "entries"),
3061        );
3062    }
3063    Ok(if fail_on_unused && !s.ok {
3064        ExitCode::FAILURE
3065    } else {
3066        ExitCode::SUCCESS
3067    })
3068}
3069
3070/// Report how many declarations carry each of htl's nine markers, and where.
3071///
3072/// The census is the check's — `htl check` already read every marker by position while
3073/// checking a file, exactly as the lints it drives do — so this is that census folded into
3074/// the nine features (`htl::adopt`). What is left here is the flags, the table, and the
3075/// exit code, which never moves: a report, not a gate.
3076fn cmd_adopt(paths: &[PathBuf], flags: AdoptFlags) -> Result<ExitCode> {
3077    let AdoptFlags {
3078        json,
3079        detail,
3080        use_cache,
3081        explain,
3082    } = flags;
3083    let paths = if paths.is_empty() {
3084        vec![PathBuf::from(".")]
3085    } else {
3086        paths.to_vec()
3087    };
3088    let cfg = load_config(&paths[0])?;
3089    // A `.d.tl` written from Rust source is an input to the check the census comes from,
3090    // exactly as it is for `htl check` and `htl unused`.
3091    auto_dts(&paths[0])?;
3092    let model = project::model_of(&cfg, &paths[0])?;
3093    let rep = htl::adopt::adopt(&htl::adopt::Options {
3094        paths: &paths,
3095        config: &cfg,
3096        model: model.as_ref(),
3097        cache: project::cache_options(use_cache, None, &cfg, explain),
3098    })?;
3099    if json {
3100        report::emit(&rep)?;
3101    } else {
3102        print_adopt(&rep, detail);
3103    }
3104    // Always 0: `htl adopt` reports what the project has adopted, and does not judge it
3105    // (the module doc says why — a threshold invites marking records to move the number).
3106    Ok(ExitCode::SUCCESS)
3107}
3108
3109/// The marker column's width, in both the table and `--detail`'s lines: the longest of
3110/// htl's nine, `---@extensible`, plus two spaces.
3111fn adopt_marker_width() -> usize {
3112    htl::adopt::FEATURES
3113        .iter()
3114        .map(|m| format!("---@{m}").len())
3115        .max()
3116        .unwrap_or(0)
3117        + 2
3118}
3119
3120/// The table, `--detail`'s lines when asked for, and the summary line — the text form of
3121/// an [`htl::adopt::Report`].
3122fn print_adopt(rep: &htl::adopt::Report, detail: bool) {
3123    let width = adopt_marker_width();
3124    eprintln!(
3125        "{:<width$} {:>4}  {:>10}",
3126        "feature",
3127        "used",
3128        "applicable",
3129        width = width
3130    );
3131    for f in &rep.features {
3132        let applicable = match f.applicable {
3133            Some(n) => n.to_string(),
3134            None => "-".to_string(),
3135        };
3136        eprintln!(
3137            "{:<width$} {:>4}  {:>10}",
3138            format!("---@{}", f.marker),
3139            f.used,
3140            applicable,
3141            width = width
3142        );
3143    }
3144    if detail {
3145        // Padded to the longest `file:line` of every site and every candidate about to
3146        // be printed, not per feature: one column, so the names line up down the whole
3147        // list, a candidate's line included.
3148        let line_width = rep
3149            .features
3150            .iter()
3151            .flat_map(|f| {
3152                f.sites
3153                    .iter()
3154                    .map(|s| format!("{}:{}", s.file.display(), s.line))
3155                    .chain(
3156                        f.candidates
3157                            .iter()
3158                            .map(|c| format!("{}:{}", c.file.display(), c.line)),
3159                    )
3160            })
3161            .map(|s| s.len())
3162            .max()
3163            .unwrap_or(0);
3164        for f in &rep.features {
3165            for s in &f.sites {
3166                eprintln!(
3167                    "  {:<width$}{:<line_width$}  {}",
3168                    format!("---@{}", f.marker),
3169                    format!("{}:{}", s.file.display(), s.line),
3170                    s.name,
3171                    width = width,
3172                    line_width = line_width
3173                );
3174            }
3175            // A candidate's line ends with how many construction sites made it one, so
3176            // a reader can tell it from a marked declaration above it.
3177            for c in &f.candidates {
3178                let sites = if c.sites == 1 {
3179                    "1 site".to_string()
3180                } else {
3181                    format!("{} sites", c.sites)
3182                };
3183                eprintln!(
3184                    "  {:<width$}{:<line_width$}  {}  (applicable: {}, {sites})",
3185                    format!("---@{}", f.marker),
3186                    format!("{}:{}", c.file.display(), c.line),
3187                    c.name,
3188                    c.reason,
3189                    width = width,
3190                    line_width = line_width
3191                );
3192            }
3193        }
3194    }
3195    let s = &rep.summary;
3196    if s.check_errors > 0 {
3197        eprintln!(
3198            "htl adopt: {} error(s) in the project's check (htl check says which); a file \
3199             with a syntax error contributes no markers",
3200            s.check_errors
3201        );
3202    }
3203    if s.markers == 0 {
3204        eprintln!(
3205            "htl adopt: {} of {} features used, no markers",
3206            s.used, s.features
3207        );
3208    } else {
3209        eprintln!(
3210            "htl adopt: {} of {} features used, {} in {}",
3211            s.used,
3212            s.features,
3213            count(s.markers, "marker", "markers"),
3214            count(s.files, "file", "files"),
3215        );
3216    }
3217}
3218
3219/// Say which file a module name resolves to, and what that hides.
3220///
3221/// The chain comes from the library ([`htl::resolve`]), over a checker set up exactly as a
3222/// check of this project sets one up — the installed deps, then the config's search paths,
3223/// in that order, because `add_path` prepends and the order is the whole answer. Asking
3224/// any other way would report a path nothing else uses.
3225///
3226/// A report, so both forms go to stdout (README, "Machine-readable output").
3227fn cmd_resolve(module: &str, path: Option<&Path>, json: bool) -> Result<ExitCode> {
3228    let start = path.unwrap_or(Path::new("."));
3229    // The declarations a check generates before it reads anything are inputs to the
3230    // answer — `types/<crate>/` is written by `htl dts` — and a report without them would
3231    // describe a project nobody checks; `checker_for` writes them. The search order the
3232    // report prints is `project::checker`'s, the one `htl check` reads under.
3233    let ProjectChecker { h, cfg, model, .. } = checker_for(start, true)?;
3234    let rep = htl::resolve::resolve(
3235        &h,
3236        module,
3237        cfg.as_ref().map(|(r, _, _)| r.as_path()),
3238        model.as_ref(),
3239    )?;
3240    if json {
3241        report::emit(&rep)?;
3242    } else {
3243        print_resolution(&rep);
3244    }
3245    Ok(if rep.summary.ok {
3246        ExitCode::SUCCESS
3247    } else {
3248        ExitCode::FAILURE
3249    })
3250}
3251
3252/// The text form of a resolution: a table in search order, then who answered — the project
3253/// 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
3254/// is why it is printed rather than left to be looked up.
3255fn print_resolution(r: &htl::resolve::Resolution) {
3256    let ambiguous = r
3257        .candidates
3258        .iter()
3259        .any(|c| c.status == htl::resolve::Status::Ambiguous);
3260    // A name the host provides: what answers it at run time is the host, and a row read
3261    // for it is only the declaration it is typed from. Refused when a file implements it
3262    // too, and then the header is the error, as a check reports it.
3263    let typed_by = r
3264        .read
3265        .as_ref()
3266        .map(|d| format!(", typed by {d}"))
3267        .unwrap_or_default();
3268    match (&r.read, &r.error, &r.provided_by) {
3269        (_, Some(e), _) => println!("htl resolve {}: error: {e}", r.module),
3270        // Declared and nothing else: the environment provides it — a library installed on
3271        // the machine, a module registered where the model cannot read it. The declaration
3272        // is the file read, and comes first, as any file the name resolves to does.
3273        (Some(read), None, Some(by)) if r.provider == Some(htl::model::Provider::Declared) => {
3274            println!(
3275                "htl resolve {}: {read}, provided by the environment ({by})",
3276                r.module
3277            )
3278        }
3279        (_, None, Some(by)) => {
3280            println!(
3281                "htl resolve {}: provided by the host ({by}){typed_by}",
3282                r.module
3283            )
3284        }
3285        (Some(read), None, None) => println!("htl resolve {}: {read}", r.module),
3286        (None, None, None) if ambiguous => println!(
3287            "htl resolve {}: more than one module implements it, and no order picks one",
3288            r.module
3289        ),
3290        (None, None, None) => println!(
3291            "htl resolve {}: nothing in the project or on the search path answers require(\"{}\")",
3292            r.module, r.module
3293        ),
3294    }
3295    if !r.candidates.is_empty() {
3296        let w = |f: fn(&htl::resolve::Candidate) -> usize, head: usize| {
3297            r.candidates.iter().map(f).max().unwrap_or(0).max(head)
3298        };
3299        let file = w(|c| c.path.len(), 4);
3300        let kind = w(|c| c.kind.as_str().len(), 4);
3301        let read_at = r
3302            .candidates
3303            .iter()
3304            .find(|c| c.status == htl::resolve::Status::Read)
3305            .map(|c| c.order);
3306        println!();
3307        println!("  order  {:file$}  {:kind$}  status", "file", "kind");
3308        for c in &r.candidates {
3309            let status = match (c.status, c.shadowed_by, read_at) {
3310                (htl::resolve::Status::Shadowed, Some(by), _) => format!("shadowed by {by}"),
3311                // Not hidden by the row that is read: typed by it, and loaded by the run.
3312                (htl::resolve::Status::Runtime, _, Some(by)) => format!("runtime, typed by {by}"),
3313                (s, _, _) => s.as_str().to_string(),
3314            };
3315            let origin = match &c.origin {
3316                Some(o) => format!("  ({})", o.describe()),
3317                None => String::new(),
3318            };
3319            println!(
3320                "  {:<5}  {:file$}  {:kind$}  {status}{origin}",
3321                c.order,
3322                c.path,
3323                c.kind.as_str()
3324            );
3325        }
3326    }
3327    println!();
3328    match r.answered_by {
3329        htl::resolve::AnsweredBy::Model => println!("  answered by the project model"),
3330        // The name is not the project's: where the search looked is half the answer, and
3331        // all of it when nothing was found.
3332        htl::resolve::AnsweredBy::Path => {
3333            println!("  searched, in order: {}", r.searched.join(", "))
3334        }
3335    }
3336}
3337
3338/// `1 module` / `2 modules`: a count whose noun agrees with it, for a line short enough
3339/// that `module(s)` would be the loudest thing on it.
3340fn count(n: usize, one: &str, many: &str) -> String {
3341    format!("{n} {}", if n == 1 { one } else { many })
3342}
3343
3344/// Delete this project's cache store.
3345///
3346/// The store sits beside `htl.toml`, which is not necessarily where anyone is standing:
3347/// `htl check src` run from anywhere in a repo writes to the project root. That is the
3348/// reason this exists rather than leaving people to work out which directory to remove.
3349///
3350/// Only `.htl/cache` goes. `.htl/` itself is left in place for whatever else may come to
3351/// live there, and the path is built here rather than taken from an argument, so there is
3352/// no spelling of the command that removes anything else.
3353fn cmd_cache_clear(path: Option<&Path>) -> Result<ExitCode> {
3354    let root = cache_root(path)?;
3355    let dir = root.join(".htl").join("cache");
3356    if !dir.is_dir() {
3357        eprintln!("htl cache: nothing stored at {}", dir.display());
3358        return Ok(ExitCode::SUCCESS);
3359    }
3360    let n = fs::read_dir(&dir)
3361        .map(|rd| {
3362            rd.flatten()
3363                .filter(|e| e.path().extension().is_some_and(|x| x == "json"))
3364                .count()
3365        })
3366        .unwrap_or(0);
3367    fs::remove_dir_all(&dir).with_context(|| format!("removing {}", dir.display()))?;
3368    eprintln!(
3369        "htl cache: removed {n} {} from {}",
3370        if n == 1 { "entry" } else { "entries" },
3371        dir.display()
3372    );
3373    Ok(ExitCode::SUCCESS)
3374}
3375
3376/// The directory a cache command works on: the project's root, as every command keeps its
3377/// store there ([`project::store_root`]), or the working directory outside a project.
3378fn cache_root(path: Option<&Path>) -> Result<PathBuf> {
3379    let start = path.unwrap_or(Path::new("."));
3380    let model = htl::model::Project::find_root(start)?.map(|r| r.root);
3381    Ok(model.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))))
3382}
3383
3384/// Say what the store holds.
3385///
3386/// Answers "what is stored", not "what would be reused": an entry listed here can still miss
3387/// on the next run because a file it recorded has changed. The two are different questions
3388/// and conflating them would make this command lie in the more dangerous direction.
3389fn cmd_cache_status(path: Option<&Path>, json: bool, list_entries: bool) -> Result<ExitCode> {
3390    let c = cache::describe(&cache_root(path)?);
3391    if json {
3392        report::emit(&c)?;
3393        return Ok(ExitCode::SUCCESS);
3394    }
3395
3396    if c.entries.is_empty() {
3397        println!("htl cache: empty ({})", c.dir);
3398        return Ok(ExitCode::SUCCESS);
3399    }
3400    let mut by_kind: std::collections::BTreeMap<&str, (usize, u64)> = Default::default();
3401    for e in &c.entries {
3402        let slot = by_kind.entry(e.kind.as_str()).or_default();
3403        slot.0 += 1;
3404        slot.1 += e.bytes;
3405    }
3406    println!(
3407        "htl cache: {} entries, {} at {}",
3408        c.entries.len(),
3409        human_bytes(c.bytes),
3410        c.dir
3411    );
3412    for (kind, (n, bytes)) in &by_kind {
3413        let what = match *kind {
3414            cache::CHECK => "checked modules",
3415            cache::GEN => "checked and generated test files (htl test)",
3416            cache::MODULE => "generated modules (htl test / build, include_bundle!)",
3417            cache::RUN => "whole runs",
3418            _ => "entries this build cannot read",
3419        };
3420        println!("  {n:>5} {what} ({})", human_bytes(*bytes));
3421    }
3422    if let (Some(min), Some(max)) = (
3423        c.entries.iter().map(|e| e.age_secs).min(),
3424        c.entries.iter().map(|e| e.age_secs).max(),
3425    ) {
3426        println!(
3427            "  last used between {} and {} ago",
3428            human_age(min),
3429            human_age(max)
3430        );
3431    }
3432    if list_entries {
3433        println!();
3434        for e in &c.entries {
3435            println!(
3436                "  {:<6} {:>8}  {}",
3437                e.kind,
3438                human_bytes(e.bytes),
3439                e.subjects.join(", ")
3440            );
3441        }
3442    }
3443    Ok(ExitCode::SUCCESS)
3444}
3445
3446fn human_bytes(n: u64) -> String {
3447    if n >= 1 << 20 {
3448        format!("{:.1} MB", n as f64 / (1u64 << 20) as f64)
3449    } else if n >= 1 << 10 {
3450        format!("{:.0} KB", n as f64 / (1u64 << 10) as f64)
3451    } else {
3452        format!("{n} B")
3453    }
3454}
3455
3456fn human_age(secs: u64) -> String {
3457    match secs {
3458        s if s < 90 => format!("{s}s"),
3459        s if s < 5400 => format!("{}m", s / 60),
3460        s if s < 172_800 => format!("{}h", s / 3600),
3461        s => format!("{}d", s / 86_400),
3462    }
3463}
3464
3465/// How many of the files a walk visited came out of a patched dependency: the files in the
3466/// home of a module the project owns as a patch ([`htl::model::Project::home_of`]) — its
3467/// tests included, which no name reaches. Zero outside a project and for a
3468/// project with no patch, which is what keeps the summary line below unchanged for
3469/// everyone who has never run `htl pkg patch`.
3470fn patched_files(model: Option<&htl::model::Project>, files: &[PathBuf]) -> usize {
3471    let Some(model) = model else {
3472        return 0;
3473    };
3474    if !model
3475        .modules
3476        .iter()
3477        .any(|m| m.owner == htl::model::Owner::Patched)
3478    {
3479        return 0;
3480    }
3481    files
3482        .iter()
3483        .filter(|f| {
3484            model
3485                .home_of(f)
3486                .is_some_and(|m| m.owner == htl::model::Owner::Patched)
3487        })
3488        .count()
3489}
3490
3491/// `htl check --statistics`'s table: one row per rule, count first, the rule names left
3492/// aligned on the longest of them. Nothing is printed for an empty slice — a run with no
3493/// finding leaves the summary line to say `0 error(s), 0 warning(s), 0 lint(s)` on its own.
3494fn print_statistics(rows: &[report::RuleStatistic]) {
3495    let width = rows.iter().map(|r| r.rule.len()).max().unwrap_or(0);
3496    for r in rows {
3497        eprintln!(
3498            "{:>5}  {:<width$}  {}",
3499            r.count,
3500            r.rule,
3501            statistics_marker(r)
3502        );
3503    }
3504}
3505
3506/// `[*]`: every finding of the rule carries a fix `htl fix` applies without being asked
3507/// (`safe`) or shows (`suggest`). `[ ]`: none does. `[-]`: the rest — some findings fixable
3508/// and some not, or every one fixable but only some `safe`/`suggest`, the remainder
3509/// needing `--unsafe`.
3510fn statistics_marker(r: &report::RuleStatistic) -> &'static str {
3511    if r.fixable == r.count {
3512        "[*]"
3513    } else if r.fixable + r.unsafe_fixable == 0 {
3514        "[ ]"
3515    } else {
3516        "[-]"
3517    }
3518}
3519
3520#[cfg(test)]
3521mod statistics_marker_tests {
3522    use super::statistics_marker;
3523    use crate::report::RuleStatistic;
3524
3525    fn stat(count: usize, fixable: usize, unsafe_fixable: usize) -> RuleStatistic {
3526        RuleStatistic {
3527            rule: "r".to_string(),
3528            count,
3529            fixable,
3530            unsafe_fixable,
3531        }
3532    }
3533
3534    /// Every finding fixable, and none of it `--unsafe`-only.
3535    #[test]
3536    fn full_safe_coverage_is_a_star() {
3537        assert_eq!(statistics_marker(&stat(2, 2, 0)), "[*]");
3538    }
3539
3540    /// No finding carries a fix at all.
3541    #[test]
3542    fn no_fix_at_all_is_blank() {
3543        assert_eq!(statistics_marker(&stat(2, 0, 0)), "[ ]");
3544    }
3545
3546    /// Some fixable, some not: a genuine mix.
3547    #[test]
3548    fn a_mix_of_fixable_and_not_is_a_dash() {
3549        assert_eq!(statistics_marker(&stat(3, 1, 0)), "[-]");
3550    }
3551
3552    /// Full coverage, but not every one of it `safe`/`suggest`: the `--unsafe` ones keep
3553    /// it from being a plain `[*]`.
3554    #[test]
3555    fn full_coverage_split_between_safe_and_unsafe_is_a_dash() {
3556        assert_eq!(statistics_marker(&stat(2, 1, 1)), "[-]");
3557    }
3558
3559    /// Full coverage, every one of it `--unsafe`-only.
3560    #[test]
3561    fn full_unsafe_only_coverage_is_a_dash() {
3562        assert_eq!(statistics_marker(&stat(2, 0, 2)), "[-]");
3563    }
3564}
3565
3566/// The one place a check reports its totals, so a replayed module and a checked one cannot
3567/// drift apart in how they are summarized. Returns whether the run counts as a failure.
3568///
3569/// `patched` is how many of those files are a patched dependency's ([`patched_files`]),
3570/// split out of the count rather than added to it. One `htl pkg patch` turned a project's
3571/// `15 file(s)` into `25 file(s)` with nothing said, and a file count that moves for a
3572/// reason the line does not give is a file count a reader cannot use: the dependency's
3573/// `tests/` are now in the walk, they are not the project's, and the line has to say so.
3574fn report_check(
3575    out: &mut report::Out,
3576    json: bool,
3577    statistics: bool,
3578    rep: &project::Report,
3579    policy: &htl::verdict::Policy,
3580    patched: usize,
3581) -> Result<bool> {
3582    let (files, replayed) = (rep.files.len(), rep.replayed);
3583    let (errors, warnings, lints) = (rep.errors, rep.warnings, rep.lints);
3584    let strict = policy.strict;
3585    let fail = htl::verdict::verdict(&rep.findings(), policy);
3586    let all_cached = rep.all_cached();
3587    if json {
3588        report::emit(&report::CheckReport {
3589            files,
3590            patched,
3591            diagnostics: out.take(),
3592            summary: report::CheckSummary {
3593                errors,
3594                warnings,
3595                lints,
3596                denied: rep.denied,
3597                strict,
3598                ok: !fail,
3599                cached: all_cached,
3600                replayed,
3601            },
3602            statistics: statistics.then(|| out.statistics()),
3603        })?;
3604    } else {
3605        if statistics {
3606            print_statistics(&out.statistics());
3607        }
3608        // Say nothing when nothing was replayed; say how much when it was only some.
3609        let cached = if all_cached {
3610            " [cached]".to_string()
3611        } else if replayed > 0 {
3612            format!(" [{replayed}/{files} cached]")
3613        } else {
3614            String::new()
3615        };
3616        // The three counts say what kinds of thing the run said; `at deny` says how much
3617        // of that it failed on, and is left out when it is none of it — its being there at
3618        // all is the news. A run with two warnings and one denied lint reads as
3619        // `0 error(s), 2 warning(s), 1 lint(s), 1 at deny`.
3620        let denied = match rep.denied {
3621            0 => String::new(),
3622            n => format!(", {n} at deny"),
3623        };
3624        // A project with no patch reads exactly as it read before the split existed: the
3625        // second number appears only when there is something on the other side of it, and
3626        // the two always add up to the one number that was there.
3627        let counted = match patched {
3628            0 => format!("{files} file(s)"),
3629            n => format!("{} file(s) + {n} in patched dependencies", files - n),
3630        };
3631        // Where the why is, once per rule this run said something under, after the
3632        // findings and before the summary — the way cargo names `cargo fix` once rather
3633        // than on every line. A finding's message names the site; the explanation names
3634        // the rule. Only rules that have one (every lint does; an error has no rule).
3635        for rule in out.rules_said() {
3636            if htl::lint::explained(rule).is_some() {
3637                eprintln!("htl check --explain {rule}");
3638            }
3639        }
3640        eprintln!(
3641            "htl check: {counted}, {errors} error(s), {warnings} warning(s), {lints} lint(s){denied}{}{cached}",
3642            if strict { " [strict]" } else { "" }
3643        );
3644    }
3645    Ok(fail)
3646}
3647
3648/// What [`checker_for`] set up, and what it set it up from.
3649struct ProjectChecker {
3650    /// The checker: the lint selection and `[lang]` of the project, its test library,
3651    /// `std.*`, the libraries this binary registers and `htl.task`, then the model's
3652    /// directories on the search path with its dependencies reachable.
3653    h: Htl,
3654    /// The config around the starting path; `None` outside a project.
3655    cfg: project::Config,
3656    /// The project model; `None` when there is neither an `htl.toml` nor an
3657    /// `mlua-pkg.toml`.
3658    model: Option<htl::model::Project>,
3659    /// What `htl check` holds a file to: the lint selection `h` was configured from
3660    /// (`scope.lints`), and the contracts and host modules a walk reads. A command
3661    /// that judges what it generates asks the same `Lints` for its levels, so the
3662    /// rules `h` runs and the rules the command reports under are one answer.
3663    scope: project::Scope,
3664}
3665
3666/// A checker set up for the project `start` is in, through the two library functions
3667/// `htl check` goes through: [`project::Scope::new`] for the lint input, and
3668/// [`project::checker`] for the checker built from it: declarations written, model
3669/// loaded, scope, checker. A command that parses `.tl` and resolves requires calls this
3670/// and does not assemble a checker itself, so a `require` the checker resolves for
3671/// `htl check` is one `htl gen`, `htl run`, `htl build`, `htl resolve`, `htl shapes` and
3672/// `htl fix` resolve too, at the same lint levels. They each used to spell the install
3673/// calls out, in an order of their own, and `htl check` agreed with none of them: `htl
3674/// gen` did not install `htl.task`, and found its declaration only when a check or a run
3675/// had left it in the temporary directory first.
3676///
3677/// `write_dts` is the one difference between callers: whether the declarations the
3678/// project publishes (`auto_dts`) are written before anything is read. `htl fix
3679/// --dry-run` is the only caller that passes `false`; what it then checks is the
3680/// declarations as they are on disk.
3681///
3682/// What one file may read beyond what the model gives — the test root, for a file under
3683/// it; its own directory, for a file in no project — is [`project::file_view`], asked per
3684/// file by the command that has one.
3685///
3686/// The sites that do not come through here, and why:
3687/// - `cmd_fmt` only parses, and resolves no `require`, so it has no search path to set up.
3688/// - `htl check` and the language server hold a [`project::Scope`] of their own across
3689///   many files and call [`project::checker`] directly.
3690/// - the proc macros in `htl-macros` (`include_tl!`, `include_bundle!`) check inside
3691///   `rustc`, which has no `main` that called `register_installer`: see the module doc of
3692///   `htl_core::registry`.
3693fn checker_for(start: &Path, write_dts: bool) -> Result<ProjectChecker> {
3694    let cfg = load_config(start)?;
3695    auto_dts_to(start, write_dts)?;
3696    let model = project::model_of(&cfg, start)?;
3697    let scope = project::Scope::new(&cfg, model.as_ref(), start, None)?;
3698    let h = project::checker(
3699        model.as_ref(),
3700        scope.lints.selection(),
3701        &project::lang_of(&cfg),
3702    )?;
3703    Ok(ProjectChecker {
3704        h,
3705        cfg,
3706        model,
3707        scope,
3708    })
3709}
3710
3711fn cmd_gen(file: &Path, out: Option<&Path>) -> Result<ExitCode> {
3712    // The search path `htl check` gives this file, so `htl gen` resolves what `htl check`
3713    // resolved and a file the checker accepts is one this command can emit.
3714    let ProjectChecker { h, model, .. } = checker_for(file, true)?;
3715    project::file_view(&h, model.as_ref(), file)?;
3716    let (code, c) = h.gen_lua(file)?;
3717    text_sink().checkinfo(&c);
3718    // Judged on its errors alone (`Policy::ERRORS_ONLY`): the warnings and lints above are
3719    // reported, and `htl check` is where they are judged.
3720    let (Some(code), false) = (code, runs_fail(&c)) else {
3721        return Ok(ExitCode::FAILURE);
3722    };
3723    // Written as it came back. This command used to append the final newline itself, which
3724    // made it the one consumer of `gen_lua` whose output was a whole file; the generator
3725    // does it for all of them now (`H.gen` in `prelude.lua` says why).
3726    match out {
3727        Some(p) => fs::write(p, code).with_context(|| format!("writing {}", p.display()))?,
3728        None => print!("{code}"),
3729    }
3730    Ok(ExitCode::SUCCESS)
3731}
3732
3733fn cmd_run(file: &Path, args: &[String]) -> Result<ExitCode> {
3734    let bytes = fs::read(file).with_context(|| format!("reading {}", file.display()))?;
3735    // The search path `htl check` gives this file, so `htl run` resolves what `htl check`
3736    // resolved rather than failing on a `require` the checker was happy with. Before the
3737    // bundle branch because a bundle carries its own modules and gains nothing from it
3738    // either way.
3739    let ProjectChecker { h, cfg, model, .. } = checker_for(file, true)?;
3740    // The executor's `[async]` is not part of what a check reads; nothing starts a task
3741    // before `load_bundle` / `run_root`, so setting it once the checker is up is early enough.
3742    h.configure_async(async_config(&cfg))?;
3743    if Bundle::is_bundle(&bytes) {
3744        let b = Bundle::decode(&bytes)?;
3745        // A bundle's frames are as good as its payload: stripped bytecode has no lines to
3746        // show, and `htl build --debug` is what keeps them.
3747        let main = h.load_bundle(&b, args)?;
3748        return run_root(&h, Entry::Main(main), args);
3749    }
3750    // Check first so lints/warnings are visible before the script runs.
3751    project::file_view(&h, model.as_ref(), file)?;
3752    h.install_searcher()?;
3753    h.set_arg(&file.to_string_lossy(), args)?;
3754    let (code, c) = h.gen_lua(file)?;
3755    text_sink().checkinfo(&c);
3756    // As `htl gen`: errors stop it, the rest is reported (`Policy::ERRORS_ONLY`).
3757    let (Some(code), false) = (code, runs_fail(&c)) else {
3758        return Ok(ExitCode::FAILURE);
3759    };
3760    run_root(
3761        &h,
3762        Entry::Chunk {
3763            code,
3764            name: format!("@{}", file.display()),
3765        },
3766        args,
3767    )
3768}
3769
3770/// What `htl run` runs as the program's root: a generated chunk, or a bundle's entry.
3771enum Entry {
3772    Chunk { code: String, name: String },
3773    Main(htl::mlua::Function),
3774}
3775
3776/// The `[async]` of a loaded `htl.toml`, or the default when there is no file.
3777fn async_config(cfg: &project::Config) -> &htl::config::AsyncConfig {
3778    static DEFAULT: htl::config::AsyncConfig = htl::config::AsyncConfig {
3779        grace_ms: None,
3780        preempt: None,
3781    };
3782    match cfg {
3783        Some((_, _, c)) => &c.async_,
3784        None => &DEFAULT,
3785    }
3786}
3787
3788/// Run the program as a root coroutine on the executor, with Ctrl-C cancelling it.
3789///
3790/// The runtime is a current-thread tokio runtime with its time driver (the grace is a
3791/// tokio timeout) and a `LocalSet`, built for this run; `Htl::run_async` is awaited to
3792/// the end, so when it resolves nothing the program started is alive.
3793///
3794/// Ctrl-C is watched from a thread of its own rather than a task on this runtime: a
3795/// program in a CPU loop never yields to the runtime, so a task here would never get to
3796/// run, while the cancel hook reads the token from any thread at its next check. The
3797/// first Ctrl-C cancels the root — the program gets its grace to clean up — and a second
3798/// one ends the process, for a program that does not.
3799///
3800/// A cancelled run prints `htl run: interrupted` and exits 130, the shell's own code for
3801/// it; a program that raised prints the innermost cause and the frames that reached it
3802/// (`htl run` is a development command, so the frames are the default; `htl::user_message`
3803/// is what an embedding host shows people who did not write the Teal).
3804fn run_root(h: &Htl, entry: Entry, args: &[String]) -> Result<ExitCode> {
3805    let token = htl::mlua_isle::runtime::CancelToken::new();
3806    {
3807        let token = token.clone();
3808        std::thread::Builder::new()
3809            .name("htl-ctrl-c".into())
3810            .spawn(move || {
3811                let Ok(rt) = tokio::runtime::Builder::new_current_thread()
3812                    .enable_all()
3813                    .build()
3814                else {
3815                    return;
3816                };
3817                rt.block_on(async {
3818                    if tokio::signal::ctrl_c().await.is_ok() {
3819                        token.cancel();
3820                    }
3821                    if tokio::signal::ctrl_c().await.is_ok() {
3822                        std::process::exit(130);
3823                    }
3824                });
3825            })
3826            .context("starting the Ctrl-C watcher")?;
3827    }
3828    let rt = tokio::runtime::Builder::new_current_thread()
3829        .enable_all()
3830        .build()
3831        .context("building the executor's runtime")?;
3832    let local = tokio::task::LocalSet::new();
3833    let out = local.block_on(&rt, async {
3834        match entry {
3835            Entry::Chunk { code, name } => h.run_async(&code, &name, args, &token).await,
3836            Entry::Main(main) => {
3837                let va: htl::mlua::Variadic<String> = args.iter().cloned().collect();
3838                h.call_async(main, va, &token).await.map(|_| ())
3839            }
3840        }
3841    });
3842    Ok(match out {
3843        Ok(()) => ExitCode::SUCCESS,
3844        Err(e) if htl::is_cancelled(&e) => {
3845            eprintln!("htl run: interrupted");
3846            ExitCode::from(130)
3847        }
3848        Err(e) => {
3849            eprintln!("{}", htl::developer_message(&e));
3850            ExitCode::FAILURE
3851        }
3852    })
3853}
3854
3855/// Whether `htl run` / `htl gen` stop at the check `c`: [`htl::verdict::verdict`] under
3856/// [`Policy::ERRORS_ONLY`](htl::verdict::Policy::ERRORS_ONLY), so an error does and
3857/// nothing else.
3858fn runs_fail(c: &htl::CheckInfo) -> bool {
3859    htl::verdict::verdict(
3860        &htl::verdict::Findings {
3861            errors: c.errors.len(),
3862            warnings: c.warnings.len(),
3863            lints: c.lints.len(),
3864            denied: 0,
3865        },
3866        &htl::verdict::Policy::ERRORS_ONLY,
3867    )
3868}
3869
3870/// The run cache as `htl build` was asked to use it: `--no-cache` and `--explain-cache`,
3871/// the two switches that mean something for one closure. There is no `--cache-mode`: a
3872/// build's entries are per module, as `htl test`'s are, since a build is many modules and
3873/// an edit to one should cost that one and its dependents rather than the closure.
3874struct BuildCache {
3875    use_cache: bool,
3876    explain: bool,
3877}
3878
3879fn cmd_build(
3880    entry: &Path,
3881    out: &Path,
3882    main: &str,
3883    mut opts: htl::link::LinkOptions,
3884    cache_flags: BuildCache,
3885) -> Result<ExitCode> {
3886    // `std.*` resolves to its declaration and nothing else, so the linker files it under
3887    // the host's modules — which is what it is: the binary that runs the bundle preloads
3888    // it, as `htl run` does before `run_bundle`.
3889    let ProjectChecker {
3890        h,
3891        cfg,
3892        model,
3893        scope,
3894    } = checker_for(entry, true)?;
3895    // The rules and levels `htl check` uses: a bundle is judged as a check of its closure
3896    // would be, and a rule the project turned off is not reported here either.
3897    let lints = scope.lints;
3898    let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
3899    // The names the host provides are the model's — `#[host_module]`s in the crate around
3900    // the project, `[build] host`, `std.*` — so the bundle leaves out what the host
3901    // registers without anyone restating it; `--host` adds to them.
3902    if let Some(m) = &model {
3903        opts.host.extend(m.provided().map(|(n, _)| n.to_string()));
3904    }
3905    if let Some((_, _, cfg)) = &cfg {
3906        opts.extra.extend(cfg.build.extra.iter().cloned());
3907        opts.host.extend(cfg.build.host.iter().cloned());
3908        // The one thing `[build] target` changes: this command produces the `hb` target and
3909        // nothing else, so a project that records another is told which command does build
3910        // it rather than handed a bundle nothing in it was going to load. Here rather than
3911        // further down because both forms of `htl build` pass through — the directory form
3912        // returns a few lines below — and because nothing has been written yet.
3913        if let Some(t) = cfg.build.target.filter(|t| *t != BuildTarget::Hb) {
3914            eprintln!(
3915                "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.",
3916                t.name(),
3917                t.runs_it()
3918            );
3919            return Ok(ExitCode::FAILURE);
3920        }
3921    }
3922    if entry.is_dir() {
3923        if cache_flags.explain {
3924            eprintln!("htl cache: the directory form of `htl build` is not cached");
3925        }
3926        // Not into an installed or vendored copy — a dependency's files, which the model
3927        // knows — and into a patch, which is the project's code.
3928        let skip = project::not_walked(
3929            model.as_ref(),
3930            &[entry.to_path_buf()],
3931            htl::model::Purpose::Check,
3932        );
3933        return cmd_build_dir(&h, entry, out, main, &opts, &skip, &lints, &policy);
3934    }
3935    project::file_view(&h, model.as_ref(), entry)?;
3936    // The name the entry is served under: the one the project's model gives the file,
3937    // which is what a `require` of it elsewhere in the project writes.
3938    opts.entry_name = model.as_ref().and_then(|m| m.locate(entry).map(|p| p.name));
3939    // The store: `module` entries keyed the way `htl test` and the macros key them — the
3940    // file the module is and the lint selection `htl.toml` puts in force — so a module any
3941    // of them generated is one the build replays, and the reverse. Nothing is swept here:
3942    // a build sees one closure, and only `htl check`, which sees the project, bounds the
3943    // store (`cache.rs`).
3944    let root = project::store_root(model.as_ref());
3945    // `htl.toml`'s spec as the scope joined it, the one `lints` was parsed from.
3946    let lint = (!scope.spec.is_empty()).then_some(scope.spec.as_str());
3947    let cache_opts = project::cache_options(
3948        cache_flags.use_cache,
3949        Some(cache::Mode::PerModule),
3950        &cfg,
3951        cache_flags.explain,
3952    );
3953    let store = project::with_model(
3954        project::store(&root, cache_opts, None, "htl build"),
3955        model.as_ref(),
3956    );
3957    let link_store = store.as_ref().map(|c| htl::link::LinkStore {
3958        cache: c,
3959        lint,
3960        root: &root,
3961        config: cfg.as_ref().map(|(_, p, c)| (p.as_path(), c)),
3962    });
3963    let linked = htl::link::link_with(&h, entry, &opts, link_store)?;
3964    project::explain_cache(store.as_ref(), cache_opts);
3965    // One sink for the whole closure: a build is many modules, and what the layer says
3966    // once per run is said once for the build rather than once per module.
3967    let mut sink = text_sink();
3968    sink.judge_by(lints.selection());
3969    for (p, c) in &linked.checks {
3970        sink.checkinfo(&linked.reported(p, c));
3971    }
3972    let n_err = linked.errors.len();
3973    let found = htl::verdict::Findings {
3974        errors: n_err,
3975        warnings: linked.checks.iter().map(|(_, c)| c.warnings.len()).sum(),
3976        lints: linked.lints.len(),
3977        denied: sink.denied(),
3978    };
3979    // The checks' own errors went through the sink above; the linker's are its own list,
3980    // said through the same sink so they read as the checks' do.
3981    for d in &linked.link_errors {
3982        sink.diagnostic(d);
3983    }
3984    if htl::verdict::verdict(&found, &policy) {
3985        eprintln!(
3986            "htl build: {n_err} error(s){}, bundle not written",
3987            judged(&found, &policy)
3988        );
3989        return Ok(ExitCode::FAILURE);
3990    }
3991    let buf = linked.bundle()?.encode();
3992    fs::write(out, &buf).with_context(|| format!("writing {}", out.display()))?;
3993    let typed = linked.modules.iter().filter(|m| m.typed).count();
3994    // The same suffix `htl check` prints: everything from the store, some of it, or
3995    // nothing said when none was.
3996    let cached = if typed > 0 && linked.cached == typed {
3997        " [cached]".to_string()
3998    } else if linked.cached > 0 {
3999        format!(" [{}/{typed} cached]", linked.cached)
4000    } else {
4001        String::new()
4002    };
4003    eprintln!(
4004        "htl build: {} module(s) ({typed} typed, {} lua) -> {} ({} bytes{}{}){cached}",
4005        linked.modules.len(),
4006        linked.modules.len() - typed,
4007        out.display(),
4008        buf.len(),
4009        if opts.source {
4010            ", source"
4011        } else {
4012            ", bytecode"
4013        },
4014        if opts.debug { " with debug info" } else { "" }
4015    );
4016    if !linked.host_modules.is_empty() {
4017        eprintln!(
4018            "htl build: host must provide: {}",
4019            linked.host_modules.join(", ")
4020        );
4021    }
4022    Ok(ExitCode::SUCCESS)
4023}
4024
4025/// The older form: every `.tl` under a directory, module names from their paths.
4026// Eight because the two judgments `cmd_build` already holds are handed in rather than
4027// asked for again from the config.
4028#[allow(clippy::too_many_arguments)]
4029fn cmd_build_dir(
4030    h: &Htl,
4031    dir: &Path,
4032    out: &Path,
4033    entry: &str,
4034    opts: &htl::link::LinkOptions,
4035    skip: &[PathBuf],
4036    lints: &htl::lint::Lints,
4037    policy: &htl::verdict::Policy,
4038) -> Result<ExitCode> {
4039    // `lints` and `policy` are the file form's, passed in by `cmd_build`: `htl.toml`'s rules
4040    // and levels, its `strict`.
4041    h.add_path(dir)?;
4042    let files = htl::collect_tl_skipping(&[dir.to_path_buf()], skip)?;
4043    let mut b = Bundle {
4044        entry: entry.to_string(),
4045        htl_version: env!("CARGO_PKG_VERSION").into(),
4046        ..Default::default()
4047    };
4048    let mut found = htl::verdict::Findings::default();
4049    let mut sink = text_sink();
4050    sink.judge_by(lints.selection());
4051    for f in &files {
4052        // Named against the directory the bundle is built from, by the naming rule.
4053        let rel = f.strip_prefix(dir).unwrap_or(f);
4054        let name = htl::naming::name_of("", rel)
4055            .with_context(|| format!("cannot derive a module name for {}", f.display()))?;
4056        let (code, c) = h.gen_lua(f)?;
4057        sink.checkinfo(&c);
4058        found.errors += c.errors.len();
4059        found.warnings += c.warnings.len();
4060        found.lints += c.lints.len();
4061        let Some(code) = code else { continue };
4062        let (kind, payload) = if opts.source {
4063            (htl::bundle::Kind::Source, code.into_bytes())
4064        } else {
4065            (
4066                htl::bundle::Kind::Bytecode,
4067                h.compile_with(&name, &code, !opts.debug)?,
4068            )
4069        };
4070        b.modules.push(htl::bundle::Module {
4071            name,
4072            kind,
4073            payload,
4074        });
4075    }
4076    found.denied = sink.denied();
4077    if htl::verdict::verdict(&found, policy) {
4078        eprintln!(
4079            "htl build: {} error(s){}, bundle not written",
4080            found.errors,
4081            judged(&found, policy)
4082        );
4083        return Ok(ExitCode::FAILURE);
4084    }
4085    if b.module(entry).is_none() {
4086        bail!(
4087            "entry module '{entry}' not found among {} module(s)",
4088            b.modules.len()
4089        );
4090    }
4091    if !opts.source {
4092        b.fingerprint = h.fingerprint()?;
4093    }
4094    let buf = b.encode();
4095    fs::write(out, &buf).with_context(|| format!("writing {}", out.display()))?;
4096    eprintln!(
4097        "htl build: {} module(s) -> {} ({} bytes)",
4098        b.modules.len(),
4099        out.display(),
4100        buf.len()
4101    );
4102    Ok(ExitCode::SUCCESS)
4103}
4104
4105/// What `htl bundle info` reports: everything the file records and nothing it does not
4106/// (no Lua state is created, nothing is loaded), which is what a build step checks in and
4107/// a bug report pastes; `--format json` is the same fields. `lua` is the header the
4108/// bytecode was compiled for, in the fields the mismatch message names; `None` when the
4109/// bundle carries no fingerprint, which `payload` explains (`source`: the text says `any`)
4110/// or `format` does (`1`, before the fingerprint: `not recorded`).
4111#[derive(serde::Serialize)]
4112struct BundleInfo {
4113    file: String,
4114    format: u8,
4115    htl_version: Option<String>,
4116    /// `bytecode`, `source`, or `mixed` when modules disagree.
4117    payload: &'static str,
4118    lua: Option<htl::bundle::LuaHeader>,
4119    entry: String,
4120    modules: Vec<BundleModuleInfo>,
4121    host_modules: Vec<String>,
4122}
4123
4124#[derive(serde::Serialize)]
4125struct BundleModuleInfo {
4126    name: String,
4127    kind: &'static str,
4128    bytes: usize,
4129}
4130
4131fn cmd_bundle_info(file: &Path, json: bool) -> Result<ExitCode> {
4132    let bytes = fs::read(file).with_context(|| format!("reading {}", file.display()))?;
4133    let Some(format) = htl::bundle::format_version(&bytes) else {
4134        bail!("{} is not an htl bundle (bad magic)", file.display());
4135    };
4136    let b = Bundle::decode(&bytes)?;
4137    let kind_name = |k: htl::bundle::Kind| match k {
4138        htl::bundle::Kind::Bytecode => "bytecode",
4139        htl::bundle::Kind::Source => "source",
4140    };
4141    let payload = match (
4142        b.modules
4143            .iter()
4144            .any(|m| m.kind == htl::bundle::Kind::Bytecode),
4145        b.modules
4146            .iter()
4147            .any(|m| m.kind == htl::bundle::Kind::Source),
4148    ) {
4149        (true, true) => "mixed",
4150        (false, true) => "source",
4151        // Bytecode, or no modules at all: what the loader would treat it as.
4152        _ => "bytecode",
4153    };
4154    let info = BundleInfo {
4155        file: file.display().to_string(),
4156        format,
4157        htl_version: (!b.htl_version.is_empty()).then(|| b.htl_version.clone()),
4158        payload,
4159        lua: htl::bundle::LuaHeader::parse(&b.fingerprint),
4160        entry: b.entry.clone(),
4161        modules: b
4162            .modules
4163            .iter()
4164            .map(|m| BundleModuleInfo {
4165                name: m.name.clone(),
4166                kind: kind_name(m.kind),
4167                bytes: m.payload.len(),
4168            })
4169            .collect(),
4170        host_modules: b.host_modules.clone(),
4171    };
4172    if json {
4173        report::emit(&info)?;
4174        return Ok(ExitCode::SUCCESS);
4175    }
4176
4177    println!(
4178        "{}: htl bundle, format {}, built by {}",
4179        info.file,
4180        info.format,
4181        match &info.htl_version {
4182            Some(v) => format!("htl {v}"),
4183            None => "htl (version not recorded)".to_string(),
4184        }
4185    );
4186    println!("  payload:  {}", info.payload);
4187    let lua = match &info.lua {
4188        Some(h) => h.to_string(),
4189        None if !b.fingerprint.is_empty() => {
4190            format!("unreadable fingerprint ({} bytes)", b.fingerprint.len())
4191        }
4192        None if info.format == 1 => {
4193            "not recorded (a format 1 bundle carries no fingerprint)".into()
4194        }
4195        None => "any (source payload, no bytecode to bind)".into(),
4196    };
4197    println!("  lua:      {lua}");
4198    println!("  entry:    {}", info.entry);
4199    let modules: Vec<String> = info
4200        .modules
4201        .iter()
4202        .map(|m| {
4203            if info.payload == "mixed" {
4204                format!("{} ({})", m.name, m.kind)
4205            } else {
4206                m.name.clone()
4207            }
4208        })
4209        .collect();
4210    println!(
4211        "  modules:  {}",
4212        if modules.is_empty() {
4213            "(none)".to_string()
4214        } else {
4215            modules.join(", ")
4216        }
4217    );
4218    println!(
4219        "  host:     {}",
4220        if info.host_modules.is_empty() {
4221            "(none)".to_string()
4222        } else {
4223            info.host_modules.join(", ")
4224        }
4225    );
4226    Ok(ExitCode::SUCCESS)
4227}