Skip to content

Several instances of one language server per project, e.g. one per module #1701

Closed

Description

@d-biehl

This picks up the original request of #1352: several instances of one language server definition in a project, each serving a part of it. #1352 turned into the workspace folder support of #1492, which decides which folders the one instance sees. That doesn't help when each instance has to be started with a different Python interpreter.

Use case

RobotCode, the Robot Framework plugin, uses LSP4IJ. Its language server always has to run with the Python interpreter of the project or module whose files it analyses, and it uses the Robot Framework installed in that interpreter's environment. The plugin ships only the server's own code and starts it with that interpreter.

An IntelliJ project can contain several modules, each with its own interpreter, for example:

my-tests/              IntelliJ project
├── web-tests/         module, .venv with Python 3.12, Robot Framework 7.3 and the Browser library
│   ├── robot.toml
│   └── tests/
└── api-tests/         module, .venv with Python 3.10, Robot Framework 6.1 and RequestsLibrary
    ├── robot.toml
    └── tests/

Such projects are common: IntelliJ IDEA lets each module have its own Python SDK, and the workspace mode of PyCharm 2026.2, enabled by default, makes every directory with a pyproject.toml a module of its own; in Poetry and Hatch monorepos each of them gets its own environment.

Each of these modules needs a server process started with the module's own interpreter, because the server analyses the module with the module's environment:

  • it parses with the Robot Framework version installed there;
  • it resolves what the tests import, such as Robot Framework libraries, Python packages, variable files and resource files shipped in packages, on that environment's Python path, and imports the libraries and variable files, with all their dependencies, to get their keywords and variables in the versions installed there;
  • completion for imports offers the packages and files installed there;
  • at startup it reads the module's robot.toml and puts the environment variables and Python paths from it into its own process.

Today LSP4IJ starts one instance per project, so all modules are analysed with one interpreter: imports that only another module's environment provides fail, keywords and their documentation come from the wrong versions, and users are told that Robot Framework is missing in a module whose interpreter has it.

Workspace folders don't solve this: one process still runs with one interpreter. This is different from the Pyright case in #1352, where one process can serve all folders.

VS Code does this

In #1352 you wrote that you would implement this if VS Code can manage the use case. It can:

Neovim works the same way: by default it reuses a running client only if the server name and the root directory match.

Proposal

  1. A server definition can tell which instance serves a file. The default stays one instance per project; other choices are one instance per module, per content root (the IntelliJ counterpart of a VS Code workspace folder), or a grouping that the adopter computes.
  2. LSP4IJ passes the instance's scope to the factory when it creates the connection provider and the language client, so the command line, the working directory and the initialization options can differ per instance.
  3. Each instance is initialized with the roots of its scope as rootUri and workspaceFolders, like a VS Code client locked to a workspace folder.
  4. An instance starts when the first file of its scope is opened and stops on its own, as servers do today.
  5. There is API to get the instance for a file and all instances of a definition. LanguageServerManager.start and stop act on all instances, as they already do for every wrapper of a definition.
  6. The Language Servers view shows each instance as a process of its own, named after its scope, with its own log and traces.
  7. VFS events and file operations go to the instance whose scope contains the file.

Definitions that don't opt in keep today's behavior: the new methods are default methods, so existing factories compile and run unchanged, and the project scope keeps one instance per project with the same root, workspace folders and file events.

A first sketch of the API:

public interface LanguageServerFactory {

    // ... existing methods

    /**
     * Returns the scope of the instance that serves the given file. Files with equal scopes share one instance.
     * The default is the project, which keeps one instance per project.
     */
    default @NotNull LanguageServerScope getServerScope(@NotNull VirtualFile file, @NotNull Project project) {
        return LanguageServerScope.project(project);
    }

    default @NotNull StreamConnectionProvider createConnectionProvider(@NotNull Project project,
                                                                       @NotNull LanguageServerScope scope) {
        return createConnectionProvider(project);
    }

    default @NotNull LanguageClientImpl createLanguageClient(@NotNull Project project,
                                                             @NotNull LanguageServerScope scope) {
        return createLanguageClient(project);
    }
}

LanguageServerScope would have an id for equality, a display name and its roots, with factories such as project(Project), module(Module) and folder(VirtualFile). User-defined servers could get the same choice in their settings later, for example one instance per workspace folder of the configured strategy.

The current code is close to this: collectLanguageServersFromDefinition reuses a started wrapper only if it can operate on the file and creates a new one otherwise, but canOperate(VirtualFile) returns true for every existing file, and LanguageServerWrapper has a constructor with an initialPath that nothing reads. The core change would give each wrapper its scope, compare scopes in canOperate, and use the scope for the root, the workspace folders and the factory calls.

If you agree with the direction, I could prepare a PR with tests. Which shape of the API would you prefer? I could also provide a sample project with two modules, two virtual environments and different Robot Framework versions.


AI/tooling disclosure: This issue was prepared with assistance from Claude Code. I reviewed the result manually and understand the use case and the proposal.

Activity

  1. angelozerr commented on Oct 31, 2026

    @angelozerr
    Contributor

    @d-biehl it not a trivial issue. We need:

    • provide this feature with LanguageServerFactory like you suggested
    • update LanguageServer UI view to see those server instances (instead to see the single instance)
    • provides the capabilty to customize this behavior for user defined ls.
  2. lagergren commented on Nov 3, 2026

    @lagergren
    Collaborator

    I think this would be a really powerful feature. Is work progressing or hasn't anything been started?

  3. angelozerr commented on Nov 3, 2026

    @angelozerr
    Contributor

    Nothing for he moment.

  4. d-biehl commented on Nov 3, 2026

    @d-biehl
    ContributorAuthor

    I'm working on it...

  5. d-biehl commented on Nov 3, 2026

    @d-biehl
    ContributorAuthor

    A correction to point 7 of the proposal, "VFS events and file operations go to the instance whose scope contains the file": that was wrong, and the point can be dropped.

    Each instance should get the file events and file operations that match what it registered, also for files outside its scope. Each wrapper already works like that today, so nothing has to change here. VS Code does the same: a client locked to a workspace folder creates its watchers from the registered globs and matches file operations by scheme and pattern, without looking at the folder.

    Servers can depend on this. RobotCode's server watches the locations of what a module imports, so its instance has to hear about changes to a shared file outside the module.

  6. angelozerr commented on Nov 3, 2026

    @angelozerr
    Contributor

    @d-biehl perhaps I am wrong but it could be a good idea to extend workspace folder strategy to define those folder instance.

    User defined uses the configurable workspace strategy.
    See doc at https://github.com/redhat-developer/lsp4ij/blob/main/docs/UserDefinedLanguageServer.md#workspace-folders-tab

    By extending workspace strategy, user could reuse the Testing configuration to define folder server instance and workspace folder both.

    It is also important to see those server instance in the Language Server view. Today the tree is filled with Language Server as root and process as child. We should see several process when we will have several instances (and it should be nice to see the folder).

  7. d-biehl commented on Nov 3, 2026

    @d-biehl
    ContributorAuthor

    @angelozerr Your suggestion works well, so I'm following it: the workspace folder strategy now decides the instances, and the Language Servers view shows each one with its folder. It's in the draft #1853. User-defined servers and the "Testing your configuration" preview come next. Feedback there is welcome.

  8. angelozerr commented on Nov 4, 2026

    @angelozerr
    Contributor

    10. @angelozerr Your suggestion works well, so I'm following it: the workspace folder strategy now decides the instances, and the Language Servers view shows each one with its folder. It's in the draft #1853. User-defined servers and the "Testing your configuration" preview come next. Feedback there is welcome.

    It is a super news that you can extend workspace strategy.

    But I think we should also provide the capability to define workspace folders inside a server instance level.

  9. 60 remaining items

  10. lagergren commented on Nov 13, 2026

    @lagergren
    Collaborator

    Done in 0.30.0 by #1853, which follows the plan above: a workspace folder strategy can give a language server several instances (hasServerInstances and getServerInstanceRoot), each with its own root and environment, and the Language Servers view shows each instance with its folder. A user-defined language server sets it with serverInstances, and #1929 added ${instanceRoot} for its command and working directory. See Several language server instances in the user guide and in the developer guide.

    Closing it; anything left can get an issue of its own.

  11. added this to the 0.30.0 milestone on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions