Skip to content

Commit 01c5921

Browse files
authored
Merge pull request DeusData#751 from DeusData/distill/693-scoped-search-spaces
fix(search): scoped search must handle repo paths containing spaces
2 parents 1cca858 + 3dd2471 commit 01c5921

2 files changed

Lines changed: 97 additions & 6 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,10 +3857,13 @@ static void build_grep_cmd(char *cmd, size_t cmd_sz, bool use_regex, bool scoped
38573857
const char *flag = use_regex ? "-E" : "-F";
38583858
if (scoped) {
38593859
if (file_pattern) {
3860-
snprintf(cmd, cmd_sz, "xargs grep -Hn %s --include='%s' -f '%s' < '%s' 2>/dev/null",
3860+
/* -0: read NUL-separated paths from the filelist so paths containing
3861+
* spaces stay one argument (issue #687). Pairs with the NUL separator
3862+
* written by write_scoped_filelist. */
3863+
snprintf(cmd, cmd_sz, "xargs -0 grep -Hn %s --include='%s' -f '%s' < '%s' 2>/dev/null",
38613864
flag, file_pattern, tmpfile, filelist);
38623865
} else {
3863-
snprintf(cmd, cmd_sz, "xargs grep -Hn %s -f '%s' < '%s' 2>/dev/null", flag, tmpfile,
3866+
snprintf(cmd, cmd_sz, "xargs -0 grep -Hn %s -f '%s' < '%s' 2>/dev/null", flag, tmpfile,
38643867
filelist);
38653868
}
38663869
} else {
@@ -4276,10 +4279,22 @@ static bool write_scoped_filelist(cbm_mcp_server_t *srv, const char *project, co
42764279
bool ok = false;
42774280
if (fl) {
42784281
for (int fi = 0; fi < indexed_count; fi++) {
4279-
/* Use forward slashes so xargs doesn't interpret Windows
4280-
* backslashes as escape sequences (e.g. \n becomes newline).
4281-
* Binary mode to prevent CRLF (xargs would see trailing \r). */
4282-
(void)fprintf(fl, "%s/%s\n", root_path, indexed_files[fi]);
4282+
/* Write "<root>/<file>" piece-by-piece (no fixed-size buffer, so an
4283+
* arbitrarily long absolute path cannot overflow). Forward slash join
4284+
* so xargs doesn't treat Windows backslashes as escapes; binary mode
4285+
* (wb) prevents CRLF translation. Record separator differs by platform:
4286+
* - Unix: NUL, consumed by `xargs -0` — handles spaces in paths (a
4287+
* newline separator would split plain xargs on the space).
4288+
* - Windows: newline, consumed by PowerShell `Get-Content |
4289+
* Select-String -LiteralPath` (NUL bytes break Get-Content). */
4290+
(void)fwrite(root_path, 1, strlen(root_path), fl);
4291+
(void)fputc('/', fl);
4292+
(void)fwrite(indexed_files[fi], 1, strlen(indexed_files[fi]), fl);
4293+
#ifdef _WIN32
4294+
(void)fputc('\n', fl);
4295+
#else
4296+
(void)fputc('\0', fl);
4297+
#endif
42834298
}
42844299
(void)fclose(fl);
42854300
ok = true;

‎tests/test_mcp.c‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,81 @@ TEST(search_code_multi_word) {
13041304
PASS();
13051305
}
13061306

1307+
/* Reproduce-first (#687): scoped content search over a repo whose ROOT PATH
1308+
* contains a space. write_scoped_filelist emits "<root>/<file>" records that the
1309+
* Unix pipeline pipes to grep via xargs. With plain `xargs` (newline-split) the
1310+
* space splits one path into several bogus args -> grep finds nothing ->
1311+
* total_grep_matches == 0 (RED on the unfixed code). The fix writes NUL-separated
1312+
* records + uses `xargs -0`, so the path stays a single argument -> match found
1313+
* (GREEN). On Windows the scoped path uses PowerShell Get-Content -LiteralPath,
1314+
* which already handles spaces, so this asserts correct behavior there too. */
1315+
TEST(search_code_scoped_path_with_spaces_issue687) {
1316+
char tmp[512];
1317+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_srch_space_XXXXXX");
1318+
if (!cbm_mkdtemp(tmp)) {
1319+
FAIL("cbm_mkdtemp failed");
1320+
}
1321+
1322+
/* Project root deliberately contains a space. */
1323+
char proj_dir[640];
1324+
snprintf(proj_dir, sizeof(proj_dir), "%s/my project", tmp);
1325+
cbm_mkdir(proj_dir);
1326+
1327+
char src_path[768];
1328+
snprintf(src_path, sizeof(src_path), "%s/main.go", proj_dir);
1329+
FILE *fp = fopen(src_path, "w");
1330+
if (!fp) {
1331+
rmdir(proj_dir);
1332+
rmdir(tmp);
1333+
FAIL("cannot write source file under spaced path");
1334+
}
1335+
fprintf(fp, "package main\n\nfunc HandleRequest() error {\n\treturn nil\n}\n");
1336+
fclose(fp);
1337+
1338+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
1339+
ASSERT_NOT_NULL(srv);
1340+
cbm_store_t *st = cbm_mcp_server_store(srv);
1341+
ASSERT_NOT_NULL(st);
1342+
const char *proj = "space-search";
1343+
cbm_mcp_server_set_project(srv, proj);
1344+
cbm_store_upsert_project(st, proj, proj_dir);
1345+
1346+
/* A node so the file is "indexed" (cbm_store_list_files -> scoped grep path)
1347+
* and the grep hit classifies to a result. */
1348+
cbm_node_t n = {.project = proj,
1349+
.label = "Function",
1350+
.name = "HandleRequest",
1351+
.qualified_name = "space-search.main.HandleRequest",
1352+
.file_path = "main.go",
1353+
.start_line = 3,
1354+
.end_line = 5};
1355+
ASSERT_GT(cbm_store_upsert_node(st, &n), 0);
1356+
1357+
char *resp = cbm_mcp_server_handle(
1358+
srv, "{\"jsonrpc\":\"2.0\",\"id\":94,\"method\":\"tools/call\","
1359+
"\"params\":{\"name\":\"search_code\","
1360+
"\"arguments\":{\"pattern\":\"HandleRequest\",\"project\":\"space-search\"}}}");
1361+
ASSERT_NOT_NULL(resp);
1362+
char *inner = extract_text_content(resp);
1363+
ASSERT_NOT_NULL(inner);
1364+
1365+
/* grep must have found the match despite the space in the root path. */
1366+
int grep_matches = -1;
1367+
const char *g = strstr(inner, "\"total_grep_matches\":");
1368+
if (g) {
1369+
sscanf(g, "\"total_grep_matches\":%d", &grep_matches);
1370+
}
1371+
ASSERT_TRUE(grep_matches > 0);
1372+
1373+
free(inner);
1374+
free(resp);
1375+
cbm_mcp_server_free(srv);
1376+
unlink(src_path);
1377+
rmdir(proj_dir);
1378+
rmdir(tmp);
1379+
PASS();
1380+
}
1381+
13071382
/* issue #283: search_code with regex=true and a syntactically invalid pattern
13081383
* must return an explicit error, not an empty result indistinguishable from a
13091384
* legitimate no-match. */
@@ -3248,6 +3323,7 @@ SUITE(mcp) {
32483323
RUN_TEST(tool_search_code_missing_pattern);
32493324
RUN_TEST(tool_search_code_no_project);
32503325
RUN_TEST(search_code_multi_word);
3326+
RUN_TEST(search_code_scoped_path_with_spaces_issue687);
32513327
RUN_TEST(search_code_invalid_regex_errors_issue283);
32523328
RUN_TEST(search_code_literal_pipe_warns_issue282);
32533329
RUN_TEST(search_code_ampersand_accepted_issue272);

0 commit comments

Comments
 (0)