{"record":{"id":"3e2ede1e7e02834a","repo":"oraios/serena","slug":"no-occurrences-of-the-pattern-were-found-no-chan","errorCode":null,"errorMessage":"No occurrences of the pattern were found - NO changes were applied. Check the mode (a literal needle containing regex metacharacters must use mode 'literal'; wildcards require mode 'regex') and the path/glob restrictions, or locate the content with search_for_pattern first.","messagePattern":"No occurrences of the pattern were found - NO changes were applied\\. Check the mode \\(a literal needle containing regex metacharacters must use mode 'literal'; wildcards require mode 'regex'\\) and the path/glob restrictions, or locate the content with search_for_pattern first\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/serena/tools/file_tools.py","lineNumber":297,"sourceCode":"        if dry_run:\n            return self._render_listing(replacer, occurrences, contents, max_answer_chars, dry_run=True)\n\n        if occurrence_ids is not None:\n            selected, problems = self._resolve_occurrence_ids(occurrence_ids, occurrences)\n            if problems:\n                problem_lines = \"\\n\".join(f\"  {p}\" for p in problems)\n                raise ValueError(\n                    f\"{len(problems)} of the given occurrence_ids could not be resolved - NO changes were applied:\\n\"\n                    f\"{problem_lines}\\n\"\n                    \"Re-run with dry_run=True to obtain current occurrence ids.\"\n                )\n            if not selected:\n                raise ValueError(\"occurrence_ids is empty - pass at least one id from a dry run, or omit the parameter to replace all.\")\n            return self._apply_occurrences(replacer, selected, contents, needle, repl)\n\n        # blind apply (no ids)\n        if not occurrences:\n            raise ValueError(\n                \"No occurrences of the pattern were found - NO changes were applied. \"\n                \"Check the mode (a literal needle containing regex metacharacters must use mode 'literal'; \"\n                \"wildcards require mode 'regex') and the path/glob restrictions, \"\n                \"or locate the content with search_for_pattern first.\"\n            )\n        if expected_count >= 0 and len(occurrences) != expected_count:\n            listing = self._render_listing(replacer, occurrences, contents, max_answer_chars, dry_run=False)\n            raise ValueError(\n                f\"expected_count={expected_count}, but the pattern matches {len(occurrences)} occurrence(s) - \"\n                f\"NO changes were applied. Review the prospective changes below; re-issue with the corrected \"\n                f\"expectation, a refined pattern, or occurrence_ids selecting the intended subset.\\n{listing}\"\n            )\n        ambiguous = [o for o in occurrences if o.is_ambiguous]\n        if ambiguous:\n            listing = self._render_listing(replacer, occurrences, contents, max_answer_chars, dry_run=False)\n            raise ValueError(\n                f\"{len(ambiguous)} occurrence(s) are ambiguous (the pattern matches again inside the matched text, \"\n                f\"indicating possible over-matching) - NO changes were applied. Review the prospective changes below \"","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/serena/tools/file_tools.py#L279-L315","documentation":"In blind-apply mode (no occurrence_ids), if the pattern matched zero occurrences, apply() raises this ValueError so no silent no-op occurs. The message points at the two most common root causes: wrong matching mode (literal vs regex) and overly restrictive path/glob scoping.","triggerScenarios":"Using mode='regex' (default) with a literal needle containing regex metacharacters like (, [, ., or *; using wildcards while in literal mode; a path/glob filter excluding the file that contains the text; the text simply not existing (already replaced or typo).","commonSituations":"Replacing strings like \"foo(1)\" or \"a.b\" without mode='literal'; IDE/agent already applied the same replacement in a previous run; case or whitespace mismatch between needle and file content.","solutions":["Set mode='literal' if the needle contains regex metacharacters and should match verbatim","Switch to mode='regex' (and escape as needed) when using wildcards like foo.*bar","Run search_for_pattern first to confirm the text exists and under which paths","Check the path/glob parameters are not excluding the target files"],"exampleFix":"// before\nagent.replace_content(needle=\"price(1)\", repl=\"price(2)\")\n// after\nagent.replace_content(needle=\"price(1)\", repl=\"price(2)\", mode=\"literal\")","handlingStrategy":"validation","validationCode":"found = agent.search_for_pattern(needle, path=path)\nif not found:\n    raise LookupError(f\"pattern not found: {needle!r}\")\nagent.replace_content(needle, repl, mode=\"literal\" if any(c in needle for c in \"()[]{}.*+?^$|\\\\\") else \"regex\")","typeGuard":"def needs_literal(needle: str) -> bool:\n    import re\n    return any(ch in needle for ch in \"()[]{}.*+?^$|\\\\\")","tryCatchPattern":"try:\n    agent.replace_content(needle, repl)\nexcept ValueError as e:\n    if \"No occurrences\" in str(e):\n        agent.replace_content(needle, repl, mode=\"literal\")\n    else:\n        raise","preventionTips":["search_for_pattern before replacing to confirm existence and scope","Default to mode='literal' for needles containing regex metacharacters","Check path/glob restrictions when matches are unexpectedly empty"],"tags":["regex","pattern-matching","replace"],"backgroundTag":"no-pattern-match","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}