Skip to content

Add custom env var support to OpenCode integration#363

Open
Destreyf wants to merge 2 commits intocloudflare:mainfrom
Destreyf:feat/opencode-custom-env
Open

Add custom env var support to OpenCode integration#363
Destreyf wants to merge 2 commits intocloudflare:mainfrom
Destreyf:feat/opencode-custom-env

Conversation

@Destreyf
Copy link

@Destreyf Destreyf commented Jan 25, 2026

Summary

Adds an optional env parameter to OpencodeOptions that allows passing custom environment variables to the OpenCode process.

Use cases:

  • OTEL telemetry (OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_PROTOCOL)
  • W3C distributed tracing (TRACEPARENT)
  • Any other custom env vars users need

Example Usage

const { client, server } = await createOpencode(sandbox, {
  config: myConfig,
  env: {
    TRACEPARENT: "00-abc123-def456-01",
    OTEL_EXPORTER_OTLP_ENDPOINT: "http://127.0.0.1:4318",
  },
});

Custom env vars are merged with config-extracted variables (like API keys) and can override them if needed.

Changes

  • Added env?: Record<string, string> to OpencodeOptions in types.ts
  • Threaded env through ensureOpencodeServer() and startOpencodeServer()
  • Added merge logic so custom env vars override config-extracted ones
  • Updated JSDoc examples to show the new option
  • Added example helper in examples/opencode/src/index.ts showing trace context propagation
  • Added documentation in examples/opencode/README.md for the new feature
  • Added unit tests for custom env vars and override behavior

Testing

  • All 24 unit tests pass (22 existing + 2 new)
  • TypeScript compilation passes
  • Biome linting passes

Enables passing additional environment variables to the OpenCode
process via the new `env` option in OpencodeOptions. Useful for
OTEL telemetry, W3C trace context propagation, and other custom needs.
@changeset-bot
Copy link

changeset-bot bot commented Jan 25, 2026

🦋 Changeset detected

Latest commit: dbe3390

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/sandbox Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

1 participant