use htl::cache;
mod junit;
mod pin;
pub mod report;
mod scaffold;
#[derive(Clone, Copy, PartialEq, Eq, Debug, clap::ValueEnum)]
enum Format {
Text,
Json,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, clap::ValueEnum)]
enum CacheModeArg {
PerModule,
WholeRun,
}
#[derive(Subcommand)]
enum TypesCmd {
Add {
library: String,
#[arg(long)]
from: Option<PathBuf>,
#[arg(long)]
force: bool,
},
}
#[derive(Subcommand)]
enum PkgCmd {
Install,
Add {
name: String,
git: String,
#[arg(long)]
tag: Option<String>,
#[arg(long)]
rev: Option<String>,
#[arg(long)]
branch: Option<String>,
#[arg(long)]
entry: Option<PathBuf>,
#[arg(long)]
target_dir: Option<PathBuf>,
},
Update {
name: Option<String>,
#[arg(long)]
dry_run: bool,
#[arg(long)]
force: bool,
},
Clean {
#[arg(long)]
all: bool,
},
Patch {
dep: String,
#[arg(long)]
force: bool,
},
}
#[derive(Subcommand)]
enum CacheCmd {
Clear {
path: Option<PathBuf>,
},
Status {
path: Option<PathBuf>,
#[arg(long, value_enum, default_value_t = Format::Text)]
format: Format,
#[arg(long)]
entries: bool,
},
}
#[derive(Subcommand)]
enum BundleCmd {
Info {
file: PathBuf,
#[arg(long, value_enum, default_value_t = Format::Text)]
format: Format,
},
}
impl From<CacheModeArg> for cache::Mode {
fn from(m: CacheModeArg) -> Self {
match m {
CacheModeArg::PerModule => cache::Mode::PerModule,
CacheModeArg::WholeRun => cache::Mode::WholeRun,
}
}
}
use anyhow::{Context, Result, bail};
use clap::{Parser, Subcommand};
use htl::BuildTarget;
use htl::Htl;
use htl::bundle::Bundle;
use htl::project;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
#[derive(Parser)]
#[command(
name = "htl",
version,
about = "Teal, hidden: run / check / build .tl on mlua",
long_about = "\
Teal, hidden: run / check / build .tl on mlua.
htl is a toolchain for Teal, and a way to embed one. It type-checks `.tl` sources with \
Teal's own checker, runs and tests them on an embedded mlua state, formats them, and \
links them into a single stripped-bytecode bundle. A Rust host embeds the same engine, \
so the scripts it ships are checked when the host is built rather than when its user \
first runs one.
The guide, with every command and what a project looks like:
https://github.com/ynishi/htl
The API a Rust host holds — `include_tl!`, `#[host_module]`, the run cache:
https://docs.rs/htl"
)]
struct Cli {
#[command(subcommand)]
cmd: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
#[command(after_long_help = "\
Examples:
htl check src every .tl under src/
htl check src --strict every warn counts as deny: warnings and lints fail
htl check src --lint nil-index=deny,no-any=warn
one rule fails the run, another is advice
htl check src --lint -tl:hint a warning kind of the Teal compiler silenced
htl check --list-lints every rule with its default level, then exit
htl check --explain union-exhaustive
why the rule exists and how to decide its cases
htl check src --statistics how many findings each rule produced, not the findings
The store is .htl/cache at the project root, the nearest htl.toml or mlua-pkg.toml above
the paths; --no-cache skips it, --explain-cache says why a lookup missed.
Caching: https://github.com/ynishi/htl#caching
")]
Check {
paths: Vec<PathBuf>,
#[arg(long)]
strict: bool,
#[arg(long, allow_hyphen_values = true)]
lint: Option<String>,
#[arg(long)]
list_lints: bool,
#[arg(long, value_name = "RULE")]
explain: Option<String>,
#[arg(long, value_enum, default_value_t = Format::Text)]
format: Format,
#[arg(long)]
no_cache: bool,
#[arg(long, value_enum)]
cache_mode: Option<CacheModeArg>,
#[arg(long)]
explain_cache: bool,
#[arg(long)]
statistics: bool,
},
#[command(after_long_help = "\
Examples:
htl test every .tl that requires htl.test
htl test tests --filter parser only tests whose \"suite > name\" contains it
htl test --coverage --coverage-lines
which lines of each module the suite never reached
htl test --junit report.xml also write the run as a JUnit report, for a CI
")]
Test {
paths: Vec<PathBuf>,
#[arg(long)]
filter: Option<String>,
#[arg(long, default_value = htl::testing::DEFAULT_LIB)]
lib: String,
#[arg(long, allow_hyphen_values = true)]
lint: Option<String>,
#[arg(long)]
fail_fast: bool,
#[arg(long)]
allow_empty: bool,
#[arg(short, long)]
verbose: bool,
#[arg(short, long, conflicts_with = "verbose")]
quiet: bool,
#[arg(long, value_name = "MS")]
slow: Option<f64>,
#[arg(long)]
update: bool,
#[arg(long)]
coverage: bool,
#[arg(long, requires = "coverage")]
coverage_lines: bool,
#[arg(long, value_name = "FILE")]
lcov: Option<PathBuf>,
#[arg(long, value_name = "FILE")]
junit: Option<PathBuf>,
#[arg(long)]
seed: Option<u64>,
#[arg(long, value_enum, default_value_t = Format::Text)]
format: Format,
#[arg(long)]
no_cache: bool,
#[arg(long)]
explain_cache: bool,
},
#[command(after_long_help = "\
Examples:
htl fix src apply the safe fixes to every .tl under src/
htl fix src --diff --dry-run what would change, as a unified diff per file
htl fix src --rule forward-ref only that rule's fixes
htl fix src --unsafe also the fixes that may change what the program does
")]
Fix {
paths: Vec<PathBuf>,
#[arg(long, value_delimiter = ',')]
rule: Vec<String>,
#[arg(long = "unsafe")]
unsafe_fixes: bool,
#[arg(long)]
dry_run: bool,
#[arg(long)]
diff: bool,
#[arg(long)]
allow_dirty: bool,
#[arg(long)]
allow_no_vcs: bool,
#[arg(long)]
exit_non_zero_on_fix: bool,
#[arg(long, value_enum, default_value_t = Format::Text)]
format: Format,
},
Dts {
dir: Option<PathBuf>,
},
#[command(after_long_help = "\
Examples:
htl new hello src/main.tl, a module, tests/, mlua-pkg.toml
htl new hello --lib a library: no entry script
htl new hello --embed the same, plus a Rust host (--target bin)
htl new hello --target cdylib --lib
a library behind a C ABI, for a caller that is not Rust
htl new hello --embed --htl main
built against the unreleased htl (a git pin on main)
htl new hello --embed --htl path:../htl
built against a local checkout of this repository
htl new hello --no-x without the htlx dependency the manifest gets by default
Build targets: https://docs.rs/htl/latest/htl/build_target/enum.BuildTarget.html
Layout of a project: https://github.com/ynishi/htl#layout-of-a-project-htl-new
")]
New {
name: String,
#[arg(long)]
lib: bool,
#[arg(long)]
embed: bool,
#[arg(long, value_name = "NAME", value_parser = clap::builder::PossibleValuesParser::new(scaffold::target_names()))]
target: Option<String>,
#[arg(long, value_name = "REQ")]
htl: Option<String>,
#[arg(long)]
no_x: bool,
},
Init {
dir: Option<PathBuf>,
#[arg(long)]
lib: bool,
#[arg(long)]
embed: bool,
#[arg(long, value_name = "NAME", value_parser = clap::builder::PossibleValuesParser::new(scaffold::target_names()))]
target: Option<String>,
#[arg(long, value_name = "REQ")]
htl: Option<String>,
#[arg(long)]
no_x: bool,
},
#[command(after_long_help = "\
Examples:
htl pin path:../htl from this project's own directory, move it onto a sibling
checkout, for a change that has to be built against it
htl pin release move it back once that checkout's change is released
")]
Pin {
htl: String,
dir: Option<PathBuf>,
#[arg(long)]
no_update: bool,
},
Pkg {
#[command(subcommand)]
cmd: PkgCmd,
},
Types {
#[command(subcommand)]
cmd: TypesCmd,
},
Cache {
#[command(subcommand)]
cmd: CacheCmd,
},
Fmt {
paths: Vec<PathBuf>,
#[arg(long)]
check: bool,
#[arg(long)]
indent: Option<usize>,
},
Gen {
file: PathBuf,
#[arg(short, long)]
out: Option<PathBuf>,
},
Run {
file: PathBuf,
#[arg(trailing_var_arg = true)]
args: Vec<String>,
},
#[command(after_long_help = "\
Examples:
htl build src/main.tl -o app.hb
the entry's require closure, as one bundle
htl build src/main.tl -o app.hb --debug
keep line numbers and local names in the bytecode
htl build src/main.tl -o app.hb --host mymod
leave a module the host provides out of the bundle
htl run app.hb run what was built
Caching: https://github.com/ynishi/htl#caching
")]
Build {
entry: PathBuf,
#[arg(short, long, default_value = "app.hb")]
out: PathBuf,
#[arg(short, long, default_value = "main")]
main: String,
#[arg(long)]
debug: bool,
#[arg(long)]
source: bool,
#[arg(long, value_delimiter = ',')]
extra: Vec<String>,
#[arg(long, value_delimiter = ',')]
host: Vec<String>,
#[arg(long)]
no_cache: bool,
#[arg(long)]
explain_cache: bool,
},
#[command(after_long_help = "\
Examples:
htl unused modules and dependencies no entry reaches
htl unused --exit-non-zero-on-unused
the same report, and a non-zero exit for a CI
htl unused --format json the same report as one JSON document
")]
Unused {
paths: Vec<PathBuf>,
#[arg(long, value_enum, default_value_t = Format::Text)]
format: Format,
#[arg(long)]
exit_non_zero_on_unused: bool,
#[arg(long)]
no_cache: bool,
#[arg(long)]
explain_cache: bool,
},
#[command(after_long_help = "\
Examples:
htl adopt which of htl's nine markers this project writes, and how much
htl adopt --detail the same, plus every marked declaration's file, line and name
htl adopt --format json the same report as one JSON document
")]
Adopt {
paths: Vec<PathBuf>,
#[arg(long, value_enum, default_value_t = Format::Text)]
format: Format,
#[arg(long)]
detail: bool,
#[arg(long)]
no_cache: bool,
#[arg(long)]
explain_cache: bool,
},
#[command(after_long_help = "\
Examples:
htl resolve mq the chain for require(\"mq\")
htl resolve socket.http a name with dots, as a require spells it
htl resolve mq --format json the same rows as one JSON document
Exits 1 when the name resolves to nothing, when two files implement it, or when a file of
the project implements a name the host provides, so a script can ask.
`htl.test` is answered like a host-provided name: no file of the project implements it
(`htl test` preloads it into the state it runs), and its one row is the declaration the
binary carries, `provided by the environment`.
")]
Resolve {
module: String,
path: Option<PathBuf>,
#[arg(long, value_enum, default_value_t = Format::Text)]
format: Format,
},
Bundle {
#[command(subcommand)]
cmd: BundleCmd,
},
}
pub fn command() -> clap::Command {
<Cli as clap::CommandFactory>::command()
}
pub fn run() -> ExitCode {
let mut argv: Vec<String> = std::env::args().collect();
let is_cargo = Path::new(&argv[0])
.file_stem()
.and_then(|s| s.to_str())
.map(|s| s == "cargo-htl")
.unwrap_or(false);
if is_cargo && argv.get(1).map(String::as_str) == Some("htl") {
argv.remove(1);
}
match real_main(Cli::parse_from(argv)) {
Ok(code) => code,
Err(e) => {
eprintln!("htl: {e:#}");
ExitCode::from(2)
}
}
}
fn real_main(cli: Cli) -> Result<ExitCode> {
match cli.cmd {
Cmd::Check {
paths,
strict,
lint,
list_lints,
explain,
format,
no_cache,
cache_mode,
explain_cache,
statistics,
} => cmd_check(
&paths,
lint.as_deref(),
CheckFlags {
strict,
list_lints,
explain_rule: explain,
json: format == Format::Json,
use_cache: !no_cache,
cache_mode: cache_mode.map(Into::into),
explain: explain_cache,
statistics,
},
),
Cmd::Fmt {
paths,
check,
indent,
} => cmd_fmt(&paths, check, indent),
Cmd::Test {
paths,
filter,
lib,
lint,
fail_fast,
allow_empty,
verbose,
quiet,
slow,
update,
coverage,
coverage_lines,
lcov,
junit,
seed,
format,
no_cache,
explain_cache,
} => cmd_test(
&paths,
filter.as_deref(),
&lib,
lint.as_deref(),
TestFlags {
fail_fast,
allow_empty,
verbose,
quiet,
slow,
update,
coverage: coverage || lcov.is_some(),
coverage_lines,
lcov,
junit,
json: format == Format::Json,
no_cache,
explain: explain_cache,
seed,
},
),
Cmd::Pkg { cmd } => match cmd {
PkgCmd::Install => cmd_pkg_install(),
PkgCmd::Add {
name,
git,
tag,
rev,
branch,
entry,
target_dir,
} => cmd_pkg_add(htl::pkg::mlua_pkg::ops::AddSpec {
name,
git,
tag,
rev,
branch,
entry,
target_dir,
}),
PkgCmd::Update {
name,
dry_run,
force,
} => cmd_pkg_update(htl::pkg::mlua_pkg::ops::UpdateOpts {
name,
dry_run,
force,
}),
PkgCmd::Clean { all } => cmd_pkg_clean(all),
PkgCmd::Patch { dep, force } => cmd_pkg_patch(&dep, force),
},
Cmd::Types { cmd } => match cmd {
TypesCmd::Add {
library,
from,
force,
} => cmd_types_add(&library, from.as_deref(), force),
},
Cmd::Cache { cmd } => match cmd {
CacheCmd::Clear { path } => cmd_cache_clear(path.as_deref()),
CacheCmd::Status {
path,
format,
entries,
} => cmd_cache_status(path.as_deref(), format == Format::Json, entries),
},
Cmd::Dts { dir } => cmd_dts(dir.as_deref()),
Cmd::New {
name,
lib,
embed,
target,
htl,
no_x,
} => cmd_new(&name, lib, embed, target.as_deref(), htl.as_deref(), no_x),
Cmd::Init {
dir,
lib,
embed,
target,
htl,
no_x,
} => cmd_init(
dir.as_deref(),
lib,
embed,
target.as_deref(),
htl.as_deref(),
no_x,
),
Cmd::Pin {
htl,
dir,
no_update,
} => pin::cmd_pin(&htl, dir, no_update),
Cmd::Gen { file, out } => cmd_gen(&file, out.as_deref()),
Cmd::Run { file, args } => cmd_run(&file, &args),
Cmd::Fix {
paths,
rule,
unsafe_fixes,
dry_run,
diff,
allow_dirty,
allow_no_vcs,
exit_non_zero_on_fix,
format,
} => cmd_fix(
&paths,
FixFlags {
rule,
unsafe_fixes,
dry_run: dry_run || diff,
diff,
allow_dirty,
allow_no_vcs,
exit_non_zero_on_fix,
json: format == Format::Json,
},
),
Cmd::Build {
entry,
out,
main,
debug,
source,
extra,
host,
no_cache,
explain_cache,
} => cmd_build(
&entry,
&out,
&main,
htl::link::LinkOptions {
debug,
source,
extra,
host,
entry_name: None,
},
BuildCache {
use_cache: !no_cache,
explain: explain_cache,
},
),
Cmd::Unused {
paths,
format,
exit_non_zero_on_unused,
no_cache,
explain_cache,
} => cmd_unused(
&paths,
UnusedFlags {
json: format == Format::Json,
fail_on_unused: exit_non_zero_on_unused,
use_cache: !no_cache,
explain: explain_cache,
},
),
Cmd::Adopt {
paths,
format,
detail,
no_cache,
explain_cache,
} => cmd_adopt(
&paths,
AdoptFlags {
json: format == Format::Json,
detail,
use_cache: !no_cache,
explain: explain_cache,
},
),
Cmd::Resolve {
module,
path,
format,
} => cmd_resolve(&module, path.as_deref(), format == Format::Json),
Cmd::Bundle { cmd } => match cmd {
BundleCmd::Info { file, format } => cmd_bundle_info(&file, format == Format::Json),
},
}
}
fn text_sink() -> project::Sink<report::Out> {
project::Sink::new(report::Out::new(false, false))
}
fn judged(found: &htl::verdict::Findings, policy: &htl::verdict::Policy) -> String {
let mut out = String::new();
if policy.capped {
return out;
}
if found.denied > 0 {
out.push_str(&format!(", {} at deny", found.denied));
}
if policy.strict && found.warnings + found.lints > 0 {
out.push_str(&format!(
", {} warning(s) and {} lint(s) under strict",
found.warnings, found.lints
));
}
out
}
fn config_lang(h: &Htl, cfg: &project::Config) -> Result<()> {
h.set_lang(&project::lang_of(cfg))
}
fn config_lints(h: &Htl, cfg: &project::Config) -> Result<htl::lint::Lints> {
let spec = cfg
.as_ref()
.map(|(_, _, c)| c.lint_spec())
.unwrap_or_default();
let lints = htl::lint::Lints::parse(&spec)?;
h.select_lints(lints.selection())?;
Ok(lints)
}
fn auto_dts(start: &Path) -> Result<()> {
auto_dts_to(start, true)
}
fn auto_dts_to(start: &Path, write: bool) -> Result<()> {
let mut project = None;
if let Some((root, _, cfg)) = load_config(start)? {
let model = htl::model::Project::load(&root, cfg)?;
let contracts: Vec<_> = model.contracts.iter().map(|c| c.terms.clone()).collect();
let (results, problems) = htl::contract::publish_to(&root, &contracts, write);
announce_dts_to(&results, &root, write);
for p in &problems {
eprintln!("dts: {p}");
}
project = Some(root);
}
let Some(root) = htl::dts::find_cargo_package_root(start) else {
return Ok(());
};
let results =
htl::dts::generate_crate_to(&root, write).map_err(|e| anyhow::anyhow!("htl dts: {e}"))?;
announce_dts_to(&results, &root, write);
let types_root = project.unwrap_or_else(|| root.clone());
let report = dep_dts_to(&root, &types_root, write);
announce_dts_to(&report.written, &types_root, write);
announce_dep_report(&report, "dts: ");
Ok(())
}
#[derive(Default)]
struct DepReport {
written: Vec<(PathBuf, bool)>,
not_written: Vec<String>,
left_in_place: Vec<String>,
unresolved: Option<String>,
}
fn dep_dts(cargo_root: &Path, types_root: &Path) -> DepReport {
dep_dts_to(cargo_root, types_root, true)
}
fn dep_dts_to(cargo_root: &Path, types_root: &Path, write: bool) -> DepReport {
let decls = match htl::dep_dts::resolve(cargo_root) {
Ok(d) => d,
Err(e) => {
return DepReport {
unresolved: Some(e),
..DepReport::default()
};
}
};
let types = match decl_root(types_root) {
Ok(t) => t,
Err(e) => {
return DepReport {
unresolved: Some(format!("{e:#}")),
..DepReport::default()
};
}
};
let left_in_place = htl::dep_dts::orphans(&types, &decls);
let (written, not_written) = htl::dep_dts::materialise_to(&types, &decls, write);
DepReport {
written,
not_written,
left_in_place,
unresolved: None,
}
}
fn announce_dep_report(report: &DepReport, prefix: &str) {
for p in &report.not_written {
eprintln!("{prefix}not written: {p}");
}
for p in &report.left_in_place {
eprintln!("{prefix}left in place: {p}");
}
if let Some(e) = &report.unresolved {
eprintln!("{prefix}{e}");
}
}
fn announce_dts_to(results: &[(PathBuf, bool)], root: &Path, wrote: bool) {
for (target, changed) in results {
if *changed {
let target = target.strip_prefix(root).unwrap_or(target).display();
if wrote {
eprintln!("dts: wrote {target}");
} else {
eprintln!(
"dts: would write {target} (dry run: not written; checked against the \
file on disk)"
);
}
}
}
}
fn cmd_dts(dir: Option<&Path>) -> Result<ExitCode> {
let start = match dir {
Some(d) => d.to_path_buf(),
None => std::env::current_dir()?,
};
let mut results = Vec::new();
let mut failed = false;
let mut project = None;
if let Some((croot, _, cfg)) = load_config(&start)? {
let model = htl::model::Project::load(&croot, cfg)?;
let contracts: Vec<_> = model.contracts.iter().map(|c| c.terms.clone()).collect();
let (published, problems) = htl::contract::publish(&croot, &contracts);
for p in &problems {
eprintln!(" {p}");
}
failed = !problems.is_empty();
results.extend(published);
project = Some(croot);
}
let code = |failed: bool| {
if failed {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
};
let Some(root) = htl::dts::find_cargo_package_root(&start) else {
if results.is_empty() && !failed {
bail!(
"no Cargo.toml with a [package] section, and no ---@contract type to publish, \
at or above {}",
start.display()
);
}
report_dts(&results, &start);
return Ok(code(failed));
};
results.extend(htl::dts::generate_crate(&root).map_err(|e| anyhow::anyhow!("{e}"))?);
let types_root = project.unwrap_or_else(|| root.clone());
let report = dep_dts(&root, &types_root);
announce_dep_report(&report, " ");
failed = failed || !report.not_written.is_empty();
results.extend(report.written);
report_dts(&results, &root);
Ok(code(failed))
}
fn report_dts(results: &[(PathBuf, bool)], root: &Path) {
let mut written = 0usize;
for (target, w) in results {
let rel = target.strip_prefix(root).unwrap_or(target).display();
eprintln!(" {} {}", if *w { "wrote " } else { "unchanged" }, rel);
written += usize::from(*w);
}
eprintln!(
"htl dts: {} declaration(s) from {}, {} written",
results.len(),
root.display(),
written
);
}
fn apply_model(
h: &Htl,
cfg: &project::Config,
start: &Path,
) -> Result<Option<htl::model::Project>> {
let model = project::model_of(cfg, start)?;
if let Some(m) = &model {
h.apply_model(m, htl::model::View::Source)?;
}
Ok(model)
}
fn report_scaffold(dir: &Path, written: &[PathBuf], kept: &[PathBuf]) {
let rel = |p: &PathBuf| p.strip_prefix(dir).unwrap_or(p).display().to_string();
for p in written {
eprintln!(" created {}", rel(p));
}
for p in kept {
eprintln!(" kept {} (already there)", rel(p));
}
if kept.is_empty() {
eprintln!(
"htl: {} file(s) written under {}",
written.len(),
dir.display()
);
} else {
eprintln!(
"htl: {} file(s) written, {} kept under {}",
written.len(),
kept.len(),
dir.display()
);
}
}
fn cmd_new(
name: &str,
lib: bool,
embed: bool,
target: Option<&str>,
htl: Option<&str>,
no_x: bool,
) -> Result<ExitCode> {
let dir = PathBuf::from(name);
let pkg_name = dir
.file_name()
.and_then(|s| s.to_str())
.ok_or_else(|| anyhow::anyhow!("invalid project name: {name}"))?
.to_string();
let target = scaffold::resolve_target(target, embed, lib)?;
let htl = scaffold::HtlPin::parse(htl)?;
let opts = scaffold::Options {
lib,
target,
htl,
no_x,
};
let done = scaffold::scaffold(&dir, &pkg_name, &opts, true)?;
report_scaffold(&dir, &done.written, &[]);
let fetch = if opts.writes_htlx() {
"htl pkg install && "
} else {
""
};
let then = match opts.target.map(|t| t.target) {
Some(BuildTarget::Window) => "htl check . && cargo run",
_ => "htl test",
};
eprintln!("next: cd {} && {fetch}{then}", dir.display());
Ok(ExitCode::SUCCESS)
}
fn cmd_init(
dir: Option<&Path>,
lib: bool,
embed: bool,
target: Option<&str>,
htl: Option<&str>,
no_x: bool,
) -> Result<ExitCode> {
let dir = match dir {
Some(d) => d.to_path_buf(),
None => std::env::current_dir()?,
};
let abs = std::fs::canonicalize(&dir).unwrap_or(dir.clone());
let name = abs
.file_name()
.and_then(|s| s.to_str())
.ok_or_else(|| anyhow::anyhow!("cannot derive a project name from {}", abs.display()))?
.to_string();
let asked_for_a_target = target.is_some() || embed;
let target = scaffold::resolve_target(target, embed, lib)?;
let htl = scaffold::HtlPin::parse(htl)?;
let opts = scaffold::Options {
lib,
target,
htl,
no_x,
};
let done = scaffold::scaffold(&dir, &name, &opts, false)?;
let kept: &[PathBuf] = if asked_for_a_target { &done.kept } else { &[] };
if done.written.is_empty() && kept.is_empty() {
eprintln!("htl init: nothing to do, all scaffold files already exist");
} else {
report_scaffold(&dir, &done.written, kept);
}
Ok(ExitCode::SUCCESS)
}
fn pkg_project() -> Result<htl::pkg::MluaProject> {
let cwd = std::env::current_dir()?;
let root = htl::model::Project::find_root(&cwd)?.map(|r| r.root);
root.filter(|r| r.join(htl::pkg::MANIFEST_NAME).is_file())
.map(|r| htl::pkg::MluaProject::at(&r))
.with_context(|| {
format!(
"no {} above {}: `htl pkg` runs in a project",
htl::pkg::MANIFEST_NAME,
cwd.display()
)
})
}
fn decl_root(root: &Path) -> Result<PathBuf> {
let cfg = load_config(root)?;
Ok(project::model_of(&cfg, root)?
.and_then(|m| m.own().roots.decl.clone())
.unwrap_or_else(|| root.join("types")))
}
fn cmd_pkg_install() -> Result<ExitCode> {
let project = pkg_project()?;
let report = project.install()?;
report_install(&report, &project);
let project = htl::pkg::MluaProject::at(&project.root);
report_types_sync(
&project.sync_types(&decl_root(&project.root)?)?,
&project.root,
);
report_patch_drift(&project);
Ok(ExitCode::SUCCESS)
}
fn report_install(
report: &htl::pkg::mlua_pkg::ops::InstallReport,
project: &htl::pkg::MluaProject,
) {
use htl::pkg::mlua_pkg::ops::Placement;
for w in &report.warnings {
if w.contains("patch_dir") && w.contains("not used") {
continue;
}
eprintln!("warning: {w}");
}
let rel = |p: &Path| {
p.strip_prefix(&project.root)
.unwrap_or(p)
.display()
.to_string()
};
for p in &report.packages {
let sha: String = p.sha.chars().take(7).collect();
let where_it_is = match (&p.placement, p.patched) {
(_, true) => format!("from {}", rel(p.root())),
(Placement::Copied(dest), _) => format!("copied to {}", rel(dest)),
(Placement::Symlink(_), _) => p.entry.display().to_string(),
};
eprintln!(" install {} {sha} ({where_it_is})", p.name);
}
let n = report.packages.len();
if report.transitive > 0 {
eprintln!(
"htl pkg install: {n} package(s), {} of them transitive",
report.transitive
);
} else {
eprintln!("htl pkg install: {n} package(s)");
}
}
fn cmd_pkg_add(spec: htl::pkg::mlua_pkg::ops::AddSpec) -> Result<ExitCode> {
use htl::pkg::mlua_pkg::ops::AddOutcome;
let cwd = std::env::current_dir()?;
let root = htl::model::Project::find_root(&cwd)?
.map(|r| r.root)
.unwrap_or(cwd);
let project = htl::pkg::MluaProject::at(&root);
let name = spec.name.clone();
let done = project.add(spec)?;
let manifest = project
.manifest
.strip_prefix(&project.root)
.unwrap_or(&project.manifest)
.display();
if done.report.manifest_created {
eprintln!(" created {manifest}");
}
let verb = match done.report.outcome {
AddOutcome::Added => "added",
AddOutcome::Replaced => "updated",
};
eprintln!(" {verb} {name} in {manifest}");
if let Some(dir) = &done.kept_patch_dir {
eprintln!(
" kept patch_dir = {} ({name} is patched)",
dir.display()
);
}
eprintln!("htl pkg add: run `htl pkg install` to fetch it");
Ok(ExitCode::SUCCESS)
}
fn cmd_pkg_update(opts: htl::pkg::mlua_pkg::ops::UpdateOpts) -> Result<ExitCode> {
use htl::pkg::mlua_pkg::ops::UpdateOutcome;
let project = pkg_project()?;
let dry_run = opts.dry_run;
let report = project.update(opts)?;
if report.entries.is_empty() {
eprintln!("htl pkg update: no dependency selected");
return Ok(ExitCode::SUCCESS);
}
for (name, outcome) in &report.entries {
match outcome {
UpdateOutcome::TagBumped { old, new } => {
eprintln!(" update {name}: tag {old} -> {new}")
}
UpdateOutcome::PrefixResolved { pin, resolved } => eprintln!(
" update {name}: prefix '{pin}' is {resolved} (the manifest keeps the prefix)"
),
UpdateOutcome::Refresh => eprintln!(" update {name}: refresh (branch or unpinned)"),
UpdateOutcome::Skipped(why) => eprintln!(" skip {name}: {why}"),
}
}
if dry_run {
eprintln!("htl pkg update: dry run; nothing was written");
return Ok(ExitCode::SUCCESS);
}
if let Some(install) = &report.install {
report_install(install, &project);
let project = htl::pkg::MluaProject::at(&project.root);
report_types_sync(
&project.sync_types(&decl_root(&project.root)?)?,
&project.root,
);
report_patch_drift(&project);
}
Ok(ExitCode::SUCCESS)
}
fn cmd_pkg_clean(all: bool) -> Result<ExitCode> {
use htl::pkg::mlua_pkg::ops::CleanReport;
let project = pkg_project()?;
match project.clean(all)? {
CleanReport::CacheRemoved => eprintln!("htl pkg clean: removed every cached package"),
CleanReport::NoLockfile => {
eprintln!("htl pkg clean: no lockfile, so nothing was ever cached")
}
CleanReport::StaleRemoved { removed: 0 } => eprintln!("htl pkg clean: nothing to remove"),
CleanReport::StaleRemoved { removed } => eprintln!(
"htl pkg clean: removed {removed} cache entr{} the lockfile no longer refers to",
if removed == 1 { "y" } else { "ies" }
),
}
Ok(ExitCode::SUCCESS)
}
fn report_patch_drift(project: &htl::pkg::MluaProject) {
let short = |s: &str| s.chars().take(7).collect::<String>();
for s in project.patch_status() {
if s.in_use {
continue;
}
let rel = s
.dir
.strip_prefix(&project.root)
.unwrap_or(&s.dir)
.display();
let why = match (&s.base, &s.locked) {
_ if !s.dir.is_dir() => "the copy is not there".to_string(),
(None, _) => "the lockfile records no revision it was taken from".to_string(),
(Some(b), Some(l)) => {
format!("taken from {}, {} is now at {}", short(b), s.name, short(l))
}
(Some(b), None) => format!("taken from {}, and nothing is locked", short(b)),
};
eprintln!(" patch {rel} is not in use ({why})");
eprintln!(
" carry the change forward: commit it, then `htl pkg patch {}`",
s.name
);
eprintln!(" drop it: remove patch_dir from mlua-pkg.toml and delete {rel}");
}
}
fn cmd_pkg_patch(dep: &str, force: bool) -> Result<ExitCode> {
let project = pkg_project().context("a patch belongs to a project, so this runs in one")?;
let done = project.patch(dep, force)?;
let report = &done.report;
let rel = report
.patch_dir
.strip_prefix(&project.root)
.unwrap_or(&report.patch_dir);
let verb = if report.created { "patched" } else { "rebuilt" };
let base: String = report.base.chars().take(7).collect();
eprintln!(" {verb} {} ({dep} at {base})", rel.display());
if !done.dropped.is_empty() {
eprintln!(
" dropped {} (the repository's, not the package's)",
done.dropped.join(", ")
);
}
eprintln!("htl: it is the project's code now — edit it, commit it, then `htl pkg install`.");
Ok(ExitCode::SUCCESS)
}
fn report_types_sync(sync: &htl::pkg::TypesSync, root: &Path) {
let rel = |p: &Path| p.strip_prefix(root).unwrap_or(p).display().to_string();
for (path, dep) in &sync.written {
eprintln!(" types {} (from {dep})", rel(path));
}
for (path, dep) in &sync.taken {
eprintln!(" kept {} ({dep} publishes one too)", rel(path));
}
}
fn cmd_types_add(library: &str, from: Option<&Path>, force: bool) -> Result<ExitCode> {
let project = pkg_project().context("`types/` is a project's, so this runs in one")?;
let types = decl_root(&project.root)?;
let sync = match from {
Some(dir) => project.add_types_from(dir, library, "local", force, &types)?,
None => project.add_types(library, force, &types)?,
};
report_types_sync(&sync, &project.root);
if sync.written.is_empty() && !sync.taken.is_empty() {
eprintln!("htl: nothing written; --force replaces what is already there");
}
Ok(ExitCode::SUCCESS)
}
struct UnusedFlags {
json: bool,
fail_on_unused: bool,
use_cache: bool,
explain: bool,
}
struct AdoptFlags {
json: bool,
detail: bool,
use_cache: bool,
explain: bool,
}
struct CheckFlags {
strict: bool,
list_lints: bool,
explain_rule: Option<String>,
json: bool,
use_cache: bool,
cache_mode: Option<cache::Mode>,
explain: bool,
statistics: bool,
}
struct TestFlags {
fail_fast: bool,
allow_empty: bool,
verbose: bool,
quiet: bool,
slow: Option<f64>,
update: bool,
coverage: bool,
coverage_lines: bool,
lcov: Option<PathBuf>,
junit: Option<PathBuf>,
json: bool,
no_cache: bool,
explain: bool,
seed: Option<u64>,
}
fn print_coverage(cov: &report::CoverageReport, with_lines: bool) {
let width = cov
.modules
.iter()
.map(|m| m.path.len())
.max()
.unwrap_or(4)
.max(5);
for m in &cov.modules {
eprintln!(
"coverage: {:<width$} {:>5}/{:<5} {:5.1}%",
m.path,
m.executed,
m.total,
100.0 * m.executed as f64 / m.total as f64
);
if !m.never_ran.is_empty() {
let names: Vec<String> = m
.never_ran
.iter()
.map(|f| format!("{} ({})", f.name, f.line))
.collect();
eprintln!(" never ran: {}", names.join(", "));
}
if with_lines && !m.unexecuted.is_empty() {
let spans: Vec<String> = m
.unexecuted
.iter()
.map(|(a, b)| {
if a == b {
a.to_string()
} else {
format!("{a}-{b}")
}
})
.collect();
eprintln!(" unexecuted: {}", spans.join(", "));
}
}
if cov.total > 0 {
eprintln!(
"coverage: {:<width$} {:>5}/{:<5} {:5.1}% (statements)",
"total",
cov.executed,
cov.total,
100.0 * cov.executed as f64 / cov.total as f64
);
} else {
eprintln!("coverage: no .tl module reached");
}
}
fn junit_suite(rep: &htl::testing::FileReport) -> junit::Suite {
let mut left: Vec<&str> = rep.failures.iter().map(String::as_str).collect();
let cases = rep
.tests
.iter()
.map(|t| {
let failure = (!t.ok)
.then(|| {
let head = format!("{}: ", t.name);
let at = left.iter().position(|m| m.starts_with(&head))?;
Some(left.remove(at)[head.len()..].to_string())
})
.flatten();
junit::Case {
name: t.name.clone(),
ms: t.ms,
failure: failure.or_else(|| (!t.ok).then(|| "failed".to_string())),
}
})
.collect();
let error = if !rep.check.ok() {
Some(junit::Error {
message: "type check failed".to_string(),
kind: "check",
body: rep
.check
.error_items
.iter()
.map(|d| d.clone().spelled().to_string())
.collect::<Vec<_>>()
.join("\n"),
})
} else {
rep.error.as_ref().map(|e| junit::Error {
message: e.clone(),
kind: "error",
body: e.clone(),
})
};
junit::Suite {
file: htl::diagnostic::display_path(&rep.path),
duration_ms: rep.duration_ms,
error,
cases,
}
}
fn cmd_test(
paths: &[PathBuf],
filter: Option<&str>,
lib: &str,
lint: Option<&str>,
flags: TestFlags,
) -> Result<ExitCode> {
let paths = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths.to_vec()
};
if let Some(first) = paths.first() {
auto_dts(first)?;
}
let cfg = load_config(&paths[0])?;
let model = project::model_of(&cfg, &paths[0])?;
let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Test);
let files = htl::testing::discover_tests_for(&paths, &skip, lib)?;
project::refuse_flat_root(model.as_ref(), &paths, &files)?;
let empty = files.is_empty();
if empty {
eprintln!("htl test: no test files found (looked for .tl files that require(\"{lib}\"))");
}
let opts = project::TestOptions {
config: &cfg,
model: model.as_ref(),
lint,
lib,
filter,
run: htl::testing::RunOptions {
fail_fast: flags.fail_fast,
update_snapshots: flags.update,
coverage: flags.coverage,
seed: flags.seed,
async_: Default::default(),
lang: Default::default(),
},
cache: project::cache_options(
!flags.no_cache,
Some(cache::Mode::PerModule),
&cfg,
flags.explain,
),
};
let mut sink = project::Sink::new(report::Out::new(flags.json, false));
let mut json_files: Vec<report::TestFile> = Vec::new();
let mut junit_suites: Vec<junit::Suite> = Vec::new();
let rep = if empty {
project::TestReport {
files: Vec::new(),
ran: 0,
passed: 0,
failed: 0,
files_with_errors: 0,
replayed: 0,
seed: flags.seed.unwrap_or(0),
duration_ms: 0.0,
coverage: None,
}
} else {
project::test(&mut sink, &files, &opts, &mut |rep, sink| {
if flags.json {
json_files.push(report::TestFile::from_report(rep, sink.out().take()));
}
if flags.junit.is_some() {
junit_suites.push(junit_suite(rep));
}
let f = htl::diagnostic::display_path(&rep.path);
let tag = if rep.ok() { "ok " } else { "FAIL" };
let detail = if !rep.check.ok() {
"type check failed".to_string()
} else if let Some(e) = &rep.error {
format!("error: {}", e.lines().next().unwrap_or_default())
} else if rep.file_level {
"ran to completion (no test library used)".to_string()
} else {
format!("{} passed, {} failed", rep.passed, rep.failed)
};
let show_file = !flags.json && (!flags.quiet || !rep.ok());
if show_file {
eprintln!("{tag} {f} ({detail}, {:.0} ms)", rep.duration_ms);
if let Some(e) = &rep.error {
for line in e.lines().skip(1) {
eprintln!(" {line}");
}
}
}
for tr in &rep.tests {
let slow = flags.slow.is_some_and(|ms| tr.ms >= ms);
if !flags.json && (flags.verbose || slow) {
if flags.quiet && !show_file {
eprintln!("slow {f} {} ({:.1} ms)", tr.name, tr.ms);
continue;
}
let mark = if tr.ok { "ok " } else { "FAIL" };
let note = if slow && !flags.verbose {
" [slow]"
} else {
""
};
eprintln!(" {mark} {} ({:.1} ms){note}", tr.name, tr.ms);
}
}
if !flags.json {
for m in &rep.failures {
eprintln!(" - {m}");
}
for p in &rep.snapshots_written {
eprintln!(
"snapshot written: {}",
htl::diagnostic::display_path(Path::new(p))
);
}
for p in &rep.snapshots_updated {
eprintln!(
"snapshot updated: {}",
htl::diagnostic::display_path(Path::new(p))
);
}
}
})?
};
if let Some(out) = &flags.junit {
std::fs::write(out, junit::document(&junit_suites, rep.duration_ms))
.with_context(|| format!("writing {}", out.display()))?;
}
if let (Some(out), Some(cov)) = (&flags.lcov, &rep.coverage) {
let root = match &cfg {
Some((dir, _, _)) => dir.clone(),
None => std::env::current_dir()?,
};
let root = std::fs::canonicalize(&root).unwrap_or(root);
std::fs::write(out, cov.lcov(&root))
.with_context(|| format!("writing {}", out.display()))?;
}
let ok = rep.ok() && (!empty || flags.allow_empty);
if flags.json {
let summary = report::TestSummary {
files: rep.files.len(),
files_run: rep.ran,
passed: rep.passed,
failed: rep.failed,
files_with_errors: rep.files_with_errors,
replayed: rep.replayed,
duration_ms: rep.duration_ms,
ok,
seed: rep.seed,
};
report::emit(&report::TestReport {
files: json_files,
summary,
coverage: rep.coverage,
})?;
} else if !empty || flags.allow_empty {
if let Some(cov) = &rep.coverage {
print_coverage(cov, flags.coverage_lines);
if let Some(out) = &flags.lcov {
eprintln!("coverage: lcov written to {}", out.display());
}
}
eprintln!(
"htl test: {} file(s), {} passed, {} failed, {} file(s) with errors{}{} ({:.0} ms)",
rep.ran,
rep.passed,
rep.failed,
rep.files_with_errors,
if rep.skipped() > 0 {
format!(", {} file(s) not run (--fail-fast)", rep.skipped())
} else {
String::new()
},
if rep.replayed > 0 {
format!(", {} checked from cache", rep.replayed)
} else {
String::new()
},
rep.duration_ms
);
if !empty {
eprintln!(
"htl test: seed {} (repeat with --seed {})",
rep.seed, rep.seed
);
}
}
Ok(if ok {
ExitCode::SUCCESS
} else {
ExitCode::FAILURE
})
}
fn report_patched(paths: &[PathBuf]) {
let roots: Vec<PathBuf> = paths
.iter()
.filter_map(|p| std::fs::canonicalize(p).ok())
.collect();
let cwd = std::env::current_dir().unwrap_or_default();
let Some(project) = paths
.first()
.and_then(|p| htl::model::Project::find_root(p).ok().flatten())
.map(|r| r.root)
.filter(|r| r.join(htl::pkg::MANIFEST_NAME).is_file())
.map(|r| htl::pkg::MluaProject::at(&r))
else {
return;
};
for p in &project.patches {
let canon = std::fs::canonicalize(&p.dir).unwrap_or_else(|_| p.dir.clone());
if !roots.iter().any(|r| canon.starts_with(r)) {
continue;
}
let shown = p.dir.strip_prefix(&cwd).unwrap_or(&p.dir);
eprintln!(" patched {} ({})", shown.display(), p.name);
}
}
fn load_config(first: &Path) -> Result<project::Config> {
let cfg = project::config_of(first)?;
if let Some((_, path, c)) = &cfg {
htl::config::check_toolchain(c, path, env!("CARGO_PKG_VERSION"))?;
}
Ok(cfg)
}
fn warn_cargo_htl_mismatch(first: &Path) {
let Some(root) = htl::dts::find_cargo_package_root(first) else {
return;
};
let Ok(text) = fs::read_to_string(root.join("Cargo.toml")) else {
return;
};
let Ok(manifest) = text.parse::<toml::Table>() else {
return;
};
let Some(dep) = manifest.get("dependencies").and_then(|d| d.get("htl")) else {
return;
};
let req_text = match dep {
toml::Value::String(s) => s.as_str(),
toml::Value::Table(t) => match t.get("version").and_then(toml::Value::as_str) {
Some(s) => s,
None => return,
},
_ => return,
};
let running = env!("CARGO_PKG_VERSION");
let (Ok(req), Ok(version)) = (
semver::VersionReq::parse(req_text),
semver::Version::parse(running),
) else {
return;
};
if req.matches(&version) {
return;
}
eprintln!(
"htl {running}; Cargo.toml asks for htl {req_text} — the crate and the CLI are \
meant to move together (cargo install htl-cli --version {req_text}, or bump the \
dependency)"
);
}
fn cmd_fmt(paths: &[PathBuf], check: bool, indent: Option<usize>) -> Result<ExitCode> {
let paths = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths.to_vec()
};
let cfg = load_config(&paths[0])?;
let indent = indent
.or_else(|| cfg.as_ref().and_then(|(_, _, c)| c.fmt.indent))
.unwrap_or(3);
let h = Htl::new()?;
config_lang(&h, &cfg)?;
let model = project::model_of(&cfg, &paths[0])?;
let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Own);
let files = htl::collect_tl_skipping(&paths, &skip)?;
let (mut changed, mut failed) = (0usize, 0usize);
for f in &files {
let before = fs::read_to_string(f).with_context(|| format!("reading {}", f.display()))?;
let after = match h.format_file(f, indent) {
Ok(s) => s,
Err(e) => {
eprintln!("error: {e}");
failed += 1;
continue;
}
};
if after != before {
changed += 1;
if check {
eprintln!("would reformat: {}", f.display());
} else {
fs::write(f, after).with_context(|| format!("writing {}", f.display()))?;
eprintln!("reformatted: {}", f.display());
}
}
}
eprintln!(
"htl fmt: {} file(s), {} {}, {} failed",
files.len(),
changed,
if check { "would change" } else { "reformatted" },
failed
);
let fail = failed > 0 || (check && changed > 0);
Ok(if fail {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
})
}
struct FixFlags {
rule: Vec<String>,
unsafe_fixes: bool,
dry_run: bool,
diff: bool,
allow_dirty: bool,
allow_no_vcs: bool,
exit_non_zero_on_fix: bool,
json: bool,
}
fn cmd_fix(paths: &[PathBuf], flags: FixFlags) -> Result<ExitCode> {
use htl::fix::{FixOptions, fix_file, git_dirty, unified_diff};
let paths = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths.to_vec()
};
let cfg = load_config(&paths[0])?;
auto_dts_to(&paths[0], !flags.dry_run)?;
let model = project::model_of(&cfg, &paths[0])?;
let scope = project::Scope::new(&cfg, model.as_ref(), &paths[0], None)?;
let h = project::checker(
model.as_ref(),
scope.lints.selection(),
&project::lang_of(&cfg),
)?;
let opts = FixOptions {
unsafe_fixes: flags.unsafe_fixes,
promoted: cfg
.as_ref()
.map(|(_, _, c)| c.fix.unsafe_.clone())
.unwrap_or_default(),
disabled: cfg
.as_ref()
.map(|(_, _, c)| c.fix.disable.clone())
.unwrap_or_default(),
only: flags.rule.clone(),
dry_run: flags.dry_run,
};
opts.validate()?;
let skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Own);
let (files, outside) = project::held_by_modules(
model.as_ref(),
&paths,
htl::collect_tl_skipping(&paths, &skip)?,
)?;
if let Some(note) = project::outside_modules_note(model.as_ref(), &outside, "fixed") {
eprintln!("htl fix: {note}");
}
let check_skip = project::not_walked(model.as_ref(), &paths, htl::model::Purpose::Check);
let checked_only: Vec<PathBuf> = project::held_by_modules(
model.as_ref(),
&paths,
htl::collect_tl_skipping(&paths, &check_skip)?,
)?
.0
.into_iter()
.filter(|f| !files.contains(f))
.collect();
if !flags.dry_run {
let mut dirty = Vec::new();
let mut no_vcs = Vec::new();
for f in &files {
match git_dirty(f)? {
Some(true) => dirty.push(f.clone()),
Some(false) => {}
None => no_vcs.push(f.clone()),
}
}
if !dirty.is_empty() && !flags.allow_dirty {
bail!(
"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",
dirty
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join("\n ")
);
}
if !no_vcs.is_empty() && !flags.allow_no_vcs {
bail!(
"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",
no_vcs
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join("\n ")
);
}
}
let mut sink = project::Sink::new(report::Out::new(flags.json, false));
sink.judge_by(scope.lints.selection());
let origins = project::Origins::new(model.as_ref());
let walk = scope.walk(&cfg, model.as_ref(), &origins);
let walked: Vec<PathBuf> = files.iter().chain(&checked_only).cloned().collect();
sink.walking(&walked);
let (mut applied, mut skipped, mut json_files) = (Vec::new(), Vec::new(), Vec::new());
let (mut changed, mut deferred, mut reverted) = (0usize, 0usize, 0usize);
let mut found = htl::verdict::Findings::default();
let mut infos: Vec<(PathBuf, htl::CheckInfo)> = Vec::with_capacity(walked.len());
for f in &files {
let saved = h.search_path()?;
project::file_view(&h, model.as_ref(), f)?;
let before = if flags.diff {
std::fs::read_to_string(f).ok()
} else {
None
};
let out = fix_file(&h, f, &opts)?;
if out.contents.is_some() {
changed += 1;
}
deferred += out.deferred;
if out.reverted.is_some() {
reverted += 1;
}
if !flags.json {
for a in &out.applied {
eprintln!(
"fixed: {}:{}: {} ({}{})",
project::display_path(f),
a.line,
a.rule,
a.applicability.as_str(),
if flags.dry_run { ", not written" } else { "" }
);
}
for s in &out.skipped {
eprintln!(
"skipped: {}:{}: {} — {}",
project::display_path(f),
s.line,
s.rule,
s.reason
);
}
if let Some(r) = &out.reverted {
eprintln!("reverted: {}: {r}", project::display_path(f));
}
if let Some(o) = &out.oscillation {
eprintln!(
"stopped: {}: fixes of {o} undo each other",
project::display_path(f)
);
}
if out.deferred > 0 {
eprintln!(
"deferred: {}: {} edit(s) overlapped applied ones; run htl fix again",
project::display_path(f),
out.deferred
);
}
if flags.diff
&& let (Some(b), Some(a)) = (&before, &out.contents)
{
print!("{}", unified_diff(&project::display_path(f), b, a));
}
if flags.diff
&& let (Some(b), Some(s)) = (
out.contents.as_ref().or(before.as_ref()),
out.suggested.as_ref(),
)
{
print!(
"{}",
unified_diff(&format!("{} (suggested)", project::display_path(f)), b, s)
);
}
}
found.lints += project::file_findings(&h, &mut sink, f, &out.check, &walk)?;
h.set_search_path(&saved)?;
found.errors += out.check.errors.len();
found.warnings += out.check.warnings.len();
infos.push((f.clone(), out.check.clone()));
if flags.json {
applied.extend(out.applied.iter().map(|a| report::FixApplied {
file: project::display_path(f),
line: a.line,
rule: a.rule.clone(),
applicability: a.applicability.as_str(),
pass: a.pass,
}));
skipped.extend(out.skipped.iter().map(|s| report::FixSkipped {
file: project::display_path(f),
line: s.line,
rule: s.rule.clone(),
reason: s.reason.clone(),
}));
json_files.push(report::FixFile {
path: project::display_path(f),
changed: out.contents.is_some(),
deferred: out.deferred,
reverted: out.reverted.clone(),
oscillation: out.oscillation.clone(),
diagnostics: sink.out().take(),
});
}
}
for f in &checked_only {
let m = project::check_one(&h, &mut sink, f, &walk)?;
found.errors += m.errors;
found.warnings += m.warnings;
found.lints += m.lints;
infos.push((f.clone(), m.requires_only()));
if flags.json {
json_files.push(report::FixFile {
path: project::display_path(f),
changed: false,
deferred: 0,
reverted: None,
oscillation: None,
diagnostics: sink.out().take(),
});
}
}
let mut whole = scope.whole(&cfg, model.as_ref());
whole.publish = !flags.dry_run;
let w = project::project_findings(&mut sink, &whole, &infos);
found.errors += w.errors;
found.lints += w.lints;
let project_diagnostics = if flags.json {
sink.out().take()
} else {
Vec::new()
};
found.errors += sink.dependency_error_count();
found.denied = sink.denied();
let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
let errors_remaining = found.errors;
let n_applied = if flags.json { applied.len() } else { 0 };
let fail =
htl::verdict::verdict(&found, &policy) || (flags.exit_non_zero_on_fix && changed > 0);
if flags.json {
let n_skipped = skipped.len();
report::emit(&report::FixReport {
dry_run: flags.dry_run,
applied,
skipped,
files: json_files,
summary: report::FixSummary {
files: files.len(),
files_changed: changed,
applied: n_applied,
skipped: n_skipped,
deferred,
reverted,
errors_remaining,
warnings: found.warnings,
lints: found.lints,
denied: found.denied,
strict: policy.strict,
ok: !fail,
},
diagnostics: project_diagnostics,
})?;
} else {
let judged = judged(&found, &policy);
eprintln!(
"htl fix: {} file(s){}, {} changed{}, {} error(s) remaining{}{}",
files.len(),
if checked_only.is_empty() {
String::new()
} else {
format!(" + {} checked in patched dependencies", checked_only.len())
},
changed,
if flags.dry_run {
" (dry run, nothing written)"
} else {
""
},
errors_remaining,
judged,
if deferred > 0 {
format!(", {deferred} deferred")
} else {
String::new()
}
);
}
Ok(if fail {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
})
}
fn cmd_check(paths: &[PathBuf], lint: Option<&str>, flags: CheckFlags) -> Result<ExitCode> {
let CheckFlags {
list_lints,
json,
use_cache,
cache_mode,
explain,
statistics,
..
} = flags;
let mut sink = project::Sink::new(report::Out::new(json, statistics));
let paths = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths.to_vec()
};
if let Some(rule) = flags.explain_rule.as_deref() {
let Some(r) = htl::lint::explained(rule) else {
eprintln!("unknown lint rule: {rule}");
return Ok(ExitCode::from(2));
};
println!("{} (default: {})\n\n{}", r.name, r.default, r.explain);
return Ok(ExitCode::SUCCESS);
}
if list_lints {
let width = htl::lint::rule_names()
.iter()
.map(|r| r.len())
.max()
.unwrap_or(0);
for (name, level) in htl::lint::rule_defaults() {
println!("{name:width$} {level}");
}
return Ok(ExitCode::SUCCESS);
}
let cfg = load_config(&paths[0])?;
warn_cargo_htl_mismatch(&paths[0]);
let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), flags.strict);
if let Some(first) = paths.first() {
auto_dts(first)?;
}
let walk_model = project::model_of(&cfg, &paths[0])?;
if walk_model.is_none()
&& let Some(dir) = paths.iter().find(|p| p.is_dir())
{
bail!(
"no {} or {} in {} or any directory above it: a directory is checked as a \
project. Run `htl init` there to make it one, or name the .tl files to check \
each on its own",
htl::config::CONFIG_NAME,
htl::pkg::MANIFEST_NAME,
fs::canonicalize(dir)
.unwrap_or_else(|_| dir.clone())
.display()
);
}
let skip = project::not_walked(walk_model.as_ref(), &paths, htl::model::Purpose::Check);
let (files, outside) = project::held_by_modules(
walk_model.as_ref(),
&paths,
htl::collect_tl_skipping(&paths, &skip)?,
)?;
if let Some(note) = project::outside_modules_note(walk_model.as_ref(), &outside, "checked") {
eprintln!("htl check: {note}");
}
if !json {
report_patched(&paths);
}
let rep = project::check(
&mut sink,
&files,
&project::Options {
paths: &paths,
config: &cfg,
model: walk_model.as_ref(),
lint,
cache: project::cache_options(use_cache, cache_mode, &cfg, explain),
},
)?;
let fail = report_check(
sink.out(),
json,
statistics,
&rep,
&policy,
patched_files(walk_model.as_ref(), &rep.files),
)?;
Ok(if fail {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
})
}
fn cmd_unused(paths: &[PathBuf], flags: UnusedFlags) -> Result<ExitCode> {
let UnusedFlags {
json,
fail_on_unused,
use_cache,
explain,
} = flags;
let paths = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths.to_vec()
};
let cfg = load_config(&paths[0])?;
auto_dts(&paths[0])?;
let model = project::model_of(&cfg, &paths[0])?;
let rep = htl::unused::unused(&htl::unused::Options {
paths: &paths,
config: &cfg,
model: model.as_ref(),
cache: project::cache_options(use_cache, None, &cfg, explain),
})?;
let s = &rep.summary;
if json {
report::emit(&rep)?;
} else if s.no_entry {
eprintln!(
"htl unused: nothing to start from, so nothing is reported. An entry is \
main.tl in the source root ([layout] source) or beside the manifest, a test file, a module under a [[contract]] directory, or a name \
in [build] extra / host"
);
} else {
if s.check_errors > 0 {
eprintln!(
"htl unused: {} error(s) in the check this graph comes from — what those \
files require is missing from it (htl check)",
s.check_errors
);
}
for m in &rep.modules {
match &m.module {
Some(n) => eprintln!("module: {} ({n})", m.path),
None => eprintln!("module: {}", m.path),
}
}
for d in &rep.dependencies {
eprintln!("dependency: {}", d.name);
}
eprintln!(
"htl unused: {}, {} [{} considered, {} reached, {}]",
count(s.modules, "module", "modules"),
count(s.dependencies, "dependency", "dependencies"),
s.considered,
s.reached,
count(s.entries, "entry", "entries"),
);
}
Ok(if fail_on_unused && !s.ok {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
})
}
fn cmd_adopt(paths: &[PathBuf], flags: AdoptFlags) -> Result<ExitCode> {
let AdoptFlags {
json,
detail,
use_cache,
explain,
} = flags;
let paths = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths.to_vec()
};
let cfg = load_config(&paths[0])?;
auto_dts(&paths[0])?;
let model = project::model_of(&cfg, &paths[0])?;
let rep = htl::adopt::adopt(&htl::adopt::Options {
paths: &paths,
config: &cfg,
model: model.as_ref(),
cache: project::cache_options(use_cache, None, &cfg, explain),
})?;
if json {
report::emit(&rep)?;
} else {
print_adopt(&rep, detail);
}
Ok(ExitCode::SUCCESS)
}
fn adopt_marker_width() -> usize {
htl::adopt::FEATURES
.iter()
.map(|m| format!("---@{m}").len())
.max()
.unwrap_or(0)
+ 2
}
fn print_adopt(rep: &htl::adopt::Report, detail: bool) {
let width = adopt_marker_width();
eprintln!(
"{:<width$} {:>4} {:>10}",
"feature",
"used",
"applicable",
width = width
);
for f in &rep.features {
let applicable = match f.applicable {
Some(n) => n.to_string(),
None => "-".to_string(),
};
eprintln!(
"{:<width$} {:>4} {:>10}",
format!("---@{}", f.marker),
f.used,
applicable,
width = width
);
}
if detail {
let line_width = rep
.features
.iter()
.flat_map(|f| {
f.sites
.iter()
.map(|s| format!("{}:{}", s.file.display(), s.line))
.chain(
f.candidates
.iter()
.map(|c| format!("{}:{}", c.file.display(), c.line)),
)
})
.map(|s| s.len())
.max()
.unwrap_or(0);
for f in &rep.features {
for s in &f.sites {
eprintln!(
" {:<width$}{:<line_width$} {}",
format!("---@{}", f.marker),
format!("{}:{}", s.file.display(), s.line),
s.name,
width = width,
line_width = line_width
);
}
for c in &f.candidates {
let sites = if c.sites == 1 {
"1 site".to_string()
} else {
format!("{} sites", c.sites)
};
eprintln!(
" {:<width$}{:<line_width$} {} (applicable: {}, {sites})",
format!("---@{}", f.marker),
format!("{}:{}", c.file.display(), c.line),
c.name,
c.reason,
width = width,
line_width = line_width
);
}
}
}
let s = &rep.summary;
if s.check_errors > 0 {
eprintln!(
"htl adopt: {} error(s) in the project's check (htl check says which); a file \
with a syntax error contributes no markers",
s.check_errors
);
}
if s.markers == 0 {
eprintln!(
"htl adopt: {} of {} features used, no markers",
s.used, s.features
);
} else {
eprintln!(
"htl adopt: {} of {} features used, {} in {}",
s.used,
s.features,
count(s.markers, "marker", "markers"),
count(s.files, "file", "files"),
);
}
}
fn cmd_resolve(module: &str, path: Option<&Path>, json: bool) -> Result<ExitCode> {
let start = path.unwrap_or(Path::new("."));
let cfg = load_config(start)?;
auto_dts(start)?;
let h = Htl::new()?;
h.install_std()?;
let model = apply_model(&h, &cfg, start)?;
let rep = htl::resolve::resolve(
&h,
module,
cfg.as_ref().map(|(r, _, _)| r.as_path()),
model.as_ref(),
)?;
if json {
report::emit(&rep)?;
} else {
print_resolution(&rep);
}
Ok(if rep.summary.ok {
ExitCode::SUCCESS
} else {
ExitCode::FAILURE
})
}
fn print_resolution(r: &htl::resolve::Resolution) {
let ambiguous = r
.candidates
.iter()
.any(|c| c.status == htl::resolve::Status::Ambiguous);
let typed_by = r
.read
.as_ref()
.map(|d| format!(", typed by {d}"))
.unwrap_or_default();
match (&r.read, &r.error, &r.provided_by) {
(_, Some(e), _) => println!("htl resolve {}: error: {e}", r.module),
(Some(read), None, Some(by)) if r.provider == Some(htl::model::Provider::Declared) => {
println!(
"htl resolve {}: {read}, provided by the environment ({by})",
r.module
)
}
(_, None, Some(by)) => {
println!(
"htl resolve {}: provided by the host ({by}){typed_by}",
r.module
)
}
(Some(read), None, None) => println!("htl resolve {}: {read}", r.module),
(None, None, None) if ambiguous => println!(
"htl resolve {}: more than one module implements it, and no order picks one",
r.module
),
(None, None, None) => println!(
"htl resolve {}: nothing in the project or on the search path answers require(\"{}\")",
r.module, r.module
),
}
if !r.candidates.is_empty() {
let w = |f: fn(&htl::resolve::Candidate) -> usize, head: usize| {
r.candidates.iter().map(f).max().unwrap_or(0).max(head)
};
let file = w(|c| c.path.len(), 4);
let kind = w(|c| c.kind.as_str().len(), 4);
let read_at = r
.candidates
.iter()
.find(|c| c.status == htl::resolve::Status::Read)
.map(|c| c.order);
println!();
println!(" order {:file$} {:kind$} status", "file", "kind");
for c in &r.candidates {
let status = match (c.status, c.shadowed_by, read_at) {
(htl::resolve::Status::Shadowed, Some(by), _) => format!("shadowed by {by}"),
(htl::resolve::Status::Runtime, _, Some(by)) => format!("runtime, typed by {by}"),
(s, _, _) => s.as_str().to_string(),
};
let origin = match &c.origin {
Some(o) => format!(" ({})", o.describe()),
None => String::new(),
};
println!(
" {:<5} {:file$} {:kind$} {status}{origin}",
c.order,
c.path,
c.kind.as_str()
);
}
}
println!();
match r.answered_by {
htl::resolve::AnsweredBy::Model => println!(" answered by the project model"),
htl::resolve::AnsweredBy::Path => {
println!(" searched, in order: {}", r.searched.join(", "))
}
}
}
fn count(n: usize, one: &str, many: &str) -> String {
format!("{n} {}", if n == 1 { one } else { many })
}
fn cmd_cache_clear(path: Option<&Path>) -> Result<ExitCode> {
let root = cache_root(path)?;
let dir = root.join(".htl").join("cache");
if !dir.is_dir() {
eprintln!("htl cache: nothing stored at {}", dir.display());
return Ok(ExitCode::SUCCESS);
}
let n = fs::read_dir(&dir)
.map(|rd| {
rd.flatten()
.filter(|e| e.path().extension().is_some_and(|x| x == "json"))
.count()
})
.unwrap_or(0);
fs::remove_dir_all(&dir).with_context(|| format!("removing {}", dir.display()))?;
eprintln!(
"htl cache: removed {n} {} from {}",
if n == 1 { "entry" } else { "entries" },
dir.display()
);
Ok(ExitCode::SUCCESS)
}
fn cache_root(path: Option<&Path>) -> Result<PathBuf> {
let start = path.unwrap_or(Path::new("."));
let model = htl::model::Project::find_root(start)?.map(|r| r.root);
Ok(model.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))))
}
fn cmd_cache_status(path: Option<&Path>, json: bool, list_entries: bool) -> Result<ExitCode> {
let c = cache::describe(&cache_root(path)?);
if json {
report::emit(&c)?;
return Ok(ExitCode::SUCCESS);
}
if c.entries.is_empty() {
println!("htl cache: empty ({})", c.dir);
return Ok(ExitCode::SUCCESS);
}
let mut by_kind: std::collections::BTreeMap<&str, (usize, u64)> = Default::default();
for e in &c.entries {
let slot = by_kind.entry(e.kind.as_str()).or_default();
slot.0 += 1;
slot.1 += e.bytes;
}
println!(
"htl cache: {} entries, {} at {}",
c.entries.len(),
human_bytes(c.bytes),
c.dir
);
for (kind, (n, bytes)) in &by_kind {
let what = match *kind {
cache::CHECK => "checked modules",
cache::GEN => "checked and generated test files (htl test)",
cache::MODULE => "generated modules (htl test / build, include_bundle!)",
cache::RUN => "whole runs",
_ => "entries this build cannot read",
};
println!(" {n:>5} {what} ({})", human_bytes(*bytes));
}
if let (Some(min), Some(max)) = (
c.entries.iter().map(|e| e.age_secs).min(),
c.entries.iter().map(|e| e.age_secs).max(),
) {
println!(
" last used between {} and {} ago",
human_age(min),
human_age(max)
);
}
if list_entries {
println!();
for e in &c.entries {
println!(
" {:<6} {:>8} {}",
e.kind,
human_bytes(e.bytes),
e.subjects.join(", ")
);
}
}
Ok(ExitCode::SUCCESS)
}
fn human_bytes(n: u64) -> String {
if n >= 1 << 20 {
format!("{:.1} MB", n as f64 / (1u64 << 20) as f64)
} else if n >= 1 << 10 {
format!("{:.0} KB", n as f64 / (1u64 << 10) as f64)
} else {
format!("{n} B")
}
}
fn human_age(secs: u64) -> String {
match secs {
s if s < 90 => format!("{s}s"),
s if s < 5400 => format!("{}m", s / 60),
s if s < 172_800 => format!("{}h", s / 3600),
s => format!("{}d", s / 86_400),
}
}
fn patched_files(model: Option<&htl::model::Project>, files: &[PathBuf]) -> usize {
let Some(model) = model else {
return 0;
};
if !model
.modules
.iter()
.any(|m| m.owner == htl::model::Owner::Patched)
{
return 0;
}
files
.iter()
.filter(|f| {
model
.home_of(f)
.is_some_and(|m| m.owner == htl::model::Owner::Patched)
})
.count()
}
fn print_statistics(rows: &[report::RuleStatistic]) {
let width = rows.iter().map(|r| r.rule.len()).max().unwrap_or(0);
for r in rows {
eprintln!(
"{:>5} {:<width$} {}",
r.count,
r.rule,
statistics_marker(r)
);
}
}
fn statistics_marker(r: &report::RuleStatistic) -> &'static str {
if r.fixable == r.count {
"[*]"
} else if r.fixable + r.unsafe_fixable == 0 {
"[ ]"
} else {
"[-]"
}
}
#[cfg(test)]
mod statistics_marker_tests {
use super::statistics_marker;
use crate::report::RuleStatistic;
fn stat(count: usize, fixable: usize, unsafe_fixable: usize) -> RuleStatistic {
RuleStatistic {
rule: "r".to_string(),
count,
fixable,
unsafe_fixable,
}
}
#[test]
fn full_safe_coverage_is_a_star() {
assert_eq!(statistics_marker(&stat(2, 2, 0)), "[*]");
}
#[test]
fn no_fix_at_all_is_blank() {
assert_eq!(statistics_marker(&stat(2, 0, 0)), "[ ]");
}
#[test]
fn a_mix_of_fixable_and_not_is_a_dash() {
assert_eq!(statistics_marker(&stat(3, 1, 0)), "[-]");
}
#[test]
fn full_coverage_split_between_safe_and_unsafe_is_a_dash() {
assert_eq!(statistics_marker(&stat(2, 1, 1)), "[-]");
}
#[test]
fn full_unsafe_only_coverage_is_a_dash() {
assert_eq!(statistics_marker(&stat(2, 0, 2)), "[-]");
}
}
fn report_check(
out: &mut report::Out,
json: bool,
statistics: bool,
rep: &project::Report,
policy: &htl::verdict::Policy,
patched: usize,
) -> Result<bool> {
let (files, replayed) = (rep.files.len(), rep.replayed);
let (errors, warnings, lints) = (rep.errors, rep.warnings, rep.lints);
let strict = policy.strict;
let fail = htl::verdict::verdict(&rep.findings(), policy);
let all_cached = rep.all_cached();
if json {
report::emit(&report::CheckReport {
files,
patched,
diagnostics: out.take(),
summary: report::CheckSummary {
errors,
warnings,
lints,
denied: rep.denied,
strict,
ok: !fail,
cached: all_cached,
replayed,
},
statistics: statistics.then(|| out.statistics()),
})?;
} else {
if statistics {
print_statistics(&out.statistics());
}
let cached = if all_cached {
" [cached]".to_string()
} else if replayed > 0 {
format!(" [{replayed}/{files} cached]")
} else {
String::new()
};
let denied = match rep.denied {
0 => String::new(),
n => format!(", {n} at deny"),
};
let counted = match patched {
0 => format!("{files} file(s)"),
n => format!("{} file(s) + {n} in patched dependencies", files - n),
};
for rule in out.rules_said() {
if htl::lint::explained(rule).is_some() {
eprintln!("htl check --explain {rule}");
}
}
eprintln!(
"htl check: {counted}, {errors} error(s), {warnings} warning(s), {lints} lint(s){denied}{}{cached}",
if strict { " [strict]" } else { "" }
);
}
Ok(fail)
}
fn cmd_gen(file: &Path, out: Option<&Path>) -> Result<ExitCode> {
let h = Htl::new()?;
auto_dts(file)?;
let cfg = load_config(file)?;
config_lints(&h, &cfg)?;
config_lang(&h, &cfg)?;
let model = apply_model(&h, &cfg, file)?;
project::file_view(&h, model.as_ref(), file)?;
h.install_std()?;
let (code, c) = h.gen_lua(file)?;
text_sink().checkinfo(&c);
let (Some(code), false) = (code, runs_fail(&c)) else {
return Ok(ExitCode::FAILURE);
};
match out {
Some(p) => fs::write(p, code).with_context(|| format!("writing {}", p.display()))?,
None => print!("{code}"),
}
Ok(ExitCode::SUCCESS)
}
fn cmd_run(file: &Path, args: &[String]) -> Result<ExitCode> {
let bytes = fs::read(file).with_context(|| format!("reading {}", file.display()))?;
let h = Htl::new()?;
auto_dts(file)?;
let cfg = load_config(file)?;
config_lints(&h, &cfg)?;
config_lang(&h, &cfg)?;
h.configure_async(async_config(&cfg))?;
let model = apply_model(&h, &cfg, file)?;
h.install_test_lib()?;
h.install_std()?;
h.install_task_lib()?;
if Bundle::is_bundle(&bytes) {
let b = Bundle::decode(&bytes)?;
let main = h.load_bundle(&b, args)?;
return run_root(&h, Entry::Main(main), args);
}
project::file_view(&h, model.as_ref(), file)?;
h.install_searcher()?;
h.set_arg(&file.to_string_lossy(), args)?;
let (code, c) = h.gen_lua(file)?;
text_sink().checkinfo(&c);
let (Some(code), false) = (code, runs_fail(&c)) else {
return Ok(ExitCode::FAILURE);
};
run_root(
&h,
Entry::Chunk {
code,
name: format!("@{}", file.display()),
},
args,
)
}
enum Entry {
Chunk { code: String, name: String },
Main(htl::mlua::Function),
}
fn async_config(cfg: &project::Config) -> &htl::config::AsyncConfig {
static DEFAULT: htl::config::AsyncConfig = htl::config::AsyncConfig {
grace_ms: None,
preempt: None,
};
match cfg {
Some((_, _, c)) => &c.async_,
None => &DEFAULT,
}
}
fn run_root(h: &Htl, entry: Entry, args: &[String]) -> Result<ExitCode> {
let token = htl::mlua_isle::runtime::CancelToken::new();
{
let token = token.clone();
std::thread::Builder::new()
.name("htl-ctrl-c".into())
.spawn(move || {
let Ok(rt) = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
else {
return;
};
rt.block_on(async {
if tokio::signal::ctrl_c().await.is_ok() {
token.cancel();
}
if tokio::signal::ctrl_c().await.is_ok() {
std::process::exit(130);
}
});
})
.context("starting the Ctrl-C watcher")?;
}
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.context("building the executor's runtime")?;
let local = tokio::task::LocalSet::new();
let out = local.block_on(&rt, async {
match entry {
Entry::Chunk { code, name } => h.run_async(&code, &name, args, &token).await,
Entry::Main(main) => {
let va: htl::mlua::Variadic<String> = args.iter().cloned().collect();
h.call_async(main, va, &token).await.map(|_| ())
}
}
});
Ok(match out {
Ok(()) => ExitCode::SUCCESS,
Err(e) if htl::is_cancelled(&e) => {
eprintln!("htl run: interrupted");
ExitCode::from(130)
}
Err(e) => {
eprintln!("{}", htl::developer_message(&e));
ExitCode::FAILURE
}
})
}
fn runs_fail(c: &htl::CheckInfo) -> bool {
htl::verdict::verdict(
&htl::verdict::Findings {
errors: c.errors.len(),
warnings: c.warnings.len(),
lints: c.lints.len(),
denied: 0,
},
&htl::verdict::Policy::ERRORS_ONLY,
)
}
struct BuildCache {
use_cache: bool,
explain: bool,
}
fn cmd_build(
entry: &Path,
out: &Path,
main: &str,
mut opts: htl::link::LinkOptions,
cache_flags: BuildCache,
) -> Result<ExitCode> {
let h = Htl::new()?;
auto_dts(entry)?;
h.install_std()?;
let cfg = load_config(entry)?;
let lints = config_lints(&h, &cfg)?;
config_lang(&h, &cfg)?;
let policy = htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
let model = apply_model(&h, &cfg, entry)?;
if let Some(m) = &model {
opts.host.extend(m.provided().map(|(n, _)| n.to_string()));
}
if let Some((_, _, cfg)) = &cfg {
opts.extra.extend(cfg.build.extra.iter().cloned());
opts.host.extend(cfg.build.host.iter().cloned());
if let Some(t) = cfg.build.target.filter(|t| *t != BuildTarget::Hb) {
eprintln!(
"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.",
t.name(),
t.runs_it()
);
return Ok(ExitCode::FAILURE);
}
}
if entry.is_dir() {
if cache_flags.explain {
eprintln!("htl cache: the directory form of `htl build` is not cached");
}
let skip = project::not_walked(
model.as_ref(),
&[entry.to_path_buf()],
htl::model::Purpose::Check,
);
return cmd_build_dir(&h, entry, out, main, &opts, &skip, &cfg);
}
project::file_view(&h, model.as_ref(), entry)?;
opts.entry_name = model.as_ref().and_then(|m| m.locate(entry).map(|p| p.name));
let root = project::store_root(model.as_ref());
let spec = cfg
.as_ref()
.map(|(_, _, c)| c.lint_spec())
.unwrap_or_default();
let lint = (!spec.is_empty()).then_some(spec.as_str());
let cache_opts = project::cache_options(
cache_flags.use_cache,
Some(cache::Mode::PerModule),
&cfg,
cache_flags.explain,
);
let store = project::with_model(
project::store(&root, cache_opts, None, "htl build"),
model.as_ref(),
);
let link_store = store.as_ref().map(|c| htl::link::LinkStore {
cache: c,
lint,
root: &root,
config: cfg.as_ref().map(|(_, p, c)| (p.as_path(), c)),
});
let linked = htl::link::link_with(&h, entry, &opts, link_store)?;
project::explain_cache(store.as_ref(), cache_opts);
let mut sink = text_sink();
sink.judge_by(lints.selection());
for (p, c) in &linked.checks {
sink.checkinfo(&linked.reported(p, c));
}
let n_err = linked.errors.len();
let found = htl::verdict::Findings {
errors: n_err,
warnings: linked.checks.iter().map(|(_, c)| c.warnings.len()).sum(),
lints: linked.lints.len(),
denied: sink.denied(),
};
for d in &linked.link_errors {
sink.diagnostic(d);
}
if htl::verdict::verdict(&found, &policy) {
eprintln!(
"htl build: {n_err} error(s){}, bundle not written",
judged(&found, &policy)
);
return Ok(ExitCode::FAILURE);
}
let buf = linked.bundle()?.encode();
fs::write(out, &buf).with_context(|| format!("writing {}", out.display()))?;
let typed = linked.modules.iter().filter(|m| m.typed).count();
let cached = if typed > 0 && linked.cached == typed {
" [cached]".to_string()
} else if linked.cached > 0 {
format!(" [{}/{typed} cached]", linked.cached)
} else {
String::new()
};
eprintln!(
"htl build: {} module(s) ({typed} typed, {} lua) -> {} ({} bytes{}{}){cached}",
linked.modules.len(),
linked.modules.len() - typed,
out.display(),
buf.len(),
if opts.source {
", source"
} else {
", bytecode"
},
if opts.debug { " with debug info" } else { "" }
);
if !linked.host_modules.is_empty() {
eprintln!(
"htl build: host must provide: {}",
linked.host_modules.join(", ")
);
}
Ok(ExitCode::SUCCESS)
}
fn cmd_build_dir(
h: &Htl,
dir: &Path,
out: &Path,
entry: &str,
opts: &htl::link::LinkOptions,
skip: &[PathBuf],
cfg: &project::Config,
) -> Result<ExitCode> {
let lints = config_lints(h, cfg)?;
config_lang(h, cfg)?;
let policy = &htl::verdict::Policy::resolve(cfg.as_ref().map(|(_, _, c)| c), false);
h.add_path(dir)?;
let files = htl::collect_tl_skipping(&[dir.to_path_buf()], skip)?;
let mut b = Bundle {
entry: entry.to_string(),
htl_version: env!("CARGO_PKG_VERSION").into(),
..Default::default()
};
let mut found = htl::verdict::Findings::default();
let mut sink = text_sink();
sink.judge_by(lints.selection());
for f in &files {
let rel = f.strip_prefix(dir).unwrap_or(f);
let name = htl::naming::name_of("", rel)
.with_context(|| format!("cannot derive a module name for {}", f.display()))?;
let (code, c) = h.gen_lua(f)?;
sink.checkinfo(&c);
found.errors += c.errors.len();
found.warnings += c.warnings.len();
found.lints += c.lints.len();
let Some(code) = code else { continue };
let (kind, payload) = if opts.source {
(htl::bundle::Kind::Source, code.into_bytes())
} else {
(
htl::bundle::Kind::Bytecode,
h.compile_with(&name, &code, !opts.debug)?,
)
};
b.modules.push(htl::bundle::Module {
name,
kind,
payload,
});
}
found.denied = sink.denied();
if htl::verdict::verdict(&found, policy) {
eprintln!(
"htl build: {} error(s){}, bundle not written",
found.errors,
judged(&found, policy)
);
return Ok(ExitCode::FAILURE);
}
if b.module(entry).is_none() {
bail!(
"entry module '{entry}' not found among {} module(s)",
b.modules.len()
);
}
if !opts.source {
b.fingerprint = h.fingerprint()?;
}
let buf = b.encode();
fs::write(out, &buf).with_context(|| format!("writing {}", out.display()))?;
eprintln!(
"htl build: {} module(s) -> {} ({} bytes)",
b.modules.len(),
out.display(),
buf.len()
);
Ok(ExitCode::SUCCESS)
}
#[derive(serde::Serialize)]
struct BundleInfo {
file: String,
format: u8,
htl_version: Option<String>,
payload: &'static str,
lua: Option<htl::bundle::LuaHeader>,
entry: String,
modules: Vec<BundleModuleInfo>,
host_modules: Vec<String>,
}
#[derive(serde::Serialize)]
struct BundleModuleInfo {
name: String,
kind: &'static str,
bytes: usize,
}
fn cmd_bundle_info(file: &Path, json: bool) -> Result<ExitCode> {
let bytes = fs::read(file).with_context(|| format!("reading {}", file.display()))?;
let Some(format) = htl::bundle::format_version(&bytes) else {
bail!("{} is not an htl bundle (bad magic)", file.display());
};
let b = Bundle::decode(&bytes)?;
let kind_name = |k: htl::bundle::Kind| match k {
htl::bundle::Kind::Bytecode => "bytecode",
htl::bundle::Kind::Source => "source",
};
let payload = match (
b.modules
.iter()
.any(|m| m.kind == htl::bundle::Kind::Bytecode),
b.modules
.iter()
.any(|m| m.kind == htl::bundle::Kind::Source),
) {
(true, true) => "mixed",
(false, true) => "source",
_ => "bytecode",
};
let info = BundleInfo {
file: file.display().to_string(),
format,
htl_version: (!b.htl_version.is_empty()).then(|| b.htl_version.clone()),
payload,
lua: htl::bundle::LuaHeader::parse(&b.fingerprint),
entry: b.entry.clone(),
modules: b
.modules
.iter()
.map(|m| BundleModuleInfo {
name: m.name.clone(),
kind: kind_name(m.kind),
bytes: m.payload.len(),
})
.collect(),
host_modules: b.host_modules.clone(),
};
if json {
report::emit(&info)?;
return Ok(ExitCode::SUCCESS);
}
println!(
"{}: htl bundle, format {}, built by {}",
info.file,
info.format,
match &info.htl_version {
Some(v) => format!("htl {v}"),
None => "htl (version not recorded)".to_string(),
}
);
println!(" payload: {}", info.payload);
let lua = match &info.lua {
Some(h) => h.to_string(),
None if !b.fingerprint.is_empty() => {
format!("unreadable fingerprint ({} bytes)", b.fingerprint.len())
}
None if info.format == 1 => {
"not recorded (a format 1 bundle carries no fingerprint)".into()
}
None => "any (source payload, no bytecode to bind)".into(),
};
println!(" lua: {lua}");
println!(" entry: {}", info.entry);
let modules: Vec<String> = info
.modules
.iter()
.map(|m| {
if info.payload == "mixed" {
format!("{} ({})", m.name, m.kind)
} else {
m.name.clone()
}
})
.collect();
println!(
" modules: {}",
if modules.is_empty() {
"(none)".to_string()
} else {
modules.join(", ")
}
);
println!(
" host: {}",
if info.host_modules.is_empty() {
"(none)".to_string()
} else {
info.host_modules.join(", ")
}
);
Ok(ExitCode::SUCCESS)
}