Allow explicitly changing working directory #1444

Open
opened 2026-02-16 17:30:59 -05:00 by yindo · 35 comments
Owner

Originally created by @garrett-hopper on GitHub (Aug 22, 2025).

Originally assigned to: @thdxr on GitHub.

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)

Originally created by @garrett-hopper on GitHub (Aug 22, 2025). Originally assigned to: @thdxr on GitHub. 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: <fullpath>" in the middle of working on a project)
Author
Owner

@github-actions[bot] commented on GitHub (Aug 22, 2025):

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] commented on GitHub (Aug 22, 2025): 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.
Author
Owner

@rekram1-node commented on GitHub (Aug 22, 2025):

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

@rekram1-node commented on GitHub (Aug 22, 2025): yeah idk what the ux would be but definitely useful to let things go outside current dir if you want them to
Author
Owner

@BSteffaniak commented on GitHub (Aug 24, 2025):

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 commented on GitHub (Aug 24, 2025): 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.
Author
Owner

@marcinpz commented on GitHub (Aug 26, 2025):

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 commented on GitHub (Aug 26, 2025): 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.
Author
Owner

@garrett-hopper commented on GitHub (Aug 29, 2025):

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 commented on GitHub (Aug 29, 2025): 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.)
Author
Owner

@ariedov commented on GitHub (Sep 16, 2025):

Would love having that feature

@ariedov commented on GitHub (Sep 16, 2025): Would love having that feature
Author
Owner

@kkpattern commented on GitHub (Oct 22, 2025):

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 commented on GitHub (Oct 22, 2025): 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.
Author
Owner

@itse4elhaam commented on GitHub (Nov 23, 2025):

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 commented on GitHub (Nov 23, 2025): 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 ?
Author
Owner

@itse4elhaam commented on GitHub (Nov 23, 2025):

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 commented on GitHub (Nov 23, 2025): 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.
Author
Owner

@Melodeiro commented on GitHub (Nov 23, 2025):

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

@Melodeiro commented on GitHub (Nov 23, 2025): What do you think about "cd" as a tool, or /cd with natural language prompt as an argument?
Author
Owner

@rekram1-node commented on GitHub (Nov 23, 2025):

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

@rekram1-node commented on GitHub (Nov 23, 2025): for those unaware in current setup if an LLM tries to access something outside cwd you are prompted for permission
Author
Owner

@Melodeiro commented on GitHub (Nov 27, 2025):

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 commented on GitHub (Nov 27, 2025): 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
Author
Owner

@databrecht commented on GitHub (Dec 5, 2025):

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 commented on GitHub (Dec 5, 2025): 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
Author
Owner

@rekram1-node commented on GitHub (Dec 6, 2025):

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 commented on GitHub (Dec 6, 2025): 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.
Author
Owner

@itse4elhaam commented on GitHub (Dec 6, 2025):

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 commented on GitHub (Dec 6, 2025): > 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.
Author
Owner

@rekram1-node commented on GitHub (Dec 6, 2025):

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 commented on GitHub (Dec 6, 2025): 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
Author
Owner

@itse4elhaam commented on GitHub (Dec 6, 2025):

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 commented on GitHub (Dec 6, 2025): > 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.
Author
Owner

@databrecht commented on GitHub (Dec 8, 2025):

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 commented on GitHub (Dec 8, 2025): > 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.
Author
Owner

@Melodeiro commented on GitHub (Dec 9, 2025):

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 commented on GitHub (Dec 9, 2025): 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
Author
Owner

@fatihdogmus commented on GitHub (Jan 6, 2026):

our use case is like this: we have two monorepos for front-end and back-end respectively. when we develop a feature in the back-end, without losing the context, we want to say "hey you implemented the back-end side of this feature, now we are at the front-end repo, and here are relevant files for front-end, implement this counterpart in front-end". so this feature would be extremely useful for us.

even if we can continue a session outside of its directory would be nice, like the one linked: https://github.com/anomalyco/opencode/issues/6652

