From 52000eebdae53fde1e5937cab11496c34aba38ac Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Sun, 29 Nov 2026 13:12:12 +0530 Subject: [PATCH] Write component macro debug output without including it The debug file set by HYPERLIGHT_COMPONENT_MACRO_DEBUG was read back with include!(), so when a crate invoked the bindgen macros more than once the included file could hold another invocation's expansion. Emit the generated tokens directly and keep writing the file for inspection. Signed-off-by: Anurag Singh --- src/hyperlight_component_macro/src/lib.rs | 5 ++- src/hyperlight_component_util/src/util.rs | 55 ++++++++++++++++++----- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/hyperlight_component_macro/src/lib.rs b/src/hyperlight_component_macro/src/lib.rs index 2c8a0cab3..9185e6514 100644 --- a/src/hyperlight_component_macro/src/lib.rs +++ b/src/hyperlight_component_macro/src/lib.rs @@ -26,7 +26,10 @@ //! The generated code can be examined by setting the environment //! variable `$HYPERLIGHT_COMPONENT_MACRO_DEBUG=/path/to/file.rs`, //! which will result in the generated code being written to that -//! file, which is then included back into the Rust source. +//! file. If the path contains `#`, it is replaced with the world +//! name. The file is written for inspection only: the macro still +//! expands to the tokens it generated, so multiple invocations in +//! one crate do not overwrite each other's expansions. //! //! The macros also can be configured to output a great deal of debug //! information about the internal elaboration and codegen diff --git a/src/hyperlight_component_util/src/util.rs b/src/hyperlight_component_util/src/util.rs index ad420d1cc..3e8975ea8 100644 --- a/src/hyperlight_component_util/src/util.rs +++ b/src/hyperlight_component_util/src/util.rs @@ -142,18 +142,49 @@ pub fn read_wit_type_from_file R>( /// macroexpansion) to the debug file and then return the token stream pub fn emit_decls(decls: proc_macro2::TokenStream, for_kebab: &str) -> proc_macro2::TokenStream { if let Ok(dbg_out) = std::env::var("HYPERLIGHT_COMPONENT_MACRO_DEBUG") { - let fs_safe = for_kebab.replace("/", "_"); - #[cfg(windows)] - let fs_safe = fs_safe.replace(":", "+"); - let dbg_out = dbg_out.replace("#", &fs_safe); - if let Ok(file) = syn::parse2(decls.clone()) { - std::fs::write(&dbg_out, prettyplease::unparse(&file)).unwrap(); - } else { - let decls = format!("{}", decls); - std::fs::write(&dbg_out, &decls).unwrap(); - } - quote::quote! { include!(#dbg_out); } + write_debug_file(&dbg_out, &decls, for_kebab); + } + decls +} + +/// Write `decls` to the file named by the +/// `$HYPERLIGHT_COMPONENT_MACRO_DEBUG` template, replacing `#` with +/// `for_kebab` (made safe for the filesystem). +fn write_debug_file(template: &str, decls: &proc_macro2::TokenStream, for_kebab: &str) { + let fs_safe = for_kebab.replace("/", "_"); + #[cfg(windows)] + let fs_safe = fs_safe.replace(":", "+"); + let dbg_out = template.replace("#", &fs_safe); + if let Ok(file) = syn::parse2(decls.clone()) { + std::fs::write(&dbg_out, prettyplease::unparse(&file)).unwrap(); } else { - decls + let decls = format!("{}", decls); + std::fs::write(&dbg_out, &decls).unwrap(); + } +} + +#[cfg(test)] +mod tests { + use proc_macro2::TokenStream; + + use super::{emit_decls, write_debug_file}; + + #[test] + fn emit_decls_returns_its_input() { + let decls: TokenStream = "fn generated() -> u32 { 1 }".parse().unwrap(); + let out = emit_decls(decls.clone(), "test:world/thing"); + assert_eq!(out.to_string(), decls.to_string()); + } + + #[test] + fn write_debug_file_substitutes_kebab() { + let dir = std::env::temp_dir().join(format!("hyperlight-debug-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let template = dir.join("#.rs"); + let decls: TokenStream = "fn generated() -> u32 { 1 }".parse().unwrap(); + write_debug_file(&template.to_string_lossy(), &decls, "test/world_thing"); + let written = std::fs::read_to_string(dir.join("test_world_thing.rs")).unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(written.contains("fn generated()")); } }