From 6b349f8196a986d2c182b683b91f75e8136c0b10 Mon Sep 17 00:00:00 2001 From: ideabib Date: Fri, 7 Aug 2026 02:00:26 -0700 Subject: [PATCH] fix: remove harmful process.chdir() in OpenCodeService\n\nprocess.chdir() mutates the global working directory of the VS Code\nextension host, which affects all other extensions and can cause\nunexpected behavior.\n\nThe OpenCode SDK's createOpencode() does not expose a cwd option, and the opencode CLI serve command has no --directory flag, so the\nonly way to scope the server to the workspace was via process.chdir().\n\nThis fix removes the global mutation entirely. The server will start\nin VS Code's own working directory. While this means the server won't\nautomatically inherit the workspace as its cwd, it eliminates the\nside-effects on the rest of the extension host.\n\nA proper long-term fix would be for the OpenCode SDK to accept a\ncwd option in ServerOptions so extensions can scope the server without\nmutating global state. --- src/OpenCodeService.ts | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/OpenCodeService.ts b/src/OpenCodeService.ts index febd645..1484687 100644 --- a/src/OpenCodeService.ts +++ b/src/OpenCodeService.ts @@ -30,12 +30,8 @@ export class OpenCodeService { this.isInitializing = true; - const prevCwd = process.cwd(); - const shouldChdir = - Boolean(workspaceRoot) && fs.existsSync(workspaceRoot as string); - - if (shouldChdir) { - this.workspaceDir = workspaceRoot as string; + if (workspaceRoot && fs.existsSync(workspaceRoot)) { + this.workspaceDir = workspaceRoot; } try { @@ -55,10 +51,6 @@ export class OpenCodeService { this.ensureOpencodeCliAvailable(); - if (shouldChdir) { - process.chdir(workspaceRoot as string); - } - logger.info("Starting OpenCode server..."); this.opencode = await createOpencode({ @@ -73,13 +65,6 @@ export class OpenCodeService { await this.showStartupError(error); throw error; } finally { - if (shouldChdir) { - try { - process.chdir(prevCwd); - } catch (e) { - getLogger().warn("Failed to restore working directory", e); - } - } this.isInitializing = false; } }