Skip to content

Commit 8135be2

Browse files
DeusDataclaude
andcommitted
fix(cli): remove the install dir from the Windows user PATH on uninstall (DeusData#2117)
On Windows, install registers the install directory in the persistent HKCU\Environment\Path, but uninstall never removed it — so every install/uninstall cycle left its entry behind and the user PATH grew without bound (a real test host reached ~8000 chars, emptying the effective PATH and breaking tooling). The reported non-ASCII "mojibake" is historical, not a current-code bug. Both install paths already write the value through the UTF-16 registry API: install.ps1 (shipped in each release archive) runs `cbm install`, which calls RegSetValueExW(L"Path", ...) — git history shows no ANSI (RegSetValueExA / SetEnvironmentVariableA) predecessor — and setup-windows.ps1 writes via .NET [Environment]::SetEnvironmentVariable (Unicode) with an ASCII-only install dir. The only path that handles a non-ASCII directory is the wide-correct C path, so current code cannot produce a garbled entry; the garbage the reporter saw was left by an older binary. No code change is needed for the encoding itself. The real defect is the missing uninstall-side removal. This adds cli_remove_windows_user_path(), the counterpart to cli_ensure_windows_user_path(): it opens the same key, walks the ';'-separated segments, and drops exactly the ones equal to the install dir using the same case- and trailing-separator-insensitive comparison the install `present` scan uses, so it removes precisely what install would have de-duplicated. Every other segment is preserved byte-for-byte, the value kind (REG_SZ / REG_EXPAND_SZ) is preserved, and a registry error is a warning rather than a hard failure so a user is never blocked from finishing an uninstall over a cosmetic PATH edit. Removing the entry on uninstall also stops accumulation on reinstall: a garbled historical entry defeats the install de-dup and gets re-added, so taking our own entry back out keeps the value bounded. Testing: - tests/test_cli.c: a hermetic Windows unit test drives the append and remove logic against a GUID-scoped scratch key via the existing CBM_TEST_WINDOWS_USER_PATH_RUN_ID seam, never the developer's live HKCU\Environment\Path. A non-ASCII install dir doubles as a mojibake guard (a broken UTF-8->UTF-16 round-trip would fail the match and leak a duplicate). It asserts append, idempotent reinstall, exact removal from the tail and the middle, idempotent removal, and byte-for-byte survival of unrelated entries. - tests/test_smoke_fixture_contract.sh: a macOS-runnable source contract that fails when the removal is absent and passes with it, proving the wiring binds (verified RED-on-revert). The Windows-only registry code is #ifdef _WIN32; its compile and the behavioral unit test are verified by CI's Windows leg (the local Windows VM is down). Pre-existing mojibaked entries from old binaries are a separate one-time cleanup, out of scope here, and setup-windows.ps1's own lack of an uninstall mode is noted as a possible follow-up. Fixes DeusData#2117 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 161df2b commit 8135be2

3 files changed

Lines changed: 271 additions & 0 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6491,6 +6491,127 @@ static int cli_ensure_windows_user_path(const char *bin_dir, bool dry_run) {
64916491
return CLI_OK;
64926492
}
64936493

6494+
/* Uninstall counterpart to cli_ensure_windows_user_path: remove exactly the
6495+
* install-dir segment install added, leaving every other segment byte-for-byte
6496+
* intact. Without this, every install/uninstall cycle leaves its entry behind
6497+
* and the current-user PATH grows without bound (#2117). Segment identity uses
6498+
* the same case- and trailing-separator-insensitive comparison as the install
6499+
* `present` scan, so we remove precisely what install would have de-duplicated.
6500+
* Returns CLI_OK when a segment was removed, CLI_TRUE when the directory was
6501+
* not present (nothing to do), CLI_ERR on a registry failure. dry_run reports
6502+
* without mutating. */
6503+
static int cli_remove_windows_user_path(const char *bin_dir, bool dry_run) {
6504+
wchar_t *wide_dir = cli_windows_utf8_to_wide(bin_dir);
6505+
HKEY environment = NULL;
6506+
if (!wide_dir || cli_windows_open_user_path_key(&environment) != CLI_OK) {
6507+
free(wide_dir);
6508+
return CLI_ERR;
6509+
}
6510+
6511+
DWORD type = REG_EXPAND_SZ;
6512+
DWORD bytes = 0;
6513+
LONG queried = RegQueryValueExW(environment, L"Path", NULL, &type, NULL, &bytes);
6514+
if (queried == ERROR_FILE_NOT_FOUND) {
6515+
/* No user PATH value at all — nothing of ours to remove. */
6516+
RegCloseKey(environment);
6517+
free(wide_dir);
6518+
return CLI_TRUE;
6519+
}
6520+
if (queried != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
6521+
RegCloseKey(environment);
6522+
free(wide_dir);
6523+
return CLI_ERR;
6524+
}
6525+
size_t existing_capacity = (size_t)bytes / sizeof(wchar_t) + 1U;
6526+
wchar_t *existing = calloc(existing_capacity, sizeof(*existing));
6527+
if (!existing) {
6528+
RegCloseKey(environment);
6529+
free(wide_dir);
6530+
return CLI_ERR;
6531+
}
6532+
DWORD read_bytes = bytes;
6533+
if (RegQueryValueExW(environment, L"Path", NULL, &type, (BYTE *)existing, &read_bytes) !=
6534+
ERROR_SUCCESS) {
6535+
RegCloseKey(environment);
6536+
free(existing);
6537+
free(wide_dir);
6538+
return CLI_ERR;
6539+
}
6540+
existing[existing_capacity - 1U] = L'\0';
6541+
6542+
/* Rebuild the value from every segment that is NOT our directory. Kept
6543+
* segments keep their exact original characters; a single ';' rejoins
6544+
* consecutive kept segments so unrelated entries survive byte-for-byte and
6545+
* no stray separator is left where our entry used to be. The result is
6546+
* never longer than the input, so the input length bounds the buffer. */
6547+
size_t existing_length = wcslen(existing);
6548+
wchar_t *rebuilt = calloc(existing_length + 1U, sizeof(*rebuilt));
6549+
if (!rebuilt) {
6550+
RegCloseKey(environment);
6551+
free(existing);
6552+
free(wide_dir);
6553+
return CLI_ERR;
6554+
}
6555+
size_t out = 0;
6556+
bool removed = false;
6557+
bool wrote_segment = false;
6558+
const wchar_t *cursor = existing;
6559+
while (*cursor) {
6560+
const wchar_t *separator = wcschr(cursor, L';');
6561+
size_t length = separator ? (size_t)(separator - cursor) : wcslen(cursor);
6562+
if (cli_windows_path_segment_equal(cursor, length, wide_dir)) {
6563+
removed = true;
6564+
} else {
6565+
if (wrote_segment) {
6566+
rebuilt[out++] = L';';
6567+
}
6568+
memcpy(rebuilt + out, cursor, length * sizeof(*rebuilt));
6569+
out += length;
6570+
wrote_segment = true;
6571+
}
6572+
cursor = separator ? separator + 1 : cursor + length;
6573+
}
6574+
rebuilt[out] = L'\0';
6575+
6576+
if (!removed) {
6577+
RegCloseKey(environment);
6578+
free(rebuilt);
6579+
free(existing);
6580+
free(wide_dir);
6581+
return CLI_TRUE;
6582+
}
6583+
if (dry_run) {
6584+
RegCloseKey(environment);
6585+
free(rebuilt);
6586+
free(existing);
6587+
free(wide_dir);
6588+
return CLI_OK;
6589+
}
6590+
DWORD output_bytes = (DWORD)((out + 1U) * sizeof(*rebuilt));
6591+
LONG stored =
6592+
RegSetValueExW(environment, L"Path", 0, type, (const BYTE *)rebuilt, output_bytes);
6593+
RegCloseKey(environment);
6594+
free(rebuilt);
6595+
free(existing);
6596+
free(wide_dir);
6597+
if (stored != ERROR_SUCCESS) {
6598+
return CLI_ERR;
6599+
}
6600+
return CLI_OK;
6601+
}
6602+
6603+
#if defined(CBM_CLI_ENABLE_TEST_API)
6604+
/* Thin seams so the hermetic Windows PATH unit test can drive the append and
6605+
* remove logic against a GUID-scoped scratch key (the CBM_TEST_WINDOWS_USER_
6606+
* PATH_RUN_ID seam) without touching the developer's live HKCU PATH. */
6607+
int cbm_cli_ensure_windows_user_path_for_test(const char *bin_dir, bool dry_run) {
6608+
return cli_ensure_windows_user_path(bin_dir, dry_run);
6609+
}
6610+
int cbm_cli_remove_windows_user_path_for_test(const char *bin_dir, bool dry_run) {
6611+
return cli_remove_windows_user_path(bin_dir, dry_run);
6612+
}
6613+
#endif
6614+
64946615
#endif
64956616

64966617
/* ── Tar.gz / zip extraction (TEST-ONLY) ──────────────────────────
@@ -12081,6 +12202,43 @@ static int cli_uninstall_activate(void *opaque) {
1208112202
return CLI_ACTIVATION_PARTIAL;
1208212203
}
1208312204

12205+
#ifdef _WIN32
12206+
/* #2117: install registers the install directory in the persistent
12207+
* current-user PATH; uninstall must take it back out, or every cycle leaves
12208+
* a stale entry and the PATH grows without bound. Remove only our segment.
12209+
* A registry hiccup here is a warning, never a hard failure: the user is
12210+
* removing the tool and must not be blocked from finishing over a cosmetic
12211+
* PATH edit. Suppress the mutation under the test-ops seam exactly as
12212+
* install does, so the CLI suite never touches the developer's real PATH. */
12213+
if (activation->bin_path && activation->bin_path[0]) {
12214+
const char *slash = strrchr(activation->bin_path, '/');
12215+
const char *backslash = strrchr(activation->bin_path, '\\');
12216+
if (backslash && (!slash || backslash > slash)) {
12217+
slash = backslash;
12218+
}
12219+
if (slash && slash != activation->bin_path) {
12220+
size_t dir_len = (size_t)(slash - activation->bin_path);
12221+
char bin_dir[CLI_BUF_1K];
12222+
if (dir_len < sizeof(bin_dir)) {
12223+
memcpy(bin_dir, activation->bin_path, dir_len);
12224+
bin_dir[dir_len] = '\0';
12225+
int path_rc = cli_remove_windows_user_path(
12226+
bin_dir, activation->dry_run || g_cli_activation_test_ops_set);
12227+
if (path_rc == CLI_OK) {
12228+
printf(activation->dry_run ? "\nWould remove %s from the current-user PATH\n"
12229+
: "\nRemoved %s from the current-user PATH\n",
12230+
bin_dir);
12231+
} else if (path_rc == CLI_ERR) {
12232+
(void)fprintf(stderr,
12233+
"warning: could not update the current-user PATH; %s may "
12234+
"remain on it\n",
12235+
bin_dir);
12236+
}
12237+
}
12238+
}
12239+
}
12240+
#endif
12241+
1208412242
if (activation->delete_indexes && !activation->dry_run) {
1208512243
int expected = count_db_indexes(activation->home);
1208612244
int idx_removed = cbm_remove_indexes(activation->home);

‎tests/test_cli.c‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#endif
4646
#include <errno.h>
4747
#include <limits.h>
48+
#include <wchar.h> /* wcscmp/wcslen/swprintf for the Windows user-PATH test (#2117) */
4849
#include <zlib.h>
4950

5051
/* Internal prompt seam used to restore process-global state after command
@@ -14670,6 +14671,110 @@ TEST(cli_windows_update_hands_off_to_install_script) {
1467014671
ASSERT_TRUE(preserved);
1467114672
PASS();
1467214673
}
14674+
14675+
/* #2117: install registers the install dir in the persistent current-user PATH
14676+
* via the wide registry API and de-duplicates on reinstall; uninstall must take
14677+
* exactly that entry back out and leave every other entry byte-for-byte intact,
14678+
* or the PATH grows without bound (a real Windows host reached ~8000 chars).
14679+
* This drives the actual query/append/remove logic through the hermetic
14680+
* CBM_TEST_WINDOWS_USER_PATH_RUN_ID seam, against a GUID-scoped scratch key
14681+
* under HKCU\Software\CodebaseMemoryMCP\Smoke — the developer's live
14682+
* HKCU\Environment\Path is never opened. The non-ASCII install dir doubles as
14683+
* the mojibake guard: a broken UTF-8->UTF-16 round-trip would fail the segment
14684+
* match and leak a duplicate instead of de-duplicating.
14685+
*
14686+
* Return codes mirror the internal enum: 0 = added/removed, 1 = already
14687+
* present / nothing to remove. */
14688+
int cbm_cli_ensure_windows_user_path_for_test(const char *bin_dir, bool dry_run);
14689+
int cbm_cli_remove_windows_user_path_for_test(const char *bin_dir, bool dry_run);
14690+
14691+
static const wchar_t k_cli_user_path_run_id[] = L"0123456789abcdef0123456789abcdef";
14692+
14693+
static int cli_test_read_smoke_path(HKEY key, wchar_t *out, DWORD out_bytes) {
14694+
DWORD type = 0;
14695+
DWORD bytes = out_bytes;
14696+
return RegQueryValueExW(key, L"Path", NULL, &type, (BYTE *)out, &bytes) == ERROR_SUCCESS ? 0
14697+
: -1;
14698+
}
14699+
14700+
static int cli_test_set_smoke_path(HKEY key, const wchar_t *value) {
14701+
DWORD bytes = (DWORD)((wcslen(value) + 1U) * sizeof(wchar_t));
14702+
LONG rc = RegSetValueExW(key, L"Path", 0, REG_EXPAND_SZ, (const BYTE *)value, bytes);
14703+
return rc == ERROR_SUCCESS ? 0 : -1;
14704+
}
14705+
14706+
TEST(cli_uninstall_removes_only_its_own_windows_user_path_entry_issue2117) {
14707+
if (!SetEnvironmentVariableW(L"CBM_TEST_WINDOWS_USER_PATH_RUN_ID", k_cli_user_path_run_id) ||
14708+
!SetEnvironmentVariableW(L"SMOKE_TEMP_ROOT", L"C:\\Temp\\cbm-smoke") ||
14709+
!SetEnvironmentVariableW(L"SMOKE_DOWNLOAD_URL", L"http://127.0.0.1:8765")) {
14710+
FAIL("could not arm the Windows user-PATH test seam");
14711+
}
14712+
14713+
wchar_t key_path[128];
14714+
swprintf(key_path, sizeof(key_path) / sizeof(key_path[0]),
14715+
L"Software\\CodebaseMemoryMCP\\Smoke\\%ls", k_cli_user_path_run_id);
14716+
/* Start from a clean leaf so a prior aborted run cannot skew the result. */
14717+
RegDeleteKeyW(HKEY_CURRENT_USER, key_path);
14718+
HKEY key = NULL;
14719+
if (RegCreateKeyExW(HKEY_CURRENT_USER, key_path, 0, NULL, REG_OPTION_NON_VOLATILE,
14720+
KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_WOW64_64KEY, NULL, &key,
14721+
NULL) != ERROR_SUCCESS) {
14722+
FAIL("could not create the scratch smoke registry key");
14723+
}
14724+
DWORD sentinel_bytes = (DWORD)((wcslen(k_cli_user_path_run_id) + 1U) * sizeof(wchar_t));
14725+
RegSetValueExW(key, L"CbmSmokeRunId", 0, REG_SZ, (const BYTE *)k_cli_user_path_run_id,
14726+
sentinel_bytes);
14727+
14728+
/* Non-ASCII install dir (Omega mu epsilon-tonos gamma alpha). */
14729+
wchar_t bin_dir_w[] = L"C:\\Users\\dev\\install_\u03A9\u03BC\u03AD\u03B3\u03B1\\bin";
14730+
char bin_dir[512];
14731+
WideCharToMultiByte(CP_UTF8, 0, bin_dir_w, -1, bin_dir, (int)sizeof(bin_dir), NULL, NULL);
14732+
14733+
wchar_t readback[1024];
14734+
wchar_t expected_appended[1024];
14735+
swprintf(expected_appended, sizeof(expected_appended) / sizeof(expected_appended[0]),
14736+
L"C:\\Tools\\alpha;C:\\Tools\\omega;%ls", bin_dir_w);
14737+
wchar_t seeded_middle[1024];
14738+
swprintf(seeded_middle, sizeof(seeded_middle) / sizeof(seeded_middle[0]),
14739+
L"C:\\Tools\\alpha;%ls;C:\\Tools\\omega", bin_dir_w);
14740+
14741+
int ok = 1;
14742+
/* Scenario A — append to a PATH lacking our dir, de-dup, then remove. */
14743+
ok &= cli_test_set_smoke_path(key, L"C:\\Tools\\alpha;C:\\Tools\\omega") == 0;
14744+
ok &= cbm_cli_ensure_windows_user_path_for_test(bin_dir, false) == 0; /* added */
14745+
ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0;
14746+
ok &= wcscmp(readback, expected_appended) == 0;
14747+
/* Reinstall must not duplicate: the non-ASCII entry must round-trip-match. */
14748+
ok &= cbm_cli_ensure_windows_user_path_for_test(bin_dir, false) == 1; /* already present */
14749+
ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0;
14750+
ok &= wcscmp(readback, expected_appended) == 0;
14751+
/* Remove our entry — the two unrelated entries survive byte-for-byte. */
14752+
ok &= cbm_cli_remove_windows_user_path_for_test(bin_dir, false) == 0; /* removed */
14753+
ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0;
14754+
ok &= wcscmp(readback, L"C:\\Tools\\alpha;C:\\Tools\\omega") == 0;
14755+
/* Idempotent removal: nothing of ours left to take out. */
14756+
ok &= cbm_cli_remove_windows_user_path_for_test(bin_dir, false) == 1; /* nothing to remove */
14757+
ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0;
14758+
ok &= wcscmp(readback, L"C:\\Tools\\alpha;C:\\Tools\\omega") == 0;
14759+
14760+
/* Scenario B — our entry in the MIDDLE: neighbours on both sides survive. */
14761+
ok &= cli_test_set_smoke_path(key, seeded_middle) == 0;
14762+
ok &= cbm_cli_remove_windows_user_path_for_test(bin_dir, false) == 0; /* removed */
14763+
ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0;
14764+
ok &= wcscmp(readback, L"C:\\Tools\\alpha;C:\\Tools\\omega") == 0;
14765+
14766+
/* Teardown: delete only our scratch leaf; clear the seam env. */
14767+
RegCloseKey(key);
14768+
RegDeleteKeyW(HKEY_CURRENT_USER, key_path);
14769+
SetEnvironmentVariableW(L"CBM_TEST_WINDOWS_USER_PATH_RUN_ID", NULL);
14770+
SetEnvironmentVariableW(L"SMOKE_TEMP_ROOT", NULL);
14771+
SetEnvironmentVariableW(L"SMOKE_DOWNLOAD_URL", NULL);
14772+
14773+
if (!ok) {
14774+
FAIL("uninstall must remove exactly its own user-PATH entry and leave others intact");
14775+
}
14776+
PASS();
14777+
}
1467314778
#endif /* _WIN32 */
1467414779

1467514780
/* ═══════════════════════════════════════════════════════════════════
@@ -14796,6 +14901,7 @@ SUITE(cli) {
1479614901
RUN_TEST(cli_activation_guard_is_bypassed_for_dry_run_and_plan);
1479714902
#ifdef _WIN32
1479814903
RUN_TEST(cli_windows_update_hands_off_to_install_script);
14904+
RUN_TEST(cli_uninstall_removes_only_its_own_windows_user_path_entry_issue2117);
1479914905
#endif
1480014906

1480114907
/* Version (2 tests — selfupdate_test.go) */

‎tests/test_smoke_fixture_contract.sh‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ require(
265265
and "CbmSmokeRunId" in cli_source,
266266
"the Windows PATH test seam must be run-ID-only, sentinel-bound, and loopback-gated",
267267
)
268+
require(
269+
"cli_remove_windows_user_path" in cli_source
270+
and "Removed %s from the current-user PATH" in cli_source
271+
and "Would remove %s from the current-user PATH" in cli_source,
272+
"uninstall must remove exactly its own segment from the current-user PATH (#2117), "
273+
"or repeated install/uninstall cycles grow the User PATH without bound",
274+
)
268275
269276
# Every maintained local/PR native leg enters through the fixture wrapper.
270277
for service in ("smoke:", "smoke-amd64:"):

0 commit comments

Comments
 (0)