Skip to content

.opencode/ config precedence is inverted in nested directories #21307

Closed as not planned

Description

@zckman

Description

When .opencode/opencode.json (or .jsonc) exists in multiple nested directories, the parent directory wins instead of the child.
This is the opposite of how plain opencode.json files behave and opposite of the documented "local overrides global" precedence model.

Root cause:
ConfigPaths.directories uses Filesystem.up, which yields directories child-first.
The merge loop in loadInstanceState applies mergeDeep(result, next) where the later-processed entry wins.
So the last parent always overrides the child.

Plain opencode.json files (not in .opencode/) don't have this problem:
Filesystem.findUp is called with { rootFirst: true }, which reverses the list before merging.

Plugins

MCP context7, no plugins

OpenCode version

1.3.7

Steps to reproduce

  1. Create configs in .opencode/ for parent and child.
  2. Verify the resulting config values

For example:

mkdir -p /tmp/oc-bug/.opencode /tmp/oc-bug/child/.opencode

echo '{ "$schema": "https://opencode.ai/config.json", "username": "parent-wins" }' \
  > /tmp/oc-bug/.opencode/opencode.json

echo '{ "$schema": "https://opencode.ai/config.json", "username": "child-should-win" }' \
  > /tmp/oc-bug/child/.opencode/opencode.json

cd /tmp/oc-bug/child && opencode debug config | grep username

# Expected: "username": "child-should-win"
# Actual:   "username": "parent-wins"

Check without .opencode/ directories (correct behavior):

mkdir -p /tmp/oc-bug2/child

echo '{ "$schema": "https://opencode.ai/config.json", "username": "parent" }' \
  > /tmp/oc-bug2/opencode.json

echo '{ "$schema": "https://opencode.ai/config.json", "username": "child-wins" }' \
  > /tmp/oc-bug2/child/opencode.json

cd /tmp/oc-bug2/child && opencode debug config | grep username
# Correct output: "username": "child-wins"

Screenshot and/or share link

No response

Operating System

Ubuntu 24

Terminal

Konsole

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Apr 7, 2026
  2. github-actions commented on Apr 7, 2026

    @github-actions
    Contributor

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

  3. QuantumEdu commented on Apr 10, 2026

    @QuantumEdu

    Comment for Issue #21307 (.opencode/ config precedence inverted)

    This is a significant bug that affects monorepo workflows where different subdirectories need different configurations. The current behavior breaks the expected "local overrides global" model.

    Additional Impact Areas

    Beyond the reported issue, this bug affects:

    1. Monorepo subprojects:

      /monorepo/
        .opencode/opencode.json    # Base config (monorepo-wide)
        services/
          .opencode/opencode.json  # Service-specific overrides
          web/
            .opencode/opencode.json # Web-specific overrides
      
    2. Feature-specific overrides:

      /project/
        .opencode/opencode.json    # Project defaults
        features/
          auth/.opencode/opencode.json  # Auth feature config
          billing/.opencode/opencode.json  # Billing feature config
      
    3. Environment-specific configs (if using directory-based env separation):

      /project/
        dev/.opencode/opencode.json
        prod/.opencode/opencode.json
      

    Suggested Testing

    Add regression tests for:

    • Nested directory precedence (child > parent > grandparent)
    • Mixed .opencode/ and root-level configs
    • Config merge behavior with conflicting keys
    • .jsonc vs .json file handling

    Temporary Workaround

    Until fixed, users can work around this by:

    • Using root-level opencode.json instead of .opencode/opencode.json
    • Avoiding nested .opencode/ directories
    • Manually merging configs before use

    The fix is critical for projects that rely on hierarchical configuration for different parts of the codebase.

  4. assigned and unassigned on Apr 26, 2026
  5. removed
    bugSomething isn't working
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  6. 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