Skip to content

Commit a250ad2

Browse files
DeusDataslvnlrt
andcommitted
Detect actual binary path at runtime in install command
Replaces hardcoded ~/.local/bin/ with OS-native runtime detection: - Windows: GetModuleFileNameA() - macOS: _NSGetExecutablePath() - Linux: readlink("/proc/self/exe") Falls back to ~/.local/bin/ if detection fails. Fixes install.ps1 placing binary in $LOCALAPPDATA/Programs/ while configs point to non-existent ~/.local/bin/ path. Fixes #158 Co-Authored-By: slvnlrt <slvnlrt@users.noreply.github.com>
1 parent 80e2752 commit a250ad2

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/cli/cli.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ enum {
6969
#include <signal.h>
7070
#include <unistd.h>
7171
#endif
72+
#ifdef __APPLE__
73+
#include <mach-o/dyld.h>
74+
#endif
7275
#include "foundation/compat_fs.h"
7376

7477
#ifndef CBM_VERSION
@@ -2816,6 +2819,32 @@ static int count_db_indexes(const char *home) {
28162819

28172820
/* ── Subcommand: install ──────────────────────────────────────── */
28182821

2822+
/* Detect the running binary's path at runtime. Falls back to ~/.local/bin/. */
2823+
static void cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
2824+
buf[0] = '\0';
2825+
#ifdef _WIN32
2826+
GetModuleFileNameA(NULL, buf, (DWORD)buf_sz);
2827+
cbm_normalize_path_sep(buf);
2828+
#elif defined(__APPLE__)
2829+
uint32_t sp_sz = (uint32_t)buf_sz;
2830+
if (_NSGetExecutablePath(buf, &sp_sz) != 0) {
2831+
buf[0] = '\0';
2832+
}
2833+
#else
2834+
ssize_t sp_len = readlink("/proc/self/exe", buf, buf_sz - SKIP_ONE);
2835+
if (sp_len > 0) {
2836+
buf[sp_len] = '\0';
2837+
}
2838+
#endif
2839+
if (!buf[0]) {
2840+
#ifdef _WIN32
2841+
snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp.exe", home);
2842+
#else
2843+
snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp", home);
2844+
#endif
2845+
}
2846+
}
2847+
28192848
int cbm_cmd_install(int argc, char **argv) {
28202849
parse_auto_answer(argc, argv);
28212850
bool dry_run = false;
@@ -2872,13 +2901,9 @@ int cbm_cmd_install(int argc, char **argv) {
28722901
}
28732902
#endif
28742903

2875-
/* Step 2: Binary path */
2876-
char self_path[CLI_BUF_1K];
2877-
#ifdef _WIN32
2878-
snprintf(self_path, sizeof(self_path), "%s/.local/bin/codebase-memory-mcp.exe", home);
2879-
#else
2880-
snprintf(self_path, sizeof(self_path), "%s/.local/bin/codebase-memory-mcp", home);
2881-
#endif
2904+
/* Step 2: Binary path — detect actual location at runtime. */
2905+
char self_path[CLI_BUF_1K] = {0};
2906+
cbm_detect_self_path(self_path, sizeof(self_path), home);
28822907

28832908
/* Step 3: Install/refresh all agent configs */
28842909
cbm_install_agent_configs(home, self_path, force, dry_run);

0 commit comments

Comments
 (0)