Skip to content

Commit ced508f

Browse files
authored
Merge pull request DeusData#2381 from wcy12378/fix/221-windows-cli-detection
fix(cli): resolve Windows CLI detection for long PATH and npm/mise/scoop shims (DeusData#221)
2 parents e2f93ba + 7d01cae commit ced508f

5 files changed

Lines changed: 435 additions & 28 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "foundation/platform.h"
2424
#include "foundation/constants.h"
2525
#include "foundation/log.h"
26+
#include "foundation/mem_core.h"
2627
#include "foundation/sha256.h"
2728
#include "foundation/str_util.h"
2829
#include "cli/client_adapter.h"
@@ -1125,36 +1126,50 @@ static bool is_executable(const char *path) {
11251126
#endif
11261127
}
11271128

1129+
/* Windows ships CLIs as extension-bearing shims -- .cmd from npm, .ps1 from
1130+
* mise, .exe from scoop -- so a bare-name probe never matches there. One
1131+
* definition serves both the PATH scan and the fallback-directory probe (#221). */
1132+
#ifdef _WIN32
1133+
static const char *const WIN_EXEC_EXTS[] = {".exe", ".cmd", ".bat", ".ps1", NULL};
1134+
#endif
1135+
11281136
/* Search for an executable named `name` in the PATH environment variable.
1129-
* Returns the full path in `out` (max out_sz) if found, else empty string. */
1137+
* Returns the full path in `out` (max out_sz) if found, else empty string.
1138+
*
1139+
* The PATH copy is sized to the REAL value, not to a fixed 4 KB stack buffer.
1140+
* That buffer was the first root cause of #221: a Windows dev machine's PATH
1141+
* routinely runs past 4096 characters, and cbm_safe_getenv() REFUSES a value it
1142+
* cannot fit rather than truncating it -- so find_in_path() returned false
1143+
* before looking at a single directory, and opencode (installed through
1144+
* mise/npm/scoop and present on PATH) read as "not installed". The same 4 KB
1145+
* cap hid entries on macOS/Linux too; the fix is not Windows-specific. */
11301146
static bool find_in_path(const char *name, char *out, size_t out_sz) {
1131-
char path_copy[CLI_BUF_4K];
1132-
if (!cbm_safe_getenv("PATH", path_copy, sizeof(path_copy), NULL)) {
1147+
char *path_copy = cbm_env_dup("PATH");
1148+
if (!path_copy) {
11331149
return false;
11341150
}
1135-
char *saveptr;
1151+
1152+
/* One exit. PATH is a full live copy of the environment, the scan has
1153+
* several ways to stop, and the previous shape returned straight out of the
1154+
* loop -- which is exactly what leaked a PATH-sized allocation per call. */
1155+
bool found = false;
1156+
char *saveptr = NULL;
11361157
char *dir = strtok_r(path_copy, PATH_DELIM, &saveptr);
1137-
while (dir) {
1158+
while (dir != NULL && !found) {
11381159
snprintf(out, out_sz, "%s/%s", dir, name);
1139-
if (is_executable(out)) {
1140-
return true;
1141-
}
1160+
found = is_executable(out);
11421161
#ifdef _WIN32
1143-
/* On Windows executables carry an extension (PATHEXT). A CLI like
1144-
* opencode is often installed as a .cmd / .ps1 / .exe shim (e.g. via
1145-
* mise or npm), so the bare-name probe above misses it (#221). Try the
1146-
* common executable extensions before moving to the next PATH entry. */
1147-
static const char *const win_exts[] = {".exe", ".cmd", ".bat", ".ps1", NULL};
1148-
for (int i = 0; win_exts[i]; i++) {
1149-
snprintf(out, out_sz, "%s/%s%s", dir, name, win_exts[i]);
1150-
if (is_executable(out)) {
1151-
return true;
1152-
}
1162+
/* PATHEXT variants for THIS entry, before moving to the next one. */
1163+
for (int i = 0; !found && WIN_EXEC_EXTS[i] != NULL; i++) {
1164+
snprintf(out, out_sz, "%s/%s%s", dir, name, WIN_EXEC_EXTS[i]);
1165+
found = is_executable(out);
11531166
}
11541167
#endif
11551168
dir = strtok_r(NULL, PATH_DELIM, &saveptr);
11561169
}
1157-
return false;
1170+
1171+
cbm_free(CBM_MEM_CLASS_OTHER, path_copy);
1172+
return found;
11581173
}
11591174

11601175
const char *cbm_find_cli(const char *name, const char *home_dir) {
@@ -1168,22 +1183,66 @@ const char *cbm_find_cli(const char *name, const char *home_dir) {
11681183
if (!home_dir || !home_dir[0]) {
11691184
return "";
11701185
}
1186+
/* PATH-independent fallbacks, tried in order. Every home-derived entry is
1187+
* built from the `home_dir` ARGUMENT and not from cbm_get_home_dir(): a
1188+
* dry-run scan or a test passes a synthetic home and must not be shown the
1189+
* host's agents. `/` separators are fine on Windows -- the wide-safe file
1190+
* layer accepts them, and the rest of this file already composes paths so. */
11711191
enum { NUM_PATHS = 5 };
11721192
char paths[NUM_PATHS][CLI_BUF_512];
1173-
snprintf(paths[0], sizeof(paths[0]), "/usr/local/bin/%s", name);
1174-
snprintf(paths[1], sizeof(paths[1]), "%s/.npm/bin/%s", home_dir, name);
1175-
snprintf(paths[2], sizeof(paths[2]), "%s/.local/bin/%s", home_dir, name);
1176-
snprintf(paths[3], sizeof(paths[3]), "%s/.cargo/bin/%s", home_dir, name);
1177-
#ifdef __APPLE__
1178-
snprintf(paths[4], sizeof(paths[4]), "/opt/homebrew/bin/%s", name);
1193+
memset(paths, 0, sizeof(paths));
1194+
int n = 0;
1195+
#ifdef _WIN32
1196+
/* Locations that were already probed on Windows before #221 and are real
1197+
* there: pipx installs into ~/.local/bin and cargo into ~/.cargo/bin, so
1198+
* replacing the list rather than extending it would make pipx- and
1199+
* cargo-installed agents disappear. */
1200+
snprintf(paths[n++], sizeof(paths[0]), "%s/.local/bin/%s", home_dir, name);
1201+
snprintf(paths[n++], sizeof(paths[0]), "%s/.cargo/bin/%s", home_dir, name);
1202+
/* Windows-shaped locations (issue #221, second root cause): npm puts its
1203+
* global bin under %APPDATA%\npm, and mise and scoop expose shims under
1204+
* %LOCALAPPDATA%\mise\shims and %USERPROFILE%\scoop\shims. Each is read
1205+
* through its own environment seam, so the list stays testable. */
1206+
const char *appdata = cbm_app_config_dir();
1207+
if (appdata) {
1208+
snprintf(paths[n++], sizeof(paths[0]), "%s/npm/%s", appdata, name);
1209+
}
1210+
const char *localapp = cbm_app_local_dir();
1211+
if (localapp) {
1212+
snprintf(paths[n++], sizeof(paths[0]), "%s/mise/shims/%s", localapp, name);
1213+
}
1214+
snprintf(paths[n++], sizeof(paths[0]), "%s/scoop/shims/%s", home_dir, name);
11791215
#else
1180-
paths[4][0] = '\0';
1216+
snprintf(paths[n++], sizeof(paths[0]), "/usr/local/bin/%s", name);
1217+
snprintf(paths[n++], sizeof(paths[0]), "%s/.npm/bin/%s", home_dir, name);
1218+
snprintf(paths[n++], sizeof(paths[0]), "%s/.local/bin/%s", home_dir, name);
1219+
snprintf(paths[n++], sizeof(paths[0]), "%s/.cargo/bin/%s", home_dir, name);
1220+
#ifdef __APPLE__
1221+
snprintf(paths[n++], sizeof(paths[0]), "/opt/homebrew/bin/%s", name);
1222+
#endif
11811223
#endif
1182-
for (int i = 0; i < NUM_RETRIES; i++) {
1183-
if (paths[i][0] && is_executable(paths[i])) {
1224+
/* NUM_PATHS, not NUM_RETRIES: both are 5 today, so the old bound only
1225+
* looked right by coincidence -- what the loop means is the list length. */
1226+
for (int i = 0; i < NUM_PATHS; i++) {
1227+
if (!paths[i][0]) {
1228+
continue;
1229+
}
1230+
if (is_executable(paths[i])) {
11841231
snprintf(buf, sizeof(buf), "%s", paths[i]);
11851232
return buf;
11861233
}
1234+
#ifdef _WIN32
1235+
/* The npm/mise/scoop shims carry an extension, so every fallback
1236+
* directory needs the same PATHEXT probe the PATH scan got. */
1237+
for (int e = 0; WIN_EXEC_EXTS[e] != NULL; e++) {
1238+
char extpath[CLI_BUF_512];
1239+
snprintf(extpath, sizeof(extpath), "%s%s", paths[i], WIN_EXEC_EXTS[e]);
1240+
if (is_executable(extpath)) {
1241+
snprintf(buf, sizeof(buf), "%s", extpath);
1242+
return buf;
1243+
}
1244+
}
1245+
#endif
11871246
}
11881247
return "";
11891248
}

‎src/foundation/platform.c‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "foundation/compat.h"
99
#include "foundation/constants.h"
10+
#include "foundation/mem_core.h"
1011
#include "foundation/platform_internal.h"
1112
#include <ctype.h>
1213
#include <errno.h>
@@ -467,6 +468,82 @@ const char *cbm_native_getenv(const char *name, char *buf, size_t buf_sz, const
467468
return NULL;
468469
}
469470

471+
/* See platform.h. The long-value sibling of cbm_safe_getenv: there is no caller
472+
* buffer to overrun, so a PATH past 4 KB is read in full instead of being
473+
* refused (#221). Both the wide staging buffer and the UTF-8 result are
474+
* allocated through the memory core, which is also the only allocator the
475+
* caller may release them with -- keeping the memory-core ratchet honest means
476+
* this file adds no raw allocation site to pay for the new read. */
477+
char *cbm_env_dup(const char *name) {
478+
if (!name || !name[0]) {
479+
return NULL;
480+
}
481+
/* The same view cbm_safe_getenv gives: the startup settings resolver
482+
* answers first, and a handled NULL masks the variable. */
483+
const char *resolved = NULL;
484+
if (environment_resolver && environment_resolver(name, &resolved)) {
485+
return resolved ? cbm_mem_strdup(CBM_MEM_CLASS_OTHER, resolved) : NULL;
486+
}
487+
#ifdef _WIN32
488+
/* Same wide route as cbm_safe_getenv: _environ holds ANSI-code-page bytes,
489+
* so a non-ASCII PATH would arrive mojibake'd or with '?' substituted --
490+
* invalid in a Windows path and fatal to every file API downstream. */
491+
wchar_t wname[CBM_SZ_256];
492+
int wn = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, name, -1, wname, CBM_SZ_256);
493+
if (wn <= 0) {
494+
return NULL;
495+
}
496+
SetLastError(ERROR_SUCCESS);
497+
DWORD needed = GetEnvironmentVariableW(wname, NULL, 0U);
498+
DWORD lookup_error = GetLastError();
499+
if (needed == 0U) {
500+
if (lookup_error != ERROR_ENVVAR_NOT_FOUND) {
501+
/* Present but empty. Distinct from unset, and callers are entitled
502+
* to tell the two apart, so return "" and not NULL. */
503+
return cbm_mem_strdup(CBM_MEM_CLASS_OTHER, "");
504+
}
505+
return NULL;
506+
}
507+
wchar_t *wvalue = cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)needed * sizeof(*wvalue));
508+
if (!wvalue) {
509+
return NULL;
510+
}
511+
SetLastError(ERROR_SUCCESS);
512+
DWORD got = GetEnvironmentVariableW(wname, wvalue, needed);
513+
DWORD read_error = GetLastError();
514+
if (got >= needed || (got == 0U && read_error != ERROR_SUCCESS)) {
515+
cbm_free(CBM_MEM_CLASS_OTHER, wvalue);
516+
return NULL;
517+
}
518+
int utf8_size =
519+
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wvalue, -1, NULL, 0, NULL, NULL);
520+
char *utf8 = utf8_size > 0 ? cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)utf8_size) : NULL;
521+
int converted = utf8 ? WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wvalue, -1, utf8,
522+
utf8_size, NULL, NULL)
523+
: 0;
524+
if (!utf8 || converted != utf8_size) {
525+
cbm_free(CBM_MEM_CLASS_OTHER, utf8);
526+
cbm_free(CBM_MEM_CLASS_OTHER, wvalue);
527+
return NULL;
528+
}
529+
cbm_free(CBM_MEM_CLASS_OTHER, wvalue);
530+
return utf8;
531+
#else
532+
/* Same environ walk as cbm_safe_getenv: getenv() returns a pointer another
533+
* thread can invalidate with setenv(), so the value is copied out here. */
534+
char **env = CBM_ENVIRON;
535+
if (env) {
536+
size_t name_len = strlen(name);
537+
for (; *env; env++) {
538+
if (strncmp(*env, name, name_len) == 0 && (*env)[name_len] == '=') {
539+
return cbm_mem_strdup(CBM_MEM_CLASS_OTHER, *env + name_len + SKIP_ONE);
540+
}
541+
}
542+
}
543+
return NULL;
544+
#endif
545+
}
546+
470547
/* See platform.h. The shape here is the one src/main.c:1104 already uses for
471548
* --port=: an end pointer says where the read stopped, errno catches a number
472549
* too large, and *end == '\0' catches anything left over. */

