google-gemini / google-gemini/gemini-cli
Load-order race condition prevents .env variables from being resolved in settings
- Dominant language
- TypeScript
- Stars
- 107k
- Forks
- 14.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 45
Description
### What happened?
During CLI startup, settings files (system, user, workspace) are parsed, immediately expanded against `process.env`, and validated in a single monolithic step inside the `load` helper. However, local `.env` files (which populate `process.env` with workspace-specific variables) are only loaded *after* this settings expansion and validation step has already finished.
Because of this load-order race condition, any settings mapped to environment variables defined only in `.env` (such as `$GITHUB_MCP_PAT`) remain unexpanded when setting up Model-Context Protocol (MCP) servers.
#### **Example Scenario**
```env
GITHUB_MCP_PAT=ghp_secretTokenHere
```
Because `.env` was loaded *after* settings resolution, `$GITHUB_MCP_PAT` remained unexpanded:
```json
{
"mcpServers": {
"github": {
"httpUrl": "https://api.githubcopilot.com/mcp/",
"headers": {
"Authorization": "Bearer $GITHUB_MCP_PAT"
}
}
}
}
```
When the CLI attempts to connect to the MCP server, it literally sends `"Bearer $GITHUB_MCP_PAT"` in the HTTP headers, resulting in a **`400 Bad Format Authorization Header`** or other auth failures.
### What did you expect to happen?
Variables from local `.env` files should be loaded and injected into `process.env` *before* the CLI attempts to expand environment variable placeholders in the settings, allowing configurations to successfully resolve local environment variable values and connect to services correctly.
```env
GITHUB_MCP_PAT=ghp_secretTokenHere
```
```json
{
"mcpServers": {
"github": {
"httpUrl": "https://api.githubcopilot.com/mcp/",
"headers": {
"Authorization": "Bearer ghp_secretTokenHere"
}
}
}
}
```
### Client information
Client Information
Run `gemini` to enter the interactive CLI, then run the `/about` command.
```console
> /about
│ CLI Version 0.53.0 │
│ Git Commit decc0b46c │
│ Model Auto │
│ Sandbox no sandbox │
│ OS darwin │
│ Auth Method Signed in with Google (REDACTED) │
│ Tier Gemini Code Assist Standard │
│ GCP Project gemini-billing-****** │
│ IDE Client VS Code
```
### Login information
Google Cloud API Key
### Anything else we need to know?
This is resolved by decoupling raw settings parsing from variable resolution and validation, creating a multi-stage pipeline:
1. **Parse Raw Files:** Read files from disk and parse raw, unexpanded configurations.
2. **Initialize Environment:** Run `loadEnvironment` to inject `.env` variables into `process.env`.
3. **Resolve & Validate:** Expand placeholder variables (including `.env`) and validate the final configuration against schemas.
This fix and accompanying unit/integration tests have been submitted in PR #28597.
Contributor guide
Research direction
Start with the CLI `load` helper and the `loadEnvironment` step described in the issue. Trace how raw settings are parsed, environment variables are loaded, and settings are resolved and validated. Compare the submitted unit and integration tests, and consider the work done when `.env` values expand correctly in settings such as MCP headers.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100