BSP Support for Bazel
The Build Server Protocol (BSP), a protocol initially developed by the Scala Center and JetBrains, enables the easy integration of IDEs and build tools. To give users even more options in the BSP ecosystem, we decided to add support for Bazel.
While there is already a Bazel plugin for IntelliJ-based IDEs, it is often not up to date with the latest releases. Bazel BSP, on the other hand, works out of the box and can be used with any version of IntelliJ IDEA that supports BSP. Currently this is implemented in the Scala plugin.
The first commits for Bazel BSP date back to October 2019, and since then the project has gone through multiple hands to become a fully working product that we are happy to present. It originally started as a proof of concept, but it has evolved to its current state through the implementation of additional features and improvements to its robustness, maintainability, and extensibility.
Currently, the server supports BSP for Java, Scala, and Kotlin, and we are working on C++. This means that any Bazel project written in those languages can be successfully imported and edited in your IDE if it supports the protocol and relevant extensions.
To make working with monorepos possible and more enjoyable, the ProjectView files mechanism has been adapted from the Bazel plugin, which makes switching between the plugins very easy. It allows you to choose the targets and directories that should be included and excluded during the import, so you do not have to work on the entire codebase.
First you will need to have Coursier.
Then, to install the server inside your project, simply run:
cs launch org.jetbrains.bsp:bazel-bsp:1.0.0 -M org.jetbrains.bsp.bazel.install.Install
More information and installation options can be found in the README file in the repository.
Further steps in IntelliJ IDEA
Currently, the Scala plugin is required for importing projects through BSP.
If you also added the
projectview.bazelproject file to the project, the imported project will contain the specified folders and targets and will not contain the excluded ones.
The architecture consists of 3 main logical components. The BSP server is responsible for communicating with the client (an IDE).
When the BSP server receives a request, it invokes a Bazel command with the appropriate parameters. Some requests (for example, asking for the list of sources) are pure queries – they don’t involve building the project. In such cases, the BSP server prepares a Bazel invocation that utilizes Bazel’s built-in query language and after that they are processed by the parser.
A different approach is used for non-pure requests, such as requests to build a file or run a test. Bazel can dynamically output information about the ongoing build in the form of a stream of protocol buffer messages. It uses another Bazel-specific protocol called the Build Event Protocol (BEP). The purpose of the BEP server is to subscribe to such a stream and to convert BEP events into BSP notifications, which can be fed into the IDE.
The foundation of the Bazel BSP project was laid out by Daniel Wagner-Hall, who made several initial commits in October 2019.The project was then largely developed by André Rocha between July and September 2020. Later, development was taken over by Marcin Abramowicz, Magdalena Augustyńska, Gerard Dróżdż, and Andrzej Głuszak as part of their collaborative Bachelor’s thesis. André also collaborated actively during this time, contributing, discussing solutions, exchanging code reviews, and more. Now the implementation has been improved, new features have been added, and a fully working product has been created.