From 98c7ee53a0d4f8dc8d6bed40dae3571f974aaf3a Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 2 Dec 2026 08:54:42 +0800 Subject: [PATCH] refactor(mcp): share client configuration presence checks (#15708) --- src/application/SupportedClientLocations.ts | 41 ++++++--------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/src/application/SupportedClientLocations.ts b/src/application/SupportedClientLocations.ts index c8fd3e9e165..b9175ecaf03 100644 --- a/src/application/SupportedClientLocations.ts +++ b/src/application/SupportedClientLocations.ts @@ -596,7 +596,10 @@ export const clineVscodeSettingsDirectory = clineSettingsDirectory("Code"); /** Flathub's Zed (`dev.zed.Zed`) gets this XDG_CONFIG_HOME inside its sandbox. */ const ZED_FLATPAK_CONFIG = [".var", "app", "dev.zed.Zed", "config"] as const; -const directoryPresent = (path: string): Result => { +const clientPathPresent = ( + path: string, + client: string, +): Result => { try { lstatSync(path); return ok(true); @@ -604,7 +607,7 @@ const directoryPresent = (path: string): Result => { if (isEnoentError(cause)) return ok(false); return err({ path, - message: `Cannot determine whether ${path} exists: ${errorMessage(cause)}. Repair its access before configuring Zed.`, + message: `Cannot determine whether ${path} exists: ${errorMessage(cause)}. Repair its access before configuring ${client}.`, }); } }; @@ -664,10 +667,10 @@ export const zedLocation = ({ } const native = join(absoluteXdgConfigHome({ home, env }), "zed"); const sandboxed = join(home, ...ZED_FLATPAK_CONFIG, "zed"); - const flatpak = directoryPresent(sandboxed); + const flatpak = clientPathPresent(sandboxed, "Zed"); if (!flatpak.ok) return flatpak; if (!flatpak.value) return ok(at(native)); - const nativePresent = directoryPresent(native); + const nativePresent = clientPathPresent(native, "Zed"); if (!nativePresent.ok) return nativePresent; if (!nativePresent.value) return ok(at(sandboxed)); // XDG_CONFIG_HOME or a link may make both names one folder. @@ -681,21 +684,6 @@ export const zedLocation = ({ }); }; -const regularPathPresent = ( - path: string, -): Result => { - try { - lstatSync(path); - return ok(true); - } catch (cause: unknown) { - if (isEnoentError(cause)) return ok(false); - return err({ - path, - message: `Cannot determine whether ${path} exists: ${errorMessage(cause)}. Repair its access before configuring MCPorter.`, - }); - } -}; - /** * MCPorter's home layer (`listConfigLayerPaths`): a non-empty * `MCPORTER_CONFIG` is the only file it loads; otherwise the first existing of @@ -738,7 +726,7 @@ export const mcporterLocation = ({ ], ); for (const candidate of candidates) { - const present = regularPathPresent(candidate); + const present = clientPathPresent(candidate, "MCPorter"); if (!present.ok) return present; // The loaded file's folder is the evidence, even when it is the legacy one. if (present.value) @@ -792,16 +780,9 @@ export const ampLocation = ({ const candidates = AMP_SETTINGS_FILES.map((name) => join(directory, name)); const present: string[] = []; for (const candidate of candidates) { - try { - lstatSync(candidate); - present.push(candidate); - } catch (cause: unknown) { - if (!isEnoentError(cause)) - return err({ - path: candidate, - message: `Cannot determine whether ${candidate} exists: ${errorMessage(cause)}. Repair its access before configuring Amp.`, - }); - } + const found = clientPathPresent(candidate, "Amp"); + if (!found.ok) return found; + if (found.value) present.push(candidate); } if (present.length > 1) return err({