Skip to content

fix(env): remove Env namespace, use direct process.env access#12822

Open
jerome-benoit wants to merge 17 commits intoanomalyco:devfrom
jerome-benoit:fix-env-caching-12698
Open

fix(env): remove Env namespace, use direct process.env access#12822
jerome-benoit wants to merge 17 commits intoanomalyco:devfrom
jerome-benoit:fix-env-caching-12698

Conversation

@jerome-benoit
Copy link
Contributor

@jerome-benoit jerome-benoit commented Feb 9, 2026

Summary

Removes the Env namespace entirely. Fixes #12698.

Why Remove Instead of Conditional Caching?

The suggested fix proposed conditional caching. However, the Env namespace has caused repeated issues since its introduction:

Direct bugs

Workaround required

Root cause

The API semantics are misleading: Env.set(key, value) appears to set an env var but external code (AWS SDK, child processes) never sees the change. This leaky abstraction led to mixed Env.* / process.env usage and inconsistent behavior.

Conditional caching preserves these problems. Removal eliminates them.

Changes

Area Change
src/env/ Deleted
src/**/*.ts Env.*process.env[key]
test/preload.ts Env snapshot/restore per test (beforeEach/afterEach)
test/env/env.test.ts Deleted

Verification

  • bun test — 980 pass, 5 skip, 0 fail
  • bun run typecheck — clean

nix/scripts/*.ts and patches/* are included in node_modules derivation.
Changes to these files invalidate hashes but didn't trigger rebuild.

Also removes non-existent install/ from node_modules.nix fileset.

Fixes anomalyco#12817
…alyco#12698)

- Add conditional caching to Env namespace: production mode uses direct process.env access, test mode uses snapshot isolation
- Migrate 15 source files from direct process.env to Env.get/set/remove/all API
- CLI: 9 files in cmd/ directory (acp, auth, github, uninstall, tui modules)
- Core: 4 files (config, ide, lsp/server, share modules)
- Enables test mode to isolate env variables per test without affecting global state
- All migrations follow constraint: no spreads, no try/catch, no utility functions
Copilot AI review requested due to automatic review settings February 9, 2026 13:35
@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2026

Hey! Your PR title Migrate all process.env to Env namespace for consistent caching (#12698) doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2026

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR completes the environment caching refactor by migrating remaining direct process.env usages to the Env namespace, aiming to keep production behavior “live” while isolating env state during tests.

Changes:

  • Updated Env implementation to bypass cached state in production mode and use cached per-instance state in test mode.
  • Migrated multiple CLI/core modules from direct process.env reads/writes to Env.get/set.
  • Updated Nix hashing workflow triggers and adjusted nix/node_modules.nix inputs.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/opencode/src/env/index.ts Adds production/test mode branching for env reads/writes and Env.all() behavior.
packages/opencode/src/share/share.ts Uses Env.get for API URL and share-disable flag.
packages/opencode/src/share/share-next.ts Uses Env.get for share-disable flag.
packages/opencode/src/lsp/server.ts Uses Env.get("PATH")/Env.get("VIRTUAL_ENV") for LSP server discovery/spawn config.
packages/opencode/src/ide/index.ts Uses Env.get for IDE detection and caller checks.
packages/opencode/src/config/config.ts Uses Env.set for auth propagation and Env.get for {env:...} interpolation.
packages/opencode/src/cli/cmd/uninstall.ts Uses Env.get for shell/xdg config detection.
packages/opencode/src/cli/cmd/tui/util/editor.ts Uses Env.get for editor detection.
packages/opencode/src/cli/cmd/tui/util/clipboard.ts Uses Env.get for OSC52 passthrough + Wayland detection.
packages/opencode/src/cli/cmd/tui/thread.ts Uses Env.get("PWD") for cwd resolution.
packages/opencode/src/cli/cmd/tui/context/route.tsx Uses Env.get to initialize route from env.
packages/opencode/src/cli/cmd/tui/attach.ts Uses Env.get for server password header.
packages/opencode/src/cli/cmd/github.ts Uses Env.get for GitHub action/runtime env inputs.
packages/opencode/src/cli/cmd/auth.ts Uses Env.get when checking active provider env vars.
packages/opencode/src/cli/cmd/acp.ts Uses Env.set to set OPENCODE_CLIENT.
nix/node_modules.nix Removes ../install from the fileset inputs.
.github/workflows/nix-hashes.yml Expands workflow trigger paths to include Nix/scripts/patches changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Migrate process.env.KEY -> Env.get("KEY") in 5 files
- Migrate process.env.KEY = value -> Env.set("KEY", value) in 3 files
- Remove 2 obsolete TODO comments from provider.ts
- Add Env import to util/proxied.ts and index.ts

Files modified:
- config/config.ts (2 reads)
- index.ts (2 writes + import)
- provider/provider.ts (4 reads + 2 writes + removed 2 TODOs)
- shell/shell.ts (3 reads)
- util/proxied.ts (4 reads + import)

All process.env dot notation now migrated (except documented exclusions).
@jerome-benoit jerome-benoit changed the title Migrate all process.env to Env namespace for consistent caching (#12698) fix(opencode): bypass Env cache in production to detect late-set env vars Feb 9, 2026
@jerome-benoit jerome-benoit force-pushed the fix-env-caching-12698 branch 5 times, most recently from a0701b7 to 3dcc92b Compare February 9, 2026 14:39
jerome-benoit and others added 4 commits February 14, 2026 12:58
Remove the Env namespace abstraction that was caching environment
variables and causing issues with AWS SDK credential provider which
writes to process.env dynamically.

Changes:
- Empty src/env/index.ts (Env namespace removed)
- Replace all Env.get/set/remove/all calls with direct process.env access
- Add test isolation via beforeEach/afterEach env snapshot in preload.ts
- Delete env.test.ts (tested removed namespace)

This ensures environment variable changes made by external libraries
(like AWS SDK's credential provider chain) are immediately visible
to the rest of the application.

Closes anomalyco#12698
@jerome-benoit jerome-benoit changed the title fix(opencode): bypass Env cache in production to detect late-set env vars fix(env): remove Env namespace, use direct process.env access Feb 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Env.all() caches process.env snapshot, preventing detection of env vars set after initialization

1 participant