From 6db11d6ab0397e60ff9ababca220e1c31446806b Mon Sep 17 00:00:00 2001 From: "S.L" <101876007+slvnlrt@users.noreply.github.com> Date: Fri, 27 Mar 2026 13:15:37 +0100 Subject: [PATCH] fix(cli): detect actual binary path at runtime in install command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit install.ps1 places the binary in $LOCALAPPDATA/Programs/ but cbm_cmd_install() hardcoded ~/.local/bin/ when writing agent MCP configs, causing MCP to fail on Windows. Use GetModuleFileNameA (Win), _NSGetExecutablePath (macOS), or readlink /proc/self/exe (Linux) — same pattern as http_server.c:index_thread_fn(). Falls back to ~/.local/bin/ if runtime detection fails. Fixes #158 --- src/cli/cli.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/cli/cli.c b/src/cli/cli.c index 05c9a560..626a2ce4 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -15,6 +15,9 @@ #include #include #endif +#ifdef __APPLE__ +#include +#endif #include "foundation/compat_fs.h" #ifndef CBM_VERSION @@ -2689,13 +2692,30 @@ int cbm_cmd_install(int argc, char **argv) { } #endif - /* Step 2: Binary path */ - char self_path[1024]; + /* Step 2: Binary path — detect actual location at runtime. + * install.ps1 places the binary in $LOCALAPPDATA/Programs/ but the old + * code hardcoded ~/.local/bin/, causing broken MCP configs on Windows. + * Use the same pattern as http_server.c:index_thread_fn(). */ + char self_path[1024] = {0}; #ifdef _WIN32 - snprintf(self_path, sizeof(self_path), "%s/.local/bin/codebase-memory-mcp.exe", home); + GetModuleFileNameA(NULL, self_path, sizeof(self_path)); + cbm_normalize_path_sep(self_path); +#elif defined(__APPLE__) + uint32_t sp_sz = sizeof(self_path); + if (_NSGetExecutablePath(self_path, &sp_sz) != 0) + self_path[0] = '\0'; #else - snprintf(self_path, sizeof(self_path), "%s/.local/bin/codebase-memory-mcp", home); + ssize_t sp_len = readlink("/proc/self/exe", self_path, sizeof(self_path) - 1); + if (sp_len > 0) + self_path[sp_len] = '\0'; #endif + if (!self_path[0]) { +#ifdef _WIN32 + snprintf(self_path, sizeof(self_path), "%s/.local/bin/codebase-memory-mcp.exe", home); +#else + snprintf(self_path, sizeof(self_path), "%s/.local/bin/codebase-memory-mcp", home); +#endif + } /* Step 3: Install/refresh all agent configs */ cbm_install_agent_configs(home, self_path, force, dry_run);