Is this the best way to develop a macro crate?

I'm writing a library that makes it easier to write GitHub Action in rust. It currently provides two functionalities:

  1. A macro_rules! macro that returns output from the Rust code to the action runner.
  2. A attribute-like proc macro to put on your main function that allows you to use the ? operator directly in main. Errors are returned to the action runner in the error output.

For my workspace setup I followed the instructions in this post. You can find the repo here, but I'll include the relevant files without documentation here as well.


name = "gha_main"
version = "0.0.2"
edition = "2021"

anyhow = "1.0.71"
gha_main-proc_macro = { path = "src/proc_macro", version = "0.0.2" }
lazy_static = "1.4.0"
uuid = { version = "1.3.3", features = ["v4"] }

members = ["src/proc_macro"]


pub use gha_main_proc_macro::gha_main;

pub extern crate anyhow;
pub extern crate lazy_static;
pub extern crate uuid;

pub type GitHubActionResult = anyhow::Result<()>;

macro_rules! gha_output {
    ($value:ident) => {
        let key = stringify!($value);
        let value = $value.to_string();
        $crate::write_output(key, value, &mut OUTPUT_FILE.lock().unwrap());

pub fn write_output(key: &str, value: String, output_file: &mut File) {
    let delimiter = uuid::Uuid::new_v4();
    .expect("Failed to write output");


name = "gha_main-proc_macro"
version = "0.0.2"
edition = "2021"

quote = "1.0.27"
syn = { version = "2.0.16", features = ["full"] }
proc-macro2 = "1.0.58"
proc-macro-error = "1.0.4"

proc-macro = true
path = ""


use proc_macro::TokenStream;
use proc_macro2::Ident;
use proc_macro_error::{abort, proc_macro_error};
use quote::quote;
use syn::{parse_macro_input, ItemFn};

pub fn gha_main(_args: TokenStream, item: TokenStream) -> TokenStream {
    let input_fn = parse_macro_input!(item as ItemFn);
    let ident = &input_fn.sig.ident;


    TokenStream::from(quote! {
        use std::fs::File;
        use std::fs::OpenOptions;
        use std::io::Write;
        use std::sync::Mutex;

        use gha_main::anyhow::{bail, Result};
        use gha_main::uuid::Uuid;
        use gha_main::lazy_static::lazy_static;

        lazy_static! {
            static ref OUTPUT: String =
            static ref OUTPUT_FILE: Mutex<File> = Mutex::new(OpenOptions::new()
                .expect("Failed to create or open output file"));

        fn main() -> Result<()> {

            // If an error was propagated from the inner function, write it to the output file
            if let Err(error) = #ident() {
                bail!("Action failed with error: {}", error);


fn verify_main(ident: &Ident) {
    if ident != "main" {
        abort!(ident, "function must be called `main`");

The attribute macro wraps the user's main function in a new main that handles errors that are returned from the inner main. It also imports the types and traits needed for the macro_rules! macro to work and initializes lazy statics to write the outputs and errors to the correct file.

This all works but a couple of things are not ideal:

  1. I need to add imports in the attribute macro output TokenStream for types reexported from the main library while the dependency is the other way around. This is a constant source of confusion.
  2. Rust analyzer doesn't seem to import to code in src/ and it doesn't help with checking the code in the proc macro TokenStream. Overall this leads to pretty bad DX.

This is my first time writing Rust macros so maybe this is inherent to the task but it all feels very hacky. Is there a better way to organize this code?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.