{"record":{"id":"3e8a6baae649c542","repo":"sgl-project/sglang","slug":"match-text-found-multiple-times-len-found-indice","errorCode":null,"errorMessage":"match text found multiple times ({len(found_indices)} occurrences) in source:\\n{preview}","messagePattern":"match text found multiple times \\((.+?) occurrences\\) in source:\\\\n(.+?)","errorType":"exception","errorClass":"PatchApplicationError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/debug_utils/source_patcher/source_editor.py","lineNumber":75,"sourceCode":"    Raises PatchApplicationError if not found or found multiple times.\n    \"\"\"\n    stripped_source: list[str] = [line.strip() for line in source_lines]\n    stripped_match: list[str] = [line.strip() for line in match_lines]\n    match_len: int = len(stripped_match)\n\n    found_indices: list[int] = [\n        i\n        for i in range(len(stripped_source) - match_len + 1)\n        if stripped_source[i : i + match_len] == stripped_match\n    ]\n\n    if len(found_indices) == 0:\n        raise PatchApplicationError(\n            _not_found_diagnostic(stripped_source, stripped_match)\n        )\n    if len(found_indices) > 1:\n        preview = \"\\n\".join(match_lines)\n        raise PatchApplicationError(\n            f\"match text found multiple times ({len(found_indices)} occurrences) in source:\\n{preview}\"\n        )\n\n    return found_indices[0]\n\n\ndef _not_found_diagnostic(stripped_source: list[str], stripped_match: list[str]) -> str:\n    preview = \"\\n\".join(stripped_match)\n    lines = [\n        f\"match text not found in source:\\n{preview}\",\n        \"\",\n        f\"source_len={len(stripped_source)} lines\",\n    ]\n\n    if not stripped_match:\n        return \"\\n\".join(lines)\n    first_match_line = stripped_match[0]\n    hits = [i for i, line in enumerate(stripped_source) if line == first_match_line]","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/debug_utils/source_patcher/source_editor.py#L57-L93","documentation":"Raised by the source patcher when the `match` text given in an edit is found more than once in the target source. The patcher applies edits by exact (stripped) text matching and requires the match to be unique so it can locate a single anchor line, so multiple occurrences make the edit ambiguous.","triggerScenarios":"Calling _apply_single_edit (e.g. via a SourceEditor apply/patch API) with a `match` string that appears 2+ times in the file after whitespace stripping. The error message previews every matching line so you can disambiguate.","commonSituations":"Patching boilerplate that repeats across a file, such as multiple identical function signatures, repeated `return None` lines, or duplicated import blocks; also matching a too-short snippet (e.g. a bare decorator or blank-line-adjacent line).","solutions":["Enlarge the `match` text to include unique surrounding lines (e.g. the def line plus the first body statement) so only one occurrence exists","Check the preview in the error message to see all matching lines and pick distinguishing context to add","If you intend to replace all occurrences, split the source or apply edits one region at a time with unique anchors"],"exampleFix":"// before\nedit = Edit(match=\"return None\", replacement=\"return 0\")  # 'return None' appears 3x\n// after\nedit = Edit(match=\"def calc(self):\\n    return None\", replacement=\"def calc(self):\\n    return 0\")","handlingStrategy":"validation","validationCode":"stripped = source.replace('\\\\r\\\\n','\\\\n').strip()\\ncount = stripped.count(match_text.strip())\\nassert count == 1, f'match appears {count} times; extend match text with unique context'","typeGuard":null,"tryCatchPattern":"from sglang.srt.debug_utils.source_patcher.source_editor import PatchApplicationError\\ntry:\\n    editor.apply(edit)\\nexcept PatchApplicationError as e:\\n    # message lists all occurrences; widen `edit.match` and retry once\\n    edit = edit.with_larger_match(context_lines=2)\\n    editor.apply(edit)","preventionTips":["Always include a unique anchor line (e.g. the def/class statement) in match text","Run a uniqueness count on the match string before applying the patch","Keep match snippets at least a few lines long in repetitive files"],"tags":["source-patching","text-match","ambiguity","sglang"],"backgroundTag":"ambiguous-text-match","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}