I managed to do it with export from BE repo -> import from FE repo but it i very tedious. it managed to hold its ground pretty well when I pointed out the correct files, so this feature would work pretty nice IMO.

@fatihdogmus commented on GitHub (Jan 6, 2026): our use case is like this: we have two monorepos for front-end and back-end respectively. when we develop a feature in the back-end, without losing the context, we want to say "hey you implemented the back-end side of this feature, now we are at the front-end repo, and here are relevant files for front-end, implement this counterpart in front-end". so this feature would be extremely useful for us. even if we can continue a session outside of its directory would be nice, like the one linked: https://github.com/anomalyco/opencode/issues/6652 I managed to do it with export from BE repo -> import from FE repo but it i very tedious. it managed to hold its ground pretty well when I pointed out the correct files, so this feature would work pretty nice IMO.
Author
Owner

@kostrse commented on GitHub (Jan 8, 2026):

Maybe it should be extended into the concept of switching between projects / workspaces (which are directories or bundles of several working directories), in that case context/sessions/configs would switch too. rather than just directory.

Also add ability to attach a another directory to existing project / workspace.

@kostrse commented on GitHub (Jan 8, 2026): Maybe it should be extended into the concept of switching between projects / workspaces (which are directories or bundles of several working directories), in that case context/sessions/configs would switch too. rather than just directory. Also add ability to attach a another directory to existing project / workspace.
Author
Owner

@AkshatGiri commented on GitHub (Jan 10, 2026):

There doesn't seem to be way to pipe in the working directory through the serve command. Maybe I'm missing something, but there's no way to select a project / working directory in the cli. The use case is to run opencode on a remote server and connect to it using a local client.

@AkshatGiri commented on GitHub (Jan 10, 2026): There doesn't seem to be way to pipe in the working directory through the serve command. Maybe I'm missing something, but there's no way to select a project / working directory in the cli. The use case is to run opencode on a remote server and connect to it using a local client.
Author
Owner

@ostwilkens commented on GitHub (Jan 10, 2026):

There doesn't seem to be way to pipe in the working directory through the serve command. Maybe I'm missing something, but there's no way to select a project / working directory in the cli. The use case is to run opencode on a remote server and connect to it using a local client.

I'm in the same situation. I'm running opencode serve on my dev server. The working directory is my home folder, but my code is not in my home folder, and I don't see an option to change working directory. So I can't really do anything.

@ostwilkens commented on GitHub (Jan 10, 2026): > There doesn't seem to be way to pipe in the working directory through the serve command. Maybe I'm missing something, but there's no way to select a project / working directory in the cli. The use case is to run opencode on a remote server and connect to it using a local client. I'm in the same situation. I'm running `opencode serve` on my dev server. The working directory is my home folder, but my code is not in my home folder, and I don't see an option to change working directory. So I can't really do anything.
Author
Owner

@AkshatGiri commented on GitHub (Jan 11, 2026):

@ostwilkens Just gotta ssh in for now. I patched my opencode to allow a --dir option for the serve command which works. But the client and server and really meant to be on the same machine. The file autocomplete still shows the files locally rather than the ones on the server.

for reference of the --dir option - https://github.com/AkshatGiri/opencode/pull/1

@AkshatGiri commented on GitHub (Jan 11, 2026): @ostwilkens Just gotta ssh in for now. I patched my opencode to allow a --dir option for the serve command which works. But the client and server and really meant to be on the same machine. The file autocomplete still shows the files locally rather than the ones on the server. for reference of the --dir option - https://github.com/AkshatGiri/opencode/pull/1
Author
Owner

@DoiiarX commented on GitHub (Jan 12, 2026):

I need this

@DoiiarX commented on GitHub (Jan 12, 2026): I need this
Author
Owner

@DoiiarX commented on GitHub (Jan 12, 2026):

My use case is that I have configured a composite command to start opencode, but I want to temporarily switch directories. My directory is always in the user directory. I don't want to manually start opencode in a specific directory because this would make my pre-configured settings invalid. My pre-configuration is to wrap opencode with git bash to achieve bash-style startup on Windows.

