Configures and executes Dart's
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.
When tasked with running a build or code generation process, evaluate the user's current context to select the correct command:
-
Verify and Inject Dependencies
Inspect the
file. Ensure that
is listed under
. If testing generated code, also ensure
is present.
yaml
dev_dependencies:
build_runner: ^2.4.0 # Use the latest compatible version
build_test: ^3.2.0 # Optional: Only if testing is required
Run the package fetch command:
-
Determine Project Policy on Generated Files
STOP AND ASK THE USER: "Should generated files (e.g.,
) be committed to version control, or are they generated on-the-fly in this project?"
- If generated on-the-fly: Ensure is added to the file.
- If committed: Proceed without modifying .
-
Execute the Build Command
Based on the Decision Logic, execute the appropriate command from the project root.
For continuous background generation during development (PREFERRED):
bash
dart run build_runner watch
For a one-time build:
bash
dart run build_runner build
For running tests that require code generation:
bash
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-outputs
flag.
bash
dart run build_runner build --delete-conflicting-outputs
Or for watch mode:
bash
dart run build_runner watch --delete-conflicting-outputs
Verify that the command succeeds after applying this flag.