NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.12k stars 14.16k forks source link

Zed Editor package can't use language servers #309662

Open ImTheSquid opened 6 months ago

ImTheSquid commented 6 months ago

Describe the bug

Zed has the same issue as many other editors where it tries to run dynamically-linked libraries. A FHS chroot like vscode-fhs would probably fix this.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Open Zed
  2. Attempt to use any language server

Expected behavior

Zed is able to download and execute the language server of choice.

Additional context

Here is the log entry:

2024-05-06T18:29:14-04:00 [INFO] starting language server. binary path: "/home/jack/.local/share/zed/languages/rust-analyzer/rust-analyzer-2024-05-06", working directory: "/home/jack/Code/Rust/CodeCheck", args: []
2024-05-06T18:29:14-04:00 [ERROR] crates/lsp/src/lsp.rs:352: cannot read LSP message headers
2024-05-06T18:29:14-04:00 [ERROR] failed to start language server "rust-analyzer": server shut down
2024-05-06T18:29:14-04:00 [ERROR] server stderr: Some("Could not start dynamically linked executable: /home/jack/.local/share/zed/languages/rust-analyzer/rust-analyzer-2024-05-06\nNixOS cannot run dynamically linked executables intended for generic\nlinux environments out of the box. For more information, see:\n")

Notify maintainers

@GaetanLepage @niklaskorz

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.6.30, NixOS, 24.05 (Uakari), 24.05pre621993.25865a40d14b`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.2`
 - channels(root): `"nixos"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos`

Add a :+1: reaction to issues you find important.

FireFragment commented 6 months ago

Temporary workaround that works for me with Rust Analyzer is to use buildFHSEnv.

Create shell.nix and paste:

{ pkgs ? import <nixpkgs> {} }:

(pkgs.buildFHSEnv {
    name = "zed";
    runScript = "zed";
}).env

Then run nix-shell in the same directory.

GaetanLepage commented 6 months ago

Is zed trying to run the language servers from your PATH, failing and then defaulting to its own binaries (~/.local/share/zed/languages) or is it directly using those regardless of your environment ?

ImTheSquid commented 6 months ago

As far as I can tell I think it only tries from the ones it downloads.

2024-05-12T08:35:16-04:00 [INFO] starting language servers for Rust: rust-analyzer
2024-05-12T08:35:16-04:00 [INFO] starting language server "rust-analyzer", path: "/home/jack/Code/Rust/CodeCheck", id: 1
2024-05-12T08:35:16-04:00 [INFO] starting language servers for Rust: rust-analyzer
2024-05-12T08:35:16-04:00 [INFO] starting language servers for Nix: 
2024-05-12T08:35:16-04:00 [INFO] starting language servers for Git Ignore: 
2024-05-12T08:35:16-04:00 [INFO] starting language servers for Rust: rust-analyzer
2024-05-12T08:35:16-04:00 [INFO] starting language servers for Rust: rust-analyzer
2024-05-12T08:35:16-04:00 [INFO] fetching latest version of language server "rust-analyzer"
2024-05-12T08:35:16-04:00 [INFO] Creating buffer 0x56126b48f6c0 of size 45472, name 'chunk-6', handle 8
2024-05-12T08:35:17-04:00 [INFO] add connection to peer
2024-05-12T08:35:17-04:00 [INFO] add_connection;
2024-05-12T08:35:17-04:00 [INFO] waiting for server hello
2024-05-12T08:35:17-04:00 [INFO] got server hello
2024-05-12T08:35:17-04:00 [INFO] set status to connected (connection id: ConnectionId { owner_id: 0, id: 0 }, peer id: PeerId { owner_id: 403, id: 1518134 })
2024-05-12T08:35:17-04:00 [INFO] set status on client 55287: Connected { peer_id: PeerId { owner_id: 403, id: 1518134 }, connection_id: ConnectionId { owner_id: 0, id: 0 } }
2024-05-12T08:35:17-04:00 [INFO] downloading language server "rust-analyzer"
2024-05-12T08:35:18-04:00 [INFO] Creating texture 0x56126b48fc50 of size 1024x1024x1 and format Bgra8Unorm, name 'atlas', handle 9
2024-05-12T08:35:18-04:00 [WARN] GPU frame is out of date
2024-05-12T08:35:18-04:00 [INFO] Using surface present mode FIFO_RELAXED
2024-05-12T08:35:20-04:00 [INFO] starting language server. binary path: "/home/jack/.local/share/zed/languages/rust-analyzer/rust-analyzer-2024-05-06", working directory: "/home/jack/Code/Rust/CodeCheck", args: []
2024-05-12T08:35:20-04:00 [ERROR] crates/lsp/src/lsp.rs:352: cannot read LSP message headers
2024-05-12T08:35:20-04:00 [ERROR] crates/lsp/src/lsp.rs:374: Broken pipe (os error 32)
2024-05-12T08:35:20-04:00 [ERROR] failed to start language server "rust-analyzer": server shut down
2024-05-12T08:35:20-04:00 [ERROR] server stderr: Some("Could not start dynamically linked executable: /home/jack/.local/share/zed/languages/rust-analyzer/rust-analyzer-2024-05-06\nNixOS cannot run dynamically linked executables intended for generic\nlinux environments out of the box. For more information, see:\n")
GaetanLepage commented 6 months ago

