Skip to content

assay fix

Applies every available fix in the project, or in the given paths.

assay fix [paths…] [--dry-run] [--rule <rule>]… [--color auto|always|never]

A fix is a mechanical edit that removes one violation. 47 rules have one; the Fix column of the index of rules and checks names each. The editor offers the same fix at the violation. assay has this command because the SDK’s bulk fix command does not apply fixes that come from plugins. See Fixes.

fix judges the project first, with assay’s engine and the same configuration as check, and fixes what that judgement reports. So it never touches:

  • a violation that a suppression hides, in either form;
  • a file in an excluded path;
  • a file that does not compile.

A violation the baseline knows is fixed like any other. fix never writes the baseline file: the entry goes the next time check, the gate on the whole project or baseline update runs.

Option Effect
paths… Fix only the files at and below these paths, relative to the project root. A path outside the project, or one where nothing is, is a usage error.
--dry-run Print every edit by file and place, with the lines as they are (-) and as they would be (+). Write nothing.
--rule <rule> Apply only this rule’s fixes. Repeatable. A name of no rule, or of a rule without a fix, is a usage error; the message names the rules that have a fix.
--color auto|always|never The terminal form or the plain form of the output, as for every command.
Terminal window
assay fix # every available fix in the project
assay fix lib/src # only at and below a path
assay fix --dry-run # print the edits, write nothing
assay fix --rule no_commented_out_code # only this rule's fixes; repeatable

Three boxes: the head box with the preset, the rules and the files judged; a box of the fixes, each file a sub-heading with what became of it over one row per fix, with its place, its rule and what the fix does; and a last box with what was done, and what to do next where fixes were left.

╭─ assay fix ──────────────────────────────────────────────────────────────────╮
│ Preset recommended │
│ Rules 109 │
│ Files 4 │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Fixed ─────────────────────────────────────────────────────── 4 in 2 files ─╮
│ Line Rule Fix │
│ ───────────────────────────────────────────────────────────────────────── │
│ lib/names.dart 3 fixed │
│ + 3:3 no_commented_out_code Delete the commented-out code │
│ + 4:32 no_commented_out_code Delete the commented-out code │
│ + 10:1 no_commented_out_code Delete the commented-out code │
│ │
│ lib/prices.dart 1 fixed │
│ + 7:3 no_commented_out_code Delete the commented-out code │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ ✓ Done ─────────────────────────────────────────────────────────────────────╮
│ Fixed 4 violations in 2 files. │
╰──────────────────────────────────────────────────────────────────────────────╯

With --dry-run, the box is To fix, and below each row stand the lines the fix changes, as they are in the file: a removed line behind -, an added one behind +.

╭─ assay fix ──────────────────────────────────────────────────────────────────╮
│ Preset recommended │
│ Rules 109 │
│ Files 4 │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ To fix ────────────────────────────────────────────────────── 4 in 2 files ─╮
│ Line Rule Fix │
│ ───────────────────────────────────────────────────────────────────────── │
│ lib/names.dart 3 to fix │
│ + 3:3 no_commented_out_code Delete the commented-out code │
│ - 3 | // print(first); │
│ + 4:32 no_commented_out_code Delete the commented-out code │
│ - 4 | final full = '$first $last'; // print(full); │
│ + 4 | final full = '$first $last'; │
│ + 10:1 no_commented_out_code Delete the commented-out code │
│ - 10 | // String nickName(String first) => first.substring(0, 3); │
│ │
│ lib/prices.dart 1 to fix │
│ + 7:3 no_commented_out_code Delete the commented-out code │
│ - 7 | // for (var index = 0; index < prices.length; index++) { │
│ - 8 | // sum += prices[index]; │
│ - 9 | // } │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Dry run ────────────────────────────────────────────────────────────────────╮
│ Would fix 4 violations in 2 files. Nothing was written. │
╰──────────────────────────────────────────────────────────────────────────────╯

The output also tells you when a fix was not written as planned:

It says What happened What to do
left for the next run Two fixes overlap, so only the earlier one was applied. The last box says so behind → Next:. Run fix again.
not written The file changed on disk after it was judged. Run fix again.
put back The file did not compile after its fixes, so its old text was restored. The output names the first compile error, at its place in the restored file (or at the fix that wrote the text). Fix it by hand.
does not compile The file did not compile after its fixes and could not be put back, because it changed again or could not be written. Fix the file by hand.

A violation whose fix found nothing safe to change, or whose fix failed, is listed with the reason. A violation whose fix is not available is not listed at all: its result has fixAvailable false, and the gate’s message shows no [fix available]. Each rule page lists the cases where its fix is not available. Fix those by hand.

A second run changes nothing, unless the first left fixes for the next run.

Code When
0 It could run, also where a file was left as it was, was put back, or nothing needed a fix.
2 A configuration error, or a usage error (--rule, a path). Nothing is written.
3 A file could not be written; a file was left not compiling because it could not be put back; or files were written while standard output was closed, so nobody was told (standard error names them).
  • Once per file. All the fixes of a file are applied to its text together, each placed by the text as it was judged.
  • Overlaps. Two fixes whose edits overlap are never both applied: the one whose violation comes later waits for the next run. Two different insertions at the same place overlap; edits that only touch do not. An edit two fixes share, such as one mounted check for two setState calls after one gap, is written once, and both fixes count as applied.
  • In one step. The new text is written under .dart_tool/assay/ and then replaces the file, so a write cut short leaves the file as it was. The file keeps its mode, its byte order mark and its line breaks; a link stays a link.
  • Judged again. Every file written is judged again, since a fix must never leave the project broken.

The gate never applies a fix. Its message marks the violations that have one ([fix available]), and running assay fix is up to you or the agent. No fix removes behaviour, and every fix stays within one file.