Skip to content

Env vars set by proc macros should be cleared after a macro expansion #11355

Closed
@vlad20012

Description

@vlad20012

Some macros change their environment variables. For example, sqlx loads all variables from .env file. These envs can affect the expansion of other macros. For example, an sqlx macro from crate A can affect sqlx macros from crate B.

Demo:

#[proc_macro]
pub fn set_var(_: TokenStream) -> TokenStream {
    std::env::set_var("FOO", "bad");
    TokenStream::new()
}

#[proc_macro]
pub fn read_var(_: TokenStream) -> TokenStream {
    let var = std::env::var("FOO").unwrap_or_else(|_| "good".to_string());
    format!("\"{}\"", var).parse().unwrap()
}

then use them like this:

fn main() {
    my_proc_macro::set_var!();
    let b = my_proc_macro::read_var!();
}

read_var!() should be expanded to "good", but it will likely be expanded to "bad".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions