question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatically run build_runner when opening a Dart or Flutter project

See original GitHub issue

For the time being, we have to run pub run build_runner watch (or flutter packages pub run build_runner watch for Flutter) each time we are working on a project with code generation.

It would be great if this command could be launched automatically if there was a dev dependency to build_runner in the pubspec.yaml file. This feature should be enabled with a setting, in order to let people decide if want to activate it or not.

What do you think about this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:16
  • Comments:31 (15 by maintainers)

github_iconTop GitHub Comments

9reactions
DanTupcommented, Oct 23, 2018

I’ve got something basic working here that contributes build_runner tasks for projects that reference build_runner in the pubspec. It’d be good to get some testing of it if anyone would like to give it a try.

Grab this file, unzip it (I can’t attach vsix files here!), then run Extensions: Install from VSIX from the VS Code command palette and browse to it, then reload. (Note: you’ll automatically be upgraded to v2.20 stable when I publish it, so this functionality may vanish then, depending on whether this work gets merged into it - which depends on testing/feedback!)

dart-code-2.20.0-beta.1.with-build-runner.vsix.zip

Then set the dart.previewBuildRunnerTasks setting to true in your VS Code settings.

With this done, if you run the Run Task command from the VS Code command palette, you should see some build_runner tasks (note: this requires that your project is a workspace root in VS Code, it won’t work for projects in sub-folders):

screen shot 2018-10-23 at 3 03 18 pm

If you have multiple roots that match, the workspace folder names will appear alongside the task names.

If you run watch (for ex.) then it’ll run the task and show Building... in the status bar. Once the initial build finishes, the Building... should vanish. When you trigger a rebuild (eg. by editing a file), you should see Building... reappear in the status bar for the duration of the build. You can click the little spanner/screwdriver icon in the status bar to jump to the command output.

If you want this to run automatically, you can add this as a preLaunchTask in launch.json (in future maybe this will be automatic, but for now it’s opt-in):

{
	"name": "Dart",
	"program": "lib/main.dart",
	"request": "launch",
	"type": "dart",
	"preLaunchTask": "pub: build_runner watch"
}

With this set, when you hit F5 to try and run the project, it’ll first run build_runner watch. Once it sees the build has completed, it’ll launch the app. build_runner will continue to run in the background, and on subsequent presses of F5 it’ll wait for any in-progress builds and then launch.

It should(!) run the correct command (Flutter vs Pub) based on your project type automatically, so this should be testable with either.

Note: It’s not currently possible to customise the command (such as adding args), please 👍 this issue to support this.

Please try it out if you can, and post feedback!

5reactions
eseidelcommented, Feb 27, 2021

For anyone who stumbles here through Google. This is now documented: https://dartcode.org/docs/tasks/

Read more comments on GitHub >

github_iconTop Results From Across the Web

build_runner - Dart
The build_runner package provides general-purpose commands for generating files, including testing the generated files or serving both source and generated ...
Read more >
flutter pub run build_runner build failed - dart - Stack Overflow
I tried with many solutions, But error not gone. flutter packages pub run build_runner watch command ran with endless log.
Read more >
build_runner | Dart Package - Pub.dev
Standalone generator and watcher for Dart using package:build . The build_runner package provides a concrete way of generating files using Dart code, outside ......
Read more >
Dart build_runner - Visual Studio Marketplace
Automatically identify Packages with build_runner dependencies in the workspace. Log printing is completely independent when running multiple ...
Read more >
Integrating Dart's Build Runner with VSCode
Launching Build Runner; Writing Task for Build Runner; Running a Task After File Save ... flutter pub run build_runner build lib/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found