Bug 492061 - rust-analyzer language server hits error condition while starting, "failed to find any projects" (LSP plugin)
Summary: rust-analyzer language server hits error condition while starting, "failed to...
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: general (other bugs)
Version First Reported In: 24.08.0
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-08-23 03:15 UTC by Adam Fontenot
Modified: 2024-09-26 16:38 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Adam Fontenot 2024-08-23 03:15:41 UTC
SUMMARY

When I open a project in Kate (e.g. a project with a Git history that Kate shows in the "open recent" box when you start it), and click on a Rust code file, rust-analyzer is automatically started. Every time it does this, it shows an error, forcing open the "Output" tab.

The error suggests that rust-analyzer is looking for my projects in my home directory. That's obviously inappropriate, as I specifically loaded the project I wanted to look at in Kate, so Kate should set the project directory (as seen by rust-analyzer) to that directory.

The help text gives some additional information about hardcoding your projects with `linkedProjects`, but that's obviously not appropriate here.

Kate version is 24.08.0 (built myself, same issue with 24.05.2 distribution package). rust-analyzer 20240819

STEPS TO REPRODUCE
1. Open a Rust source file in a project.

OBSERVED RESULT

The following text gets printed to Output:

[22:49:14 LSP Client Log] Started server rust@/home/adam: /usr/bin/rust-analyzer
[22:49:14 LSP Server Log] rust@/home/adam
2024-08-23T02:49:14.728559Z ERROR failed to find any projects in [AbsPathBuf("/home/adam")]
[22:49:14 LSP Server Warning] rust@/home/adam
Failed to discover workspace.
Consider adding the `Cargo.toml` of the workspace to the [`linkedProjects`](https://rust-analyzer.github.io/manual.html#rust-analyzer.linkedProjects) setting.
[22:49:15 LSP Server Info] Fetching
[22:49:14 LSP Server Error] rust@/home/adam
Failed to load workspaces.

EXPECTED RESULT

No error, loads correctly.

SOFTWARE/OS VERSIONS
Operating System: Arch Linux 
KDE Plasma Version: 6.1.4
KDE Frameworks Version: 6.5.0
Qt Version: 6.7.2
Kernel Version: 6.10.5-arch1-1 (64-bit)
Graphics Platform: Wayland
Comment 1 Waqar Ahmed 2024-08-23 08:14:54 UTC
You can ignore that error, rust-analyzer continues to work correctly despite what it says.

> The error suggests that rust-analyzer is looking for my projects in my home directory. That's obviously inappropriate, as I specifically loaded the project I wanted to look at in Kate, so Kate should set the project directory (as seen by rust-analyzer) to that directory.

If we do it, then Kate will open a server for every new root it finds, which is a disaster. If you want to do it, change your lsp settings and specify the following for rust-analyzer:

            "rootIndicationFileNames": ["Cargo.lock", "Cargo.toml"],

I would suggest taking this up with rust-analyzer, it should be able to discover the root on its own (and it does).
Comment 2 Adam Fontenot 2024-08-23 11:41:40 UTC
(In reply to Waqar Ahmed from comment #1)
> You can ignore that error, rust-analyzer continues to work correctly despite
> what it says.

If the error is expected, could the plugin be configured to accept this specific error response when starting the server? My issue isn't about the message in the LSP output, it's that it forces open the Output tab (and flashes the tab at you) because it thinks there's some serious problem you need to deal with. I think fixing that would be a good QoL improvement.

> If we do it, then Kate will open a server for every new root it finds, which
> is a disaster. If you want to do it, change your lsp settings and specify
> the following for rust-analyzer:
> 
>             "rootIndicationFileNames": ["Cargo.lock", "Cargo.toml"],

I don't immediately see why this is so bad: is the idea that if I open multiple projects in a single Kate session, one rust-analyzer instance should be sufficient to handle all of them as if they were one big project? Does rust-analyzer's "project awareness" have any benefits that we're forgoing by doing it this way?

> I would suggest taking this up with rust-analyzer, it should be able to
> discover the root on its own (and it does).

I will file a bug on that end too and see what they make of it, thanks!
Comment 3 Waqar Ahmed 2024-08-23 13:58:26 UTC
You don't have to open multiple projects in an actual sense. I mean, you are working on your project and you jump to a definition in a crate or std library. This will start a new server because there is a new root. This is annoying in multiple ways and leads to excessive resource usage quickly.

Another possible way to fix this might be to specify `root` and `userWorkspace` in the lsp settings:

```
            "command": ["rust-analyzer"],
            "path": ["%{ENV:CARGO_HOME}/bin", "%{ENV:HOME}/.cargo/bin", "%{ENV:USERPROFILE}/.cargo/bin"],
            "url": "https://rust-analyzer.github.io",
            "highlightingModeRegex": "^Rust$",
            "root": "", // this
            "useWorkspace": true, // and this
```
IIRC, this tries to use the open project's root. Project here means kate's project in the sidebar.
Comment 4 Adam Fontenot 2024-08-25 22:12:09 UTC
(In reply to Waqar Ahmed from comment #3)
> Another possible way to fix this might be to specify `root` and
> `userWorkspace` in the lsp settings:

Thanks for the hints! I found that the following combination of settings gives the correct behavior:

            "useWorkspace": true,
            "rootIndicationFileNames": ["Cargo.lock", "Cargo.toml"],

If you have both of these, rust-analyzer

  ✅ Automatically detects the correct project when you open a Rust file
  ✅ Does *not* open a new rust-analyzer instance when I select "go to definition" on an external symbol.

If you can verify this behavior and can check that this doesn't have any weird side effects, we could consider making these options part of the Rust defaults?
Comment 5 Waqar Ahmed 2024-08-27 12:28:50 UTC
Yeah, I think we can make this the default. Ultimately the users of a language server have to decide what's the best possible default. Also, we have a lot of time to test this before the next release.
Comment 6 Bug Janitor Service 2024-09-26 04:58:04 UTC
A possibly relevant merge request was started @ https://invent.kde.org/utilities/kate/-/merge_requests/1592
Comment 7 Waqar Ahmed 2024-09-26 16:38:00 UTC
Git commit 8a790b3b895624fca4848fe308753ab22dd46e61 by Waqar Ahmed.
Committed on 26/09/2024 at 16:37.
Pushed by waqar into branch 'master'.

Update dart & rust lsp settings

We need rootIndicationFileNames for both these servers so that a
rootPath is sent to the server correctly. The servers fail to do this
job themselves even though they shouldn't. Besides that, we must set
useWorkspace=true so that a new server isn't spawned for every new
project root.

M  +4    -3    addons/lspclient/settings.json

https://invent.kde.org/utilities/kate/-/commit/8a790b3b895624fca4848fe308753ab22dd46e61