tauri-apps/tauri · error

missing `CARGO_PKG_NAME` environment variable

Error message

missing `CARGO_PKG_NAME` environment variable

What it means

The tauri command wrapper macro reads the CARGO_PKG_NAME environment variable at compile time to detect whether the crate is a tauri-plugin-* package. It panics when the variable is absent, which should never happen in a normal cargo build since cargo always sets it.

Solutions

  1. Build with cargo (or cargo build/test) so CARGO_PKG_NAME is set automatically
  2. If using a custom runner, export CARGO_PKG_NAME=<crate-name> before compiling
  3. Check sanitizer configs (env -i, docker -e filters, CI wrappers) that drop cargo-provided variables

Example fix

// before
rustc --edition 2021 src/lib.rs   # no cargo env
// after
cargo build
# or, if a custom runner is required:
CARGO_PKG_NAME=my-crate rustc --edition 2021 src/lib.rs
Defensive patterns

Strategy: fallback

Validate before calling

if std::env::var_os("CARGO_PKG_NAME").is_none() {
    panic!("build must run under cargo: CARGO_PKG_NAME missing");
}

Try / catch

// set the var when a custom runner strips env
if std::env::var_os("CARGO_PKG_NAME").is_none() {
    std::env::set_var("CARGO_PKG_NAME", env!("CARGO_PKG_NAME"));
}

Prevention

When it happens

Trigger: Compiling a crate that uses #[tauri::command] outside a normal cargo invocation — e.g. invoking rustc directly, or running the compiler in an environment where CARGO_PKG_NAME was stripped from the env.

Common situations: Custom build runners sanitizing environment variables, IDE tooling invoking rustc without cargo's env, sandboxed CI with restricted env, or exotic build orchestration.

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 tauri-apps/tauri@460ec35447 (2026-09-18). Data as JSON: /api/errors/2e269d175f45d613. Report an issue: GitHub.

Appendix: source

Thrown at crates/tauri-macros/src/command/wrapper.rs:237

                #diagnostic
                trait AsyncCommandMustReturnResult {}
                impl<A, B> AsyncCommandMustReturnResult for ::std::result::Result<A, B> {}
                let _check: #return_type = unreachable!();
                let _: &dyn AsyncCommandMustReturnResult = &_check;
              };
            };
          } else {
            return quote_spanned! {
              span => compile_error!("async commands that contain references as inputs must return a `Result`");
            }.into();
          }
        }
      }
    }
  }

  let plugin_name = var("CARGO_PKG_NAME")
    .expect("missing `CARGO_PKG_NAME` environment variable")
    .strip_prefix("tauri-plugin-")
    .map(|name| quote!(::core::option::Option::Some(#name)))
    .unwrap_or_else(|| quote!(::core::option::Option::None));

  let body = match attrs.execution_context {
    ExecutionContext::Async => body_async(&plugin_name, &function, &invoke, &attrs)
      .unwrap_or_else(syn::Error::into_compile_error),
    ExecutionContext::Blocking => body_blocking(&plugin_name, &function, &invoke, &attrs)
      .unwrap_or_else(syn::Error::into_compile_error),
  };

  let Invoke {
    message,
    resolver,
    acl,
  } = invoke;

  let root = attrs.root;

View on GitHub (pinned to 460ec35447)