Hmbown/CodeWhale · error

invalid lane environment key

Error message

invalid lane environment key {key:?}

What it means

write_lane_environment serializes the private environment key/value pairs to a JSON file owned by the lane. Before writing anything it validates every key against valid_environment_key (underscore or ASCII alphanumeric only) and fails the whole write if any key is malformed, so no partial secret file is ever left on disk.

Solutions

  1. Rename the offending key to use only ASCII letters, digits, and underscores.
  2. Sanitize/normalize keys before passing the environment list to start().
  3. Check the error message's {key:?} value to identify exactly which entry is invalid.
  4. Test environment input with the existing guard (invalid_environment_key_never_leaves_a_partial_secret_file scenario) when adding new key sources.

Example fix

// before
let env = vec![("MY-VAR".to_string(), "1".to_string())];
// after
let env = vec![("MY_VAR".to_string(), "1".to_string())];
Defensive patterns

Strategy: validation

Validate before calling

fn valid_env_key(key: &str) -> bool { !key.is_empty() && key.chars().all(|c| c == '_' || c.is_ascii_alphanumeric()) }
env.iter().find(|(k, _)| !valid_env_key(k)).map_or(Ok(()), |(k, _)| Err(format!("invalid env key {k:?}"))).?;

Try / catch

if let Err(e) = registry.start(...) { if e.to_string().contains("invalid lane environment key") { env.retain(|(k, _)| valid_env_key(k)); /* retry */ } }

Prevention

When it happens

Trigger: Calling start() (which calls write_lane_environment) with an environment list containing a key with invalid characters, e.g. containing '-', '.', '=', spaces, or non-ASCII characters.

Common situations: Copying shell-style env definitions like "MY-VAR" or "some.key" into the lane environment; building keys dynamically by concatenating names with separators; inheriting env from an external system with laxer key rules.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/9a88b229d65d839c. Report an issue: GitHub.

Appendix: source

Thrown at crates/lane/src/runtime.rs:234

    match std::fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(err) => Err(err).with_context(|| format!("remove {}", path.display())),
    }
}

fn valid_environment_key(key: &str) -> bool {
    let mut chars = key.chars();
    chars
        .next()
        .is_some_and(|ch| ch == '_' || ch.is_ascii_alphabetic())
        && chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
}

fn write_lane_environment(path: &Path, environment: &[(String, String)]) -> Result<()> {
    for (key, _) in environment {
        if !valid_environment_key(key) {
            bail!("invalid lane environment key {key:?}");
        }
    }
    let encoded = serde_json::to_vec(environment).context("serialize private lane environment")?;
    if encoded.len() as u64 > MAX_ENVIRONMENT_BYTES {
        bail!(
            "private lane environment exceeds {} bytes",
            MAX_ENVIRONMENT_BYTES
        );
    }

    let tmp_path = lane_environment_tmp_path(path);
    remove_file_if_present(path)?;
    remove_file_if_present(&tmp_path)?;
    let mut options = OpenOptions::new();
    options.create_new(true).write(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;

View on GitHub (pinned to 73e0f67d83)