max_file_length
Reports a Dart file that has more lines than the limit. Split it into files that each hold one thing.
- Tier
- advisory
- Scope
- Dart
- Fix
- none
What it reports
Section titled “What it reports”A Dart file with more lines than the limit max. A file exactly at max is not reported.
A file has at most one violation, and its message names the length and the limit: “This file has 742 lines, which is over the limit of 600.”
No single line is wrong, so the violation is shown at line 1, column 1, and covers nothing.
A generated file is measured like any other: leave it out by its path, with exclude here or with assay.exclude for every rule. A file the analyzer itself excludes is not judged.
The baseline knows the violation by the rule and the file’s path, not by a line: a file in the baseline stays known while it grows, and its violation is new once the file is moved or renamed.
A long file holds several things that happen to live together. Finding one means scrolling or searching, a change to one shows up in the history of all, and two people working on different parts meet in the same merge.
A file is also the unit an AI coding agent reads and rewrites. A file of two thousand lines costs the agent its whole attention for a change of three. The gate judges every violation in a changed file, so the longer the file, the more old code each small change answers for. And adding to the open file is always the nearest move for an agent; a limit asks whether the new class belongs there.
The rule is advisory because length alone proves nothing: a file that holds one long table, or one widget with its state, can be long and still be one thing.
How the length is counted
Section titled “How the length is counted”The length is the number of lines, the number an editor shows on the last line:
- Every line counts: code, blank lines, comments, documentation comments, imports, a licence header.
- A final line break starts no further line: a file of ten lines has length 10 with or without one.
- An empty file has length 0.
- A part file is measured alone; its lines are not added to its library.
max_function_length counts differently: it leaves blank lines and comment lines out.
Example
Section titled “Example”Violating, with max: 400: a file orders.dart of 950 lines that declares the Order model, its JSON conversion, the OrderRepository, the OrderListScreen widget and its state, and six helper functions for formatting prices.
lib/orders/order.dart the model and its JSON conversionlib/orders/order_repository.dart loading and savinglib/orders/order_list_screen.dart the widget and its statelib/orders/price_format.dart the formatting functionsEach file now has one reason to change, and a change to the formatting no longer puts the screen in the diff.
Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
max |
none in the rule; the presets give it | The largest number of lines a file may have. A whole number, zero or more. |
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
The rule has no limit of its own. The presets give one:
| Preset | max |
|---|---|
core |
the rule is not in this preset |
recommended |
600 |
strict |
400 |
To change it, and to leave directories out:
assay: rules: max_file_length: max: 800 exclude: - lib/l10n/**Where no limit is in effect, the rule reports nothing.
recommended and strict also set this rule’s exclude to the test directories, test/**, integration_test/** and test_driver/**, read from the project root, so the test directory of a package nested in the project is judged: a test file is a list of cases, and its length is that of the list.
Suppressing it
Section titled “Suppressing it”Use a suppression for the whole file, with why on the line directly above it:
// The file is the complete table of country codes and is edited as one list.// ignore_for_file: assay/max_file_length// ignore: assay/max_file_length for one place does not fit this rule. The violation is on line 1: a suppression on a line of its own covers the line below it and so never reaches line 1, and one at the end of line 1 has no line above it for its justification.
// ignore_for_file: max_file_length without the prefix works too. See Suppressions.