The Build Server Protocol (BSP) is a standardized protocol that allows IDEs and development tools to communicate with build servers. sbt implements BSP to provide a consistent interface for tools like IntelliJ IDEA, VS Code (Metals), and other build clients.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/sbt/sbt/llms.txt
Use this file to discover all available pages before exploring further.
What is BSP?
BSP defines a JSON-RPC-based protocol for communication between:- Build Client - IDE or development tool
- Build Server - sbt or other build tool
- Request compilation
- Get build target information
- Receive compilation diagnostics
- Run tests
- Resolve dependencies
BSP is language-agnostic and is also implemented by other build tools like Bazel, Mill, Gradle, and Maven. Learn more at build-server-protocol.github.io.
BSP in sbt Architecture
sbt’s BSP implementation is spread across two main areas:1. Protocol Module
Location:protocol/src/main/contraband-scala/sbt/internal/bsp/
Defines BSP data types using Contraband:
| File | Purpose |
|---|---|
BuildTarget.scala | Build target representation |
BuildTargetIdentifier.scala | Unique target IDs |
CompileParams.scala | Compilation request parameters |
CompileReport.scala | Compilation results |
BuildServerCapabilities.scala | Server capability declarations |
BuildClientCapabilities.scala | Client capability declarations |
BspConnectionDetails.scala | Connection configuration |
BspCompileResult.scala | BSP-specific compile results |
2. Server Implementation
Location:main/src/main/scala/sbt/internal/server/BuildServerProtocol.scala:1
Implements the BSP server within sbt:
Server Capabilities
sbt’s BSP server advertises these capabilities:Compile Provider
Compile Provider
Supports compiling build targets.Languages: Scala, JavaHandles
buildTarget/compile requests to trigger incremental compilation via Zinc.Test Provider
Test Provider
Supports running tests.Languages: Scala, JavaHandles
buildTarget/test requests to execute test frameworks.Run Provider
Run Provider
Supports running main classes.Languages: Scala, JavaHandles
buildTarget/run requests to execute applications.Dependency Sources
Dependency Sources
Provides source JARs for dependencies.Handles
buildTarget/dependencySources to enable IDE navigation to library code.Resources
Resources
Provides resource directories.Handles
buildTarget/resources to inform IDEs about resource files.Output Paths
Output Paths
Provides compilation output directories.Handles
buildTarget/outputPaths (added in sbt 1.8.0) to tell IDEs where compiled classes are located.JVM Environments
JVM Environments
Provides JVM runtime configuration.Handles
buildTarget/jvmRunEnvironment and buildTarget/jvmTestEnvironment for proper JVM setup.Reload
Reload
Supports reloading the build.Handles build file changes and project reloading without restarting the server.
BSP Keys in sbt
sbt provides several keys for BSP configuration: Key file:main/src/main/scala/sbt/Keys.scala:1
Setting Up BSP
Generate Connection File
Run the This creates
bspConfig task to create the BSP connection file:.bsp/sbt.json in your project directory.BSP Connection Details
The.bsp/sbt.json file contains:
The
argv field contains the command to start the BSP server. The IDE launches this command and communicates via stdin/stdout.BSP Request Handlers
Implementation location:main/src/main/scala/sbt/internal/server/BuildServerProtocol.scala:47
Key BSP endpoints implemented in sbt:
build/initialize
Initializes the BSP connection and exchanges capabilities.workspace/buildTargets
Returns all build targets in the workspace.buildTarget/compile
Compiles specified build targets.buildTarget/test
Runs tests for specified build targets.buildTarget/dependencySources
Provides source JARs for dependencies.bspReload
Custom sbt command for BSP reload: Key file:main/src/main/scala/sbt/internal/server/BuildServerProtocol.scala:71
Build Target Mapping
sbt maps projects to BSP build targets:- Each sbt project becomes a BSP build target
- Each configuration (Compile, Test) can be a separate target
- Target IDs are URI-based for uniqueness
Configuration Scope
BSP can be configured at different scopes:Meta-targets for SBT Build
ThebspSbtEnabled setting controls whether BSP exports targets for the sbt build itself (meta-build):
- Navigate sbt build definition code
- Get autocompletion in
build.sbt - Compile
project/directory
Error Handling
BSP implementation includes robust error handling: Key file:main/src/main/scala/sbt/internal/server/BuildServerProtocol.scala:92
Debugging BSP
To debug BSP communication:Limitations and Known Issues
Multiple sbt instances
Multiple sbt instances
Only one sbt instance should handle BSP per project directory. Running multiple instances may cause conflicts.
Build file changes
Build file changes
Some build file changes require BSP reload. IDEs should trigger
bspReload when detecting build changes.Debug provider
Debug provider
Debug support is currently limited (DebugProvider returns empty capabilities).
BSP Protocol Version
sbt implements BSP version 2.1.0, which includes:- Output paths support (since sbt 1.8.0)
- JVM environment providers
- Enhanced compilation reporting
Integration Examples
VS Code with Metals
Metals uses BSP to communicate with sbt:- Metals detects
.bsp/sbt.json - Launches sbt BSP server
- Requests build targets
- Triggers compilation on file changes
- Displays diagnostics in editor
IntelliJ IDEA
IntelliJ can use BSP instead of its native sbt integration:- Import project as BSP
- IntelliJ uses BSP for all build operations
- Benefits from standardized protocol
Further Reading
BSP Specification
Official Build Server Protocol documentation
Architecture
How BSP fits into sbt’s architecture
Protocol Module
Source code for protocol definitions
Server Implementation
BSP server implementation in sbt