Bug 458624 (omnisharp)

Summary: [LSP] Omnisharp-roslyn Launches Out of Scope
Product: [Applications] kate Reporter: nekonexus
Component: scriptingAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: christoph, eric1, waqar.17a
Priority: NOR Keywords: needs_verification, usability
Version First Reported In: 22.08.0Flags: nekonexus: Usability+
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
URL: https://aur.archlinux.org/packages/omnisharp-roslyn
See Also: https://bugs.kde.org/show_bug.cgi?id=452813
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: LSP Output Log

Description nekonexus 2022-09-02 04:29:01 UTC
Created attachment 151776 [details]
LSP Output Log

SUMMARY
The Omnisharp language server is started by Kate in what seems to be the home directory rather than where the project folder is; this causes Omnisharp not to be able to find the desired C# solution (.sln) and not provide LSP support as it should.
(Omnisharp version 1.39.1)

STEPS TO REPRODUCE
1. Install Omnisharp-roslyn (There's several AUR pages for it; I provided one in the URL section of this report)
2. Clone a C# project from a source repository
3. Open the project folder with Kate (Kate will prompt the user to enable the language server)

OBSERVED RESULT
The language server is started but doesn't provide any of the intended functions, possibly due to being out of scope of the project location

EXPECTED RESULT
The language server should be directed to the C# project appropriately to provide LSP data

SOFTWARE/OS VERSIONS
Linux: Arch Linux
KDE Plasma Version: 5.25.4
KDE Frameworks Version: 5.97.0
Qt Version: 5.15.5

ADDITIONAL INFORMATION
Comment 1 Waqar Ahmed 2022-09-02 06:40:53 UTC
Hi, if you program in C# can you try a different config or perhaps some variable is missing from the current config?
Comment 2 nekonexus 2022-09-02 06:47:47 UTC
I mean, I can try a different config (Kate already includes one by default) but I'm not too sure how I'm going to figure it out; my experience with Omnisharp is mostly limited to VSCode, where it typically does this on its own, so...
Comment 3 Waqar Ahmed 2022-09-02 06:59:39 UTC
You can check what other editors do. Point is, its highly unlikely any of the kate devs will pick this as I think no one is a c# programmer.
Comment 4 Christoph Cullmann 2022-09-03 18:43:51 UTC
I would assume one needs some logic to find the first directory with an .csproj file like e.g. the vim plugin does do it.

https://github.com/nickspoons/vim-sharpenup/blob/master/autoload/sharpenup/legacycsproj.vim

Would that help? Does your project have such a file?

Unfortunately ATM we only allow a fixed list of names for such a root directory search via

"rootIndicationFileNames": ["pubspec.yaml"],
Comment 5 Eric Armbruster 2022-09-23 19:09:56 UTC
I've looked a bit into this and the rootpath of the language server is set correctly for me. It uses the rootpath from the project base directory.

I've also started work on a patch that implements Christoph's suggestion. Though it should not be of any help here really, as the rootpath is already set and we generally don't want to override it.

Can you provide us additional info?
- Did you open the project using the Open Folder... action?
- In the Kate Output View are there any warnings, erros by the OmniSharp server?

For me the server seems to have trouble finding dotnet libraries. Probably, because some env var is not set correctly.
Comment 6 Eric Armbruster 2022-09-23 19:11:05 UTC
I missed that output log :)
Comment 7 nekonexus 2022-09-23 19:14:35 UTC
For more context, I did use the "open folder" action but I opened a folder that had a .csproj file and the .sln file was in a subdirectory
Comment 8 Eric Armbruster 2022-09-23 19:19:34 UTC
From the logs it looks like the server started up, but maybe something went wrong because of the permission denied:

/home/neko-san/.local/share/Steam/steamapps/compatdata/0/pfx/dosdevices/z:/var/cache/ldconfig' is denied.
    ---> System.IO.IOException: Permission denied

Can you fix your permissions and retry?

