Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(search): make scoped file patterns portable

Martin Vogel7a1e069b2950
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 6a9b356d1..f2772835a 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -92,6 +92,7 @@ enum {
#define mcp_fdopen _fdopen
#define mcp_close _close
#else
+#include <fnmatch.h>
#include <unistd.h>
#include <poll.h>
#include <fcntl.h>
@@ -12400,21 +12401,15 @@ void cbm_search_code_build_grep_cmd(char *cmd, size_t cmd_sz, bool use_regex, bo
#else
const char *flag = use_regex ? "-E" : "-F";
if (scoped) {
- if (file_pattern) {
- /* -0: read NUL-separated paths from the filelist so paths containing
- * spaces stay one argument (issue #687). Pairs with the NUL separator
- * written by write_scoped_filelist. */
- snprintf(cmd, cmd_sz,
- "xargs -0 sh -c 'grep -Hn -d skip %s --include=\"%s\" -f \"%s\" \"$@\"; "
- "status=$?; [ \"$status\" -eq 0 ] || [ \"$status\" -eq 1 ]' sh < '%s' "
- "2>/dev/null",
- flag, file_pattern, tmpfile, filelist);
- } else {
- snprintf(cmd, cmd_sz,
- "xargs -0 sh -c 'grep -Hn -d skip %s -f \"%s\" \"$@\"; status=$?; "
- "[ \"$status\" -eq 0 ] || [ \"$status\" -eq 1 ]' sh < '%s' 2>/dev/null",
- flag, tmpfile, filelist);
- }
+ /* file_pattern was already applied to the canonical file list in C.
+ * Keep this scan compatible with BusyBox grep, which has no GNU
+ * --include option (the shipped Alpine/static runtime). */
+ snprintf(cmd, cmd_sz,
+ "xargs -0 sh -c 'pat=$1; shift; if [ \"$#\" -eq 0 ]; then exit 0; fi; "
+ "grep -Hn %s -f \"$pat\" -- \"$@\"; rc=$?; if [ \"$rc\" -eq 1 ]; then "
+ "exit 0; fi; if [ \"$rc\" -ne 0 ]; then exit 255; fi; exit 0' sh '%s' "
+ "< '%s' 2>/dev/null",
+ flag, tmpfile, filelist);
} else {
/* Do not pipe discovery directly into sort/xargs: POSIX sh reports only
* the final pipeline command, which can hide a partial find or failed
@@ -13785,6 +13780,17 @@ static bool write_scoped_filelist(cbm_mcp_server_t *srv, const char *project, co
continue;
}
}
+#ifndef _WIN32
+ /* GNU grep's --include is unavailable in BusyBox grep. Filter the
+ * canonical list before xargs instead, preserving grep's basename
+ * glob semantics without making the shipped static binary depend
+ * on GNU userland. Windows keeps its PowerShell -like filter. */
+ const char *basename = strrchr(indexed_files[fi], '/');
+ basename = basename ? basename + SKIP_ONE : indexed_files[fi];
+ if (file_pattern && fnmatch(file_pattern, basename, 0) != 0) {
+ continue;
+ }
+#endif
#ifdef _WIN32
if (cbm_search_code_file_pattern_can_prefilter(file_pattern) &&
!cbm_search_code_windows_path_matches_prefilter(indexed_files[fi], file_pattern)) {
@@ -14366,7 +14372,7 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
bool scan_ok = sr != NULL;
uint64_t scan_t0 = metrics.include_phase_timings ? cbm_now_ms() : 0;
if (scoped && scoped_written == 0 && !scan_cancellation_latched && !scan_deadline_latched) {
- /* The path_filter excluded every indexed file — nothing to scan.
+ /* The path_filter (or POSIX file_pattern) excluded every indexed file — nothing to scan.
* Skip the grep subprocess: xargs on an empty filelist is
* platform-dependent (GNU execs grep once with no operands, BSD
* skips), and the post-grep filter would drop every hit anyway. */
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index 0a087e314..ae962e624 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -8568,6 +8568,86 @@ TEST(search_code_scoped_scan_uses_canonical_file_nodes) {
PASS();
}
+TEST(search_code_scoped_file_pattern_is_busybox_portable) {
+#ifdef _WIN32
+ SKIP_PLATFORM("POSIX BusyBox-compatible grep contract");
+#else
+ char tmp[CBM_SZ_512];
+ snprintf(tmp, sizeof(tmp), "%s/cbm-search-busybox-XXXXXX", cbm_tmpdir());
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+
+ char bin_dir[CBM_SZ_1K];
+ char fake_grep[CBM_SZ_1K];
+ char source_path[CBM_SZ_1K];
+ snprintf(bin_dir, sizeof(bin_dir), "%s/bin", tmp);
+ snprintf(fake_grep, sizeof(fake_grep), "%s/grep", bin_dir);
+ snprintf(source_path, sizeof(source_path), "%s/R&D-main.c", tmp);
+ ASSERT_EQ(cbm_mkdir(bin_dir), 0);
+ ASSERT_EQ(th_write_file(fake_grep,
+ "#!/bin/sh\n"
+ "for arg in \"$@\"; do\n"
+ " case \"$arg\" in --include*) exit 97 ;; esac\n"
+ "done\n"
+ "if [ -x /usr/bin/grep ]; then exec /usr/bin/grep \"$@\"; fi\n"
+ "exec /bin/grep \"$@\"\n"),
+ 0);
+ ASSERT_EQ(chmod(fake_grep, 0700), 0);
+ ASSERT_EQ(th_write_file(source_path, "int BUSYBOX_GREP_NEEDLE = 1;\n"), 0);
+
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ cbm_store_t *store = cbm_mcp_server_store(srv);
+ ASSERT_NOT_NULL(store);
+ const char *project = "search-busybox";
+ cbm_mcp_server_set_project(srv, project);
+ ASSERT_EQ(cbm_store_upsert_project(store, project, tmp), CBM_STORE_OK);
+ cbm_node_t file = {.project = project,
+ .label = "File",
+ .name = "R&D-main.c",
+ .qualified_name = "search-busybox.R&D-main.c",
+ .file_path = "R&D-main.c"};
+ cbm_node_t symbol = {.project = project,
+ .label = "Variable",
+ .name = "BUSYBOX_GREP_NEEDLE",
+ .qualified_name = "search-busybox.BUSYBOX_GREP_NEEDLE",
+ .file_path = "R&D-main.c",
+ .start_line = 1,
+ .end_line = 1};
+ ASSERT_GT(cbm_store_upsert_node(store, &file), 0);
+ ASSERT_GT(cbm_store_upsert_node(store, &symbol), 0);
+
+ mcp_test_env_backup_t path_backup = {.name = "PATH"};
+ const char *path = getenv(path_backup.name);
+ path_backup.present = path != NULL;
+ path_backup.value = path ? strdup(path) : NULL;
+ ASSERT_TRUE(!path || path_backup.value != NULL);
+ char forced_path[CBM_SZ_2K];
+ int forced_length = snprintf(forced_path, sizeof(forced_path), "%s:/usr/bin:/bin", bin_dir);
+ ASSERT_TRUE(forced_length > 0 && (size_t)forced_length < sizeof(forced_path));
+ ASSERT_EQ(cbm_setenv("PATH", forced_path, 1), 0);
+
+ char *response = cbm_mcp_handle_tool(
+ srv, "search_code",
+ "{\"pattern\":\"BUSYBOX_GREP_NEEDLE\",\"project\":\"search-busybox\","
+ "\"file_pattern\":\"*R&D*.c\",\"format\":\"json\"}");
+ mcp_test_restore_env(&path_backup, 1U);
+ char *inner = extract_text_content(response);
+ bool portable_success = response && !strstr(response, "\"isError\":true") && inner &&
+ strstr(inner, "\"total_grep_matches\":1") &&
+ strstr(inner, "search-busybox.BUSYBOX_GREP_NEEDLE");
+
+ free(inner);
+ free(response);
+ cbm_mcp_server_free(srv);
+ cbm_unlink(source_path);
+ cbm_unlink(fake_grep);
+ cbm_rmdir(bin_dir);
+ cbm_rmdir(tmp);
+ ASSERT_TRUE(portable_success);
+ PASS();
+#endif
+}
+
TEST(search_code_recursive_fallback_propagates_discovery_failures) {
#ifdef _WIN32
SKIP_PLATFORM("POSIX find/sort/xargs fallback contract");
@@ -19763,6 +19843,7 @@ SUITE(mcp) {
RUN_TEST(search_code_scans_complete_stream_and_ranks_globally);
RUN_TEST(search_code_fails_closed_when_complete_scan_is_impossible);
RUN_TEST(search_code_scoped_scan_uses_canonical_file_nodes);
+ RUN_TEST(search_code_scoped_file_pattern_is_busybox_portable);
RUN_TEST(search_code_recursive_fallback_propagates_discovery_failures);
RUN_TEST(search_code_default_budget_limits_raw_rows_before_graph_results);
RUN_TEST(search_code_raw_and_directory_remainders_are_independently_pageable);