Run Configurations
IntelliJ with Bazel

Run Configurations

Overview

In all IntelliJ IDEs, you run and debug your application binaries and tests using run configurations, which specify what target to run and a variety of options for how to run that target.

Bazel run configurations are different from normal IntelliJ run configurations because they are organized around Bazel targets (rather than “modules” and “variants”). Run configurations are managed and launched from this collection of widgets at the top of the IDE:

IntelliJ with Bazel Run Configurations

Run configurations created by default

The IDE automatically creates run configurations for supported targets explicitly listed in your project view (not pulled in by an :all or /...:all expression). If most of your team builds, launches, and debugs the same few android_binary targets, you should probably add those targets to a checked-in *.bazelproject file.

Run configurations for all other targets must be created manually.

Creating a run configuration manually

To create a run configuration (or edit existing configurations), go to the run configuration dropdown and click Edit Configurations…

IntelliJ with Bazel Edit Configurations

ASwB Android binary

  1. Add/delete/copy a configuration.
  2. Select the Bazel Command run configuration type. This works for all types of targets (Android/Java/generic). The UI and options will vary depending on the target type.
  3. Select the target you want to run/debug.
  4. Customize the launch settings as you see fit. For binaries, you can pick activity launch settings. For tests, you can add test filters. For configurations delegating to Bazel, you can add flags. android_binary and android_test targets share a large number of device-oriented settings.
  5. IntelliJ loves confirmations, so you get two! Your changes are not saved until you click OK or Apply.

Creating and running test configurations with right-click menu

You can quickly create supported test configurations by right-clicking on classes and methods. The tests in the corresponding Bazel test target will be filtered to only run the tests in the target class, or to only run that test method.

  1. Right click on a class (in the project view, the structure view, the file tabs, or in a source file), or a method (in the structure view or a source file), or a test target in a BUILD file.
  2. Go to Create a Run Configuration, Run or Debug (to create a run configuration or immediately launch it).
  3. Select the type of test target as appropriate. If you pick the wrong kind of test, it will fail.

You can re-run the same configuration by launching it from the top of the IDE or right-clicking on the same class or method.

Right click test

Displaying test results

Bazel test results are integrated into IntelliJ’s test UI, showing the tests run in a navigable tree:

Test results tree UI

Test results console

Navigating to test cases, and re-running failed tests are only supported for test targets included in the project view targets section, and only for supported languages (i.e. Java, Python in IntelliJ; Java, Android in Android Studio; C/C++ in CLion).

In addition, re-running failed tests is only supported for single-test targets (not test_suite or target patterns).

For unsupported languages or custom test runners, we make a best-effort attempt to display a tree of test targets (without navigation support) by parsing whatever test XML the test runner produces.

Java HotSwapping

HotSwap support is available when debugging java_binary targets.

To update modified source files in a running JVM:

  1. Debug a java_binary target as described above.
  2. Modify source files.
  3. Run IntelliJ’s Reload Changed Classes action.

Sharing run configurations

Run configurations can be shared between projects / team members via the project view:

  1. Export bazel run configurations to XML using the Bazel > Export Run Configurations action (e.g. check them into source code alongside the *.bazelproject file).
  2. Import the resulting XML files via the project view.

Imported run configurations will by default be kept up to date with the original XML during bazel sync. This can be disabled in the run configuration settings, or by removing the corresponding entry from the import_run_configurations project view section.

If and when the run configuration format changes, we will try to support importing older versions for a limited time. We do not guarantee that exported run configurations stay valid forever.

Variable expansion

The run configurations flags section supports expanding environment variables using ${VAR} syntax, to make run configuration sharing easier.

See also Path Variables.

Example:

--user_param=${USER}

Non-Bazel Configuration Producers

If your project contains a non-Bazel JavaScript part, you can enable the JavaScript regular non-Bazel configuration producers for this part from the plugin settings. From Settings -> Bazel Settings, check the Enable JavaScript test configuration producers option. Please note that this requires restarting the IDE to take effect.

Note: Debugging is only supported for targets built during the last sync. If the debug button is disabled for a recently-added target, try running either a full or partial sync in Bazel > Sync.

Note: Improved button icon submissions are welcome!