diesel-rs/diesel · error

Expected ` ` to be set as environment variable

Error message

Expected `{}` to be set as environment variable

What it means

When `diesel_for_each_tuple!` receives `env!(VAR)` as the max tuple size, the proc macro reads the environment variable at compile time. If `std::env::var` fails (unset or non-UTF-8), the macro errors with this message naming the variable.

Solutions

  1. Set the environment variable before compiling (e.g. `export MAX_TUPLE_SIZE=32` or via a cargo config/`.env`-loading build script).
  2. Replace `env!(VAR)` with a literal integer to remove the build-time dependency.
  3. Fix a misspelled variable name in the `env!` call.

Example fix

// before
diesel_for_each_tuple!(my_table, env!(MAX_TUPLE_SIZE));

// after
diesel_for_each_tuple!(my_table, 32);
Defensive patterns

Strategy: validation

Validate before calling

// Shell check before building
# test -n "$MAX_TUPLE_SIZE" || { echo "MAX_TUPLE_SIZE must be set"; exit 1; }
# or in a script:
if [ -z "${MAX_TUPLE_SIZE:-}" ]; then echo "Set MAX_TUPLE_SIZE, e.g. export MAX_TUPLE_SIZE=32"; exit 1; fi

Prevention

When it happens

Trigger: `diesel_for_each_tuple!(table, env!(MAX_SIZE));` compiled without `MAX_SIZE` set in the build environment.

Common situations: CI environments missing a variable set locally; running `cargo build` outside a Makefile/setup script that exports the variable; misremembering the variable name (cargo env vars are not automatically inherited in all contexts).

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of diesel-rs/diesel@6fa6ed01b2 (2026-09-07). Data as JSON: /api/errors/c8b9ecf9aaaf7abf. Report an issue: GitHub.

Appendix: source

Thrown at diesel_derives/src/diesel_for_each_tuple.rs:71

impl syn::parse::Parse for ForEachTupleInput {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let inner = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let max_size = if input.peek(syn::Ident) {
            let macro_ident = input.parse::<syn::Ident>()?;
            if macro_ident != "env" {
                return Err(syn::Error::new(
                    macro_ident.span(),
                    "only the `env!` macro is expected here",
                ));
            }
            let _bang = input.parse::<syn::Token![!]>()?;
            let name;
            syn::parenthesized!(name in input);
            let s = name.parse::<syn::LitStr>()?;
            std::env::var(s.value())
                .map_err(|_| {
                    syn::Error::new(
                        s.span(),
                        format!("Expected `{}` to be set as environment variable", s.value()),
                    )
                })?
                .parse::<u16>()
                .map_err(|_| {
                    syn::Error::new(
                        s.span(),
                        format!("Expected `{}` to be a u16 integer value", s.value()),
                    )
                })?
        } else {
            input.parse::<syn::LitInt>()?.base10_parse()?
        };
        Ok(Self { inner, max_size })
    }
}

View on GitHub (pinned to 6fa6ed01b2)