According to their website, Zed is not yet able to connect with "arbitrary language servers". Maybe we'll have to wait for this to be able to inject our nix built binaries...

Liamolucko commented 5 months ago

According to their website, Zed is not yet able to connect with "arbitrary language servers". Maybe we'll have to wait for this to be able to inject our nix built binaries...

There seems to be an (undocumented) configuration option to override the language server binary it uses for a language: https://github.com/NixOS/nixpkgs/issues/284064#issuecomment-2106030589.

EDIT: seems like it only works for C, Tailwind, Rust and Go right now though, since each language has to set it up manually.

oliveirarleo commented 5 months ago

Try this: In my configuration.nix:

 with pkgs; [
  unstable-small.zed-editor
  nodePackages.typescript-language-server
  tree-sitter-grammars.tree-sitter-javascript
  elixir-ls
  tree-sitter-grammars.tree-sitter-elixir
 ...
]

Run with:

steam-run zed &

I got it working by running with steam-run, since the default download fails and binaries for the lsp are not patched. The editor uses the system's lsp in case it cannot download.

I've noticed this by investigating zed logs and seeing logs of the downloaded node command was not patched. Seems like there is more to it, since elixir was not working without steam run as well. image

vinicius507 commented 5 months ago

I'm executing Zed in a FHS environment using:

{pkgs, ...}: let
  zed-fhs = pkgs.buildFHSUserEnv {
    name = "zed";
    targetPkgs = pkgs:
      with pkgs; [
        zed-editor
      ];
    runScript = "zed";
  };
in {
  home.packages = [zed-fhs];
};

And it works quite well. The only problem is that it does not show up in the desktop entries.

ioitiki commented 4 months ago

I'm executing Zed in a FHS environment using:

{pkgs, ...}: let
  zed-fhs = pkgs.buildFHSUserEnv {
    name = "zed";
    targetPkgs = pkgs:
      with pkgs; [
        zed-editor
      ];
    runScript = "zed";
  };
in {
  home.packages = [zed-fhs];
};

And it works quite well. The only problem is that it does not show up in the desktop entries.

this works for me but every time i open the editor it has to reinitialize and i see this:

image

@vinicius507 is this the same for you?

bbigras commented 4 months ago

Mine is not reinitializing on every launch. I'm using 0.143.7 though.

ioitiki commented 4 months ago

okay thanks @bbigras mind sharing the overlay?

bbigras commented 4 months ago

@ioitiki I didn't make an overlay yet, but I made a devShell https://github.com/NixOS/nixpkgs/pull/324549#issuecomment-2221753351 . I also pushed a x86_64 build to cachix.

domenkozar commented 4 months ago

I'd recommend leaving a thumbs up at https://github.com/zed-industries/zed/issues/4977

ioitiki commented 4 months ago

image @bbigras I was able to get an overlay working :D :D

jansol commented 3 months ago

FWIW for many language servers Zed will first check PATH before trying to download and run its own. Additionally some language servers let you explicitly specify a binary path, which is what I have been doing:


  "lsp": {
    "bash-language-server": {
      "binary":{
        "path": "/etc/profiles/per-user/myself/bin/bash-language-server",
        "arguments": ["start"]
      }
    },
    "clangd": {
      "binary":{
        "path": "/etc/profiles/per-user/myself/bin/clangd",
        "arguments": ["--background-index", "--compile-commands-dir=build"]
      }
    },
    "rust-analyzer": {
      "binary": {
        "path": "/run/current-system/sw/bin/bash",
        "arguments": ["-c", "if [ -e flake.nix ]; then nix develop --command rust-analyzer; else rust-analyzer; fi"]
      }
    },
    "taplo": {
      "binary":{
        "path": "/etc/profiles/per-user/myself/bin/taplo",
        "arguments": ["lsp"]
      }
    },
    "json-language-server": {
      "binary":{
        "path": "/etc/profiles/per-user/myself/bin/vscode-json-languageserver",
        "arguments": ["--stdio"]
      }
    }
  }

(the rust-analyzer is a bit hacky but I wanted it to work both for simple projects with no system dependencies and for complex ones with a flake.nix that specifies system deps & stuff)

nixos-discourse commented 3 months ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/zed-editor-the-server-certificate-could-not-be-validated/49992/2

Th1nkK1D commented 3 months ago

I'd recommend leaving a thumbs up at zed-industries/zed#4977

Look like they add direnv support in v0.147.0-pre according to the changelog

