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.
Rust agent-skillsai-agentsclaude-codeclaude-code-plugincodexdeveloper-toolsffimcpmcp-servernapi-rsnodejsoptimizationperformanceprofilerpyo3pythonrewrite-it-in-rustrubyrusttypescript
scan/enable: strip BOM symmetrically in state::forms, handle BOM package.json, and canonicalize test temp dirs
RRFZ6b79151ef73c
diff --git a/src/backend.rs b/src/backend.rsindex 787b5dc..fb730fc 100644--- a/src/backend.rs+++ b/src/backend.rs@@ -461,9 +461,12 @@ mod tests {let two = append_marked(&append_marked("a\n", "#", "one", "1"), "#", "two", "2");assert_eq!(strip_marked(&strip_marked(&two, "#", "one"), "#", "two"), "a\n");- // Leading UTF-8 BOM on line 1 is preserved when inserting and stripping a block.+ // Leading UTF-8 BOM on line 1 is preserved when inserting and stripping a block,+ // while indented BOM lines and non-line-1 BOM characters are not treated as top-level guards.let bom_main = "\u{feff}if __name__ == \"__main__\":\r\n main()\r\n";assert_eq!(guard(bom_main), Some("\u{feff}".len()));+ assert_eq!(guard("\u{feff} if __name__ == \"__main__\":\r\n main()\r\n"), None);+ assert_eq!(guard("x = 1\n\u{feff}if __name__ == \"__main__\":\n main()\n"), None);let bom_on = insert_marked(bom_main, "#", "m.f", "f = g", guard);assert!(bom_on.starts_with('\u{feff}'));assert_eq!(strip_marked(&bom_on, "#", "m.f"), bom_main);diff --git a/src/node.rs b/src/node.rsindex 6532467..2a6cd64 100644--- a/src/node.rs+++ b/src/node.rs@@ -538,7 +538,7 @@ fn is_esm(root: &Path, file: &Path) -> bool {let mut dir = file.parent();while let Some(d) = dir {if let Ok(text) = fs::read_to_string(d.join("package.json")) {- return serde_json::from_str::<Value>(&text)+ return serde_json::from_str::<Value>(text.strip_prefix('\u{feff}').unwrap_or(&text)).ok().and_then(|v| v["type"].as_str().map(|t| t == "module")).unwrap_or(false);@@ -865,8 +865,18 @@ mod tests {.as_slice(),),];- let base = std::env::temp_dir().join(format!("fa-node-bom-{}", std::process::id()));+ let tmp = std::env::temp_dir();+ let tmp = if cfg!(windows) { tmp } else { tmp.canonicalize().unwrap() };+ let base = tmp.join(format!("fa-node-bom-{}", std::process::id()));let _ = fs::remove_dir_all(&base);+ // Bare `const` after line-1 BOM matches, while non-line-1 U+FEFF is not skipped.+ assert_eq!(declaration("\u{feff}const crunch = (n) => n;\n", "const", "crunch"), Some("\u{feff}".len()));+ assert_eq!(declaration("const a = 1;\n\u{feff}const crunch = (n) => n;\n", "const", "crunch"), None);+ // package.json with UTF-8 BOM is still recognized as ESM.+ let pkg_dir = base.join("bom-pkg");+ fs::create_dir_all(&pkg_dir).unwrap();+ fs::write(pkg_dir.join("package.json"), b"\xef\xbb\xbf{\"type\": \"module\"}\r\n").unwrap();+ assert!(is_esm(&pkg_dir, &pkg_dir.join("work.js")));for (name, work_bytes) in cases {let dir = base.join(name);fs::create_dir_all(&dir).unwrap();@@ -897,10 +907,15 @@ mod tests {"{name}: const must switch to let after BOM: {text}");}+ assert!(!Ctx::open(&dir).unwrap().stale(port), "{name}: enabled BOM port must not be reported stale");crate::pipeline::disable(&dir, Some("work.crunch")).unwrap();let restored_bytes = fs::read(dir.join("work.mjs")).unwrap();- let _ = fs::remove_dir_all(&dir);assert_eq!(restored_bytes, work_bytes, "{name}: disable must restore BOM file byte for byte");+ let edited = String::from_utf8_lossy(work_bytes).replace("let t = 0;", "let t = 1;");+ fs::write(dir.join("work.mjs"), edited.as_bytes()).unwrap();+ let stale_after_edit = Ctx::open(&dir).unwrap().stale(port);+ let _ = fs::remove_dir_all(&dir);+ assert!(stale_after_edit, "{name}: editing first-line BOM function must trigger stale detection");}let _ = fs::remove_dir_all(&base);}diff --git a/src/python.rs b/src/python.rsindex 1935e0b..97606cc 100644--- a/src/python.rs+++ b/src/python.rs@@ -1361,7 +1361,9 @@ sys.exit(1 if fails else 0)eprintln!("no python on PATH, skipping");return;};- let dir = std::env::temp_dir().join(format!("fa-py-bom-{}", std::process::id()));+ let tmp = std::env::temp_dir();+ let tmp = if cfg!(windows) { tmp } else { tmp.canonicalize().unwrap() };+ let dir = tmp.join(format!("fa-py-bom-{}", std::process::id()));let _ = fs::remove_dir_all(&dir);fs::create_dir_all(&dir).unwrap();let work_bytes = b"\xef\xbb\xbfdef crunch(n: int) -> int:\r\n t = 0\r\n for i in range(n):\r\n t += (i * i) % 97\r\n return t\r\n";@@ -1382,10 +1384,15 @@ sys.exit(1 if fails else 0)crate::pipeline::enable(&dir, Some("work.crunch"), true).unwrap();let enabled_bytes = fs::read(dir.join("work.py")).unwrap();assert!(enabled_bytes.starts_with(b"\xef\xbb\xbf"), "enable must preserve UTF-8 BOM");+ assert!(!Ctx::open(&dir).unwrap().stale(port), "enabled BOM port must not be reported stale");crate::pipeline::disable(&dir, Some("work.crunch")).unwrap();let restored_bytes = fs::read(dir.join("work.py")).unwrap();- let _ = fs::remove_dir_all(&dir);assert_eq!(restored_bytes, work_bytes, "disable must restore BOM file byte for byte");+ let edited = String::from_utf8_lossy(work_bytes).replace("t = 0", "t = 1");+ fs::write(dir.join("work.py"), edited.as_bytes()).unwrap();+ let stale_after_edit = Ctx::open(&dir).unwrap().stale(port);+ let _ = fs::remove_dir_all(&dir);+ assert!(stale_after_edit, "editing first-line BOM function must trigger stale detection");}struct WorkerEnv {diff --git a/src/state.rs b/src/state.rsindex f526ee2..930d1ef 100644--- a/src/state.rs+++ b/src/state.rs@@ -79,7 +79,7 @@ pub struct Verify {/// The forms a source may take in the file: as recorded, or with const and let swapped (a Node/// port switches its function's const to let while enabled).fn forms(source: &str) -> [String; 3] {- let source = source.replace("\r\n", "\n");+ let source = source.strip_prefix('\u{feff}').unwrap_or(source).replace("\r\n", "\n");[source.replacen("const ", "let ", 1), source.replacen("let ", "const ", 1), source]}@@ -268,5 +268,15 @@ mod tests {let edited = file.replace("return x", "return x + 1");assert!(!found(&edited, &kept[0]), "an edited function is no longer found");assert!(found(&format!("x = 1\n{}", kept[0]), &kept[0]), "at the end of a file without a final newline");++ // Leading UTF-8 BOM in either the file or the recorded source matches symmetrically.+ let bom_file = "\u{feff}def f(x):\n return x\n";+ let plain_src = "def f(x):\n return x";+ let bom_src = "\u{feff}def f(x):\n return x";+ assert_eq!(verbatim(bom_file, &[plain_src]), [plain_src]);+ assert_eq!(verbatim(bom_file, &[bom_src]), [plain_src]);+ assert!(found(bom_file, plain_src));+ assert!(found(bom_file, bom_src));+ assert!(!found(&bom_file.replace("return x", "return x + 1"), plain_src));}}