use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use crate::{CheckInfo, DependencyError, Fix, RequireSite};
const FORMAT: u32 = 6;
const DIR: &str = ".htl/cache";
fn default_bound(files: usize) -> usize {
(files * 4).max(256)
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
struct Stamp {
format: u32,
htl: String,
checker: String,
exe_len: u64,
exe_mtime_ns: u128,
}
impl Stamp {
fn for_kind(kind: &str) -> Option<Self> {
if kind == MODULE {
Some(Self::portable())
} else {
Self::current()
}
}
fn current() -> Option<Self> {
let exe = std::env::current_exe().ok()?;
let m = std::fs::metadata(&exe).ok()?;
let mtime = m
.modified()
.ok()?
.duration_since(std::time::UNIX_EPOCH)
.ok()?
.as_nanos();
Some(Self {
exe_len: m.len(),
exe_mtime_ns: mtime,
..Self::portable()
})
}
fn portable() -> Self {
Self {
format: FORMAT,
htl: env!("CARGO_PKG_VERSION").to_string(),
checker: crate::checker_identity().to_string(),
exe_len: 0,
exe_mtime_ns: 0,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
struct Input {
path: String,
hash: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct Probe {
dir: String,
names: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Recorded {
pub severity: String,
pub text: String,
pub fix: Option<FixJson>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub dependency: Option<DependencyJson>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FixJson {
pub applicability: String,
pub edits: Vec<EditJson>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EditJson {
pub line: usize,
pub col: usize,
pub end_line: usize,
pub end_col: usize,
pub text: String,
}
impl FixJson {
pub fn from_fix(f: &Fix) -> Self {
Self {
applicability: f.applicability.as_str().to_string(),
edits: f
.edits
.iter()
.map(|e| EditJson {
line: e.line,
col: e.col,
end_line: e.end_line,
end_col: e.end_col,
text: e.text.clone(),
})
.collect(),
}
}
pub fn to_fix(&self) -> Fix {
Fix {
applicability: match self.applicability.as_str() {
"unsafe" => crate::Applicability::Unsafe,
"suggest" => crate::Applicability::Suggest,
"safe" => crate::Applicability::Safe,
_ => crate::Applicability::Suggest,
},
edits: self
.edits
.iter()
.map(|e| crate::Edit {
line: e.line,
col: e.col,
end_line: e.end_line,
end_col: e.end_col,
text: e.text.clone(),
})
.collect(),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DependencyJson {
pub file: String,
pub required_by: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub origin: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RequireJson {
pub module: String,
pub path: Option<String>,
pub line: usize,
pub col: usize,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Module {
pub diagnostics: Vec<Recorded>,
pub errors: usize,
pub warnings: usize,
pub lints: usize,
pub deps: Vec<String>,
pub requires: Vec<RequireJson>,
#[serde(default)]
pub code: Option<String>,
#[serde(default)]
pub check: Option<CheckInfoJson>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct CheckInfoJson {
pub errors: Vec<String>,
pub warnings: Vec<String>,
pub lints: Vec<String>,
pub deps: Vec<String>,
pub requires: Vec<RequireJson>,
pub error_fixes: Vec<Option<FixJson>>,
pub lint_fixes: Vec<Option<FixJson>>,
#[serde(default)]
pub dependency_errors: Vec<DependencyErrorJson>,
}
impl CheckInfoJson {
pub fn from_check(c: &CheckInfo) -> Self {
Self {
errors: c.errors.clone(),
warnings: c.warnings.clone(),
lints: c.lints.clone(),
deps: c.deps.iter().map(|p| normal(p)).collect(),
requires: requires_json(c),
error_fixes: c
.error_fixes
.iter()
.map(|f| f.as_ref().map(FixJson::from_fix))
.collect(),
lint_fixes: c
.lint_fixes
.iter()
.map(|f| f.as_ref().map(FixJson::from_fix))
.collect(),
dependency_errors: c
.dependency_errors
.iter()
.map(|e| DependencyErrorJson {
file: e.file.display().to_string(),
required_by: e.required_by.display().to_string(),
text: e.text.clone(),
})
.collect(),
}
}
pub fn to_check(&self) -> CheckInfo {
CheckInfo {
errors: self.errors.clone(),
warnings: self.warnings.clone(),
lints: self.lints.clone(),
deps: self.deps.iter().map(PathBuf::from).collect(),
requires: requires_from_json(&self.requires),
error_fixes: self
.error_fixes
.iter()
.map(|f| f.as_ref().map(FixJson::to_fix))
.collect(),
lint_fixes: self
.lint_fixes
.iter()
.map(|f| f.as_ref().map(FixJson::to_fix))
.collect(),
dependency_errors: self
.dependency_errors
.iter()
.map(|e| DependencyError {
file: PathBuf::from(&e.file),
required_by: PathBuf::from(&e.required_by),
text: e.text.clone(),
})
.collect(),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DependencyErrorJson {
pub file: String,
pub required_by: String,
pub text: String,
}
pub fn requires_json(c: &CheckInfo) -> Vec<RequireJson> {
c.requires
.iter()
.map(|r| RequireJson {
module: r.module.clone(),
path: r.path.as_ref().map(|p| p.to_string_lossy().into_owned()),
line: r.line,
col: r.col,
})
.collect()
}
fn requires_from_json(requires: &[RequireJson]) -> Vec<RequireSite> {
requires
.iter()
.map(|r| RequireSite {
module: r.module.clone(),
path: r.path.as_ref().map(PathBuf::from),
line: r.line,
col: r.col,
})
.collect()
}
impl Module {
pub fn requires_only(&self) -> CheckInfo {
CheckInfo {
deps: self.deps.iter().map(PathBuf::from).collect(),
requires: requires_from_json(&self.requires),
..Default::default()
}
}
pub fn generated(c: &CheckInfo, code: String) -> Self {
Self {
diagnostics: Vec::new(),
errors: c.errors.len(),
warnings: c.warnings.len(),
lints: c.lints.len(),
deps: c.deps.iter().map(|p| normal(p)).collect(),
requires: requires_json(c),
code: Some(code),
check: Some(CheckInfoJson::from_check(c)),
}
}
}
pub fn search_dirs(
file: &Path,
root: &Path,
cfg: Option<(&Path, &crate::config::HtlConfig)>,
) -> Vec<PathBuf> {
let mut out: Vec<PathBuf> = file.parent().map(Path::to_path_buf).into_iter().collect();
out.push(root.to_path_buf());
out.push(root.join("src"));
out.push(root.join("types"));
out.extend(crate::materialised_types_dirs(&root.join("types")));
if let Some((r, c)) = cfg {
out.extend(c.search_paths(r));
}
out.extend(crate::dependency_dirs(root));
out
}
#[derive(Serialize, Deserialize, Debug)]
struct Entry {
stamp: Stamp,
subject: String,
kind: String,
inputs: Vec<Input>,
probes: Vec<Probe>,
module: Module,
}
#[derive(Serialize, Deserialize, Debug)]
struct RunEntry {
stamp: Stamp,
subjects: Vec<String>,
kind: String,
inputs: Vec<Input>,
probes: Vec<Probe>,
modules: Vec<Module>,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub enum Mode {
#[default]
PerModule,
WholeRun,
}
impl Mode {
pub fn parse(s: &str) -> Option<Self> {
match s {
"per-module" => Some(Mode::PerModule),
"whole-run" => Some(Mode::WholeRun),
_ => None,
}
}
pub fn as_str(self) -> &'static str {
match self {
Mode::PerModule => "per-module",
Mode::WholeRun => "whole-run",
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct Options {
pub enabled: bool,
pub mode: Mode,
pub explain: bool,
pub max_entries: Option<usize>,
}
impl Default for Options {
fn default() -> Self {
Self {
enabled: true,
mode: Mode::default(),
explain: false,
max_entries: None,
}
}
}
#[derive(Default, Copy, Clone, Debug)]
pub struct Stats {
pub hits: usize,
pub misses: usize,
pub stored: usize,
pub evicted: usize,
}
impl Stats {
pub fn summary(&self, mode: Mode) -> Option<String> {
let touched = self.hits + self.misses + self.stored + self.evicted;
(touched > 0).then(|| {
format!(
"htl cache: {} hit, {} missed, {} written, {} evicted ({})",
self.hits,
self.misses,
self.stored,
self.evicted,
mode.as_str()
)
})
}
}
#[derive(Debug, Clone)]
pub struct Key {
hash: String,
kind: &'static str,
}
fn hash_file(p: &Path) -> Option<String> {
let bytes = std::fs::read(p).ok()?;
Some(blake3::hash(&bytes).to_hex().to_string())
}
pub fn normal(p: &Path) -> String {
std::fs::canonicalize(p)
.unwrap_or_else(|_| p.to_path_buf())
.to_string_lossy()
.into_owned()
}
fn resolves_in(dir: &Path, name: &str) -> bool {
let stem = name.replace('.', "/");
["tl", "d.tl", "lua"]
.iter()
.any(|ext| dir.join(format!("{stem}.{ext}")).is_file())
|| ["init.tl", "init.d.tl", "init.lua"]
.iter()
.any(|f| dir.join(&stem).join(f).is_file())
}
pub fn module_key(spelling: &Path, lint: Option<&str>) -> Key {
key_with(CHECK, spelling, lint)
}
pub const CHECK: &str = "check";
pub const GEN: &str = "gen";
pub fn gen_key(spelling: &Path, lint: Option<&str>) -> Key {
key_with(GEN, spelling, lint)
}
pub const MODULE: &str = "module";
pub fn module_gen_key(path: &Path, lint: Option<&str>) -> Key {
let mut h = blake3::Hasher::new();
h.update(MODULE.as_bytes());
h.update(b"\0");
h.update(normal(path).as_bytes());
h.update(b"\0");
h.update(lint.unwrap_or("").as_bytes());
Key {
hash: h.finalize().to_hex().to_string(),
kind: MODULE,
}
}
pub fn source_mentions_require(text: &str) -> bool {
let bytes = text.as_bytes();
let ident = |b: u8| b.is_ascii_alphanumeric() || b == b'_';
text.match_indices("require").any(|(i, _)| {
let before = i.checked_sub(1).map(|j| bytes[j]);
let after = bytes.get(i + "require".len()).copied();
!before.is_some_and(ident) && !after.is_some_and(ident)
})
}
impl Options {
pub fn from_env() -> Self {
Self {
enabled: std::env::var_os("HTL_NO_CACHE").is_none(),
mode: Mode::PerModule,
explain: std::env::var_os("HTL_CACHE_DEBUG").is_some(),
max_entries: std::env::var_os("HTL_CACHE_MAX_ENTRIES")
.and_then(|v| v.to_str().and_then(|s| s.parse().ok())),
}
}
}
pub fn root_for(path: &Path) -> Option<PathBuf> {
crate::config::HtlConfig::find(path)
.ok()
.flatten()
.map(|(file, _)| crate::parent_dir(&file))
}
pub fn scratch_root(root: &Path) -> Option<&'static str> {
if root.components().any(|c| c.as_os_str() == "target") {
return Some("under a `target` directory");
}
let cargo_home = std::env::var_os("CARGO_HOME")
.map(PathBuf::from)
.or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".cargo")));
if let Some(registry) = cargo_home.map(|h| h.join("registry"))
&& root.starts_with(®istry)
{
return Some("a registry checkout");
}
None
}
fn key_with(kind: &'static str, spelling: &Path, lint: Option<&str>) -> Key {
let mut h = blake3::Hasher::new();
h.update(kind.as_bytes());
h.update(b"\0");
h.update(spelling.to_string_lossy().as_bytes());
h.update(b"\0");
h.update(lint.unwrap_or("").as_bytes());
h.update(b"\0");
h.update(cwd().as_bytes());
Key {
hash: h.finalize().to_hex().to_string(),
kind,
}
}
pub fn run_key(files: &[PathBuf], lint: Option<&str>) -> Key {
let mut h = blake3::Hasher::new();
h.update(b"htl-run\0");
for f in files {
h.update(f.to_string_lossy().as_bytes());
h.update(b"\0");
}
h.update(lint.unwrap_or("").as_bytes());
h.update(b"\0");
h.update(cwd().as_bytes());
Key {
hash: h.finalize().to_hex().to_string(),
kind: RUN,
}
}
pub const RUN: &str = "run";
fn cwd() -> &'static str {
static CWD: std::sync::OnceLock<String> = std::sync::OnceLock::new();
CWD.get_or_init(|| {
std::env::current_dir()
.map(|p| normal(&p))
.unwrap_or_default()
})
}
#[derive(Serialize, Debug)]
pub struct EntrySummary {
pub kind: String,
pub subjects: Vec<String>,
pub bytes: u64,
pub age_secs: u64,
}
#[derive(Serialize, Debug)]
pub struct Contents {
pub dir: String,
pub entries: Vec<EntrySummary>,
pub bytes: u64,
}
pub fn describe(root: &Path) -> Contents {
let dir = root.join(DIR);
let mut out = Contents {
dir: dir.to_string_lossy().into_owned(),
entries: Vec::new(),
bytes: 0,
};
let Ok(rd) = std::fs::read_dir(&dir) else {
return out;
};
let now = std::time::SystemTime::now();
for e in rd.flatten() {
let path = e.path();
if path.extension().is_none_or(|x| x != "json") {
continue;
}
let Ok(meta) = e.metadata() else { continue };
out.bytes += meta.len();
let age_secs = meta
.modified()
.ok()
.and_then(|t| now.duration_since(t).ok())
.map(|d| d.as_secs())
.unwrap_or(0);
let v: serde_json::Value = std::fs::read_to_string(&path)
.ok()
.and_then(|raw| serde_json::from_str(&raw).ok())
.unwrap_or(serde_json::Value::Null);
let kind = v
.get("kind")
.and_then(|k| k.as_str())
.unwrap_or("unreadable")
.to_string();
let subjects = match (v.get("subject"), v.get("subjects")) {
(Some(s), _) if s.is_string() => vec![s.as_str().unwrap_or_default().to_string()],
(_, Some(a)) if a.is_array() => a
.as_array()
.unwrap()
.iter()
.filter_map(|x| x.as_str().map(str::to_string))
.collect(),
_ => Vec::new(),
};
out.entries.push(EntrySummary {
kind,
subjects,
bytes: meta.len(),
age_secs,
});
}
out.entries
.sort_by(|a, b| a.kind.cmp(&b.kind).then(a.subjects.cmp(&b.subjects)));
out
}
pub struct Cache {
dir: PathBuf,
opts: Options,
stats: RefCell<Stats>,
hashes: RefCell<HashMap<String, Option<String>>>,
dirs: RefCell<HashMap<(String, String), bool>>,
}
impl Cache {
pub fn open(root: &Path, opts: Options) -> Option<Self> {
if !opts.enabled {
return None;
}
Stamp::current()?;
Some(Self {
dir: root.join(DIR),
opts,
stats: RefCell::new(Stats::default()),
hashes: RefCell::new(HashMap::new()),
dirs: RefCell::new(HashMap::new()),
})
}
pub fn stats(&self) -> Stats {
*self.stats.borrow()
}
fn miss(&self, reason: &str) {
self.stats.borrow_mut().misses += 1;
if self.opts.explain {
eprintln!("htl cache: miss ({reason})");
}
}
fn hash_of(&self, path: &str) -> Option<String> {
if let Some(h) = self.hashes.borrow().get(path) {
return h.clone();
}
let h = hash_file(Path::new(path));
self.hashes.borrow_mut().insert(path.to_string(), h.clone());
h
}
fn resolves(&self, dir: &str, name: &str) -> bool {
let k = (dir.to_string(), name.to_string());
if let Some(v) = self.dirs.borrow().get(&k) {
return *v;
}
let v = resolves_in(Path::new(dir), name);
self.dirs.borrow_mut().insert(k, v);
v
}
fn probe_hash(&self, dir: &str, names: &[String]) -> String {
let mut h = blake3::Hasher::new();
for n in names {
h.update(n.as_bytes());
h.update(if self.resolves(dir, n) {
b"\x01"
} else {
b"\x00"
});
}
h.finalize().to_hex().to_string()
}
fn entry_path(&self, key: &Key) -> PathBuf {
self.dir.join(format!("{}.json", key.hash))
}
pub fn mode(&self) -> Mode {
self.opts.mode
}
fn parse<T: serde::de::DeserializeOwned>(&self, raw: &str) -> Option<T> {
match serde_json::from_str(raw) {
Ok(e) => Some(e),
Err(e) => {
self.miss(&format!("unreadable entry: {e}"));
None
}
}
}
fn required_names(modules: &[Module]) -> Vec<String> {
let mut names: Vec<String> = modules
.iter()
.flat_map(|m| m.requires.iter().map(|r| r.module.clone()))
.collect();
names.sort();
names.dedup();
names
}
fn still_valid(
&self,
kind: &str,
stamp: &Stamp,
inputs: &[Input],
probes: &[Probe],
names: &[String],
) -> bool {
let Some(current) = Stamp::for_kind(kind) else {
return false;
};
if *stamp != current {
self.miss("written by a different build");
return false;
}
for i in inputs {
match self.hash_of(&i.path) {
Some(h) if h == i.hash => {}
Some(_) => {
self.miss(&format!("changed: {}", i.path));
return false;
}
None => {
self.miss(&format!("gone: {}", i.path));
return false;
}
}
}
for p in probes {
if self.probe_hash(&p.dir, names) != p.names {
self.miss(&format!(
"what {} offers for this module's requires changed",
p.dir
));
return false;
}
}
self.stats.borrow_mut().hits += 1;
true
}
pub fn lookup_all(&self, keys: &[Key], run: &Key, files: usize) -> Vec<Option<Module>> {
match self.opts.mode {
Mode::PerModule => keys.iter().map(|k| self.lookup(k)).collect(),
Mode::WholeRun => match self.lookup_run(run, files) {
Some(ms) => ms.into_iter().map(Some).collect(),
None => vec![None; files],
},
}
}
pub fn lookup(&self, key: &Key) -> Option<Module> {
let raw = std::fs::read_to_string(self.entry_path(key)).ok()?;
let entry: Entry = self.parse(&raw)?;
let names = Self::required_names(std::slice::from_ref(&entry.module));
if !self.still_valid(
&entry.kind,
&entry.stamp,
&entry.inputs,
&entry.probes,
&names,
) {
return None;
}
self.touch(key);
Some(entry.module)
}
fn touch(&self, key: &Key) {
if let Ok(f) = std::fs::File::options()
.write(true)
.open(self.entry_path(key))
{
let _ = f.set_modified(std::time::SystemTime::now());
}
}
fn lookup_run(&self, key: &Key, files: usize) -> Option<Vec<Module>> {
let raw = std::fs::read_to_string(self.entry_path(key)).ok()?;
let entry: RunEntry = self.parse(&raw)?;
if entry.modules.len() != files {
self.miss("the walk visits a different number of files");
return None;
}
let names = Self::required_names(&entry.modules);
if !self.still_valid(RUN, &entry.stamp, &entry.inputs, &entry.probes, &names) {
return None;
}
self.touch(key);
Some(entry.modules)
}
fn inputs_for(&self, paths: impl Iterator<Item = String>) -> Option<Vec<Input>> {
let mut paths: Vec<String> = paths.collect();
paths.sort();
paths.dedup();
let mut inputs = Vec::with_capacity(paths.len());
for p in paths {
inputs.push(Input {
path: p.clone(),
hash: self.hash_of(&p)?,
});
}
Some(inputs)
}
fn probes_for(&self, dirs: &[PathBuf], names: &[String]) -> Vec<Probe> {
let mut dirs: Vec<String> = dirs.iter().map(|p| normal(p)).collect();
dirs.sort();
dirs.dedup();
dirs.into_iter()
.map(|d| {
let h = self.probe_hash(&d, names);
Probe { dir: d, names: h }
})
.collect()
}
pub fn store_module(
&self,
key: &Key,
file: &Path,
extra_inputs: &[PathBuf],
dirs: &[PathBuf],
module: &Module,
) {
let Some(stamp) = Stamp::for_kind(key.kind) else {
return;
};
let paths = std::iter::once(normal(file))
.chain(module.deps.iter().cloned())
.chain(extra_inputs.iter().map(|p| normal(p)));
let Some(inputs) = self.inputs_for(paths) else {
return;
};
let names = Self::required_names(std::slice::from_ref(module));
let entry = Entry {
stamp,
subject: normal(file),
kind: key.kind.to_string(),
inputs,
probes: self.probes_for(dirs, &names),
module: module.clone(),
};
match self.write(key, &entry) {
Ok(()) => self.stats.borrow_mut().stored += 1,
Err(e) if self.opts.explain => eprintln!("htl cache: not stored ({e})"),
Err(_) => {}
}
}
pub fn store_run(
&self,
key: &Key,
files: &[PathBuf],
extra_inputs: &[PathBuf],
dirs: &[PathBuf],
modules: &[Module],
) {
let Some(stamp) = Stamp::current() else {
return;
};
let paths = files
.iter()
.map(|f| normal(f))
.chain(modules.iter().flat_map(|m| m.deps.iter().cloned()))
.chain(extra_inputs.iter().map(|p| normal(p)));
let Some(inputs) = self.inputs_for(paths) else {
return;
};
let names = Self::required_names(modules);
let entry = RunEntry {
stamp,
subjects: files.iter().map(|f| normal(f)).collect(),
kind: key.kind.to_string(),
inputs,
probes: self.probes_for(dirs, &names),
modules: modules.to_vec(),
};
match self.write(key, &entry) {
Ok(()) => self.stats.borrow_mut().stored += 1,
Err(e) if self.opts.explain => eprintln!("htl cache: not stored ({e})"),
Err(_) => {}
}
}
pub fn sweep(&self, keep: &[Key], files: usize) {
let bound = self
.opts
.max_entries
.unwrap_or_else(|| default_bound(files));
let Ok(rd) = std::fs::read_dir(&self.dir) else {
return;
};
let keep: std::collections::HashSet<&str> = keep.iter().map(|k| k.hash.as_str()).collect();
let mut all = 0usize;
let mut candidates: Vec<(PathBuf, std::time::SystemTime)> = Vec::new();
for e in rd.flatten() {
let path = e.path();
if path.extension().is_none_or(|x| x != "json") {
continue;
}
all += 1;
let stem = path
.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_default();
if keep.contains(stem.as_str()) {
continue;
}
let mtime = e
.metadata()
.and_then(|m| m.modified())
.unwrap_or(std::time::UNIX_EPOCH);
candidates.push((path, mtime));
}
if all <= bound {
return;
}
let mut removed = 0usize;
candidates.retain(|(p, _)| {
if all - removed <= bound || !self.is_orphan(p) {
return true;
}
if std::fs::remove_file(p).is_ok() {
removed += 1;
}
false
});
if all - removed > bound {
candidates.sort_by_key(|(_, t)| *t);
for (p, _) in &candidates {
if all - removed <= bound {
break;
}
if std::fs::remove_file(p).is_ok() {
removed += 1;
}
}
}
self.stats.borrow_mut().evicted += removed;
if removed > 0 && self.opts.explain {
eprintln!("htl cache: dropped {removed} of {all} entries (bound {bound})");
}
}
fn is_orphan(&self, path: &Path) -> bool {
let Ok(raw) = std::fs::read_to_string(path) else {
return true;
};
let Ok(v) = serde_json::from_str::<serde_json::Value>(&raw) else {
return true;
};
let Some(inputs) = v.get("inputs").and_then(|i| i.as_array()) else {
return true;
};
!inputs
.iter()
.filter_map(|i| i.get("path").and_then(|p| p.as_str()))
.any(|p| Path::new(p).exists())
}
fn write<T: Serialize>(&self, key: &Key, entry: &T) -> Result<()> {
std::fs::create_dir_all(&self.dir)?;
let final_path = self.entry_path(key);
let tmp = self
.dir
.join(format!(".{}.{}.tmp", key.hash, std::process::id()));
std::fs::write(&tmp, serde_json::to_vec(entry)?)?;
if let Err(e) = std::fs::rename(&tmp, &final_path) {
let _ = std::fs::remove_file(&tmp);
return Err(e.into());
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn scratch(name: &str) -> PathBuf {
let dir =
std::env::temp_dir().join(format!("htl-core-cache-{name}-{}", std::process::id()));
std::fs::create_dir_all(dir.join("src")).unwrap();
dir
}
#[test]
fn a_module_entry_is_served_whatever_binary_wrote_it() {
let root = scratch("portable");
let file = root.join("src/m.tl");
std::fs::write(&file, "return {}\n").unwrap();
let cache = Cache::open(&root, Options::default()).unwrap();
assert_ne!(Stamp::current().unwrap(), Stamp::portable());
let module = Module {
diagnostics: Vec::new(),
errors: 0,
warnings: 0,
lints: 0,
deps: Vec::new(),
requires: Vec::new(),
code: Some("return {}".into()),
check: Some(CheckInfoJson::default()),
};
let entry = |kind: &str| Entry {
stamp: Stamp::portable(),
subject: normal(&file),
kind: kind.to_string(),
inputs: cache.inputs_for(std::iter::once(normal(&file))).unwrap(),
probes: Vec::new(),
module: module.clone(),
};
let mk = module_gen_key(&file, None);
cache.write(&mk, &entry(MODULE)).unwrap();
assert!(
cache.lookup(&mk).is_some(),
"a module entry: the binary is not part of its stamp"
);
let gk = gen_key(&file, None);
cache.write(&gk, &entry(GEN)).unwrap();
assert!(
cache.lookup(&gk).is_none(),
"a test-file entry from another binary: written by a different build"
);
cache.store_module(&mk, &file, &[], &[], &module);
let raw = std::fs::read_to_string(cache.entry_path(&mk)).unwrap();
let stored: Entry = serde_json::from_str(&raw).unwrap();
assert_eq!(stored.stamp, Stamp::portable());
}
#[test]
fn source_mentions_require_is_word_bounded() {
assert!(source_mentions_require("local a = require(\"a\")\n"));
assert!(source_mentions_require("require 'a'"));
assert!(!source_mentions_require("local required_by = 1\n"));
assert!(!source_mentions_require("local x = prerequire\n"));
assert!(!source_mentions_require("local record c\nend\nreturn c\n"));
assert!(!source_mentions_require(""));
}
#[test]
fn scratch_roots_are_recognised() {
assert_eq!(
scratch_root(Path::new("/w/target/package/host-0.1.0")),
Some("under a `target` directory")
);
assert_eq!(scratch_root(Path::new("/w/host")), None);
let cargo_home = std::env::var_os("CARGO_HOME")
.map(PathBuf::from)
.or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".cargo")))
.expect("CARGO_HOME or HOME");
assert_eq!(
scratch_root(&cargo_home.join("registry/src/index/htl-0.3.0")),
Some("a registry checkout")
);
}
}