Skip to content

search_code returns 0 results on Windows post-#310: Select-String via _popen fails silently #348

@tangmengran

Description

@tangmengran

Bug: search_code returns 0 results on Windows — Select-String via _popen fails with "系统找不到指定的路径"

Repository: DeusData/codebase-memory-mcp
Environment: Windows 11 25H2, PowerShell 5.1, codebase-memory-mcp binary (post-#310 merge, PowerShell Select-String path)
Index mode: full (5,853 nodes / 10,978 edges)
Project: Python backend + TypeScript frontend (~200 files)


Summary

After PR #310 ("add Windows support for code search via PowerShell"), search_code is callable without errors but always returns 0 results on Windows. The underlying PowerShell Select-String command executes but fails to match any files, with the error 系统找不到指定的路径 (system cannot find the path specified). Manual reproduction of the same PowerShell pipeline works correctly, pointing to a path resolution issue in the _popen child-process context.

Steps to Reproduce

  1. Index a project with mode=full (confirmed 5,853 nodes via index_status)
  2. Call search_code via MCP:
{
  "pattern": "def create_capsule",
  "project": "C-projects-edit_center",
  "limit": 10,
  "mode": "compact"
}
  1. Result: {"results":[], "total_grep_matches": 0, "total_results": 0}
  2. Try CLI directly:
codebase-memory-mcp cli search_code "{\"pattern\":\"def create_capsule\",\"project\":\"C-projects-edit_center\",\"limit\":5,\"mode\":\"compact\"}"
  1. Result: Same — 0 results, stderr includes 系统找不到指定的路径

Manual Verification (PowerShell Works)

The exact same PowerShell pipeline, when run manually, works correctly:

$pat = Get-Content "$env:TEMP/cbm_search_<pid>.pat"
Get-Content "$env:TEMP/cbm_search_<pid>.files" | ForEach-Object {
    Select-String -LiteralPath $_ -Pattern $pat -SimpleMatch -ErrorAction SilentlyContinue
} | ForEach-Object { $_.Path + "`t" + $_.LineNumber + "`t" + $_.Line }

✅ This finds matches. The Select-String invocation itself is correct.

Root Cause Analysis

The issue is in build_grep_cmd() at src/mcp/mcp.c. The Windows branch (post-#310) constructs:

powershell -Command "$pat = Get-Content '<tmpdir>/cbm_search_<pid>.pat'; Get-Content '<tmpdir>/cbm_search_<pid>.files' | ForEach-Object { Select-String -LiteralPath $_ -Pattern $pat -SimpleMatch -ErrorAction SilentlyContinue } | ForEach-Object { $_.Path + [char]9 + $_.LineNumber + [char]9 + $_.Line }"

The write_scoped_filelist function writes indexed file paths to the temp filelist. Per PR #310, these paths now use forward slashes (e.g., C:/projects/edit_center/backend/services/capsule_service.py).

Hypothesis: When _popen/CreateProcess spawns the PowerShell child process, the working directory or path resolution context differs from the interactive shell. This causes Select-String -LiteralPath to fail resolving the forward-slash paths from the filelist, even though the same paths work in an interactive PowerShell session.

Alternative possibility: The temp filelist itself may not be flushed/closed before the PowerShell child process reads it, causing an empty filelist to be piped into Select-String.

Impact

search_code is effectively non-functional on Windows post-#310. Users see silent empty results with no error surfaced through the MCP/CLI response — only visible in stderr. search_graph (BM25) works correctly and serves as a workaround.

Suggested Fixes

  1. Flush and close the filelist before spawning PowerShell — ensure fclose() on the filelist before _popen().
  2. Use absolute paths with backslashes in the filelist — Windows Select-String handles both, but backslashes are canonical and avoid potential resolution issues in child processes.
  3. Add -NoProfile -NonInteractive to the PowerShell invocation to avoid profile-loading side effects.
  4. Capture and surface stderr from the PowerShell child process so failures are visible to the caller instead of silently returning 0 results.

Environment Details

  • OS: Windows 11 25H2 (OS Build 26200+)
  • Shell: PowerShell 5.1.22621 (also tested with cmd.exe — same result)
  • Binary source: codebase-memory-mcp.exe (post feat(mcp): add Windows support for code search via PowerShell #310 merge, includes PowerShell Select-String path)
  • Index: full mode, 5,853 nodes, 10,978 edges, project C-projects-edit_center
  • Affected: Both MCP search_code tool and CLI search_code subcommand

Related Issues/PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions