Skip to content

Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.

build: don't put an enabled port live if its Rust changed since verify (#26)

chessMan437c19227b95
diff --git a/src/backend.rs b/src/backend.rs
index 098f7b6..64e9013 100644
--- a/src/backend.rs
+++ b/src/backend.rs
@@ -265,6 +265,32 @@ pub fn build(ctx: &Ctx, port: &Port) -> Result<std::result::Result<PathBuf, Stri
Ok(Ok(dest))
}
+/// FNV-1a over the crate's Cargo.toml and everything under src/, so build can tell whether the
+/// sources are still the ones verify checked. Stable across Rust versions, unlike DefaultHasher.
+pub fn source_hash(dir: &Path) -> Result<String> {
+ fn walk(dir: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
+ for entry in fs::read_dir(dir).with_context(|| format!("reading {}", dir.display()))? {
+ let path = entry?.path();
+ if path.is_dir() { walk(&path, out)? } else { out.push(path) }
+ }
+ Ok(())
+ }
+ let mut files = vec![dir.join("Cargo.toml")];
+ if dir.join("src").is_dir() {
+ walk(&dir.join("src"), &mut files)?;
+ }
+ files.sort();
+ let mut h: u64 = 0xcbf29ce484222325;
+ for file in &files {
+ let rel = file.strip_prefix(dir).unwrap_or(file).to_string_lossy().replace('\\', "/");
+ let bytes = fs::read(file).with_context(|| format!("reading {}", file.display()))?;
+ for b in rel.bytes().chain([0]).chain(bytes).chain([0]) {
+ h = (h ^ b as u64).wrapping_mul(0x100000001b3);
+ }
+ }
+ Ok(format!("{h:016x}"))
+}
+
// ------------------------------------------------------------------ marked blocks
pub fn markers(comment: &str, id: &str) -> (String, String) {
@@ -332,6 +358,21 @@ pub fn line_starting(text: &str, prefixes: &[&str]) -> Option<usize> {
mod tests {
use super::*;
+ #[test]
+ fn source_hash_follows_the_sources() {
+ let dir = std::env::temp_dir().join(format!("fa-hash-test-{}", std::process::id()));
+ fs::create_dir_all(dir.join("src")).unwrap();
+ fs::write(dir.join("Cargo.toml"), "[package]\n").unwrap();
+ fs::write(dir.join("src/lib.rs"), "fn f() {}\n").unwrap();
+ let first = source_hash(&dir).unwrap();
+ assert_eq!(source_hash(&dir).unwrap(), first);
+ fs::write(dir.join("TASK.md"), "not a source").unwrap();
+ assert_eq!(source_hash(&dir).unwrap(), first, "only Cargo.toml and src/ count");
+ fs::write(dir.join("src/lib.rs"), "fn f() { }\n").unwrap();
+ assert_ne!(source_hash(&dir).unwrap(), first);
+ let _ = fs::remove_dir_all(&dir);
+ }
+
#[test]
fn blocks_come_out_byte_for_byte() {
let guard = |t: &str| line_starting(t, &["if__name__==\"__main__\":", "if__name__=='__main__':"]);
diff --git a/src/pipeline.rs b/src/pipeline.rs
index 7a56be2..a7242a6 100644
--- a/src/pipeline.rs
+++ b/src/pipeline.rs
@@ -141,6 +141,7 @@ pub fn verify(root: &Path, id: Option<&str>) -> Result<(String, Verify)> {
let mut ctx = Ctx::open(root)?;
let id = ctx.resolve_port(id)?;
let port = ctx.state.ports[&id].clone();
+ let source_hash = backend::source_hash(&ctx.crate_dir(&port))?;
ui::step(format!("building {} (release)", ui::bold(&port.ext)));
let v = match backend::build(&ctx, &port)? {
Err(log) => Verify { built: false, build_log: Some(log), ..Verify::default() },
@@ -155,6 +156,7 @@ pub fn verify(root: &Path, id: Option<&str>) -> Result<(String, Verify)> {
};
let mut v = v;
v.lang = port.lang;
+ v.source_hash = Some(source_hash);
if !v.ok() && port.enabled {
port.lang.disable(&ctx, &port)?;
if let Some(p) = ctx.state.ports.get_mut(&id) {
@@ -210,6 +212,18 @@ pub fn build_all(root: &Path) -> Result<()> {
ensure!(!ctx.state.ports.is_empty(), "nothing ported yet");
let mut failed = 0;
for port in ctx.state.ports.values() {
+ // An enabled port loads whatever lands in .fast/lib: only rebuild what verify checked.
+ let verified = port.verify.as_ref().and_then(|v| v.source_hash.as_deref());
+ if port.enabled
+ && verified.is_some_and(|h| backend::source_hash(&ctx.crate_dir(port)).ok().as_deref() != Some(h))
+ {
+ failed += 1;
+ ui::error(format!(
+ "{} is switched on and its Rust changed since it passed verify; run fast-anything verify {}",
+ port.id, port.id
+ ));
+ continue;
+ }
ui::step(format!("building {}", ui::bold(&port.id)));
match backend::build(&ctx, port)? {
Ok(path) => ui::ok(format!("{}", path.display())),
diff --git a/src/state.rs b/src/state.rs
index 023d131..f92945b 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -68,6 +68,8 @@ pub struct Verify {
pub failures: Vec<Failure>,
pub micro: Option<Micro>,
pub lang: Lang,
+ /// Hash of the crate's sources this result is for (see backend::source_hash).
+ pub source_hash: Option<String>,
}
impl Verify {