Skip to content

unused_files

Reports a Dart file that no entry point reaches through imports, exports or parts. Delete it, or import it where it is used.

Kind
cross-file check
Tier
advisory
Host
command-line program only (assay cross-file)
Fix
none

A Dart file of the project that no entry point reaches through imports, exports or parts.

  • Message: “No entry point reaches this file through imports, exports or parts.”
  • How to fix: “Delete the file if nothing needs it, or import it where it is used.”

The check follows every import, export and part from each entry point. An entry point is:

  • a file that declares a top-level main: a program in bin/, a script in tool/, a test, an example;
  • lib/main.dart or a lib/main_<name>.dart, the program of a Flutter app and of each of its flavours;
  • flutter_test_config.dart;
  • a file with a declaration that carries @pragma('vm:entry-point');
  • in a publishable package, every Dart file in lib/ outside lib/src/;
  • a file that does not compile.

Tests count, so a file that only a test imports is reached. Both branches of a platform-dependent import are reached, and so is what only generated code imports.

A file nothing reaches is compiled into no program and run by no test, yet people, agents and every rule still read it, and a change made in it changes nothing. Agents leave such files behind: a first attempt replaced by a second one in a new file, a helper the final version no longer imports. The check is advisory, because some files are kept on purpose without being reached, such as a template a script copies.

Under the assay: key of the project’s root analysis_options.yaml. The check is on under every preset unless switched off.

Key Default Effect
assay.cross_file.unused_files.enabled true false switches the check off. The short form unused_files: false means the same.
assay.cross_file.unused_files.exclude [] Path globs, read from the project root, where the check places no result. The files are still read and followed.
assay:
cross_file:
unused_files:
exclude:
- tool/templates/**

assay cross-file runs every cross-file check on the whole project, and assay cross-file unused_files runs this one alone (see assay cross-file). It does not run in the editor or in assay check. In the gate its results never fail the gate (see In the gate).

A suppression comment does not silence a cross-file check: the result is of the whole file, not of a line. Use the check’s exclude for a file kept on purpose.

All cases

The result is a violation with the source cross_file, the rule unused_files and the advisory tier.

The result is of the whole file, since no line of it is what is wrong. It stands at line 1, column 1, and covers nothing, as a result of max_file_length does. Its fingerprint is made of an empty line text, so it stays the same while the file is edited, and becomes a new result when the file is moved or renamed.

A file nothing reaches is compiled into no program of the project and run by no test. It is still read: by a person looking for where something is done, by an agent that searches the project and edits what it finds, by every rule and metric that judges the project. A change made in it changes nothing, and a reader who does not know that loses the time.

Code written by an agent leaves such files behind: a first attempt replaced by a second one in a new file, a helper the final version no longer imports, a screen a refactoring stopped routing to. No single file shows it; only the whole project does.

Some files are kept on purpose without being reached, such as a template a script copies or a sample kept for reference. Each project decides.

The check follows imports, exports and parts from every entry point, through every file they reach. A file is an entry point when:

  • it declares a top-level function main, wherever it is: a program in bin/, a script in tool/, a test in test/, integration_test/ or test_driver/, an example in example/, a benchmark, the program of a web app in web/, a build hook, the program of a Flutter app;
  • it is lib/main.dart or a lib/main_<name>.dart of its package, with or without a main function: the program of a Flutter app and of each of its flavours, which the Flutter tool and the platform runners start, and the library an analyzer plugin is loaded from;
  • it is named flutter_test_config.dart, which the Flutter test runner loads by itself for the tests in its directory and below;
  • a declaration in it carries @pragma('vm:entry-point'), which the Dart VM calls from outside Dart, such as a background callback or an add-to-app entry point;
  • it is a public library of a package with a public API: every Dart file in lib/ outside lib/src/ of a publishable package, or of a project that is one member of a pub workspace judged on its own (see The two package types);
  • it does not compile, or cannot be read. Such a file is listed as not judged, with its compile errors, and is not reported itself. What it imports, exports and parts is still followed as far as the analyzer could read it, so a compile error never makes another file reported.

What is followed:

  • Every import, export and part, by a relative address or a package: address of a package of the project. A dart: library and a package outside the project are not files of the project. A package:<name>/… address that the importing file’s package configuration does not know, as in a nested example/ app whose packages were not fetched, is followed to the lib/ of the project’s package whose pubspec has that name.
  • Every branch of a platform-dependent import or export. import 'io.dart' if (dart.library.js_interop) 'web.dart'; reaches both files: which one is compiled depends on the platform, and both are needed.
  • A part of with an address reaches its library, as the library reaches the part. So a generated part (*.g.dart, *.freezed.dart and the like) is reached when its library is. A part of that names its library by a library name does not reach the library; the library still reaches the part.
  • Tests. A test is an entry point, so a file of lib/ that only a test imports is reached, and so is a test helper a test imports. A file of the test side that no test reaches is reported like any other.
  • Generated files. The pass reads every Dart file of the project, files in assay.exclude and analyzer.exclude among them. So a file that only generated code imports, such as a router or an injection configuration a code generator writes, is reached.

Nothing else is followed. A file reached in another way is reported: a builder named in build.yaml, a file a script copies or runs by its path, a file flutter run -t starts that is neither lib/main.dart nor a lib/main_<name>.dart and declares no main, a library native code loads by its address without @pragma('vm:entry-point'). Put such files in the check’s exclude.

A file’s package is the nearest directory above it, inside the project, that holds a pubspec.yaml. A nested package (an example/ app, a package of a pub workspace below the project root) is judged by its own pubspec. See also The two package types.

Package Public libraries
marked publish_to: none (an application, an internal package) none: nothing outside the project can import the package, so a file in lib/ that no entry point reaches is reported, outside lib/src/ as well
publishable: any other publish_to, or no publish_to line at all every Dart file in lib/ outside lib/src/ is an entry point, and what it imports and exports is reached
the project root’s package, when its pubspec says resolution: workspace (one member of a pub workspace judged on its own) as a publishable package: the other members import its public libraries from outside the project. Judge the workspace from its root to have them seen.
a directory with no pubspec none

A package with no publish_to line counts as publishable, as pub treats it. So in an internal package not marked publish_to: none, a file in lib/ outside lib/src/ that nothing uses is not reported. Mark such a package publish_to: none to have it judged as an application.

Where a pubspec cannot be read, no cross-file check judges that package’s files in that run (see A pubspec that cannot be read).

  • In a file that assay.exclude or analyzer.exclude covers. Such a file is still read, and what it reaches is reached. With the default assay.exclude, an unreached generated file is therefore not reported.
  • In a file that the check’s own exclude covers.
  • In a directory that holds no source of the project: .dart_tool, a directory directly under the project root whose name starts with a dot, and the others check leaves out.
  • In a file that is not judged.

The short form switches the check off:

assay:
cross_file:
unused_files: false

The gate runs the check with the other cross-file checks: its results never fail the gate, and they pass through the baseline (see In the gate).

No fix exists, and none is planned: a file is never deleted mechanically.