opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Allow explicitly changing working directory

Open garrett-hopper opened this issue 4 months ago • 26 comments

When using Claude Code, it would occasionally make sense to change directory mid-conversation. With OpenCode, I'm running into "... is not in the current working directory" errors due to starting in a child directory. Ideally I could use "!" command mode to "cd ..." into a different directory and have the OpenCode working directory changed for future messages. Or perhaps a "/cd" command could work similarly.

Additionally, having the restriction to not modifying stuff outside the working directory is great. It would be nice though to be able to disable it and allow arbitrary file writes. (E.g. "modify this user config file: " in the middle of working on a project)

garrett-hopper avatar Aug 22 '25 19:08 garrett-hopper

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

  • #1143: Requests the ability to create sessions with different root/working directories and change build/test execution directory within sessions
  • #841: Discusses locking sessions to current directory and preventing opencode from reading/writing above the starting directory
  • #1877: Proposes a root folder configuration to aggregate sessions across subdirectories with automatic cwd switching when changing sessions

Feel free to ignore if none of these address your specific case.

github-actions[bot] avatar Aug 22 '25 19:08 github-actions[bot]

yeah idk what the ux would be but definitely useful to let things go outside current dir if you want them to

rekram1-node avatar Aug 22 '25 19:08 rekram1-node

Agreed. This was a functionality that I used pretty heavily that is no longer supported. Things like "look at /x/y/z file as a reference". I can get it to work by enabling the bash permission (and approving each command explicitly), but it feels less natural than having it be able to read the file in the more standard way.

BSteffaniak avatar Aug 24 '25 18:08 BSteffaniak

I am working on multiple connected projects (team area) and yes, while working on one of them it was good opencode was able to check the code in other projects. Maybe also give it permanent access to let say docs readme.

marcinpz avatar Aug 26 '25 13:08 marcinpz

https://github.com/sst/opencode/issues/2308 is also relevant (MCP servers accessing other directories). Ideally the builtin file tools which rely on working directory would utilize the same set of MCP roots. (I can't imagine a scenario the two sets of directories shouldn't be in sync.)

garrett-hopper avatar Aug 29 '25 14:08 garrett-hopper

Would love having that feature

ariedov avatar Sep 16 '25 08:09 ariedov

This would significantly improve the workflow in a monorepo if we could explicitly change the working directory and have the LSP automatically restart based on the config file in the new directory.

kkpattern avatar Oct 22 '25 08:10 kkpattern

Would love this, the UX is a bit challenging, for a lack of better word, to figure out.

is it in the plans @rekram1-node ?

itse4elhaam avatar Nov 23 '25 16:11 itse4elhaam

I have two ideas related to this primarily.

  1. A /cd command can be used to change the directory with an arg, this would change the context dir for opencode
  2. When creating a session using /new, an optional arg can be accepted to change the directory of the new session, by default, it would create the new session in the same directory.

itse4elhaam avatar Nov 23 '25 16:11 itse4elhaam

What do you think about "cd" as a tool, or /cd with natural language prompt as an argument?

Melodeiro avatar Nov 23 '25 16:11 Melodeiro

for those unaware in current setup if an LLM tries to access something outside cwd you are prompted for permission

rekram1-node avatar Nov 23 '25 17:11 rekram1-node

Yeah right now its much better, so changing directory would be useful in a scenario when you want to start to work on another project for example. Instead of ../../../another-project/file.md Not a big deal if you work with anthropic models, tho, they are handling those ../../../ with ease

Melodeiro avatar Nov 27 '25 18:11 Melodeiro

Good suggestion 👍 Very interested in the cd switching possibility. In my case, I wrote a tool to start a worktree and cd into it, a !cd or cd! or force_cd would work fine for my usecase. Personally I find it uncomfortable if I need to rely on the agent to remember that it always has to cd into the worktree directory to execute tasks. With claude code it used to just cd into it, that's something I can't seem to do in opencode so I'll be forced to close the editor in order to switch. There are many other reasons though why I would like to use opencode

databrecht avatar Dec 05 '25 23:12 databrecht

Question for you guys, is this distinctly different from something like /add-dir in terms of what you're looking for?

I feel like most people ask just to add another directory to the working context. But this sounds more like hey I started a session in one directory and I want the llm to be in an entirely different directory now.

OR is this hey I want to start opencode in one directory, do a session, then my next session I want to be in a different directory but I dont want to close & reopen opencode?

Also note: you can do opencode <dir> and it will open opencode in that dir regardless if it is in cwd or not.

rekram1-node avatar Dec 06 '25 19:12 rekram1-node

Question for you guys, is this distinctly different from something like /add-dir in terms of what you're looking for?

I feel like most people ask just to add another directory to the working context. But this sounds more like hey I started a session in one directory and I want the llm to be in an entirely different directory now.

OR is this hey I want to start opencode in one directory, do a session, then my next session I want to be in a different directory but I dont want to close & reopen opencode?

Also note: you can do opencode <dir> and it will open opencode in that dir regardless if it is in cwd or not.

I think It can be both.

Changing directory certainly comes with a new session in new directory, that's exactly like that.

The first one you described is more like the workspace feature in vscode, it's cool as well and I can already think of many many usecases.


Essentially this is two features

  1. Want to create a new session in new directory without reopening opencode

  2. Want to add another directory to the current context (like a workspace)

That's what I think.

itse4elhaam avatar Dec 06 '25 19:12 itse4elhaam

okay that makes sense to me, what makes less sense/seems messy, is "cd-ing" mid session, I think that'd confuse llm, but if it is just adding stuff to context giving it awareness of filetree & whatnot, I think that's valid

rekram1-node avatar Dec 06 '25 19:12 rekram1-node

okay that makes sense to me, what makes less sense/seems messy, is "cd-ing" mid session, I think that'd confuse llm, but if it is just adding stuff to context giving it awareness of filetree & whatnot, I think that's valid

Yeah exactly, it should be either a new session in a new directory or adding the directory in the context.

itse4elhaam avatar Dec 06 '25 19:12 itse4elhaam

When using Claude Code, it would occasionally make sense to change directory mid-conversation.

Context: I followed this ticket because I run commands like "create a worktree, file a ticket, make a branch" and expect Claude to cd into that worktree and treat it as the new working directory. This works in Claude Code but not in opencode.

Here's the typical flow:

  1. Agent does the work - creates worktree, files ticket, CDs into the new directory
  2. Next command fails - something breaks because it's running in the wrong folder
  3. Agent realizes the problem - runs pwd and discovers: "That's odd, I CD'd into this directory but I'm still in the original one"
  4. Agent adapts - eventually realizes it's forever bound to the launch directory and suggests using absolute paths for everything (which, iirc, is what the bash tool instructions already tell it to do anyway)

When I have 5 worktrees open, I can't trust the agent to remember the correct absolute path for every command. It's error-prone and uncomfortable to rely on. So I end up:

  • Closing the OpenCode session
  • Reopening it in the target directory
  • Losing all conversation context This breaks the flow significantly.

To me personally, this is "changing directory mid-conversation" - but maybe the OP meant something different. I didn't interpret the original request as "adding the directory to context," but rather actually changing the working directory.

databrecht avatar Dec 08 '25 09:12 databrecht

Yes, worktree is a good case, because if you don't change the dir - it will every time load the AGENTS.md from the original tree, and not sure that it will load the whole hierarchy of agent.md-s properly if you are trying to work outside the current directory

Melodeiro avatar Dec 09 '25 06:12 Melodeiro