CherryHQ/cherry-studio · warning · Error

Pattern is required for grep

Error message

Pattern is required for grep

What it means

After zod parsing succeeds, the handler does an explicit truthiness check on data.pattern — an empty string ('') passes z.string() but is not a usable regex. This guard fires before the regex is constructed, so the caller gets a clear message instead of a confusing 'Invalid regex pattern' for the empty string. It is belt-and-suspenders with the later regex try/catch (error 317).

Source

Thrown at src/main/ai/mcp/servers/filesystem/tools/grep.ts:45

- Results are limited to 100 matches
- Binary files are automatically skipped
- Common directories (node_modules, .git, dist) are excluded
- The path parameter must resolve within the configured workspace root if specified
- If path is not specified, defaults to the base directory`,
  inputSchema: z.toJSONSchema(GrepToolSchema)
}

// Handler implementation
export async function handleGrepTool(args: unknown, baseDir: string) {
  const parsed = GrepToolSchema.safeParse(args)
  if (!parsed.success) {
    throw new Error(`Invalid arguments for grep: ${parsed.error}`)
  }

  const data = parsed.data

  if (!data.pattern) {
    throw new Error('Pattern is required for grep')
  }

  const searchPath = data.path || baseDir
  const validPath = await validatePath(searchPath, baseDir)

  const matches: GrepMatch[] = []
  let truncated = false
  let regex: RegExp

  // Build ripgrep arguments
  const rgArgs: string[] = [
    '--no-heading',
    '--line-number',
    '--color',
    'never',
    '--ignore-case',
    '--glob',
    '!.git/**',

View on GitHub (pinned to 726446b54c)

Solutions

  1. Provide a concrete regex pattern. To match everything, use '.*'.
  2. If the intent is 'list files' rather than 'search contents', use the glob tool instead of grep.

Example fix

// before
if (!data.pattern) {
  throw new Error('Pattern is required for grep')
}

// after — collapse this guard into the zod schema so there is one validation site
// (in the schema): pattern: z.string().min(1).describe('...')
// then remove the redundant runtime check
Defensive patterns

Strategy: validation

Validate before calling

// Reject empty grep patterns before dispatching.
function isNonEmptyGrepPattern(p: unknown): boolean {
  return typeof p === 'string' && p.length > 0
}

Prevention

When it happens

Trigger: The caller sends pattern as ''. zod accepts ''; this guard rejects it. Distinct from a malformed regex (which would hit error 317).

Common situations: A model sending an empty pattern expecting 'match everything'; a templating layer that substitutes undefined with ''; a client bug that drops the pattern field value while keeping the key.

Related errors


AI-assisted analysis of CherryHQ/cherry-studio@726446b54c (2026-08-12). Data as JSON: /api/errors/3c05ffb911481b7f. Report an issue: GitHub.