feat: shell.env plugin hook for environment variable injection #8459

Closed
opened 2026-02-16 18:10:01 -05:00 by yindo · 1 comment
Owner

Originally created by @tylergannon on GitHub (Feb 3, 2026).

Originally assigned to: @thdxr on GitHub.

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Feature Request

Allow plugins to inject environment variables into all shell execution contexts (AI tools, shell mode, PTY terminals).

Motivation

The desire is to run a single opencode server that can operate against an arbitrary number of projects. Currently this can be done by instructing the agent to load .env files or wrap calls in utilities like doppler, but this is error-prone and adds cognitive load for the LLM.

Proposed Solution

A shell.env plugin hook that allows plugins to introduce environment variables that will be added to the env for:

  1. LLM tool calls (bash tool)
  2. Shell mode (!command)
  3. PTY terminals

Example Plugin

export const InjectEnvPlugin = async () => {
  return {
    "shell.env": async (input, output) => {
      output.env.MY_API_KEY = "secret"
      output.env.PROJECT_ROOT = input.cwd
    },
  }
}

Design Considerations

  • Secrets should not become part of opencode's application state
  • Caching behavior should be left to the plugin implementation
  • Minimal changes to existing codebase
  • Very simple interface -- plugin only receives the cwd and therefore isn't attempting to introspect deeply into the intent of the tool call, just simply returns envvars for the given cwd
Originally created by @tylergannon on GitHub (Feb 3, 2026). Originally assigned to: @thdxr on GitHub. ## Feature hasn't been suggested before. - [x] I have verified this feature I'm about to request hasn't been suggested before. ## Feature Request Allow plugins to inject environment variables into all shell execution contexts (AI tools, shell mode, PTY terminals). ## Motivation The desire is to run a single opencode server that can operate against an arbitrary number of projects. Currently this can be done by instructing the agent to load .env files or wrap calls in utilities like doppler, but this is error-prone and adds cognitive load for the LLM. ## Proposed Solution A `shell.env` plugin hook that allows plugins to introduce environment variables that will be added to the env for: 1. LLM tool calls (bash tool) 2. Shell mode (`!command`) 3. PTY terminals ## Example Plugin ```typescript export const InjectEnvPlugin = async () => { return { "shell.env": async (input, output) => { output.env.MY_API_KEY = "secret" output.env.PROJECT_ROOT = input.cwd }, } } ``` ## Design Considerations - Secrets should not become part of opencode's application state - Caching behavior should be left to the plugin implementation - Minimal changes to existing codebase - Very simple interface -- plugin only receives the cwd and therefore isn't attempting to introspect deeply into the intent of the tool call, just simply returns envvars for the given cwd
yindo closed this issue 2026-02-16 18:10:01 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Feb 3, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #11065: feat(bash): add env parameter for setting environment variables - Proposes adding an env parameter to the bash tool itself to inject environment variables
  • #9292: [FEATURE]: Expose session context to child processes via environment variables - Requests exposing session context via env vars in bash and shell commands
  • #9334: How to make OpenCode load environment variables from a file when executing commands in skills - Asks about loading environment variables for skill execution
  • #10458: [FEATURE]: Auto-load .env files in scoped directories for {env:VAR} substitution - Proposes auto-loading .env files for environment variable substitution

Your feature request takes a plugin-based approach to environment variable injection via a shell.env hook, while these issues approach it from different angles (bash tool parameters, session context exposure, .env file loading, and config substitution).

Feel free to ignore if none of these address your specific use case.

@github-actions[bot] commented on GitHub (Feb 3, 2026): This issue might be a duplicate of existing issues. Please check: - #11065: feat(bash): add env parameter for setting environment variables - Proposes adding an env parameter to the bash tool itself to inject environment variables - #9292: [FEATURE]: Expose session context to child processes via environment variables - Requests exposing session context via env vars in bash and shell commands - #9334: How to make OpenCode load environment variables from a file when executing commands in skills - Asks about loading environment variables for skill execution - #10458: [FEATURE]: Auto-load .env files in scoped directories for {env:VAR} substitution - Proposes auto-loading .env files for environment variable substitution Your feature request takes a plugin-based approach to environment variable injection via a shell.env hook, while these issues approach it from different angles (bash tool parameters, session context exposure, .env file loading, and config substitution). Feel free to ignore if none of these address your specific use case.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8459