continuedev/continue · error · ContinueError

FindAndReplaceMultipleOccurrences

FindAndReplaceMultipleOccurrences

Error message

Edit at index ${editIndex}: String "${oldString}" appears ${matches.length} times in the file. Either provide a more specific string with surrounding context to make it unique, or use replace_all=true to replace all occurrences.

What it means

Thrown when old_string matches more than once and replace_all is not enabled. Because a non-replace_all edit must be unambiguous, the library reports the match count and asks for either a more specific string or replace_all=true.

Source

Thrown at core/edit/searchAndReplace/performReplace.ts:121

    let result = fileContent;
    for (let i = matches.length - 1; i >= 0; i--) {
      const match = matches[i];
      const adjustedNew = adjustReplacementIndentation(
        result,
        match,
        oldString,
        newString,
      );
      result =
        result.substring(0, match.startIndex) +
        adjustedNew +
        result.substring(match.endIndex);
    }
    return result;
  } else {
    // For single replacement, check for multiple matches first
    if (matches.length > 1) {
      throw new ContinueError(
        ContinueErrorReason.FindAndReplaceMultipleOccurrences,
        `Edit at index ${editIndex}: String "${oldString}" appears ${matches.length} times in the file. Either provide a more specific string with surrounding context to make it unique, or use replace_all=true to replace all occurrences.`,
      );
    }

    // Apply single replacement
    const match = matches[0];
    const adjustedNew = adjustReplacementIndentation(
      fileContent,
      match,
      oldString,
      newString,
    );
    return (
      fileContent.substring(0, match.startIndex) +
      adjustedNew +
      fileContent.substring(match.endIndex)
    );

View on GitHub (pinned to 5522c6f44c)

Solutions

  1. Add surrounding context lines to old_string so it matches exactly once
  2. Set replace_all: true if you really intend to replace every occurrence
  3. Use the reported match count to iterate: narrow with preceding/following lines until unique

Example fix

// before
{ old_string: "return null;", new_string: "return undefined;" }
// after
{ old_string: "function findUser(id) {\n  return null;", new_string: "function findUser(id) {\n  return undefined;" }
Defensive patterns

Strategy: validation

Validate before calling

const n = fileContent.split(oldString).length - 1; if (n > 1 && !replaceAll) throw new Error(`ambiguous: ${n} matches`);

Type guard

const isUnique = (content: string, s: string) => content.indexOf(s) === content.lastIndexOf(s);

Try / catch

catch (e) { if (e.message.includes('appears') && e.message.includes('times')) { retry with replace_all or longer context; } else throw e; }

Prevention

When it happens

Trigger: old_string matches 2+ locations in the file, e.g. replacing a common identifier like "const x" that appears multiple times, without replace_all.

Common situations: Replacing short/common tokens (import lines, repeated JSX patterns, variable names); refactoring duplicated code blocks; LLMs quoting only a function signature that appears in both declaration and calls.

Related errors


AI-assisted analysis of continuedev/continue@5522c6f44c (2026-08-27). Data as JSON: /api/errors/681003d07619433f. Report an issue: GitHub.