Skip to content

Commit 32351b6

Browse files
bingh0claude
andcommitted
fix(platform): fall back to USERPROFILE when HOME is unset (DeusData#77)
On Windows, HOME is not set by default — the system uses USERPROFILE instead. This caused `codebase-memory-mcp install` and all CLI commands to fail with "error: HOME not set". Add cbm_get_home_dir() helper in platform.{h,c} that tries HOME first, then USERPROFILE. Replace all 17 raw getenv("HOME") callsites across 9 files with the new helper. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent d6a5f89 commit 32351b6

9 files changed

Lines changed: 59 additions & 40 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
#include "cli/cli.h"
88
#include "foundation/compat.h"
9+
#include "foundation/platform.h"
910

1011
// the correct standard headers are included below but clang-tidy doesn't map them.
1112
#include <ctype.h>
@@ -1613,7 +1614,7 @@ unsigned char *cbm_extract_binary_from_targz(const unsigned char *data, int data
16131614
static const char *get_cache_dir(const char *home_dir) {
16141615
static char buf[1024];
16151616
if (!home_dir) {
1616-
home_dir = getenv("HOME");
1617+
home_dir = cbm_get_home_dir();
16171618
}
16181619
if (!home_dir) {
16191620
return NULL;
@@ -1837,10 +1838,9 @@ int cbm_cmd_config(int argc, char **argv) {
18371838
return 0;
18381839
}
18391840

1840-
// NOLINTNEXTLINE(concurrency-mt-unsafe)
1841-
const char *home = getenv("HOME");
1841+
const char *home = cbm_get_home_dir();
18421842
if (!home) {
1843-
fprintf(stderr, "error: HOME not set\n");
1843+
fprintf(stderr, "error: HOME not set (use USERPROFILE on Windows)\n");
18441844
return 1;
18451845
}
18461846

@@ -1968,9 +1968,9 @@ int cbm_cmd_install(int argc, char **argv) {
19681968
}
19691969
}
19701970

1971-
const char *home = getenv("HOME");
1971+
const char *home = cbm_get_home_dir();
19721972
if (!home) {
1973-
fprintf(stderr, "error: HOME not set\n");
1973+
fprintf(stderr, "error: HOME not set (use USERPROFILE on Windows)\n");
19741974
return 1;
19751975
}
19761976

@@ -2232,9 +2232,9 @@ int cbm_cmd_uninstall(int argc, char **argv) {
22322232
}
22332233
}
22342234

2235-
const char *home = getenv("HOME");
2235+
const char *home = cbm_get_home_dir();
22362236
if (!home) {
2237-
fprintf(stderr, "error: HOME not set\n");
2237+
fprintf(stderr, "error: HOME not set (use USERPROFILE on Windows)\n");
22382238
return 1;
22392239
}
22402240

@@ -2425,9 +2425,9 @@ int cbm_cmd_uninstall(int argc, char **argv) {
24252425
int cbm_cmd_update(int argc, char **argv) {
24262426
parse_auto_answer(argc, argv);
24272427

2428-
const char *home = getenv("HOME");
2428+
const char *home = cbm_get_home_dir();
24292429
if (!home) {
2430-
fprintf(stderr, "error: HOME not set\n");
2430+
fprintf(stderr, "error: HOME not set (use USERPROFILE on Windows)\n");
24312431
return 1;
24322432
}
24332433

‎src/foundation/platform.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,19 @@ int64_t cbm_file_size(const char *path) {
216216
}
217217

218218
#endif /* _WIN32 */
219+
220+
/* ── Home directory (cross-platform) ──────────────────────────── */
221+
222+
const char *cbm_get_home_dir(void) {
223+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
224+
const char *h = getenv("HOME");
225+
if (h && h[0]) {
226+
return h;
227+
}
228+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
229+
h = getenv("USERPROFILE");
230+
if (h && h[0]) {
231+
return h;
232+
}
233+
return NULL;
234+
}

‎src/foundation/platform.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ cbm_system_info_t cbm_system_info(void);
6464
* initial=false: max(1, perf_cores-1) (leave headroom for user apps) */
6565
int cbm_default_worker_count(bool initial);
6666

67+
/* ── Home directory ─────────────────────────────────────────────── */
68+
69+
/* Cross-platform home directory: tries HOME first, then USERPROFILE (Windows).
70+
* Returns NULL when neither is set. */
71+
const char *cbm_get_home_dir(void);
72+
6773
/* ── File system ───────────────────────────────────────────────── */
6874

6975
/* Check if a path exists. */

‎src/main.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "store/store.h"
2020
#include "cli/cli.h"
2121
#include "foundation/log.h"
22+
#include "foundation/platform.h"
2223
#include "foundation/compat_thread.h"
2324
#include "foundation/mem.h"
2425
#include "ui/config.h"
@@ -221,7 +222,7 @@ int main(int argc, char **argv) {
221222

222223
/* Open config store for runtime settings */
223224
char config_dir[1024];
224-
const char *cfg_home = getenv("HOME");
225+
const char *cfg_home = cbm_get_home_dir();
225226
cbm_config_t *runtime_config = NULL;
226227
if (cfg_home) {
227228
snprintf(config_dir, sizeof(config_dir), "%s/.cache/codebase-memory-mcp", cfg_home);

‎src/mcp/mcp.c‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,9 @@ bool cbm_mcp_server_has_cached_store(cbm_mcp_server_t *srv) {
564564
/* Returns the platform cache directory: ~/.cache/codebase-memory-mcp
565565
* Writes to buf, returns buf for convenience. */
566566
static const char *cache_dir(char *buf, size_t bufsz) {
567-
// NOLINTNEXTLINE(concurrency-mt-unsafe)
568-
const char *home = getenv("HOME");
567+
const char *home = cbm_get_home_dir();
569568
if (!home) {
570-
home = "/tmp";
569+
home = cbm_tmpdir();
571570
}
572571
snprintf(buf, bufsz, "%s/.cache/codebase-memory-mcp", home);
573572
return buf;
@@ -1995,8 +1994,7 @@ static void detect_session(cbm_mcp_server_t *srv) {
19951994
/* 1. Try CWD */
19961995
char cwd[1024];
19971996
if (getcwd(cwd, sizeof(cwd)) != NULL) {
1998-
// NOLINTNEXTLINE(concurrency-mt-unsafe)
1999-
const char *home = getenv("HOME");
1997+
const char *home = cbm_get_home_dir();
20001998
/* Skip useless roots: / and $HOME */
20011999
if (strcmp(cwd, "/") != 0 && (home == NULL || strcmp(cwd, home) != 0)) {
20022000
snprintf(srv->session_root, sizeof(srv->session_root), "%s", cwd);
@@ -2067,8 +2065,7 @@ static void maybe_auto_index(cbm_mcp_server_t *srv) {
20672065
}
20682066

20692067
/* Check if project already has a DB */
2070-
// NOLINTNEXTLINE(concurrency-mt-unsafe)
2071-
const char *home = getenv("HOME");
2068+
const char *home = cbm_get_home_dir();
20722069
if (home) {
20732070
char db_check[1024];
20742071
snprintf(db_check, sizeof(db_check), "%s/.cache/codebase-memory-mcp/%s.db", home,

‎src/pipeline/pipeline.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ static char *resolve_db_path(const cbm_pipeline_t *p) {
127127
if (p->db_path) {
128128
snprintf(path, 1024, "%s", p->db_path);
129129
} else {
130-
// NOLINTNEXTLINE(concurrency-mt-unsafe)
131-
const char *home = getenv("HOME");
130+
const char *home = cbm_get_home_dir();
132131
if (!home) {
133-
home = "/tmp";
132+
home = cbm_tmpdir();
134133
}
135134
snprintf(path, 1024, "%s/.cache/codebase-memory-mcp/%s.db", home, p->project_name);
136135
}
@@ -680,14 +679,13 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
680679
if (!check_cancel(p)) {
681680
cbm_clock_gettime(CLOCK_MONOTONIC, &t);
682681

683-
// NOLINTNEXTLINE(concurrency-mt-unsafe) — called once during single-threaded dump
684-
const char *home = getenv("HOME");
682+
const char *home = cbm_get_home_dir();
685683
char db_path[1024];
686684
if (p->db_path) {
687685
snprintf(db_path, sizeof(db_path), "%s", p->db_path);
688686
} else {
689687
if (!home) {
690-
home = "/tmp";
688+
home = cbm_tmpdir();
691689
}
692690
snprintf(db_path, sizeof(db_path), "%s/.cache/codebase-memory-mcp/%s.db", home,
693691
p->project_name);

‎src/store/store.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,9 @@ cbm_store_t *cbm_store_open(const char *project) {
348348
return NULL;
349349
}
350350
/* Build path: ~/.cache/codebase-memory-mcp/<project>.db */
351-
const char *home = getenv("HOME"); // NOLINT(concurrency-mt-unsafe) — called once during
352-
// single-threaded store open, never concurrently
351+
const char *home = cbm_get_home_dir();
353352
if (!home) {
354-
home = "/tmp";
353+
home = cbm_tmpdir();
355354
}
356355
char path[1024];
357356
snprintf(path, sizeof(path), "%s/.cache/codebase-memory-mcp/%s.db", home, project);

‎src/ui/config.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
*/
77
#include "ui/config.h"
88
#include "foundation/log.h"
9+
#include "foundation/platform.h"
910
#include "foundation/compat_fs.h"
11+
#include "foundation/compat.h"
1012

1113
#include <yyjson/yyjson.h>
1214

@@ -17,9 +19,9 @@
1719
/* ── Path ────────────────────────────────────────────────────── */
1820

1921
void cbm_ui_config_path(char *buf, int bufsz) {
20-
const char *home = getenv("HOME"); // NOLINT(concurrency-mt-unsafe)
22+
const char *home = cbm_get_home_dir();
2123
if (!home) {
22-
home = "/tmp";
24+
home = cbm_tmpdir();
2325
}
2426
snprintf(buf, (size_t)bufsz, "%s/.cache/codebase-memory-mcp/config.json", home);
2527
}

‎src/ui/http_server.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ static void handle_browse(struct mg_connection *c, struct mg_http_message *hm) {
315315
char path[1024] = {0};
316316
if (!get_query_param(hm->query, "path", path, (int)sizeof(path)) || path[0] == '\0') {
317317
/* Default to home directory */
318-
const char *home = getenv("HOME");
318+
const char *home = cbm_get_home_dir();
319319
if (home)
320320
snprintf(path, sizeof(path), "%s", home);
321321
else
@@ -387,9 +387,9 @@ static void handle_adr_get(struct mg_connection *c, struct mg_http_message *hm)
387387
return;
388388
}
389389

390-
const char *home = getenv("HOME");
390+
const char *home = cbm_get_home_dir();
391391
if (!home)
392-
home = "/tmp";
392+
home = cbm_tmpdir();
393393
char db_path[1024];
394394
snprintf(db_path, sizeof(db_path), "%s/.cache/codebase-memory-mcp/%s.db", home, name);
395395

@@ -480,9 +480,9 @@ static void handle_adr_save(struct mg_connection *c, struct mg_http_message *hm)
480480
const char *proj = yyjson_get_str(v_proj);
481481
const char *content = yyjson_get_str(v_content);
482482

483-
const char *home = getenv("HOME");
483+
const char *home = cbm_get_home_dir();
484484
if (!home)
485-
home = "/tmp";
485+
home = cbm_tmpdir();
486486
char db_path[1024];
487487
snprintf(db_path, sizeof(db_path), "%s/.cache/codebase-memory-mcp/%s.db", home, proj);
488488

@@ -777,9 +777,9 @@ static void handle_delete_project(struct mg_connection *c, struct mg_http_messag
777777
return;
778778
}
779779

780-
const char *home = getenv("HOME"); // NOLINT(concurrency-mt-unsafe)
780+
const char *home = cbm_get_home_dir();
781781
if (!home)
782-
home = "/tmp";
782+
home = cbm_tmpdir();
783783
char db_path[1024];
784784
snprintf(db_path, sizeof(db_path), "%s/.cache/codebase-memory-mcp/%s.db", home, name);
785785

@@ -815,9 +815,9 @@ static void handle_project_health(struct mg_connection *c, struct mg_http_messag
815815
return;
816816
}
817817

818-
const char *home = getenv("HOME"); // NOLINT(concurrency-mt-unsafe)
818+
const char *home = cbm_get_home_dir();
819819
if (!home)
820-
home = "/tmp";
820+
home = cbm_tmpdir();
821821
char db_path[1024];
822822
snprintf(db_path, sizeof(db_path), "%s/.cache/codebase-memory-mcp/%s.db", home, name);
823823

@@ -873,9 +873,9 @@ static void handle_layout(struct mg_connection *c, struct mg_http_message *hm) {
873873
}
874874

875875
/* Open a read-only store for this project */
876-
const char *home = getenv("HOME"); // NOLINT(concurrency-mt-unsafe)
876+
const char *home = cbm_get_home_dir();
877877
if (!home)
878-
home = "/tmp";
878+
home = cbm_tmpdir();
879879
char db_path[1024];
880880
snprintf(db_path, sizeof(db_path), "%s/.cache/codebase-memory-mcp/%s.db", home, project);
881881

0 commit comments

Comments
 (0)