Skip to content

fix(cli): Resolve {env:VAR} and {file:path} patterns in OPENCODE_CONFIG_CONTENT#128

Merged
kdcokenny merged 3 commits intokdcokenny:mainfrom
beppe2880:fix/env-var-resolution
Feb 11, 2026
Merged

fix(cli): Resolve {env:VAR} and {file:path} patterns in OPENCODE_CONFIG_CONTENT#128
kdcokenny merged 3 commits intokdcokenny:mainfrom
beppe2880:fix/env-var-resolution

Conversation

@beppe2880
Copy link
Contributor

@beppe2880 beppe2880 commented Feb 9, 2026

Problem

OCX passes {env:VAR} patterns as literal strings in OPENCODE_CONFIG_CONTENT.
OpenCode only resolves these patterns when reading files from disk (load()),
not when receiving config via OPENCODE_CONFIG_CONTENT (JSON.parse only).

This breaks any MCP server using {env:VAR} in its environment or headers fields
when launched through ocx oc.

Fix

Resolve {env:VAR} patterns on the serialized JSON string in buildOpenCodeEnv,
matching OpenCode's own resolution approach (string replace before parse).


Summary by cubic

Resolve {env:VAR} and {file:path} placeholders in OPENCODE_CONFIG_CONTENT before parsing so MCP servers using env vars or file-based secrets work when launched via ocx oc.

  • Bug Fixes
    • Added resolve-config utility (resolveEnvVars, resolveFilePatterns, resolveConfigPatterns); exported via utils.
    • Resolve patterns in runOpencode and pass the pre-resolved serialized JSON as configContent to buildOpenCodeEnv (replaces mergedConfig param).
    • Updated tests for env/file patterns, JSON-safe escaping, path resolution, ~ expansion, and the buildOpenCodeEnv configContent signature.

Written for commit e4238f2. Summary will update on new commits.

@beppe2880 beppe2880 force-pushed the fix/env-var-resolution branch from 9d1f010 to ca9d68e Compare February 9, 2026 12:02
@beppe2880 beppe2880 changed the title fix(cli): resolve {env:VAR} patterns in OPENCODE_CONFIG_CONTENT fix(cli): Resolve env var patterns in OPENCODE_CONFIG_CONTENT Feb 9, 2026
@beppe2880 beppe2880 marked this pull request as draft February 9, 2026 12:04
@beppe2880 beppe2880 marked this pull request as ready for review February 9, 2026 12:04
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files


Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Ask questions if you need clarification on any suggestion

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

@kdcokenny
Copy link
Owner

Ah very interesting! Do you know if this is the same for their other patterns like file:?

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 76.74419% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 48.34%. Comparing base (9e72ac9) to head (e4238f2).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
packages/cli/src/commands/opencode.ts 25.00% 6 Missing ⚠️
packages/cli/src/utils/resolve-config.ts 88.23% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #128      +/-   ##
==========================================
+ Coverage   48.05%   48.34%   +0.28%     
==========================================
  Files          46       47       +1     
  Lines        4903     4944      +41     
==========================================
+ Hits         2356     2390      +34     
- Misses       2547     2554       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@beppe2880
Copy link
Contributor Author

beppe2880 commented Feb 9, 2026

Yes, {file:path} has the same bug, both patterns are resolved in load() which OPENCODE_CONFIG_CONTENT bypasses entirely. I was already planning a follow-up PR for it. Would you prefer that as a separate PR or should I add it to this one?

@kdcokenny
Copy link
Owner

Yes, {file:path} has the same bug, both patterns are resolved in load() which OPENCODE_CONFIG_CONTENT bypasses entirely. I was already planning a follow-up PR for it. Would you prefer that as a separate PR or should I add it to this one?

It would be great if you did those in this PR! I would greatly appreciate it.

…IG_CONTENT

Both patterns are resolved in OpenCode's load() which OPENCODE_CONFIG_CONTENT
bypasses entirely. Resolve them in OCX before passing the serialized config.

- Rename resolve-env to resolve-config, add resolveFilePatterns and resolveConfigPatterns
- buildOpenCodeEnv accepts pre-resolved configContent string (stays sync)
- Pattern resolution happens in runOpencode caller before spawning
@beppe2880 beppe2880 changed the title fix(cli): Resolve env var patterns in OPENCODE_CONFIG_CONTENT fix(cli): resolve {env:VAR} and {file:path} patterns in OPENCODE_CONFIG_CONTENT Feb 9, 2026
@beppe2880 beppe2880 changed the title fix(cli): resolve {env:VAR} and {file:path} patterns in OPENCODE_CONFIG_CONTENT fix(cli): Resolve {env:VAR} and {file:path} patterns in OPENCODE_CONFIG_CONTENT Feb 9, 2026
@beppe2880
Copy link
Contributor Author

beppe2880 commented Feb 9, 2026

Hey! Just pushed {file:path} support to this PR. Same approach as {env:VAR} — patterns are resolved on the serialized JSON string before passing it to OPENCODE_CONFIG_CONTENT. Let me know if it looks good or if there's anything else I can do!

@beppe2880 beppe2880 force-pushed the fix/env-var-resolution branch 2 times, most recently from b594efa to 9cf782c Compare February 9, 2026 23:52
@beppe2880
Copy link
Contributor Author

@kdcokenny ready for review when you get a chance!

@kdcokenny kdcokenny merged commit ed74da6 into kdcokenny:main Feb 11, 2026
8 checks passed
@kdcokenny
Copy link
Owner

Thank you!

@kdcokenny
Copy link
Owner

Looks like the core issue has been fixed anomalyco/opencode#13384

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants