Every language server I know struggles with the same problem: how to describe the structure of your code and build process to the system so that the LS can accurately provide the LSP servivces like flagging errors the same way your build would and giving precise references to symbols in your editor. In fact, the popular jdt.ls language server shows false error messages in around one third of all maven and gradle project off github we tried to import. Often, the failures are as simple as a folder with generated sources not being detected.
Why Is Importing Workspaces So Hard?
You would think that is should be simple to solve: just detect the additional source folder and be done with it. But both maven and gradle implement these generator plugins as executable code. There is no standard way to reliably detect what the plugin does without running at least parts of the build. And even then the build systems need to be instrumented to detect the proper language server configuration. So detecting the right LS configuration from any particular build description is quite simple, but solving this detection problem for all builds is quite hard.
Language servers usually contain built-in “workspace importers” that can read build system files (like pom.xml) and translate the build files into an internal project model that the language server uses to provide its services. Where these workspace importers can be extended, they usually need to be programmed against a language-server-specific API. For example, to add Bazel support to jdt.ls, you have to write an Eclipse plugin. So if a new build system arises, you would have to provide plugins to all IDE’s and language servers.
If we think about it, there are two problems we need to solve: one is the classic m:n problem: you have m build systems and for each you have to write n plugins for each language server. The second is a problem of edge cases: it’s easy to write a system that works in 95% of cases, but hard to solve the remaining five percent.
A Common Format: mbt.json
To solve the m:n problem, we can apply the logic behind the language server protocol: you put a standard protocol between producers and consumers (in their case: language servers and editors). In the case of workspace importers, this common protocol is mbt.json. It is a file that contains a minimal description of the workspace structure, compiler parameters and external dependencies. It has all the information a language server needs, but nothing else. You might argue that there already is a protocol for integrating build systems into IDE systems: the Build Server Protocol. However, in practice, build servers seem rather hard to implement well. The protocol covers much more than just describing the build to a language server and it is geared towards general builds in arbitrary programming languages. mbt.json provides a much simpler alternative: just the necessary basics and only for JVM languages.
Example mbt.json
So what does a mbt.json file look like?
{
"namespaces": {
"io.trino:trino-cli:483-SNAPSHOT": {
"sources": [
"D:\\anysphere\\test-projects\\trino\\client\\trino-cli\\src\\main\\java",
"D:\\anysphere\\test-projects\\trino\\client\\trino-cli\\target\\generated-sources\\annotations"
],
"javacOptions": [
"-release",
"11"
],
"dependencyModules": [
"com.fasterxml.jackson.core:jackson-core:2.22.0",
"com.google.errorprone:error_prone_annotations:2.50.0",
...
],
"javaHome": "C:\\Program Files\\Eclipse Adoptium\\jdk-25.0.2.10-hotspot",
"dependsOn": [
"io.trino:trino-client:483-SNAPSHOT",
"io.trino:trino-grammar:483-SNAPSHOT"
],
"classDirectories": [
"D:\\anysphere\\test-projects\\trino\\client\\trino-cli\\target\\classes"
],
"projectPath": "D:\\anysphere\\test-projects\\trino\\client\\trino-cli"
},
...
},
"dependencyModules": [
{
"id": "com.fasterxml.jackson.core:jackson-core:2.22.0",
"jar": "file:/C:/Users/thomas/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.22.0/jackson-core-2.22.0.jar",
"sources": "file:/C:/Users/thomas/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.22.0/jackson-core-2.22.0-sources.jar"
},
{
"id": "com.google.errorprone:error_prone_annotations:2.50.0",
"jar": "file:/C:/Users/thomas/.m2/repository/com/google/errorprone/error_prone_annotations/2.50.0/error_prone_annotations-2.50.0.jar",
"sources": "file:/C:/Users/thomas/.m2/repository/com/google/errorprone/error_prone_annotations/2.50.0/error_prone_annotations-2.50.0-sources.jar"
},
...
]
}
You can see that the file contains a list of namespaces keyed by a build-system-specific id. The namespace describes which sources belong to the
namespace and what internal (dependsOn) and external (dependencyModules) dependencies are on the compile classpath for files in this namespace.
The namespace also mentions compiler options and which JDK this namespace compiles against. The dependencyModules refer to an id in a shared list of
dependencies: this is purely to save space, as the list of external dependencies is heavily shared between namespaces.
That seems so simple, but can it be enough? It turns out that yes, all the LSP features can be implemented with this simple description.
Handling the Edge Cases
Now that we have a target format for our workspace import, how do we handle the edge cases? The answer is: we don’t. Instead of trying to handle all the possible permutations, we allow the user to “bring your own script”. If the standard maven importer can’t handle the import of your workspace, you can configure your own import script. Since you generally know your own build process well, it should be quite simple to write a proper mbt.json. I still expect most use cases to be handled by standard builds system importers, but particularly large organisation sometimes have builds that might even contain their own build system plugins. For these cases, “BYOS” offers an “escape hatch” to make the LS work correctly.
Future Directions
What I’d like to see is that we have a “canonical” build importer for each built tool. Ideally provided by the authors of the build tools themselves and updated as the build tools evolve.
On the technical side, I’d like to evolve handling mbt.json in java-ls such that we folks can commit the mbt.json into their repositories. We
can already handle workspace-relative paths in sourcesitems, so what is needed is resolving the javaHome and external dependencies upon loading
mbt.json instead of during the generation of the file. For javaHome, we could record the constraints (java version, vendor, etc.) instead of
this location on disk. For external dependencies, we are already use URIs, so instead of file:// we could use mvn://... uris and resolve them
when loading the mbt.json file.
See It in Action
If you want to see a Java language server based on mbt.json in action, you can simply install java-ls from the VS Code Marketplace or from Open VSX. It contains a built-in importer that should work for most maven-based workspaces: just open your maven workspace and it should just work. It generates `.javals/mbt.json upon startup and regenerates it when any pom.xml in the workspace changes.
