skip to content

In Standard Ruby, what is the difference between standardrb --fix, --fix-unsafely and --no-fix, and how do you make fixing the default?

level: middleimportance: should knowfreq 35%

answer

  1. --fix: safe corrections only
  2. --fix-unsafely: behaviour may change
  3. rake standard:fix and standard:fix_unsafely
  4. fix: true in .standard.yml
  5. --no-fix overrides the file

basics

~10 s

standardrb --fix applies only corrections RuboCop marks safe; --fix-unsafely also applies unsafe ones that can change behaviour. fix: true in .standard.yml makes fixing the default, and --no-fix turns it off for one run.

solid answer

~40 s

`standardrb` on its own only reports. `--fix` turns on autocorrection limited to **safe** corrections, the ones whose cops have `Safe` and `SafeAutoCorrect` true, so it is meant to run all the time, even on save. `--fix-unsafely` turns on **all** corrections, including those that may change behaviour, such as rewriting `x && x.y` to `x&.y` or `list.reverse.find` to `list.rfind`; it is for a reviewed change with tests run afterwards. `--no-fix` turns autocorrection off. The Rake equivalents are `rake standard:fix` and `rake standard:fix_unsafely`. Setting `fix: true` in `.standard.yml` makes every `standardrb` run fix safely, and `--no-fix` overrides it for a CI check. When a run leaves correctable offenses, Standard prints the next command to try: `standardrb --fix`, or `standardrb --fix-unsafely` after a safe run.

code

yaml · 3 lines
yaml
# .standard.yml
fix: true          # plain `standardrb` now applies safe fixes
parallel: true

go deeper

for a junior

Recall --fix for safe corrections, --fix-unsafely for everything, and the rake standard:fix task.

for a middle

Explain that the safe/unsafe split comes from RuboCop's Safe and SafeAutoCorrect settings, and how fix: true and --no-fix interact.

for a senior

Set up the routine: safe fixes in the editor, --no-fix in CI, and --fix-unsafely only as a reviewed, tested, separate commit.

for a principal

Keep automatic rewriting inside the safe boundary for every unattended tool, and make unsafe rewrites a deliberate change someone owns.

## Three switches Standard exposes RuboCop's autocorrection through its own flags, because it hides RuboCop's configuration: | Standard flag | Rake task | What it corrects | |---|---|---| | (none) | `rake standard` | nothing; reports only, unless `fix: true` is set | | `--fix` | `rake standard:fix` | safe corrections only | | `--fix-unsafely` | `rake standard:fix_unsafely` | safe and unsafe corrections | | `--no-fix` | - | nothing, even with `fix: true` | Internally, `--fix` turns on autocorrection with the safe-only flag, `--fix-unsafely` turns it on with the safe-only flag off, and `--no-fix` turns both off. The difference between safe and unsafe comes from each cop's `Safe` and `SafeAutoCorrect` settings in RuboCop: a correction is safe only when both are true. ## What "unsafe" means in practice Unsafe corrections are those RuboCop's authors know can change behaviour in some programs. Examples among cops Standard enables: - `Style/SafeNavigation` rewrites `user && user.name` to `user&.name`, which raises `NoMethodError` instead of returning `false` when `user` is `false`; - `Style/ReverseFind` (`Safe: false`) rewrites `list.reverse.find { ... }` to `list.rfind { ... }`, which raises `NoMethodError` when `list` is an object without `rfind` rather than an `Array`; - in general, cops with `Safe: false` may flag, and rewrite, code that was correct. The project's advice is to run `--fix` all the time and `--fix-unsafely` only when you intend to review the diff and run the tests. ## Making fixing the default `.standard.yml` accepts `fix: true` (default `false`). Then: 1. `standardrb` alone applies safe fixes, which suits editors and local runs. 2. `standardrb --no-fix` reports without changing files, which is what CI needs: a check that modifies files and passes would hide the problem. 3. `standardrb --fix-unsafely` still opts into unsafe corrections for one run. ## The hint Standard prints When correctable offenses remain, Standard's formatter suggests the next step: - after a plain run: `Run standardrb --fix to fix up to 12 problems` (or `rake standard:fix` when run through Rake); - after a safe fix run that left unsafe-correctable offenses: the same message with `standardrb --fix-unsafely`. Offenses that no correction can fix are printed and need a human. ## A workable routine - **Editor:** format on save with safe fixes, through Standard's language server or `standardrb --fix` on the file. - **Before committing:** `standardrb --fix`, then review what changed. - **CI:** `standardrb --no-fix` (or plain `standardrb` without `fix: true`), failing the build on any offense. - **Occasionally:** `standardrb --fix-unsafely` on a branch, reviewed and tested like any refactoring. ## Worked example A file contains a single-quoted string and `user && user.email`: 1. `standardrb` reports both and ends with `standard: Run standardrb --fix to fix up to 2 problems.` ("up to", because some corrections may be unsafe). 2. `standardrb --fix` changes the quotes to double quotes, a safe correction; the `&&` chain stays, because `Style/SafeNavigation`'s correction is unsafe. 3. The output now suggests `standardrb --fix-unsafely`, and its message warns that it would *DANGEROUSLY* fix the remaining problem. 4. Running it rewrites the chain to `user&.email`; whether that is correct depends on whether `user` can ever be `false`, which only a person or a test can answer. ## Relation to RuboCop's flags The mapping is direct: `--fix` corresponds to RuboCop's safe autocorrection and `--fix-unsafely` to its autocorrect-all mode. Standard forwards most other RuboCop flags, so `standardrb --fix --format progress` works, but the correction mode is chosen with Standard's own flag names.

  • Why should CI run standardrb --no-fix in a project whose .standard.yml says fix: true?
    With `fix: true`, a plain run corrects what it can and then reports only what is left, so a CI job would quietly fix offenses in its own checkout and pass. `--no-fix` turns autocorrection off for that run, so every offense in the pushed code fails the build.
  • After standardrb --fix, Standard suggests running --fix-unsafely. What should you do before accepting that?
    Treat it as a refactoring: run it on a clean working tree, read the diff for behaviour changes such as new `&.` operators or `rfind` calls on objects that may not be arrays, run the test suite, and commit it separately from other work so it can be reverted alone.

  • standardrb --fix also applies unsafe corrections if they look harmless.
  • fix: true in .standard.yml cannot be overridden from the command line.
  • --fix-unsafely only reformats whitespace, so it never changes behaviour.
  • A CI job with fix: true catches every offense in the pushed code.
  • rake standard:fix applies unsafe fixes, while standardrb --fix does not.