The Gemini CLI reading the project .env breaks cc-switch - CC Switch Fix
Quick fix
Wrap the gemini command in PowerShell to load the ~/.gemini/.env environment variables by hand.
Symptom
Section titled “Symptom”gemini-cli 在项目目录下存在 .env 导致 ccswitch 失效A .env in the project directory breaks ccswitch for gemini-cli
cc-switch writes the Gemini CLI configuration into ~/.gemini/.env in the user home directory. But the Gemini CLI variable loading order ranks that path lower: it reads the .env in the current working directory (the project root) first. When a Python project directory has a local .env, the Gemini CLI loads it and may override or ignore the global configuration, so the API Key and other variables cc-switch injects never apply.
This is a behavioural mechanism of the Gemini CLI client, not a cc-switch bug. Since the load order is fixed, cc-switch cannot change the internal CLI logic. The only answer is to export the environment variables from ~/.gemini/.env forcibly before calling gemini, through a custom function or alias, so they hold the highest priority in the current shell session.
Open your PowerShell profile ($PROFILE) and add the following function to intercept the gemini command. It parses and exports the variables from ~/.gemini/.env first, then calls the original gemini program.
# $PROFILE (PowerShell Profile)
Affected Versions
Section titled “Affected Versions”Source Issues
Section titled “Source Issues”This page is distilled from 2 real issues
- Why does removing the .env in the project directory make it work?
- Because with the project .env gone, the Gemini CLI cannot find a higher-priority local config file and falls back to ~/.gemini/.env or the system environment variables, so the cc-switch configuration applies. It is not the recommended approach, though — a project usually needs its own .env for local development configuration.
- Does this fix work in CMD or Git Bash?
- The snippet above is written for PowerShell. On CMD or Git Bash (pwsh7), see the bash shim scripts other users provide in Issue #423 — the principle is the same: export the environment variables by hand before running gemini.
- Do I still need to set GOOGLE_GEMINI_BASE_URL by hand?
- No. As long as GOOGLE_GEMINI_BASE_URL is defined correctly in ~/.gemini/.env and exported successfully by the shim function above, it applies automatically. If it still does not work, check the .env file format (no stray spaces or interfering comments).
Related problems
Section titled “Related problems”This is an unofficial community wiki with no affiliation to the cc-switch authors or the project itself. Its content is compiled from the project's public GitHub issues. This site distributes no software.