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. */
11301146static 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
11601175const 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}
0 commit comments