‎src/foundation/platform.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ const char *cbm_runtime_getenv(const char *name);
147147
/* Explicit inherited environment access for bootstrap/settings provenance. */
148148
const char *cbm_native_getenv(const char *name, char *buf, size_t buf_sz, const char *fallback);
149149

150+
/* Allocating sibling of cbm_safe_getenv, for a value that must not be capped at
151+
* a caller-chosen size. A Windows PATH routinely runs past the 4096-byte buffer
152+
* cbm_safe_getenv's callers pass, and the copy helper REPORTS that as failure
153+
* rather than truncating -- so the whole PATH was skipped and every directory
154+
* past the cut became invisible (#221).
155+
*
156+
* Returns a UTF-8 copy owned by the memory core: release it with
157+
* cbm_free(CBM_MEM_CLASS_OTHER, ptr), never free(). NULL when the variable is
158+
* unset or cannot be read; a variable that exists but is empty yields "".
159+
* On Windows the value is read with the wide API and converted, the same route
160+
* cbm_safe_getenv takes, so a non-ASCII value survives intact. Like
161+
* cbm_safe_getenv, an installed settings resolver answers before the process
162+
* environment does. */
163+
char *cbm_env_dup(const char *name);
164+
150165
/* Read an environment variable as a whole number.
151166
*
152167
* Answers true only when the variable is set, is not empty, and reads cleanly

0 commit comments

Comments
 (0)