Autofixing Code
Autofixing Code
Acts as the "second pair of hands" behind a coder: after code is written, this skill inspects it, finds every error, fixes it directly in the files, re-runs checks, and repeats until clean.
1. Detect project type (scan for package.json, requirements.txt, *.csproj, CMakeLists.txt, pom.xml, composer.json, etc.)
2. Run the matching linter/compiler/type-checker
3. Parse errors/warnings
4. Fix each one directly in the source file
5. Re-run checks
6. Run tests
7. Repeat until zero errors and all tests pass
No preamble, no asking permission — just fix and report what was fixed.
Progress checklist to follow on every run:
- [ ] Step 1: Identify project language(s) and framework(s)
- [ ] Step 2: Select the right toolchain (see table below)
- [ ] Step 3: Run static analysis / compiler / type-checker
- [ ] Step 4: Fix every error found, directly in the code
- [ ] Step 5: Run formatter (Prettier, Black/Ruff format, Clang-Format)
- [ ] Step 6: Run the test suite
- [ ] Step 7: If tests fail or new errors appear, go back to Step 3
- [ ] Step 8: Report a short summary of what was fixed
Step 1 — Detect the stack
| File/marker found | Language | Framework guess |
|---|---|---|
package.json | JS/TS | React/Next.js/Node/Express |
tsconfig.json | TypeScript | — |
requirements.txt/pyproject.toml | Python | Django/FastAPI |
*.csproj/*.sln | C# | .NET |
CMakeLists.txt | C++ | Qt/CMake |
pom.xml/build.gradle | Java | Spring etc. |
composer.json | PHP | — |
*.html/*.css alongside JS | Frontend | — |
If multiple markers exist, treat it as full-stack and fix both sides, frontend first then backend (or vice versa — whichever has errors first).
Step 2 — Pick the toolchain automatically
| Language | Lint/Analyze | Format | Test | Debug |
|---|---|---|---|---|
| JS/TS | ESLint, tsc | Prettier | Jest/Vitest, Playwright | Chrome DevTools/node --inspect |
| Python | Ruff, Pylint | Black/Ruff format | pytest | pdb/debugpy |
| C++ | Clang-Tidy | Clang-Format | CTest | GDB/LLDB |
| C#/.NET | Roslyn analyzers | dotnet format | dotnet test | — |
| Java | SonarQube/Checkstyle | — | JUnit | — |
| PHP | PHPStan/PHP_CodeSniffer | — | PHPUnit | — |
Step 3-4 — Analyze then fix in place
- Run the tool, capture every error/warning with file + line number.
- Open each file, apply the minimal correct fix (don't rewrite unrelated code).
- Common fix categories: syntax errors, type mismatches, unused imports/vars, null/undefined access, missing error handling, security lints (SQL injection, XSS), deprecated API usage, missing await/async mismatches, memory leaks (C++ pointers), off-by-one bugs.
- For logic bugs caught by failing tests: do root-cause analysis (read the stack trace, find the actual faulty line — not just where it crashed) before patching.
Step 5 — Format
Always run the project's formatter after fixing logic, so diffs stay clean and consistent with existing style.
Step 6-7 — Test and loop
- Run the test suite relevant to the stack.
- If new failures appear that weren't there before your fix, that fix introduced a regression — revert that specific change and try a different fix.
- Stop looping when: zero lint errors, zero compiler errors, all tests green. Max 5 iterations — if still failing after 5, report the remaining issue clearly instead of guessing blindly.
Step 8 — Report
Give a short, concrete summary:
Fixed 4 issues in src/api/users.js: missing await (L23), undefined var (L45),
unused import (L1), SQL injection risk (L67) — parameterized the query.
Ran ESLint + Jest: 0 errors, 12/12 tests passing.
Example 1:
Input: A React component throws Cannot read properties of undefined (reading 'map') at runtime; ESLint also flags an unused import.
Process: Detect JS/React stack → run ESLint → find unused import and missing null-check → add data?.map(...) guard (or default data = []) → remove unused import → run Prettier → run Jest → all pass.
Output: File patched, ESLint clean, tests passing, one-line summary of the two fixes.
Example 2:
Input: A Python FastAPI endpoint crashes with TypeError: 'NoneType' object is not subscriptable and Ruff flags an f-string typo.
Process: Detect Python/FastAPI → run Ruff → fix typo → trace crash to a DB query that can return None → add if not record: raise HTTPException(404) → run Black → run pytest → green.
Output: Endpoint fixed, Ruff clean, pytest 8/8 passing.
Example 3:
Input: C++ project with CMake fails to build due to a dangling pointer caught by Clang-Tidy.
Process: Detect C++/CMake → run Clang-Tidy → locate the dangling pointer → fix with proper ownership (unique_ptr or scope fix) → run Clang-Format → run CTest.
Output: Build succeeds, Clang-Tidy clean, all CTest cases pass.
- Fix root causes, not symptoms — don't silence an error with a broad try/catch if the real bug is a null check.
- Make the smallest correct diff; don't refactor unrelated code while autofixing.
- Always re-run tests after every fix batch, never assume a fix worked.
- Prefer the project's existing style/config (its own
.eslintrc,pyproject.toml, etc.) over generic defaults. - When frontend and backend are linked (e.g. API contract mismatch), fix both sides consistently in the same pass.
- Treat this as hand-off stage: coding agent writes code → this skill inspects and repairs right after, before anything ships.
- Don't suppress errors with
// eslint-disableor# noqainstead of actually fixing them. - Don't keep looping forever — cap retries and report unresolved issues instead of silently giving up.
- Don't fix style issues before logic/compile errors — order matters: compile/type errors → logic/test failures → lint/style.
- Don't apply a fix that passes tests by accident (e.g., deleting a failing test) — never delete or weaken tests to make them pass.
- Don't ignore newly introduced errors after a fix — always re-scan the whole project, not just the touched file.