sgl-project/sglang · error · PatchApplicationError

match text not found in source:\n{preview}\n\nsource_len={le

Error message

match text not found in source:\n{preview}\n\nsource_len={len(stripped_source)} lines

What it means

Raised by _find_match when the (whitespace-stripped) match lines from an EditSpec appear nowhere in the (whitespace-stripped) source lines. The message embeds a diagnostic preview plus source length to help locate near-misses. Matching is line-based and indentation-insensitive, but content must match exactly otherwise.

Source

Thrown at python/sglang/srt/debug_utils/source_patcher/source_editor.py:70

def _find_match(*, source_lines: list[str], match_lines: list[str]) -> int:
    """Find the start index of match_lines in source_lines (strip-compared).

    Returns the index of the first matching line.
    Raises PatchApplicationError if not found or found multiple times.
    """
    stripped_source: list[str] = [line.strip() for line in source_lines]
    stripped_match: list[str] = [line.strip() for line in match_lines]
    match_len: int = len(stripped_match)

    found_indices: list[int] = [
        i
        for i in range(len(stripped_source) - match_len + 1)
        if stripped_source[i : i + match_len] == stripped_match
    ]

    if len(found_indices) == 0:
        raise PatchApplicationError(
            _not_found_diagnostic(stripped_source, stripped_match)
        )
    if len(found_indices) > 1:
        preview = "\n".join(match_lines)
        raise PatchApplicationError(
            f"match text found multiple times ({len(found_indices)} occurrences) in source:\n{preview}"
        )

    return found_indices[0]


def _not_found_diagnostic(stripped_source: list[str], stripped_match: list[str]) -> str:
    preview = "\n".join(stripped_match)
    lines = [
        f"match text not found in source:\n{preview}",
        "",
        f"source_len={len(stripped_source)} lines",
    ]

View on GitHub (pinned to 0132848349)

Solutions

  1. Open the target file and copy the exact current lines into the match field
  2. Re-run the patch pipeline that generated the spec against the current source version
  3. Check the diagnostic preview in the message for near-miss lines (often reveals a one-word difference)
  4. Pin the sglang version the patch was authored against, or regenerate patches on upgrade

Example fix

# before
EditSpec(match='x = attention_old(input)', replace='x = attention_new(input)')
# source actually contains: x = attention_ref(input)

# after
EditSpec(match='x = attention_ref(input)', replace='x = attention_new(input)')
Defensive patterns

Strategy: validation

Validate before calling

def strip_lines(text: str) -> list[str]:
    return [l.strip() for l in text.splitlines()]
src = strip_lines(source); m = strip_lines(edit.match)
if not any(src[i:i+len(m)] == m for i in range(len(src) - len(m) + 1)):
    raise ConfigError('match text absent from source — patch is stale')

Type guard

def match_exists(source: str, match: str) -> bool:
    s = [l.strip() for l in source.splitlines()]
    m = [l.strip() for l in match.strip().splitlines()]
    return any(s[i:i+len(m)] == m for i in range(len(s) - len(m) + 1))

Try / catch

try:
    patched = apply_edits(source, edits)
except PatchApplicationError as e:
    if 'not found' in str(e):
        log.warning('stale patch skipped: %s', e)
        patched = source
    else:
        raise

Prevention

When it happens

Trigger: Calling apply_edits with an EditSpec whose match text does not correspond to any contiguous block of lines in the target source — stale match text after the file changed, wrong file targeted, or subtle content differences (comments, renamed identifiers, spacing inside lines).

Common situations: Source file was edited/upgraded after the patch was written, so match text is stale; patch YAML targets a different sglang version's source; subtle diffs like changed keyword-arg names or split lines; tabs vs spaces normalized only per-line-stripped but intra-line whitespace still differs.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/830e59a25d55fde8. Report an issue: GitHub.