Skip to content

avoid_banned_exports

v1.0.0WarningConfigurableArchitecture

Flags export directives for libraries you ban, optionally only inside the directories you name. Barrel-file hygiene: it keeps internal libraries out of a package’s public API.

This rule reports nothing until you configure it.

A barrel file decides what a package promises. An export added there for convenience — to save one import in a test, say — silently makes everything in that library public, and removing it later is a breaking change for every consumer.

This is kept separate from avoid_banned_imports on purpose: depending on a library internally and re-exporting it to consumers are different decisions. A package is often perfectly free to use something it must not expose.

See also: Effective Dart: libraries, Dart package layout conventions

// in lib/my_package.dart
//
// With an entry banning 'src/internal/.*' in lib/*.dart:
export 'src/internal/cache.dart'; // LINT: makes the cache public API
// in lib/my_package.dart
//
// Export only what consumers are meant to depend on.
export 'src/api/client.dart';
export 'src/api/models.dart';

This rule is in no preset, so it is off unless you enable it by name:

many_lints.yaml
rules:
avoid_banned_exports: true

To turn it off again:

many_lints.yaml
rules:
avoid_banned_exports: false

To keep the rule on but skip certain paths, use per-rule exclude.

analysis_options.yaml
many_lints:
rules:
avoid_banned_exports:
banned:
- deny_pattern: ['src/internal/.*']
in: ['lib/*.dart']
message: 'Internal libraries must not be part of the public API.'
Option Type Default Description
banned list of maps [] The entries to enforce. With none, the rule reports nothing

Each entry accepts:

Key Type Required Description
deny string or list one of deny / deny_pattern Export URIs banned by exact match
deny_pattern string or list one of deny / deny_pattern Regular expressions, anchored to the whole URI
in list of globs no Paths, relative to the package root, where the entry applies. Omit to apply everywhere
message string no A project-specific explanation appended to the diagnostic

The URI is matched as written in the export directive, so a relative export is matched as the relative path you typed.

Scoping with in: ['lib/*.dart'] is the useful default here: a single * matches only top-level barrel files, leaving everything under lib/src/** free to export internally.