Skip to content

[Bug] Unhandled crash in Provider.list when Cloudflare environment variables exist without CLOUDFLARE_API_TOKEN #42739

Description

@mindofcharles

Description

Description

When launching OpenCode (opencode), the TUI interface fails to start and immediately crashes with the following generic error:

Error: Unexpected server error. Check server logs for details.
    at <anonymous> (/$bunfs/root/chunk-3mfps532.js:8:7615)
    at processTicksAndRejections (native:7:39)

No relevant logs are written to ~/.local/share/opencode/log/, making troubleshooting difficult in default mode.

Root Cause

Running the server directly via opencode serve --print-logs --log-level DEBUG --pure exposed the underlying unhandled exception:

Error: CLOUDFLARE_API_TOKEN (or CF_AIG_TOKEN) is required for Cloudflare AI Gateway. Set it via environment variable or run `opencode auth cloudflare-ai-gateway`.
    at <anonymous> (/$bunfs/root/chunk-dfdqwmkx.js:439:73202)
    at Provider.list (/$bunfs/root/chunk-wrjsh8a1.js:4:1338)
    at Provider.list (definition) (/$bunfs/root/chunk-dfdqwmkx.js:439:87629)
    at ProviderHttpApi.list (/$bunfs/root/chunk-wrjsh8a1.js:2:84381)
    at ProviderHttpApi.list (definition) (/$bunfs/root/chunk-wrjsh8a1.js:4:1057)
    at Server.listen (/$bunfs/root/chunk-wrjsh8a1.js:6:15456)
    at Server.listen (definition) (/$bunfs/root/chunk-wrjsh8a1.js:6:15579)
  • Trigger: OpenCode automatically parses system environment variables (e.g., CLOUDFLARE_ACCOUNT_ID in ~/.zshrc).
  • Bug: During Provider.list initialization, if a partial Cloudflare configuration is detected without a corresponding CLOUDFLARE_API_TOKEN (or CF_AIG_TOKEN), the backend throws an unhandled fatal error instead of skipping or gracefully disabling the provider. This completely breaks the local server and prevents OpenCode from starting.

Expected Behavior

  1. Graceful Fallback: If required credentials for a provider are missing, OpenCode should ignore, skip, or mark that specific provider as inactive rather than terminating the entire server process.
  2. Proper Error Logging: Fatal startup errors should be properly captured and written to the log directory so users can diagnose the failure without running internal headless debugging flags.

I believe OpenCode should not automatically read or infer system environment variables without the user's explicit consent or specification.


Issue #33780 was also caused by this problem.

Plugins

NO

OpenCode version

1.18.18

Steps to reproduce

  1. Set a partial Cloudflare environment variable in your shell profile (e.g., export CLOUDFLARE_ACCOUNT_ID="your_account_id") without setting CLOUDFLARE_API_TOKEN.
  2. Run opencode in the terminal.
  3. Observe the startup crash with Unexpected server error.

Screenshot and/or share link

NO

Operating System

macOS 26.6.1 (Darwin arm64)

Terminal

macOS Terminal

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