Automatically Fixing Errors

Super-Linter can automatically fix many common linting and formatting issues, saving you time and ensuring consistency.

How Fix Mode Works

By default, all linters and formatters run in check-only mode. They report errors without modifying your source code.

To enable automatic fixes, you need to activate fix mode for specific linters using FIX_[LANGUAGE] environment variables. When fix mode is enabled, the linter will attempt to correct any issues it finds and overwrite the source files in the workspace.

Enabling a FIX_[LANGUAGE] variable automatically implies that the corresponding VALIDATE_[LANGUAGE] variable is also enabled. Setting FIX_[LANGUAGE] to true while setting VALIDATE_[LANGUAGE] to false is a configuration error.

Supported Linters for Fix Mode

Refer to the Configuration documentation for a complete list of FIX_[LANGUAGE] variables and the tools that support auto-fixing.

Example: Auto-Committing Fixes in a GitHub Actions Workflow

You can create a powerful workflow that not only lints your code but also commits any automatic fixes back to the pull request branch. This keeps your codebase clean with minimal manual intervention.

Here is an example workflow that runs on pull requests, fixes issues for Shell scripts (using shfmt) and YAML files (using prettier), and then commits the changes.

---
name: Lint and Fix

on:
  pull_request:

permissions:
  # Required to write linting fixes to the repository
  contents: write
  # Required to write Super-Linter status checks
  statuses: write

jobs:
  fix-lint-issues:
    name: Fix Linting Issues
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          # We need the token to be able to push changes back to the PR branch
          token: ${{ secrets.PAT_TOKEN }} # Use a PAT to allow pushing to protected branches and triggering workflows
          fetch-depth: 0

      - name: Super-Linter
        uses: super-linter/super-linter@v8.0.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          # Enable fix mode for specific linters
          FIX_SHELL_SHFMT: true
          FIX_YAML_PRETTIER: true

      - name: Commit and push changes
        uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "chore: apply automated linting fixes"
          branch: ${{ github.head_ref }}

Important Considerations for Auto-Committing

  • Permissions: This workflow requires contents: write to commit changes.
  • Authentication: Using the default secrets.GITHUB_TOKEN might not be sufficient to push to protected branches or to trigger subsequent workflow runs. It's often better to use a Personal Access Token (PAT) with repo scope, stored as a secret (e.g., PAT_TOKEN), to ensure the commit can be pushed successfully.
  • Separate Workflows: It is recommended to maintain two separate workflows: one for checking (on all PRs and pushes) and another dedicated workflow for fixing (which might run on a specific trigger like a PR comment or label) to avoid unexpected behavior or commit loops.