Also, from the one C-Sharp project I just opened *.sln is located one level above *.csproj. Not sure if I understood you correctly, but I would try opening on the topmost level of the project.
Comment 9 nekonexus 2022-09-23 19:24:13 UTC
The funny thing about this is: that's not where I had the editor open at all
I had the editor open in /mnt/extraSgorage/<rest of the path> (nowhere near my home directory or any "Z:" drive
What you're seeing is omnisharp looking in a Steam Proton prefix for no clear reason; which has nothing to do with what the language server should even be looking at
Comment 10 nekonexus 2022-09-23 19:25:59 UTC
For me, the peoject was arranged with .csproj in the top level and the .sln in a sub-level (the opposite of what you described)
Comment 11 Eric Armbruster 2022-09-23 19:35:52 UTC
Tested now with and without my patch, and the server just works for me... even without finding dotnet libraries. Some LSP actions seem to be not supported by the server (Go To Declaration, Go To type definition, Highlight, Expand and Shrink Selection) but the rest should work fine.

I would really recommend trying to fix that permissions issue. I tested with this project
- https://github.com/ttu/csharp-tutorial as project opened at toplevel
- https://aur.archlinux.org/packages/omnisharp-roslyn

> What you're seeing is omnisharp looking in a Steam Proton prefix for no clear reason; which has nothing to do with what the language server should even be looking at

Yeah, don't know why it is doing that, maybe it is searching for dotnet libraries. In any case, that cannot be influenced by Kate, so you would need to report that at the omnisharp repo.
Comment 12 Eric Armbruster 2022-09-23 20:10:09 UTC
This might also be relevant for you: https://bugs.kde.org/show_bug.cgi?id=459579

Essentially, ensure before opening any document (that's when the LSP server is started) that you have activated (selected) the project which belongs to this document. If you want to be 100% sure that you are doing this correctly, open only one project in Kate.
Comment 13 Waqar Ahmed 2022-09-26 07:59:26 UTC
After we fixed the mismatch in project path. This should now work all the time, right? If so, can we mark it as resolved?
Comment 14 nekonexus 2022-09-26 08:14:00 UTC
I still have this problem (though it was a cloned Visual Studio project from someone else's machine) but whatever...
I'm resigned to not having a solution at this point.
Comment 15 Waqar Ahmed 2022-09-26 08:55:04 UTC
> I had the editor open in /mnt/extraSgorage/<rest of the path> (nowhere near my home directory or any "Z:" drive

Can you try with a project that exists in your home directory somewhere? Maybe the lsp server just isn't able to access that `/mnt/...` path and falls back to your home dir.
Comment 16 nekonexus 2022-09-26 10:08:45 UTC
> Can you try with a project that exists in your home directory somewhere? Maybe the lsp server just isn't able to access that `/mnt/...` path and falls back to your home dir.

Doing this in the home folder made no difference, apparently; it still falls back to the home directory.

```
git clone (project url)
cd (project url)
kate .
(click on files in the project folde view on the right until something triggers kate to run the language server)
(same result as before)
```
Comment 17 Waqar Ahmed 2022-09-26 10:39:00 UTC
Can you use the following config. Put it in "User Server Settings" in the settings dialog:
```
{
    "servers": {
        "c-sharp": {
            "command": ["omnisharp", "-lsp"],
            "commandDebug": ["omnisharp", "--debug", "--verbose"],
            "url": "https://github.com/OmniSharp/omnisharp-roslyn",
            "rootIndicationFileNames": ["*.csproj"],
            "highlightingModeRegex": "^C#$"
        }
    }
}
```
make sure the json is valid.

Then 
- open your project
- open a csharp file => should start the server

In the above config I added an explicit `            "rootIndicationFileNames": ["*.csproj"],`

I am not yet sure whether *.csproj will work, but we already have stuff like this so maybe it works.
Comment 18 Christoph Cullmann 2022-09-26 11:00:09 UTC
> I am not yet sure whether *.csproj will work, but we already have stuff like this so maybe it works.

Unfortunately I think that doesn't work, we lookup only precise filenames, no idea why that was added to some other places.
If we want that, we need to extend that.
Comment 19 Eric Armbruster 2022-09-26 11:45:23 UTC
Also, rootIndicationFileNames will be ignored as the rootpath is already set previously to the project base directory. I've verified this.
Comment 20 Christoph Cullmann 2022-11-12 20:58:10 UTC
We merged a change to support wildcards

https://invent.kde.org/utilities/kate/-/merge_requests/907
Comment 21 Christoph Cullmann 2023-02-26 22:59:44 UTC
Should we now add the *.csproj stuff?
Comment 22 Christoph Cullmann 2023-03-06 19:16:20 UTC
https://invent.kde.org/utilities/kate/-/merge_requests/1142