@@ -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