Managing Dart Packages
Contents
Package Layout Conventions
Structure Dart packages using standardized directories to ensure tooling compatibility and clear API boundaries.
lib/: Place public libraries and assets here. Export only the public API surface.lib/src/: Place internal implementation files here. Never import from another package'slib/src/. Use relative imports within your own package when importing fromlib/src/tolib/.bin/: Place public command-line executables here.tool/: Place internal scripts and tools (e.g., code generation, documentation scripts) here.test/: Place unit tests here, suffixed with_test.dart.integration_test/: Place slow, integration-level tests here.example/: Place standalone example programs demonstrating package usage. Usepackage:imports to reference the parent package.web/: Place web-specific entrypoints (main.dart) and assets (index.html, CSS) here.hook/: Place SDK build hooks (e.g.,build.dart) here.
Pubspec Configuration
Maintain a valid pubspec.yaml at the root of every package.
- Version Constraints: Use caret syntax (
^) for dependency version constraints (e.g.,^3.2.0) to allow non-breaking updates. - SDK Constraints: Always define an
environmentwith a lower-bound SDK constraint. - Public Assets: Explicitly list all public assets (images, fonts) required by the package.
- Executables: Map scripts from
bin/to command names under theexecutablesfield. - Metadata: Include
name,version,description,repository, andissue_trackerfor published packages. - Topics: Categorize published packages using the
topicsfield (max 5 topics, lowercase alphanumeric and hyphens). - False Secrets: Use the
false_secretsfield with gitignore patterns to prevent false positives during pub's pre-publish leak detection.
Monorepo Workspaces
Implement workspaces in monorepos to share dependencies across local packages, reducing memory usage and ensuring version consistency.
- Root Pubspec: Define the workspace at the repository root. Set
publish_to: none, require SDK^3.6.0or higher, and use theworkspacefield with glob patterns (e.g.,packages/*). - Child Pubspecs: In each workspace package, require SDK
^3.6.0or higher and setresolution: workspace. - Interdependencies: Depend on other workspace packages normally. Pub automatically resolves to the local workspace version.
- Overrides: Place
dependency_overridesin the rootpubspec.yamlto apply them globally across the workspace.
Workflows
Setting up a Monorepo Workspace
Use this checklist to convert a standard repository into a Dart workspace.
- Create a root
pubspec.yaml. - Set
publish_to: noneandenvironment: sdk: ^3.6.0in the root pubspec. - Add the
workspace:field to the root pubspec using glob patterns (e.g.,- packages/*). - Update all child
pubspec.yamlfiles to includeenvironment: sdk: ^3.6.0(or higher). - Add
resolution: workspaceto all childpubspec.yamlfiles. - Run
dart pub getat the repository root. - Feedback Loop: Run validator -> review errors -> fix. Ensure no stray
pubspec.lockor.dart_tool/package_config.jsonfiles exist in child directories. If resolution fails, align conflicting dependency versions across child packages.
Managing Dependencies
- Run
dart pub getto fetch dependencies and generate thepackage_config.json. - Run
dart pub upgradeto update dependencies to their latest compatible versions. - Commit
pubspec.lockONLY for application packages. Omit it for library packages.
Examples
Standard Library Pubspec
name: enchilada
description: A comprehensive toolkit for newt transmogrification.
version: 1.2.3
repository: https://github.com/example/enchilada
issue_tracker: https://github.com/example/enchilada/issues
environment:
sdk: ^3.6.0
dependencies:
path: ^1.8.0
transmogrify: ^0.4.0
dev_dependencies:
test: ^2.0.0
lints: ^3.0.0
executables:
enchilada: main
topics:
- transmogrification
- utilitiesWorkspace Root Pubspec
name: my_monorepo
publish_to: none
environment:
sdk: ^3.6.0
workspace:
- packages/*
# Apply overrides globally across all workspace packages
dependency_overrides:
transmogrify: ^0.5.0-devWorkspace Child Pubspec
name: client_package
description: The client application for the monorepo.
version: 1.0.0
publish_to: none
environment:
sdk: ^3.6.0
resolution: workspace
dependencies:
shared_package: ^1.0.0 # Resolves locally within the workspace
http: ^1.1.0