@DoiiarX commented on GitHub (Jan 12, 2026): My use case is that I have configured a composite command to start opencode, but I want to temporarily switch directories. My directory is always in the user directory. I don't want to manually start opencode in a specific directory because this would make my pre-configured settings invalid. My pre-configuration is to wrap opencode with git bash to achieve bash-style startup on Windows.
Author
Owner

@ThallesP commented on GitHub (Jan 12, 2026):

As an example, at our company we have two backend repositories: one is the core of our product, and the other is a product built on top of it. Sometimes I need to work between these two repositories to implement a feature, and using the previous context (even by using /compact) would be extremely useful instead of having to explain everything again to the LLM.

@ThallesP commented on GitHub (Jan 12, 2026): As an example, at our company we have two backend repositories: one is the core of our product, and the other is a product built on top of it. Sometimes I need to work between these two repositories to implement a feature, and using the previous context (even by using /compact) would be extremely useful instead of having to explain everything again to the LLM.
Author
Owner

@berenar commented on GitHub (Jan 17, 2026):

What about https://github.com/anomalyco/opencode/pull/8943

@berenar commented on GitHub (Jan 17, 2026): What about https://github.com/anomalyco/opencode/pull/8943
Author
Owner

@kostrse commented on GitHub (Jan 17, 2026):

OpenCode could support workspaces which include a few attached directories. Switching 'current" directory is a messy hack

@kostrse commented on GitHub (Jan 17, 2026): OpenCode could support workspaces which include a few attached directories. Switching 'current" directory is a messy hack
Author
Owner

@rekram1-node commented on GitHub (Jan 17, 2026):

Yeah I'm liking #8943, I think we will ship it soon just gonna make some tweaks

@rekram1-node commented on GitHub (Jan 17, 2026): Yeah I'm liking #8943, I think we will ship it soon just gonna make some tweaks
Author
Owner

@moonray commented on GitHub (Jan 18, 2026):

I have the same worktree needs as mentioned above. I don't think an add-dir command will work well enough for a worktree case.

@moonray commented on GitHub (Jan 18, 2026): I have the same worktree needs as mentioned above. I don't think an add-dir command will work well enough for a worktree case.
Author
Owner

@detongz commented on GitHub (Jan 20, 2026):

i need this, just like the claude add-dir 🙏

@detongz commented on GitHub (Jan 20, 2026): i need this, just like the claude add-dir 🙏
Author
Owner

@fatihdogmus commented on GitHub (Jan 26, 2026):

hey any update on the issue, we would really like to get this feature.

@fatihdogmus commented on GitHub (Jan 26, 2026): hey any update on the issue, we would really like to get this feature.
Author
Owner

@antonavy commented on GitHub (Feb 7, 2026):

In my case the directory I've been working on changed name, and opencode seems helpless: "Error: No such file or directory".
Can't find a way to keep the current session but in a new directory (different name).

@antonavy commented on GitHub (Feb 7, 2026): In my case the directory I've been working on changed name, and opencode seems helpless: "Error: No such file or directory". Can't find a way to keep the current session but in a new directory (different name).
Author
Owner

@yann-soubeyrand commented on GitHub (Feb 10, 2026):

Hello,
I’m not sure if this is the same issue, but at least it’s related. In my case, I don’t necessarily want to change directory, working at the root of my Git repository is fine. However, my Terraform code is under a terraform directory. The LLM discovers that the Terraform code is in this subdirectory, but it always thinks that doing a cd using the bash tool changes the directory and keeps failing running terraform commands. It seems to me that having a cd tool for the LLM to change its working directory would be a great addition.

@yann-soubeyrand commented on GitHub (Feb 10, 2026): Hello, I’m not sure if this is the same issue, but at least it’s related. In my case, I don’t necessarily want to change directory, working at the root of my Git repository is fine. However, my Terraform code is under a `terraform` directory. The LLM discovers that the Terraform code is in this subdirectory, but it always thinks that doing a `cd` using the `bash` tool changes the directory and keeps failing running `terraform` commands. It seems to me that having a `cd` tool for the LLM to change its working directory would be a great addition.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#1444