{"record":{"id":"8246b0b1403e5a39","repo":"astral-sh/ruff","slug":"fix-introduced-a-syntax-error","errorCode":null,"errorMessage":"Fix introduced a syntax error","messagePattern":"Fix introduced a syntax error","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/linter.rs","lineNumber":639,"sourceCode":"            &suppressions,\n        );\n\n        if iterations == 0 {\n            has_valid_syntax = parsed.has_valid_syntax();\n            has_no_syntax_errors = !diagnostics.iter().any(Diagnostic::is_invalid_syntax);\n        } else {\n            // If the source code had no syntax errors on the first pass, but\n            // does on a subsequent pass, then we've introduced a\n            // syntax error. Return the original code.\n            if has_valid_syntax && has_no_syntax_errors {\n                if let Some(error) = parsed.errors().first() {\n                    report_fix_syntax_error(\n                        path,\n                        transformed.source_code(),\n                        error,\n                        fixed.identifiers(),\n                    );\n                    return Err(anyhow!(\"Fix introduced a syntax error\"));\n                }\n            }\n        }\n\n        // Apply fix.\n        if let Some(FixResult {\n            code: fixed_contents,\n            fixes: applied,\n            source_map,\n        }) = fix_file(&diagnostics, &locator, unsafe_fixes)\n        {\n            if iterations < MAX_ITERATIONS {\n                // Count the number of fixed errors.\n                for (id, code, count) in applied.iter() {\n                    *fixed.entry(id).or_default(code) += count;\n                }\n\n                transformed = Cow::Owned(transformed.updated(fixed_contents, &source_map));","sourceCodeStart":621,"sourceCodeEnd":657,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/linter.rs#L621-L657","documentation":"After applying an autofix, `lint_fix` re-parses the transformed source. If that parse fails, the fix would have introduced invalid syntax, so Ruff reports a fix syntax error (pointing at the offending fix) and returns this error instead of emitting broken code.","triggerScenarios":"Running `ruff check --fix` (via `lint_path` or `lint_stdin`) when a rule's generated fix produces source that fails re-parsing, with `report_fix_syntax_error` printing details of the rule and identifiers.","commonSituations":"Bugs in rule fix implementations, fixes interacting badly when several rules' fixes combine, fixes applied to unusual syntax patterns the fixer didn't anticipate, or usage of unstable/preview fixes.","solutions":["Read the `report_fix_syntax_error` output to identify the rule producing the bad fix and disable that rule for the file (e.g. `# ruff: noqa` or per-file-ignores).","Retry with `--unsafe-fixes` disabled (or vice versa) to avoid the specific fix class.","Apply fixes rule-by-rule (`--select` one rule at a time) to isolate the interaction.","If it is a fixer bug, minimize the reproducing file and report it upstream to the Ruff repository."],"exampleFix":"// before\n$ ruff check --fix --unsafe-fixes module.py\n# error: Fix introduced a syntax error\n\n// after: isolate the offending rule\n$ ruff check --select RULE_ID module.py  # confirm culprit\n$ ruff check --fix --extend-ignore RULE_ID module.py","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match run_ruff_fix(file) {\n    Err(e) if e.to_string() == \"Fix introduced a syntax error\" => {\n        eprintln!(\"See the report_fix_syntax_error output above; disable the culprit rule and re-run\");\n        // fallback: restore from VCS and fix rule-by-rule\n        git_checkout(file);\n    }\n    other => other?,\n}","preventionTips":["Run `ruff check --fix` only on a clean git working tree so fixes can be reverted.","Preview fixes with `--diff` before applying.","Enable unstable/preview fixes in CI only, not on unreviewed code.","Apply fixes incrementally (one rule group at a time) to isolate bad interactions."],"tags":["autofix","syntax-error","linter","ruff"],"backgroundTag":"fix-introduced-syntax-error","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}