dart-code-generation
dart-build-runner-automation
Goal
Configures and executes Dart's build_runner tool to automate code generation, testing, and serving of Dart applications. Analyzes project requirements to inject necessary development dependencies, determines the optimal build strategy (one-time build vs. continuous watching), and resolves file conflict errors during the generation phase.
Decision Logic
When tasked with running a build or code generation process, evaluate the user's current context to select the correct command:
- Is the user actively developing and modifying files? -> Choose
watch. - Is this a CI/CD pipeline or a one-off production build? -> Choose
build. - Is the user building a web application? -> Do NOT use
build_runner serve; delegate towebdev serve(seedart-web-development). - Does the user need to run tests on generated code? -> Choose
test.
Instructions
-
Verify and Inject Dependencies Inspect the
pubspec.yamlfile. Ensure thatbuild_runneris listed underdev_dependencies. If testing generated code, also ensurebuild_testis present.dev_dependencies: build_runner: ^2.4.0 # Use the latest compatible version build_test: ^3.2.0 # Optional: Only if testing is requiredRun the package fetch command:
dart pub get -
Determine Project Policy on Generated Files STOP AND ASK THE USER: "Should generated files (e.g.,
.g.dart) be committed to version control, or are they generated on-the-fly in this project?"- If generated on-the-fly: Ensure
.g.dartis added to the.gitignorefile. - If committed: Proceed without modifying
.gitignore.
- If generated on-the-fly: Ensure
-
Execute the Build Command Based on the Decision Logic, execute the appropriate command from the project root.
For continuous background generation during development (PREFERRED):
dart run build_runner watchFor a one-time build:
dart run build_runner buildFor running tests that require code generation:
dart run build_runner test -
Validate-and-Fix: Handle Conflicting Outputs Monitor the terminal output for the build command. If the build fails with a
ConflictingOutputsException(indicating that generated files already exist and conflict with the new build), automatically recover by appending the--delete-conflicting-outputsflag.dart run build_runner build --delete-conflicting-outputsOr for watch mode:
dart run build_runner watch --delete-conflicting-outputsVerify that the command succeeds after applying this flag.
Constraints
- Strict Web App Routing: Never use
dart run build_runner servefor web applications. You must use thewebdevtool instead. - File Tracking: AVOID committing
.g.dartfiles if the project policy is to generate them on-the-fly. Always clarify this policy before running git commands. - Development Preference: PREFER the
watchcommand for continuous background generation during active development sessions to prevent stale code errors. - Conflict Resolution: Always use
--delete-conflicting-outputswhen regenerating files that have changed structure or when switching branches, rather than manually deleting.dart_toolor.g.dartfiles. - Related Skills: Defer to
dart-testingfor advanced test configurations anddart-web-developmentfor web-specific serving and compilation.