charmbracelet/crush · error

invalid include pattern: %w

Error message

invalid include pattern: %w

What it means

When an 'include' glob filter is provided, it is converted to a regex with globToRegex and compiled through a cache. If that derived regex fails to compile, the error is wrapped as 'invalid include pattern'. The search pattern itself is valid; only the file-filtering glob is bad.

Source

Thrown at internal/agent/tools/grep.go:301

		} `json:"submatches"`
	} `json:"data"`
}

func searchFilesWithRegex(pattern, rootPath, include string) ([]grepMatch, error) {
	matches := []grepMatch{}

	// Use cached regex compilation
	regex, err := searchRegexCache.get(pattern)
	if err != nil {
		return nil, fmt.Errorf("invalid regex pattern: %w", err)
	}

	var includePattern *regexp.Regexp
	if include != "" {
		regexPattern := globToRegex(include)
		includePattern, err = globRegexCache.get(regexPattern)
		if err != nil {
			return nil, fmt.Errorf("invalid include pattern: %w", err)
		}
	}

	// Create walker with gitignore and crushignore support
	walker := fsext.NewFastGlobWalker(rootPath)

	err = filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return nil // Skip errors
		}

		if info.IsDir() {
			// Check if directory should be skipped
			if walker.ShouldSkip(path) {
				return filepath.SkipDir
			}
			return nil // Continue into directory
		}

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Use a simple, well-formed glob such as '*.go' or '**/*.ts'.
  2. Check for unbalanced braces or brackets in the include string.
  3. Omit the include parameter to search all files, then filter results manually.

Example fix

// before
searchFiles(pattern, root, "**/*.{go,")
// after
searchFiles(pattern, root, "**/*.go")
Defensive patterns

Strategy: validation

Validate before calling

re := globToRegex(include)
if _, err := regexp.Compile(re); err != nil {
    return fmt.Errorf("bad include %q: %w", include, err)
}

Type guard

null

Try / catch

if err != nil {
    return nil, fmt.Errorf("include filter rejected: %w", err)
}

Prevention

When it happens

Trigger: Calling the grep tool with an include parameter whose glob-to-regex translation yields an uncompilable regexp, e.g. pathological character classes or malformed brace expansions in the include string.

Common situations: Typing include filters like '**/*.{go,rs' with unbalanced braces, or platform-specific path separators slipping into the include argument.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/5538f273847c2592. Report an issue: GitHub.