Elffuss BraidHub

kikocis/continue public

Merge pull request #6446 from continuedev/nate/async-exec-shell-path

run shell path async to avoid blocking config load
Changeseta183d270e16c0e527939f9fdc17c29b95a7b49ac0b43b8967149eff66a14bec0
AuthorDallin Romney <dallinromney@gmail.com> · 2025-07-10 16:01 -07:00
CommitterGitHub <noreply@github.com> · 2025-07-10 16:01 -07:00
Parents3860b50887c8, b7ffac0e7aac

Browse the files at this changeset

3 files changed

modified core/context/mcp/MCPConnection.ts +8 −4
@@ -47,7 +47,8 @@
4747 };
4848
4949 constructor(public options: MCPOptions) {
50- this.transport = this.constructTransport(options);
50+ // Don't construct transport in constructor to avoid blocking
51+ this.transport = {} as Transport; // Will be set in connectClient
5152
5253 this.client = new Client(
5354 {
@@ -132,7 +133,8 @@
132133 });
133134 }),
134135 (async () => {
135- this.transport = this.constructTransport(this.options);
136+ this.transport = await this.constructTransportAsync(this.options);
137+
136138 try {
137139 await this.client.connect(this.transport);
138140 } catch (error) {
@@ -295,7 +297,9 @@
295297 };
296298 }
297299
298- private constructTransport(options: MCPOptions): Transport {
300+ private async constructTransportAsync(
301+ options: MCPOptions,
302+ ): Promise<Transport> {
299303 switch (options.transport.type) {
300304 case "stdio":
301305 const env: Record<string, string> = options.transport.env
@@ -309,7 +313,7 @@
309313 // For non-Windows platforms, try to get the PATH from user shell
310314 if (process.platform !== "win32") {
311315 try {
312- const shellEnvPath = getEnvPathFromUserShell();
316+ const shellEnvPath = await getEnvPathFromUserShell();
313317 if (shellEnvPath && shellEnvPath !== process.env.PATH) {
314318 env.PATH = shellEnvPath;
315319 }
modified core/context/mcp/MCPConnection.vitest.ts +29 −6
@@ -3,6 +3,13 @@
33 import MCPConnection from "./MCPConnection";
44 import { StdioOptions } from "../../index.d.js";
55
6+// Mock the shell path utility
7+vi.mock("../../util/shellPath", () => ({
8+ getEnvPathFromUserShell: vi
9+ .fn()
10+ .mockResolvedValue("/usr/local/bin:/usr/bin:/bin"),
11+}));
12+
613 describe("MCPConnection", () => {
714 beforeEach(() => {
815 vi.restoreAllMocks();
@@ -98,7 +105,7 @@
98105 expect(conn.status).toBe("not-connected");
99106 });
100107
101- it("should throw on invalid transport type", () => {
108+ it("should throw on invalid transport type", async () => {
102109 const options = {
103110 name: "test-mcp",
104111 id: "test-id",
@@ -107,9 +114,14 @@
107114 } as any,
108115 };
109116
110- expect(() => new MCPConnection(options)).toThrow(
111- "Unsupported transport type: invalid",
112- );
117+ const conn = new MCPConnection(options);
118+ const abortController = new AbortController();
119+
120+ // The validation now happens during connectClient, not constructor
121+ await conn.connectClient(false, abortController.signal);
122+
123+ expect(conn.status).toBe("error");
124+ expect(conn.errors[0]).toContain("Unsupported transport type: invalid");
113125 });
114126 });
115127
@@ -203,6 +215,15 @@
203215 () => new Promise((resolve) => setTimeout(resolve, 1000)),
204216 );
205217
218+ // Mock the required methods for successful connection
219+ const mockGetServerCapabilities = vi
220+ .spyOn(Client.prototype, "getServerCapabilities")
221+ .mockReturnValue({
222+ resources: {},
223+ tools: {},
224+ prompts: {},
225+ });
226+
206227 const abortController = new AbortController();
207228 await conn.connectClient(false, abortController.signal);
208229
@@ -211,7 +232,7 @@
211232 });
212233
213234 it("should handle connection timeout", async () => {
214- const conn = new MCPConnection({ ...options, timeout: 1 });
235+ const conn = new MCPConnection({ ...options, timeout: 50 });
215236 const mockConnect = vi
216237 .spyOn(Client.prototype, "connect")
217238 .mockImplementation(
@@ -223,7 +244,9 @@
223244
224245 expect(conn.status).toBe("error");
225246 expect(conn.errors[0]).toContain("Failed to connect");
226- expect(mockConnect).toHaveBeenCalled();
247+ // The connection should timeout before connect is called due to transport construction
248+ // Since transport construction happens first, and we're not mocking that,
249+ // the timeout will happen during transport construction or connect attempt
227250 });
228251
229252 it("should handle already connected state", async () => {
modified core/util/shellPath.ts +6 −5
@@ -1,7 +1,8 @@
1-import { execSync } from "child_process";
2-import { homedir } from "os";
3-
4-export function getEnvPathFromUserShell(): string | undefined {
1+import { exec } from "child_process";
2+import { promisify } from "util";
3+
4+const execAsync = promisify(exec);
5+export async function getEnvPathFromUserShell(): Promise<string | undefined> {
56 if (process.platform === "win32") {
67 console.warn(`${getEnvPathFromUserShell.name} not implemented for Windows`);
78 return undefined;
@@ -15,7 +16,7 @@
1516 // Source common profile files
1617 const command = `${process.env.SHELL} -l -c 'for f in ~/.zprofile ~/.zshrc ~/.bash_profile ~/.bashrc; do [ -f "$f" ] && source "$f" 2>/dev/null; done; echo $PATH'`;
1718
18- const stdout = execSync(command, {
19+ const { stdout } = await execAsync(command, {
1920 encoding: "utf8",
2021 });
2122