Skip to content

git worktrees and root-directory #2445

Closed

Description

@maiterth

I'm using git worktrees and opencode has issues identifying what the root directory of the project is.
Opencode always falls back to the git root directory, even if it should be work in in a worktree.

I'm working inside a worktree:
however, when doing any operations, opencode tries to work in the git main directory, therefore I get errors like
Error: File $HOME/Repositories/my-proj/AGENTS.md is not in the
current working directory

While I'm running opencode from $HOME/Repositories/my-proj-new-feature/AGENTS.md and the agents should actually be working in this directory not in the original git repo.

Reproducer:
Go into a git repository and run
git worktree add ../my-proj-new-feature
cd ../my-proj-new-feature' opencode`

From there try anything, such as /init
Or ask it to interact with the code.
The agent will often/always land in the
$HOME/Repositories/my-proj/ directory
instead of the
$HOME/Repositories/my-proj-new-feature/ directory

General Comments:
It makes sense that a shared AGENTS.md may be desired (e.g. compare to !2094), however identifying the root "working" directory should work first.

I tried a quick fix (https://github.com/maiterth/opencode/compare/dev...feature-test-worktree-rootdir)
But this did not do the correct thing, and I have to admit that I'm not familiar enough with the project to make an informed decision here.
This seems to be working, but I'm not sure what it may be breaking.

Thanks! and Kudos to the dev's this project is awesome and I'm happy to contribute and help fix things, if possible.

Activity

  1. rekram1-node commented on Sep 5, 2025

    @rekram1-node
    Collaborator

    Hmm I will try to reproduce this

  2. maiterth commented on Sep 5, 2025

    @maiterth
    Author

    Ok, I may have a strange way of using worktrees:
    In fact I do the following: Inside my Repos directory
    ~/Repositories/
    I create a project directory and checkout main or dev. e.g.:

    > mkdir opencode
    > cd opencode
    > git checkout https://github.com/sst/opencode dev
    > cd dev
    

    From this i create my worktrees:

    > git worktree add ../feature-x
    > cd ../feature-x
    

    This may be a bit unconventional but keeps my Repositories clean.
    When checking the --git-common-dir I get this:

    > git rev-parse --path-format=absolute --git-common-dir
    $HOME/Repositories/opencode/dev/.git
    > git rev-parse --path-format=absolute --git-dir
    $HOME/Repositories/opencode/dev/.git/worktrees/feature-x
    

    and

    > git rev-parse --show-toplevel
    $HOME/Repositories/opencode/feature-x
    

    In this case opencode often moves up to here: $HOME/Repositories/opencode or lands in $HOME/Repositories/opencode/dev instead of my $HOME/Repositories/opencode/feature-x.

  3. rekram1-node commented on Sep 5, 2025

    @rekram1-node
    Collaborator

    I do mine in the same way P much but I will look into it

    git worktree list
    /Users/aidencline/development/opencode
    /Users/aidencline/development/opencode-dev

  4. maiterth commented on Sep 10, 2025

    @maiterth
    Author

    I checked out the new code changes in !2360 , and it initially seemed like this fixed my issue, however:

    /Init seemed fine, reading files seemed fine, but now with the project, worktree and directory, we at times still land in the most common shared directory.

    Fun sidestory:
    I asked to find a function signature, and the agent ran off grepping in the most common directory:
    To test if the changes worked, I asked "Which file is function_x in?" (after having interacted based on that function and file before)

    The LLM in opencode ran off into the directory one up and grepped through my venv. Reading 90K of context and LICENSE files, forgot my prompt and gave a generic response "And this is how you run TensorFlow"

           $HOME/Repositories/my-project/venv-python3.13/lib/python3.13/site-packages/boto3-1.40.26.dist-info/LICENSE:    ┃
      ┃    Line 70:       copyright license to reproduce, prepare Derivative Works of,                                         ┃
      ┃                                                                                                                        ┃
      ┃  $HOME/Repositories/my-project/venv-python3.13/lib/python3.13/site-packages/s3transfer-0.13.1.dist-             ┃
      ┃  info/LICENSE.txt:                                                                                                     ┃
      ┃                                                                                                                        ┃
    
    
         Sure! Here’s a quick guide to using TensorFlow.js (tfjs) for classification and inference with a pre-trained model.
    
         1. Set up the environment
    
         • Add the TensorFlow.js script to your HTML or install it via npm:
         <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
         or
         npm install @tensorflow/tfjs
    
    
         2. Load a pre-trained model
    
         • TensorFlow.js supports several
    

    It completely forgot the prompt or codebase and stating "That’s it! With these steps you can load any pre-trained TensorFlow.js models" (the project is about something else, and the output is definitely not the intended one, not sure if I want to blame it on the model ;) .)

    Going back to the subject,
    These are the directories I have:

    $HOME/Repositories/my-project/main
    $HOME/Repositories/my-project/worktree-x
    While the common upper directory
    $HOME/Repositories/my-project/
    only contains the worktrees and main branch in thei respective directory, and in this case my venv. (no git or hidden files).

    The fixe in !2360 I think does the right thing with identifying project, directory and worktree, but something seems to not go right.
    The path where I expected it to run in is was worktree-x, while the common my-project directory (on up) was chosen.

    The merge in !2360 mentions every endpoint takes an optional directory parameter to operate as though opencode is running in that directory
    Do I have to set this directory as a user or am I missing something?

    I run from the worktree-x directory and
    My status line shows opencode v0.7.0 ~/Repositories/my-project/worktree-x:main
    git status shows On branch worktree-x (worktree and feature are named alike)

  5. rekram1-node commented on Sep 29, 2025

    @rekram1-node
    Collaborator

    @maiterth are you still encountering this. think it may have been fixed recently

  6. lgladysz commented on Sep 29, 2025

    @lgladysz
    Contributor

    In v0.13.5 /init command still pointing to the git main directory instead of the git worktree.

     ./                                                                                                                                                                                                                                 
    ├──  project-1/                 //main directory                                                                                                                                                                                                  
    ├──  project-1-worktree/        //worktree     
    

    After using /init in the project-1-worktree directory, I see project-1 instead of project-1-worktree:

    If there's already an AGENTS.md, improve it if it's located in /home/lgladysz/dev/project-1

  7. rekram1-node commented on Sep 29, 2025

    @rekram1-node
    Collaborator

    ty @lgladysz I will check it out

  8. rekram1-node commented on Sep 30, 2025

    @rekram1-node
    Collaborator

    I just merged a fix for this #2870

    will be in next release

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