config: warn when {env:VAR} in provider options resolves empty
Summary
{env:VAR} templates in provider.<id>.options.apiKey resolve to an empty string when the variable is not present in the background service's environment. No warning names the config field or the missing variable, so a missing service env var surfaces only as a generic downstream auth failure. Proposal: emit an actionable warning at config load and, optionally, mark the provider as misconfigured instead of sending unauthenticated requests. Related: #33853, #57393.
Environment
- opencode version: 2.3.0
- OS: Linux murilo-System-Product-Name 7.0.0-43-generic #43-Ubuntu SMP PREEMPT_DYNAMIC Fri Oct 23 13:25:25 UTC 2026 x86_64 GNU/Linux
- Terminal: TERM=xterm-256color, COLORTERM=truecolor (TERM_PROGRAM unset)
- Shell: /bin/zsh
- Install/channel: latest, installed at
~/.opencode/bin/opencode - Active plugins: no configured
pluginsentries; two auto-discovered local plugins under~/.config/opencode/plugins/(ai-memory-opencode2.ts,rate-limit-retry.ts). The behavior does not depend on plugins.
Reproduction
-
In the global
~/.config/opencode/opencode.json, define a custom provider whose key comes from a variable that does not exist in the service environment:{ "provider": { "probe": { "npm": "@ai-sdk/openai-compatible", "name": "Probe", "options": { "baseURL": "http://127.0.0.1:1/v1", "apiKey": "{env:OPENCODE_PROBE_MISSING_KEY}" }, "models": { "dummy": { "name": "dummy" } } } } } -
Confirm the variable is absent from the service env:
opencode service get env | jq 'has("OPENCODE_PROBE_MISSING_KEY")'→false. -
Restart so the service reloads config:
opencode service restart. -
Inspect the resolved value without printing it:
opencode api get /api/provider/probe | jq '.data.settings.apiKey | length'→0. -
Check
~/.local/share/opencode/log/opencode.log: there is no warning namingOPENCODE_PROBE_MISSING_KEY, and the provider appears withactivation: enabled.
Expected Behavior
At config load (or provider activation), a clear, actionable warning naming the field, the variable, and the remediation, for example:
WARN provider.probe.options.apiKey: {env:OPENCODE_PROBE_MISSING_KEY} resolved to
an empty string in the service environment. Set it with
'opencode service set env OPENCODE_PROBE_MISSING_KEY <value>' and restart the service.
Optionally, mark the provider as misconfigured rather than sending requests without credentials.
Actual Behavior
- The resolved
apiKeyis the empty string (len=0). - No warning in the server log; the provider is reported as active.
- Requests fail downstream with generic errors such as
AI.Error: Invalid bearer token/Invalid token, with no indication that the service environment is the cause.
Additional Context
- Live evidence from this environment on the same field: before setting the variable,
/api/provider/nightrouterreportedapiKeylength0; afteropencode service set env OPENCODE_NIGHTROUTER_API_KEY <value>plusopencode service restart, the same field reported length51. Two other providers in the same config whose variables already existed in the service env resolved normally (lengths47and36). No key values are included here — only lengths. - Root cause is already understood from #57393: in V2 the background service resolves
{env:...}from its own environment, not from the shell runningopencode run; an unset variable silently becomes""(#33853). The reporter confirmedservice set env+ restart resolves it. - This is therefore a diagnostics/UX gap, not a broken substitution: substitution works correctly once the variable exists in the service environment.
- Same class of problem: #58394, where a stored credential silently overrides
options.apiKeywith no warning about which source won. Both would benefit from one explicit "credential source resolution" log line.
Thanks for the careful write-up, and for checking only the lengths instead of the key values.
I think this is a duplicate of #58736. That issue asks for the same thing: when a provider's credential ends up empty because an {env:...} variable is unset (or a {file:...} is missing), OpenCode should show a clear error that names the provider, not just pass on a generic auth error from upstream.
For context, the empty value is how it's designed and documented right now. The config docs say: "If the environment variable is not set, it will be replaced with an empty string." In packages/core/src/config/variable.ts, an unset variable falls back to "" without logging anything, while a missing {file:...} reference does log a warning. So adding a warning for unset variables would be a change to the product, which is what #58736 asks for.
Possible duplicates and related issues, ordered by likelihood:
- #58736: same request, a clear error when a provider credential resolves to empty from an unset
{env:...}or a missing{file:...} - #57393: the same setup; the service resolved
{env:...}from its own environment, andopencode service set envplus a restart fixed it - #33853: the original report that an unset
{env:VAR}silently becomes an empty string (closed as not planned) - #58394: the related case you mentioned, a stored credential silently overriding
options.apiKey
I'm closing this issue now as a duplicate of #58736. Please add your details there, especially the proposed log line that names the field and the variable and suggests the opencode service set env fix, and the idea of marking the provider as misconfigured. If you think this is different from #58736, reply here and that will reopen this issue.