Skip to content

.env files loaded without warning #2033

Closed

Description

@pbjorklund

When GH_TOKEN is set as an environment variable, it conflicts with gh CLI authentication when running commands through opencode's bash tool.

Opencode uses Bun runtime (via bun build --compile) which automatically loads .env files from the working directory. Variables like GH_TOKEN in .env get inherited by child processes, causing gh CLI authentication conflicts.

Should perhaps warn user when using .env vars that was perhaps not meant to be loaded by opencode.

Activity

  1. changed the title [-]GH_TOKEN environment variable conflicts with gh CLI authentication[/-] [+].env files loaded without warning[/+] on Aug 18, 2025
  2. rekram1-node commented on Aug 18, 2025

    @rekram1-node
    Contributor

    what kind of warning would you expect to see? I believe this is mentioned in docs but we could be more explicit perhaps?

  3. pbjorklund commented on Aug 18, 2025

    @pbjorklund
    Author

    Not sure, left the issue here as a fyi for the project and as a reference for if I google it again in the future. I did not expect opencode to be autoloading .env as default behavior.

    I see it's documented under the login section https://opencode.ai/docs/cli/#login and providers https://opencode.ai/docs/providers/#amazon-bedrock / https://opencode.ai/docs/providers/#azure-openai, perhaps could be under config main heading?

    And while digging I found a solution to my problem via this plugin https://opencode.ai/docs/plugins/#env-protection

  4. rekram1-node commented on Aug 18, 2025

    @rekram1-node
    Contributor

    in regards to:

    And while digging I found a solution to my problem via this plugin

    ^ that plugin example was for the read tool specifically (that the agent invokes), but your issue seemed to be about the opencode binary loading all the env vars from .env, are you having a separate problem with the agent?

  5. pbjorklund commented on Aug 18, 2025

    @pbjorklund
    Author

    Yes I just noticed it didn't really do what I wanted.

    Image

    It does that but still sources .env. Spoke to soon.

  6. rekram1-node commented on Aug 18, 2025

    @rekram1-node
    Contributor

    gotcha, so to address this best for you should I document this behavior better? or should we have some way of you overriding the variable or something?

    Just wanna help address this best not sure what makes most sense. Curious to get your take

  7. aspiers commented on Aug 18, 2025

    @aspiers
    Contributor

    Is this different to #539? Sorry if I'm missing something.

  8. rekram1-node commented on Aug 18, 2025

    @rekram1-node
    Contributor

    yes @aspiers different because this is about the .env being loaded into process.env vs the other one is about LLM reading .env file

  9. aspiers commented on Aug 18, 2025

    @aspiers
    Contributor

    I see. IMHO opencode should never read .env, except perhaps if there is some configuration option the user chooses to explicitly enable to allow it. My rationale is that .env is a widely accepted place for applications to put their secrets. So if opencode is automatically loading that into process.env, it converts the whole of opencode into a huge attack surface through which those secrets could be leaked.

    If opencode wants to support configuration based on environment variables, I think it should load them from a separate file such as .env.opencode or .config/opencode/env. Separation of concerns is important for building a strong security model.

  10. aspiers commented on Aug 18, 2025

    @aspiers
    Contributor

    Also at a glance it seems possible to override bun's default behaviour:

    https://bun.com/docs/runtime/env#manually-specifying-env-files

  11. nikhilmaddirala commented on Aug 19, 2025

    @nikhilmaddirala

    I haven't been working on any opencode projects that have api keys in env files, but I totally would not have expected this behavior if I hadn't read this thread. This is typically not the intended purpose of an env file in a project. Perhaps you could change this to read from '.opencode/.env' instead of '.env'.

  12. rekram1-node commented on Aug 19, 2025

    @rekram1-node
    Contributor

    I will mention this to team

  13. th1m0 commented on Sep 1, 2025

    @th1m0

    This is also causing other issues not just GH CLI issues. I have a development env (.env) and a testing env (.env.testing) for a laravel project and it should automatically load the testing env when running tests. However when I run the tests in opencode (not even having to use the agent I can just run it using the ! <cmd>. it will always force use the development env (because that is in .env?). This means when the agent is running my tests it will just clear my local db always... running this outside of opencode it does just work normally.

  14. dwarfi09 commented on Sep 6, 2025

    @dwarfi09

    I haven't configured openrouter for opencode.
    Nevertheless, I can access the openrouter models in opencode in some projects. It took me some time to realize that this is because I stored openrouter keys in the projects' .env files (OPENROUTER_API_KEY). However, the keys are stored for the projects (applications), not for opencode...

  15. chnln commented on Sep 8, 2025

    @chnln

    I haven't configured openrouter for opencode.我还没有为 opencode 配置 openrouter。 Nevertheless, I can access the openrouter models in opencode in some projects. It took me some time to realize that this is because I stored openrouter keys in the projects' .env files (OPENROUTER_API_KEY). However, the keys are stored for the projects (applications), not for opencode...尽管如此,我仍然可以在某些项目中通过 OpenCode 访问 OpenRouter 模型。我花了一段时间才意识到,这是因为我将 OpenRouter 密钥存储在了项目的 .env 文件(OPENROUTER_API_KEY)中。然而,这些密钥是为项目(应用程序)存储的,而不是为 OpenCode 存储的……

    same for my openai api ...

  16. luisrudge commented on Sep 16, 2025

    @luisrudge

    I got hit by this today when I signed out of my personal Anthropic account, signed into
    OpenCode Zen for testing, then opened OpenCode in my employer's project folder containing
    a .env file with Anthropic API keys.

    OpenCode defaulted to using an anthropic/xxxx model
    and silently consumed my employer's API credits from the .env file without any warning. I only noticed when the account balance hit $0. This automatic .env loading creates a
    serious risk of unknowingly burning through unintended API credits.

    IMO this is pretty serious and shouldn't be taken this lightly. Reading .env file by default makes no sense for a tool like this cc @thdxr @rekram1-node

  17. rekram1-node commented on Sep 17, 2025

    @rekram1-node
    Contributor

    Sorry to hear that luis :/ I will bring this up to Dax

  18. thdxr commented on Sep 17, 2025

    @thdxr
    Member

    damn sorry about this - this is default behavior in bun, we just pushed a change to disable it

  19. aspiers commented on Sep 17, 2025

    @aspiers
    Contributor

    Hey @thdxr thanks a lot, that sounds great!

    Btw it would be really helpful in future if you could reference the specific commit or PR that fixes the issue so that interested parties can learn the nature of the fix. As you probably already know, if the relevant commit or PR contains "Fixes #2033" then GitHub takes care of this automatically, which is a really handy time saver for establishing bi-directional links.

  20. rekram1-node commented on Sep 17, 2025

    @rekram1-node
    Contributor

    Here is commit: ff1f4d6

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions