{"record":{"id":"f9e21e9ce5fc32ad","repo":"nikivdev/code","slug":"missing-gemini-api-key-google-api-key-set-env-var","errorCode":null,"errorMessage":"Missing GEMINI_API_KEY/GOOGLE_API_KEY (set env var or add to personal env)","messagePattern":"Missing GEMINI_API_KEY/GOOGLE_API_KEY \\(set env var or add to personal env\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ai.rs","lineNumber":14555,"sourceCode":"    }\n    if let Ok(key) = std::env::var(\"GOOGLE_API_KEY\") {\n        if !key.trim().is_empty() {\n            return Ok(key);\n        }\n    }\n\n    if let Ok(Some(key)) = crate::env::get_personal_env_var(\"GEMINI_API_KEY\") {\n        if !key.trim().is_empty() {\n            return Ok(key);\n        }\n    }\n    if let Ok(Some(key)) = crate::env::get_personal_env_var(\"GOOGLE_API_KEY\") {\n        if !key.trim().is_empty() {\n            return Ok(key);\n        }\n    }\n\n    bail!(\"Missing GEMINI_API_KEY/GOOGLE_API_KEY (set env var or add to personal env)\")\n}\n\nfn truncate_for_summary(context: &str) -> String {\n    let max_chars = summary_max_chars();\n    if context.chars().count() <= max_chars {\n        return context.to_string();\n    }\n    let start = context.chars().count().saturating_sub(max_chars);\n    context.chars().skip(start).collect()\n}\n\nfn truncate_for_handoff(context: &str) -> String {\n    let max_chars = handoff_max_chars();\n    if context.chars().count() <= max_chars {\n        return context.to_string();\n    }\n    let start = context.chars().count().saturating_sub(max_chars);\n    context.chars().skip(start).collect()","sourceCodeStart":14537,"sourceCodeEnd":14573,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/ai.rs#L14537-L14573","documentation":"The Gemini summarization path requires an API key and checks `GEMINI_API_KEY`/`GOOGLE_API_KEY`, including personal env lookups via `crate::env::get_personal_env_var`. If neither is set (or the value is whitespace-only), the function bails telling the user to set the env var or add it to their personal env file. No request is attempted without credentials.","triggerScenarios":"Calling the Gemini summary command with neither GEMINI_API_KEY nor GOOGLE_API_KEY exported in the environment; key present but empty/whitespace-only; key defined only in a shell rc not loaded by the current process; personal env file missing the entry.","commonSituations":"Fresh checkout on a new machine without the key; CI secrets not propagated; using a shell wrapper (systemd, Docker, IDE task) that strips environment variables; typo in the variable name (e.g. GOOGLE_APIKEY).","solutions":["Export the key: `export GEMINI_API_KEY=...` (or GOOGLE_API_KEY) in the shell before running the command","Add the key to the tool's personal env file so it persists across shells/sessions","In CI, pass the secret through as an environment variable to the process","Verify with `echo ${GEMINI_API_KEY:+set}` that the variable is non-empty in the exact environment running the tool"],"exampleFix":"// before\n$ myapp ai summarize gemini\nError: Missing GEMINI_API_KEY/GOOGLE_API_KEY (set env var or add to personal env)\n// after\n$ export GEMINI_API_KEY=AIza...\n$ myapp ai summarize gemini   # succeeds","handlingStrategy":"validation","validationCode":"fn ensure_gemini_key() -> Result<(), String> {\n    let ok = |v: &str| std::env::var(v).map(|k| !k.trim().is_empty()).unwrap_or(false);\n    if ok(\"GEMINI_API_KEY\") || ok(\"GOOGLE_API_KEY\") { Ok(()) }\n    else { Err(\"set GEMINI_API_KEY or GOOGLE_API_KEY before running\".into()) }\n}\nensure_gemini_key()?;","typeGuard":null,"tryCatchPattern":"match summarize_with_gemini(ctx) {\n    Err(e) if e.to_string().contains(\"Missing GEMINI_API_KEY\") => {\n        eprintln!(\"{e}; export GEMINI_API_KEY or add it to your personal env file\");\n    }\n    Err(e) => return Err(e),\n    Ok(s) => println!(\"{s}\"),\n}","preventionTips":["Add GEMINI_API_KEY to your personal env file once so every shell/session has it","In CI, wire the secret explicitly into the job environment; never rely on rc files there","Guard against whitespace-only values: `export GEMINI_API_KEY=\"$(cat key.txt | tr -d '[:space:]')\"`","Verify non-emptiness with `echo ${GEMINI_API_KEY:+set}` before launching long-running commands"],"tags":["missing-env-var","api-key","gemini","configuration"],"backgroundTag":"missing-env-var","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}