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.

Merge #112: go variadic functions

EL4CTEO29a8b263c408
diff --cc src/golang.rs
index 88d0117,e04c994..777480f
--- a/src/golang.rs
+++ b/src/golang.rs
@@@ -522,38 -535,47 +541,79 @@@ pub fn disable(ctx: &Ctx, port: &Port)
mod tests {
use super::*;
+ #[test]
+ fn variadic_functions_are_portable() {
+ if !Command::new("go").arg("version").output().is_ok_and(|o| o.status.success()) {
+ eprintln!("no go on PATH, skipping");
+ return;
+ }
+ let dir = std::env::temp_dir().join(format!("fa-go-variadic-test-{}", std::process::id()));
+ fs::create_dir_all(&dir).unwrap();
+ fs::write(dir.join("go.mod"), "module demo\n\ngo 1.22\n").unwrap();
+ let lib = "package main\n\nfunc total(scale int, xs ...int) int {\n\ts := 0\n\tfor _, x := range xs {\n\t\ts += x * scale\n\t}\n\treturn s\n}\n";
+ let main = "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(total(2, 1, 2, 3), total(3), total(1, []int{4, 5}...))\n}\n";
+ fs::write(dir.join("lib.go"), lib).unwrap();
+ fs::write(dir.join("main.go"), main).unwrap();
+ let ctx = Ctx::open(&dir).unwrap();
+ let f = describe(&ctx, "go").unwrap().into_iter().find(|f| f.name == "total").unwrap();
+ assert!(f.blockers.is_empty(), "{:?}", f.blockers);
+ assert_eq!(rust_type(f.params[1].annotation.as_deref().unwrap()).as_deref(), Some("Vec<i64>"));
+ let hs = Hotspot {
+ id: "main.total".into(),
+ file: "lib.go".into(),
+ line: 3,
+ name: "total".into(),
+ qualname: "total".into(),
+ params: f.params.clone(),
+ returns: f.returns.clone(),
+ ..Hotspot::default()
+ };
+ let out = dir.join("rec.jsonl");
+ let rec = record(&ctx, "go", &["run".into(), ".".into()], &hs, &out, 50);
+ let lines = fs::read_to_string(&out).unwrap_or_default();
+ let after = fs::read_to_string(dir.join("lib.go")).unwrap();
+ let _ = fs::remove_dir_all(&dir);
+ assert_eq!(rec.unwrap().calls, 3);
+ assert!(lines.contains(r#"{"args":[2,[1,2,3]],"result":12}"#), "{lines}");
+ assert!(
+ lines.contains(r#"{"args":[3,null],"result":0}"#) || lines.contains(r#"{"args":[3,[]],"result":0}"#),
+ "{lines}"
+ );
+ assert_eq!(after, lib);
+ }
+
+ #[test]
+ fn records_a_function_that_lives_next_to_main() {
+ if !Command::new("go").arg("version").output().is_ok_and(|o| o.status.success()) {
+ eprintln!("no go on PATH, skipping");
+ return;
+ }
+ let dir = std::env::temp_dir().join(format!("fa-go-main-file-test-{}", std::process::id()));
+ fs::create_dir_all(&dir).unwrap();
+ fs::write(dir.join("go.mod"), "module demo\n\ngo 1.22\n").unwrap();
+ let main = "package main\n\nimport \"fmt\"\n\nfunc double(x int) int {\n\treturn 2 * x\n}\n\nfunc main() {\n\tt := 0\n\tfor i := 0; i < 7; i++ {\n\t\tt += double(i)\n\t}\n\tfmt.Println(t)\n}\n";
+ fs::write(dir.join("main.go"), main).unwrap();
+ let ctx = Ctx::open(&dir).unwrap();
+ let hs = Hotspot {
+ id: "main.double".into(),
+ file: "main.go".into(),
+ line: 5,
+ name: "double".into(),
+ qualname: "double".into(),
+ params: vec![Param { name: "x".into(), annotation: Some("int".into()), ..Param::default() }],
+ returns: Some("int".into()),
+ ..Hotspot::default()
+ };
+ let out = dir.join("rec.jsonl");
+ let rec = record(&ctx, "go", &["run".into(), ".".into()], &hs, &out, 50);
+ let after = fs::read_to_string(dir.join("main.go")).unwrap();
+ let lines = fs::read_to_string(&out).unwrap_or_default();
+ let _ = fs::remove_dir_all(&dir);
+ assert_eq!(rec.unwrap().calls, 7);
+ assert!(lines.contains("{\"args\":[3],\"result\":6}"), "{lines}");
+ assert_eq!(after, main, "the app is left as it was");
+ }
+
#[test]
fn go_commands_and_profiles() {
let s = |v: &[&str]| v.iter().map(|x| x.to_string()).collect::<Vec<_>>();