Skip to content

Commit 4877ecc

Browse files
authored
Fix shell cwd after login startup (#24215)
1 parent f7d527c commit 4877ecc

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

packages/opencode/src/session/prompt.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
787787
const shellName = (
788788
process.platform === "win32" ? path.win32.basename(sh, ".exe") : path.basename(sh)
789789
).toLowerCase()
790+
const cwd = ctx.directory
790791
const invocations: Record<string, { args: string[] }> = {
791792
nu: { args: ["-c", input.command] },
792793
fish: { args: ["-c", input.command] },
@@ -795,25 +796,27 @@ NOTE: At any point in time through this workflow you should feel free to ask the
795796
"-l",
796797
"-c",
797798
`
798-
__oc_cwd=$PWD
799799
[[ -f ~/.zshenv ]] && source ~/.zshenv >/dev/null 2>&1 || true
800800
[[ -f "\${ZDOTDIR:-$HOME}/.zshrc" ]] && source "\${ZDOTDIR:-$HOME}/.zshrc" >/dev/null 2>&1 || true
801-
cd "$__oc_cwd"
801+
cd -- "$1"
802802
eval ${JSON.stringify(input.command)}
803803
`,
804+
"opencode",
805+
cwd,
804806
],
805807
},
806808
bash: {
807809
args: [
808810
"-l",
809811
"-c",
810812
`
811-
__oc_cwd=$PWD
812813
shopt -s expand_aliases
813814
[[ -f ~/.bashrc ]] && source ~/.bashrc >/dev/null 2>&1 || true
814-
cd "$__oc_cwd"
815+
cd -- "$1"
815816
eval ${JSON.stringify(input.command)}
816817
`,
818+
"opencode",
819+
cwd,
817820
],
818821
},
819822
cmd: { args: ["/c", input.command] },
@@ -823,7 +826,6 @@ NOTE: At any point in time through this workflow you should feel free to ask the
823826
}
824827

825828
const args = (invocations[shellName] ?? invocations[""]).args
826-
const cwd = ctx.directory
827829
const shellEnv = yield* plugin.trigger(
828830
"shell.env",
829831
{ cwd, sessionID: input.sessionID, callID: part.callID },

packages/opencode/test/session/prompt.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,30 @@ unix("shell completes a fast command on the preferred shell", () =>
10781078
),
10791079
)
10801080

1081+
unix("shell commands can change directory after startup", () =>
1082+
provideTmpdirInstance(
1083+
(dir) =>
1084+
Effect.gen(function* () {
1085+
const { prompt, run, chat } = yield* boot()
1086+
const parent = path.dirname(dir)
1087+
const result = yield* prompt.shell({
1088+
sessionID: chat.id,
1089+
agent: "build",
1090+
command: "cd .. && pwd",
1091+
})
1092+
1093+
expect(result.info.role).toBe("assistant")
1094+
const tool = completedTool(result.parts)
1095+
if (!tool) return
1096+
1097+
expect(tool.state.output).toContain(parent)
1098+
expect(tool.state.metadata.output).toContain(parent)
1099+
yield* run.assertNotBusy(chat.id)
1100+
}),
1101+
{ git: true, config: cfg },
1102+
),
1103+
)
1104+
10811105
unix("shell lists files from the project directory", () =>
10821106
provideTmpdirInstance(
10831107
(dir) =>

0 commit comments

Comments
 (0)