Skip to content

Env.all() caches process.env snapshot, preventing detection of env vars set after initialization #12698

@jerome-benoit

Description

@jerome-benoit

Description

Env.all() in packages/opencode/src/env/index.ts creates a frozen snapshot of process.env on first invocation via Instance.state():

const state = Instance.state(() => {
  return { ...process.env } as Record<string, string | undefined>
})

This snapshot is never refreshed. When Provider.list() calls Env.all() (line 834 in provider/provider.ts) to detect env-based providers, it uses this cached snapshot.

Consequence: If any environment variable (e.g., AICORE_SERVICE_KEY, OPENAI_API_KEY) is set after the first call to Env.all(), it will never be detected, and the provider will never appear in the connected list.

This affects plugins/extensions that set environment variables dynamically or load them asynchronously.

Root Cause

The singleton pattern was introduced in commit ee4437ff3 ("core: add provider test coverage for upcoming refactor") and the shallow copy was later added in 90f39bf67 ("core: prevent parallel test runs from contaminating environment variables") to isolate parallel tests.

While this works for test isolation, it breaks runtime behavior where env vars may be set after OpenCode initializes.

Plugins

oh-my-opencode (reported in code-yeongyu/oh-my-opencode#1635)

OpenCode version

1.1.51

Steps to reproduce

  1. Start OpenCode without AICORE_SERVICE_KEY set
  2. Set AICORE_SERVICE_KEY in the environment after OpenCode initializes
  3. Call provider.list() API
  4. Observe: sap-ai-core never appears in connected array

Screenshot and/or share link

N/A

Operating System

Linux

Terminal

N/A

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions