use htl::cache;
mod junit;
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
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_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,
},
#[command(after_long_help = "\
Examples:
htl test every *_test.tl and tests/**/*.tl
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(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://github.com/ynishi/htl#build-targets---target-name
The cdylib target: https://github.com/ynishi/htl#the-cdylib-target---target-cdylib
")]
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,
},
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 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, so a script can ask.
`htl.test` is not on a project's search path: `htl test` preloads it into the state it
runs, and the declarations behind it come from the binary rather than the project.
")]
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,
format,
no_cache,
cache_mode,
explain_cache,
} => cmd_check(
&paths,
lint.as_deref(),
CheckFlags {
strict,
list_lints,
json: format == Format::Json,
use_cache: !no_cache,
cache_mode: cache_mode.map(Into::into),
explain: explain_cache,
},
),
Cmd::Fmt {
paths,
check,
indent,
} => cmd_fmt(&paths, check, indent),
Cmd::Test {
paths,
filter,
lib,
lint,
fail_fast,
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,
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::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,
},
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::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))
}
fn auto_dts(start: &Path) -> Result<()> {
let mut project = None;
if let Some((root, _, cfg)) = load_config(start)? {
let (contracts, _) = htl::contract::resolve(&root, &cfg);
let (results, problems) = htl::contract::publish(&root, &contracts);
announce_dts(&results, &root);
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(&root).map_err(|e| anyhow::anyhow!("htl dts: {e}"))?;
announce_dts(&results, &root);
let types_root = project.unwrap_or_else(|| root.clone());
let report = dep_dts(&root, &types_root);
announce_dts(&report.written, &types_root);
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 {
let decls = match htl::dep_dts::resolve(cargo_root) {
Ok(d) => d,
Err(e) => {
return DepReport {
unresolved: Some(e),
..DepReport::default()
};
}
};
let left_in_place = htl::dep_dts::orphans(types_root, &decls);
let (written, not_written) = htl::dep_dts::materialise(types_root, &decls);
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(results: &[(PathBuf, bool)], root: &Path) {
for (target, written) in results {
if *written {
eprintln!(
"dts: wrote {}",
target.strip_prefix(root).unwrap_or(target).display()
);
}
}
}
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 (contracts, _) = htl::contract::resolve(&croot, &cfg);
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_project(h: &Htl, start: &Path) -> Result<Option<htl::pkg::Project>> {
let Some(p) = htl::pkg::Project::find(start) else {
return Ok(None);
};
h.apply_project(&p)?;
Ok(Some(p))
}
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::Project> {
let cwd = std::env::current_dir()?;
htl::pkg::Project::find(&cwd).with_context(|| {
format!(
"no {} above {}: `htl pkg` runs in a project",
htl::pkg::MANIFEST_NAME,
cwd.display()
)
})
}
fn cmd_pkg_install() -> Result<ExitCode> {
let project = pkg_project()?;
let report = project.install()?;
report_install(&report, &project);
let project = htl::pkg::Project::at(&project.root);
report_types_sync(&project.sync_types()?, &project.root);
report_patch_drift(&project);
Ok(ExitCode::SUCCESS)
}
fn report_install(report: &htl::pkg::mlua_pkg::ops::InstallReport, project: &htl::pkg::Project) {
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 project = htl::pkg::Project::find(&cwd).unwrap_or_else(|| htl::pkg::Project::at(&cwd));
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::Project::at(&project.root);
report_types_sync(&project.sync_types()?, &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::Project) {
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 cwd = std::env::current_dir()?;
let project = htl::pkg::Project::find(&cwd).context(
"no mlua-pkg.toml above the current directory: 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 cwd = std::env::current_dir()?;
let project = htl::pkg::Project::find(&cwd).context(
"no mlua-pkg.toml above the current directory: `types/` is a project's, so this runs in one",
)?;
let sync = match from {
Some(dir) => project.add_types_from(dir, library, "local", force)?,
None => project.add_types(library, force)?,
};
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 CheckFlags {
strict: bool,
list_lints: bool,
json: bool,
use_cache: bool,
cache_mode: Option<cache::Mode>,
explain: bool,
}
struct TestFlags {
fail_fast: 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; code run inside coroutines is not seen)",
"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.errors.join("\n"),
})
} else {
rep.error.as_ref().map(|e| junit::Error {
message: e.clone(),
kind: "error",
body: e.clone(),
})
};
junit::Suite {
file: rep.path.display().to_string(),
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 files = htl::testing::discover_tests_skipping(&paths, &project::patched(&paths))?;
if files.is_empty() {
eprintln!("htl test: no test files found (looked for *_test.tl and tests/**/*.tl)");
return Ok(ExitCode::FAILURE);
}
let cfg = load_config(&paths[0])?;
let opts = project::TestOptions {
config: &cfg,
lint,
lib,
filter,
run: htl::testing::RunOptions {
fail_fast: flags.fail_fast,
update_snapshots: flags.update,
coverage: flags.coverage,
seed: flags.seed,
},
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));
let mut json_files: Vec<report::TestFile> = Vec::new();
let mut junit_suites: Vec<junit::Suite> = Vec::new();
let rep = 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 = &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} {} ({detail}, {:.0} ms)",
f.display(),
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 {} {} ({:.1} ms)", f.display(), 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: {p}");
}
for p in &rep.snapshots_updated {
eprintln!("snapshot updated: {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();
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 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
);
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::pkg::Project::find(p)) 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()?;
let files = htl::collect_tl_skipping(&paths, &project::patched(&paths))?;
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 h = Htl::new()?;
let cfg = load_config(&paths[0])?;
let file_spec = cfg
.as_ref()
.map(|(_, _, c)| c.lint_spec())
.unwrap_or_default();
if !file_spec.is_empty() {
h.configure_lints(&file_spec)?;
}
if let Some(first) = paths.first() {
auto_dts(first)?;
apply_project(&h, first)?;
}
if let Some((root, _, c)) = &cfg {
h.apply_config(root, c)?;
}
h.install_test_lib()?;
h.install_std()?;
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 files = htl::collect_tl(&paths)?;
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));
let fix_root = cfg
.as_ref()
.map(|(r, _, _)| r.clone())
.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
let contracts = match &cfg {
Some((r, _, c)) => htl::contract::resolve(r, c).0,
None => Vec::new(),
};
let origins = project::Origins::new(&paths[0], &fix_root, &cfg, &contracts);
sink.walking(&files);
let (mut applied, mut skipped, mut json_files) = (Vec::new(), Vec::new(), Vec::new());
let (mut changed, mut deferred, mut reverted, mut errors_remaining) =
(0usize, 0usize, 0usize, 0usize);
for f in &files {
h.add_layout_paths(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;
}
errors_remaining += out.check.errors.len();
if !flags.json {
for a in &out.applied {
eprintln!(
"fixed: {}:{}: {} ({}{})",
f.display(),
a.line,
a.rule,
a.applicability.as_str(),
if flags.dry_run { ", not written" } else { "" }
);
}
for s in &out.skipped {
eprintln!(
"skipped: {}:{}: {} — {}",
f.display(),
s.line,
s.rule,
s.reason
);
}
if let Some(r) = &out.reverted {
eprintln!("reverted: {}: {r}", f.display());
}
if let Some(o) = &out.oscillation {
eprintln!("stopped: {}: fixes of {o} undo each other", f.display());
}
if out.deferred > 0 {
eprintln!(
"deferred: {}: {} edit(s) overlapped applied ones; run htl fix again",
f.display(),
out.deferred
);
}
if flags.diff
&& let (Some(b), Some(a)) = (&before, &out.contents)
{
print!("{}", unified_diff(&f.display().to_string(), 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)", f.display()), b, s)
);
}
}
sink.checkinfo(&out.check);
sink.dependency_errors(&out.check, &|p| origins.of(p));
if flags.json {
applied.extend(out.applied.iter().map(|a| report::FixApplied {
file: f.display().to_string(),
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: f.display().to_string(),
line: s.line,
rule: s.rule.clone(),
reason: s.reason.clone(),
}));
json_files.push(report::FixFile {
path: f.display().to_string(),
changed: out.contents.is_some(),
deferred: out.deferred,
reverted: out.reverted.clone(),
oscillation: out.oscillation.clone(),
diagnostics: sink.out().take(),
});
}
}
errors_remaining += sink.dependency_error_count();
let n_applied = if flags.json { applied.len() } else { 0 };
let fail = errors_remaining > 0 || (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,
ok: !fail,
},
})?;
} else {
eprintln!(
"htl fix: {} file(s), {} changed{}, {} error(s) remaining{}",
files.len(),
changed,
if flags.dry_run {
" (dry run, nothing written)"
} else {
""
},
errors_remaining,
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,
..
} = flags;
let mut sink = project::Sink::new(report::Out::new(json));
let paths = if paths.is_empty() {
vec![PathBuf::from(".")]
} else {
paths.to_vec()
};
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 strict = flags.strict
|| cfg
.as_ref()
.and_then(|(_, _, c)| c.lint.strict)
.unwrap_or(false);
if let Some(first) = paths.first() {
auto_dts(first)?;
}
let files = htl::collect_tl(&paths)?;
if !json {
report_patched(&paths);
}
let rep = project::check(
&mut sink,
&files,
&project::Options {
paths: &paths,
config: &cfg,
lint,
cache: project::cache_options(use_cache, cache_mode, &cfg, explain),
},
)?;
let fail = report_check(
sink.out(),
json,
&rep,
strict,
patched_files(&paths, &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 rep = htl::unused::unused(&htl::unused::Options {
paths: &paths,
config: &cfg,
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 \
src/main.tl, 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_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 project = apply_project(&h, start)?;
if let Some((root, _, c)) = &cfg {
h.apply_config(root, c)?;
}
let rep = htl::resolve::resolve(
&h,
module,
cfg.as_ref().map(|(r, _, _)| r.as_path()),
project.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) {
match &r.read {
Some(read) => println!("htl resolve {}: {read}", r.module),
None => println!(
"htl resolve {}: nothing 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!();
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 start = path.unwrap_or(Path::new("."));
let root = load_config(start)?
.map(|(r, _, _)| r)
.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
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("."));
Ok(cache::root_for(start)
.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(paths: &[PathBuf], files: &[PathBuf]) -> usize {
let dirs: Vec<PathBuf> = project::patched(paths)
.into_iter()
.map(|d| fs::canonicalize(&d).unwrap_or(d))
.collect();
if dirs.is_empty() {
return 0;
}
files
.iter()
.filter(|f| {
let f = fs::canonicalize(f).unwrap_or_else(|_| (*f).to_path_buf());
dirs.iter().any(|d| f.starts_with(d))
})
.count()
}
fn report_check(
out: &mut report::Out,
json: bool,
rep: &project::Report,
strict: bool,
patched: usize,
) -> Result<bool> {
let (files, replayed) = (rep.files.len(), rep.replayed);
let (errors, warnings, lints) = (rep.errors, rep.warnings, rep.lints);
let fail = rep.failed(strict);
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,
},
})?;
} else {
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),
};
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()?;
h.add_layout_paths(file)?;
auto_dts(file)?;
apply_project(&h, file)?;
h.install_std()?;
let (code, c) = h.gen_lua(file)?;
text_sink().checkinfo(&c);
let Some(mut code) = code else {
return Ok(ExitCode::FAILURE);
};
if !code.ends_with('\n') {
code.push('\n');
}
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)?;
apply_project(&h, file)?;
h.install_test_lib()?;
h.install_std()?;
if Bundle::is_bundle(&bytes) {
let b = Bundle::decode(&bytes)?;
return Ok(match h.run_bundle(&b, args) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("{}", htl::developer_message(&e));
ExitCode::FAILURE
}
});
}
h.add_layout_paths(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) = code else {
return Ok(ExitCode::FAILURE);
};
match h.exec(&code, &format!("@{}", file.display()), args) {
Ok(()) => Ok(ExitCode::SUCCESS),
Err(e) => {
eprintln!("{}", htl::developer_message(&e));
Ok(ExitCode::FAILURE)
}
}
}
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)?;
apply_project(&h, entry)?;
h.install_std()?;
let cfg = load_config(entry)?;
if let Some((root, _, cfg)) = &cfg {
h.apply_config(root, 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");
}
return cmd_build_dir(&h, entry, out, main, &opts);
}
h.add_layout_paths(entry)?;
let root = cache::root_for(entry)
.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
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::store(&root, cache_opts, None, "htl build");
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();
for (_, c) in &linked.checks {
sink.checkinfo(c);
}
let n_err = linked.errors.len();
for e in linked
.errors
.iter()
.filter(|e| e.contains("is not on the search path"))
{
eprintln!("error: {e}");
}
if n_err > 0 {
eprintln!("htl build: {n_err} error(s), bundle not written");
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,
) -> Result<ExitCode> {
h.add_path(dir)?;
let files = htl::collect_tl(&[dir.to_path_buf()])?;
let mut b = Bundle {
entry: entry.to_string(),
htl_version: env!("CARGO_PKG_VERSION").into(),
..Default::default()
};
let mut n_err = 0usize;
let mut sink = text_sink();
for f in &files {
let name = htl::module_name(dir, f)?;
let (code, c) = h.gen_lua(f)?;
sink.checkinfo(&c);
n_err += c.errors.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,
});
}
if n_err > 0 {
eprintln!("htl build: {n_err} error(s), bundle not written");
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)
}