Improved the environment-variable detection when running tasks so that tasks can now access environment variables as if the task had been spawned in a terminal that cded into a project directory. That means environment variables set by direnv/asdf/mise and other tools are now picked up

Waiting for the stable release. I'm not sure if it can solve our issues, we might need to wait for https://github.com/zed-industries/zed/issues/4978 as well.

Mastermindaxe commented 2 months ago

I'm asking because there is some conflicting information out there. Is there any workaround atm to make Zed work (including language servers)? And would I have to provide a shell every time or can I use this workaround https://github.com/NixOS/nixpkgs/issues/309662#issuecomment-2105983570 only in shells?

jansol commented 2 months ago

It's not so much conflicting information as there being multiple viable workarounds. Pick whichever you like most.

jrunestone commented 2 months ago

I'm executing Zed in a FHS environment using:

{pkgs, ...}: let
  zed-fhs = pkgs.buildFHSUserEnv {
    name = "zed";
    targetPkgs = pkgs:
      with pkgs; [
        zed-editor
      ];
    runScript = "zed";
  };
in {
  home.packages = [zed-fhs];
};

And it works quite well. The only problem is that it does not show up in the desktop entries.

(https://github.com/NixOS/nixpkgs/issues/309662#issuecomment-2155122284)

@Mastermindaxe this works perfectly for me, using that in my nix config and running zed as usual.

k3yss commented 2 months ago

@GaetanLepage I don't think the issue is fixed yet and we should close it, my comment here describe that rust analyzer still doesn't work with zed

niklaskorz commented 2 months ago

@GaetanLepage I don't think the issue is fixed yet and we should close it, my comment here describe that rust analyzer still doesn't work with zed

I'll have a look which packages might be missing for rust-analyzer

niklaskorz commented 2 months ago

@k3yss rust-analyzer inside Zed works fine for me without additional configuration, are you using pkgs.zed-editor.fhs? (not pkgs.zed-editor)

k3yss commented 2 months ago

@niklaskorz

Here are the steps that I am doing: 1: Clone the latest nixpkgs 2: Run nix build .#zed-editor (I also tried nix build .#zed-editor.fhs) 3: Run zed via ./result/bin/zed 4: Open my rust based project, using zed Ctrl+o option

I get a similar error that I described before

niklaskorz commented 2 months ago

@niklaskorz

Here are the steps that I am doing: 1: Clone the latest nixpkgs 2: Run nix build .#zed-editor (I also tried nix build .#zed-editor.fhs) 3: Run zed via ./result/bin/zed 4: Open my rust based project, using zed Ctrl+o option

I get a similar error that I described before

Can you try removing your custom LSP path config and then run zed-editor.fhs again?

k3yss commented 2 months ago

I tried removing it and am received with the error Failed to load workspaces. When you say I need to run zed-editor.fhs you mean running the ./result/bin/zed right?

niklaskorz commented 2 months ago

I tried removing it and am received with the error Failed to load workspaces. When you say I need to run zed-editor.fhs you mean running the ./result/bin/zed right?

Either nix-build -A zed-editor.fhs && result/bin/zed or nix run .#zed-editor.fhs, yes (they're essentially doing the same)

k3yss commented 2 months ago

hmmm... that's weird, do you want me to share any other logs so we know what the exact error is?

k3yss commented 2 months ago

This worked for me to setup rust-analyzer https://github.com/NixOS/nixpkgs/pull/339852#issuecomment-2334824685

I am not sure how it would work natively though : (

yatesco commented 2 months ago

(not read the whole thread so maybe missing something) Since https://github.com/zed-industries/zed/pull/17075, running zed . in my nix develop environment works fine with Rust. I only have rust/cargo setup in my nix develop environment.

Before https://github.com/zed-industries/zed/pull/17075 it would fail miserably to find rust-analyser. Starting Zed from outside my nix develop environment still fails miserably.

My config:

// Zed settings
//
// For information on how to configure Zed, see the Zed
// documentation: https://zed.dev/docs/configuring-zed
//
// To see all of Zed's default settings without changing your
// custom settings, run `zed: open default settings` from the
// command palette
{
  "ui_font_size": 14,
  "buffer_font_size": 14,
  "theme": {
    "mode": "system",
    "light": "One Light",
    "dark": "One Dark"
  }
}
jansol commented 2 months ago

@yatesco just read the thread. I've posted a config that worked both before and after that PR if you have a flake.nix for your project that provides rust-analyzer. (essentially by running nix develop as part of the r-a command)

jansol commented 1 week ago

Now that zed will check PATH for a node.js installation before downloading its own (as of 0.155.2), maybe we should wrap zed to bring in node from nixpkgs? That should solve the problem for node-based language servers (and copilot & prettier).

jansol commented 1 week ago

Initial attempt at wrapping Zed to make node-based LSPs Just Work in #354063