Skip to content

OpenCode crashes when no permissions to read .env in project root #8225

Closed as not planned

Description

@robbash

Description

Accepting the fact that one can get around OpenCode's permission settings using CLI commands, I'm running it in a sandbox using Apple's sandbox-exec. (See #2242)

Narrowing down the rules to prevent access to the .env* files by sandbox-exec, I found that a .env file in the project root results in an unexpected error.

DOMException {
  line: 236100,
  column: 29,
  sourceURL: "/$bunfs/root/src/index.js",
  stack: "postMessage@[native code]\n@/$bunfs/root/src/index.js:236100:29\nPromise@\ncall@/$bunfs/root/src/index.js:236098:27\n@/$bunfs/root/src/index.js:236212:23\nprocessTicksAndRejections@",
  code: 11,
  name: "InvalidStateError",
  message: "Worker has been terminated",
  ...
}

For simpler replication, I tried to deny access via file system permissions. Even though cat is not allowed to show the content when executed, OpenCode did not run into the error.

I understand it's quite an edge case, but since it's a sensitive area and hoping handling errors when reading the .env in the project root is simple enough, a fix would be appreciated! Alternatively, a config flag to avoid loading .env files in the project folder would also be great.

Thanks, for this, and the whole OpenCode tool - it's awesome!

Plugins

Standard (opencode-copilot-auth@0.0.11, opencode-anthropic-auth@0.0.8)

OpenCode version

1.1.10

Steps to reproduce

  1. On macOS, set up sandbox-exec ie. by using this config file.
  2. Create an empty folder, and change into that folder
  3. Create a .env file (can be empty)
  4. Start sandboxed OpenCode inside that "project folder" with TMPDIR=/tmp sandbox-exec -f ~/.opencode-sandbox.sb opencode (where ~/.opencode-sandbox.sb is the sandbox config)
  5. The issue should appear immediately

Move the .env into a sub-folder and things should be fine.

Screenshot and/or share link

No response

Operating System

macOS Tahoe 26.1

Terminal

kitty

Activity

  1. github-actions commented on Jan 13, 2026

    @github-actions
    Contributor

    This issue appears to be related to .env file permission handling during startup. While there are several existing issues about .env file access (#4969, #5484) and sandboxing (#2242), this specific crash (Worker termination with InvalidStateError) when permission is denied appears to be unique.

    The crash occurs specifically when:

    • A .env file exists in the project root
    • OpenCode lacks read permissions (e.g., via sandbox-exec)
    • OpenCode attempts to access this file during initialization

    The error should be handled gracefully rather than crashing the Worker process. Consider checking related issues for context:

    A fix could include:

    1. Better error handling when .env files cannot be read due to permissions
    2. A configuration option to skip loading .env files from the project root
    3. Clear error messages instead of Worker termination
  2. nickturrietta commented on Feb 18, 2026

    @nickturrietta

    I'm experiencing a related issue.

    When I run opencode in the CLI on Windows 11 Ubuntu WSL1, it won't start in an existing project. This is the error I see:

    opencode --print-logs
    INFO  2026-02-18T04:47:53 +251ms service=default version=1.2.6 args=["--print-logs"] opencode
    INFO  2026-02-18T04:47:53 +3ms service=config path=/home/nick/.config/opencode/config.json loading
    INFO  2026-02-18T04:47:53 +2ms service=config path=/home/nick/.config/opencode/opencode.json loading
    INFO  2026-02-18T04:47:53 +3ms service=config path=/home/nick/.config/opencode/opencode.jsonc loading
    ERROR 2026-02-18T04:47:53 +27ms service=default e=Worker has been terminated rejection
    ERROR 2026-02-18T04:47:53 +0ms service=default InvalidStateError: Worker has been terminated
    

    The temporary solution I have found thanks to @robbash is to remove the .env file in the project root. When I do this, it starts successfully, but obviously this isn't obvious or practical.

  3. nickturrietta commented on Mar 13, 2026

    @nickturrietta

    I'm experiencing a related issue.

    When I run opencode in the CLI on Windows 11 Ubuntu WSL1, it won't start in an existing project. This is the error I see:

    opencode --print-logs
    INFO  2026-02-18T04:47:53 +251ms service=default version=1.2.6 args=["--print-logs"] opencode
    INFO  2026-02-18T04:47:53 +3ms service=config path=/home/nick/.config/opencode/config.json loading
    INFO  2026-02-18T04:47:53 +2ms service=config path=/home/nick/.config/opencode/opencode.json loading
    INFO  2026-02-18T04:47:53 +3ms service=config path=/home/nick/.config/opencode/opencode.jsonc loading
    ERROR 2026-02-18T04:47:53 +27ms service=default e=Worker has been terminated rejection
    ERROR 2026-02-18T04:47:53 +0ms service=default InvalidStateError: Worker has been terminated
    

    The temporary solution I have found thanks to @robbash is to remove the .env file in the project root. When I do this, it starts successfully, but obviously this isn't obvious or practical.

    Solved by upgrading to v1.2.26

  4. removed
    bugSomething isn't working
    on May 3, 2026
  5. github-actions commented on Jul 6, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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