Skip to content

prefer_module_barrel_imports

v1.3.0WarningConfigurableArchitecture

Reports imports into another module that bypass its barrel. Direct imports inside the same module remain allowed. This rule is in no preset; enable it by name. By default a module is a directory directly below lib, with an entry point named after that directory, such as lib/users/users.dart.

In lib/orders/page.dart, given lib/users/model.dart declaring User:

import '../users/model.dart';
User? selectedUser;

Export model.dart from lib/users/users.dart, then import that barrel:

import '../users/users.dart';
User? selectedUser;

Both relative and package imports are checked, including every conditional-import branch, and so are exports that reach into another module. A barrel exporting files of its own module is fine. Generated source files are skipped.

A directory named src directly below a module root is not a module: lib/src is pub’s private implementation directory. To organise modules under lib/src, set module_roots: [lib/src]. Imports into unconfigured external packages are unaffected. Missing import targets remain the analyzer’s concern.

There is no quick fix: a barrel might not export the required names, or might introduce an ambiguous name. Review its exports before changing an import.

analysis_options.yaml
many_lints:
rules:
prefer_module_barrel_imports:
module_roots: [lib]
package_entry_points: [package:models/models.dart]
Option Type Default Description
module_roots List of strings [lib] Package-relative parent directories of modules. The longest matching root wins. Trailing slashes and backslashes are ignored, so lib/features/ means lib/features.
package_entry_points List of strings [] Package URIs required for external consumers and same-package files outside lib. A package can have several; importing any of them is allowed, and the message names them all. Inside lib, importing your own package’s entry point is reported to prevent an import back into the umbrella. A path dependency stored inside your package root is another package.

Use include and exclude to scope approved exceptions. An empty or wrongly typed module_roots uses [lib]. An entry point is a convention, not a proof of layer separation: a barrel can still export infrastructure to presentation.

many_lints:
rules:
prefer_module_barrel_imports: false