nikivdev/code · error · anyhow::Error

Key cannot be empty

Error message

Key cannot be empty

What it means

Input validation in `set_personal_env_var`: the function bails immediately if the `key` argument is an empty string. Personal env vars are keyed by name, so an empty key is meaningless and would produce an unusable record; the check happens before any local or cloud write.

Source

Thrown at src/env.rs:3329

        // Show description if available
        if let Some(desc) = descriptions.as_ref().and_then(|map| map.get(key)) {
            println!("  {} = {}  # {}", key, masked, desc);
        } else {
            println!("  {} = {}", key, masked);
        }
    }

    println!();
    println!("{} env var(s)", vars.len());

    Ok(())
}

/// Set a personal (global) env var.
pub(crate) fn set_personal_env_var(key: &str, value: &str) -> Result<()> {
    if key.is_empty() {
        bail!("Key cannot be empty");
    }

    let target = resolve_personal_target()?;
    let environment = "production";

    if local_env_enabled() {
        let path = set_local_env_var(&target, environment, key, value)?;
        println!(
            "✓ Set personal env var locally: {} (stored at {})",
            key,
            path.display()
        );
        return Ok(());
    }

    let auth = load_auth_config()?;
    let token = match auth.token.as_ref() {
        Some(token) => token,

View on GitHub (pinned to a747e741ae)

Solutions

  1. Pass a non-empty key name to the set command
  2. In scripts, guard: `[ -n "$KEY" ] || exit 1` before invoking
  3. Check for typos where the value and key arguments got swapped or the key variable is empty

Example fix

// before
let key = std::env::var("ENV_KEY").unwrap_or_default();
set_personal_env_var(&key, "value")?;

// after
let key = std::env::var("ENV_KEY").context("ENV_KEY must be set")?;
set_personal_env_var(&key, "value")?;
Defensive patterns

Strategy: validation

Validate before calling

if key.trim().is_empty() {
    anyhow::bail!("env key must be a non-empty identifier");
}

Type guard

fn is_valid_env_key(key: &str) -> bool {
    !key.is_empty()
        && key.chars().all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_')
}

Try / catch

match set_personal_env_var(key, value) {
    Err(e) if e.to_string().contains("Key cannot be empty") => {
        eprintln!("Provide a non-empty env var name, e.g. f env set MY_KEY value");
    }
    Err(e) => return Err(e),
    Ok(_) => (),
}

Prevention

When it happens

Trigger: Calling the personal env var setter with `key == ""` — e.g. a CLI arg parsed from an empty shell variable (`f env set "$KEY" ...` where KEY is unset), a split/trim bug that yields an empty string, or a missing positional argument defaulting to empty.

Common situations: Shell scripts with unset environment variables interpolated as the key; over-trimming input; CLI arg parsing that doesn't enforce non-empty positional args.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/c8a5250068b93785. Report an issue: GitHub.