zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
50.85k stars 3.15k forks source link

Use `direnv` environment #4977

Open diktomat opened 1 year ago

diktomat commented 1 year ago

Check for existing issues

Describe the feature

Zed should support direnv, to enable using a different environment (espeacially $PATH) for different projects. In combination with zed-industries/zed#4978 this would enable e.g. using different versions of language servers and tooling, or tooling just installed for the project instead of system-wide using Nix.

If applicable, add mockups / screenshots to help present your vision of the feature

Inspiration from other editors:

szlend commented 1 year ago

A few issues that almost every editor suffers from when implementing direnv support:

Just something worth considering when designing the extension API and implementing this feature.

misuzu commented 10 months ago
  • Direnv initializes too late, causing problems with certain extensions that rely on direnv to set the PATH (e.g. for utilities like language servers and linters). Ideally there was a way for the direnv extension to hook into some really early phase of initialization where it can set environmental variables before other extensions initialize.

Yep, the extension API must have support for this usecase to properly implement direnv integration. This is the reason why https://github.com/misuzu/direnv-subl is flawed - ST just doesn't have the necessary API. The direnv integration that just works would be The Feature that can ease the pain of setting up any project for many people.

szlend commented 9 months ago

Another useful feature that I think a direnv extension should have is to be able to re-load the environment so you don't have to restart your editor. The way this works in vscode for example is that it changes the process environment and then restarts the extension host (and all language servers as a result).

Also initializing direnv can take some time. For example if you use direnv to load a nix shell, it might actually start downloading packages or even compiling. So it might make sense to show some sort of indicator that direnv is still evaluating something. And a way to cancel it.

jeremylightsmith commented 6 months ago

This is definitely keeping us from using zed on our project. We use direnv + nix, and we can't use tasks w/o better support.

With VSCode, it's quite easy to use direnv, you just have to launch the editor from a directory that direnv has already run with code .. Would it be possible to have the same behavior with zed?

jeremylightsmith commented 6 months ago

it seems the termina doesn't maintain its env between runs, because if it did, we could work around this by sourcing a script like this:

#!/bin/bash

if [ -z "${DIRENV_INITIALIZED}" ]; then
  echo "Loading direnv..."
  eval "$(direnv export bash)"
  export DIRENV_INITIALIZED=1
else
  echo "direnv is already loaded."
fi
mersinvald commented 5 months ago

I found a workaround that is semi-usable: running zed with zed --foreground ./ after loading the environment in shell works, the editor plugins can access my nix shell environment, but it's far from ideal: this way you can only have one project/environment open at any time.

If I try to open a second project with a different env, with --foreground I get zed is already running error, and without the flag the environment is shared between all windows.

mrnugget commented 5 months ago

When you open a project, we spawn a login shell in that project's root dir, get the env from that process, and store it to be used with some language servers.

It sounds like you have direnv/nix setup so that would work, is the problem now that it doesn't work with tasks?

jeremylightsmith commented 5 months ago

Thanks @mersinvald , but your workaround doesn't work for me, specifically with tasks, which is my problem. I need to be able to run the test under my cursor from my IDE. And when I open up a task to do that, it doesn't have any of the env that direnv setup :(

szlend commented 5 months ago

With VSCode, it's quite easy to use direnv, you just have to launch the editor from a directory that direnv has already run with code .. Would it be possible to have the same behavior with zed?

This definitely does not work well in vscode, except for the initial window. The direnv vscode extension fixes this and makes it somewhat usable, but it still causes cross-project env pollution from the initial vscode window.

I think how Zed does it is a step in the right direction, though it's difficult to diagnose issues when something goes wrong. It might be a good idea to document the env loading behavior and some steps to help diagnose issues (e.g. how do I inspect the window's env, what login shell Zed used to initialize the env, etc.)

mrnugget commented 5 months ago

I think how Zed does it is a step in the right direction, though it's difficult to diagnose issues when something goes wrong. It might be a good idea to document the env loading behavior and some steps to help diagnose issues (e.g. how do I inspect the window's env, what login shell Zed used to initialize the env, etc.)

Right now that's all printed in the logs (zed: open logs). So when you open Zed, you should see that we do the same in the home directory.

But yes, documentation is a good idea.

Not sure how Zed figures out which shell to use to say if this is a problem or not though.

It uses SHELL and falls back to passwd lookup if SHELL isn't set.

It doesn't spawn nix shell. It uses your $SHELL.

This is what we do on start, in $HOME: https://github.com/zed-industries/zed/blob/05b658114750e9b63717ec3d05b316a927d8378e/crates/zed/src/main.rs#L309-L312

And this is what we do for the language servers, per project:

https://github.com/zed-industries/zed/blob/05b658114750e9b63717ec3d05b316a927d8378e/crates/project/src/project.rs#L11636-L11665

musjj commented 5 months ago

It looks like that rust-analyzer can now pick up the binary from the environment: https://github.com/zed-industries/zed/pull/12418. So I added this to my config:

{
  "lsp": { "rust-analyzer": { "binary": { "path_lookup": true } } }
}

But it's still tries to download and run its own binary anyways (and failing because I'm on NixOS).

mrnugget commented 5 months ago

But it's still tries to download and run its own binary anyways (and failing because I'm on NixOS).

Yes, because that commit you linked to hasn't been in any release yet.

mersinvald commented 5 months ago

@mrnugget it doesn't work for me without the foreground hack. If I just open the project, the env from .envrc wouldn't be loaded for anything except the integrated shell. With foreground, both plugins and tasks work fine, as zed is loaded within the direnv-initialized PTY. I have cargo and rustup setup only in the flake shell, so it's easy enough to test.

When you open a project, we spawn a login shell in that project's root dir,

Is there a way to modify what runs in this shell before editor initialization?

mrnugget commented 5 months ago

@mrnugget it doesn't work for me without the foreground hack

what doesn't work? Only certain language servers make use of this environment right now.

mersinvald commented 5 months ago

@mrnugget it doesn't work for me without the foreground hack

what doesn't work? Only certain language servers make use of this environment right now.

I would like the editor to respect the directory env regardless of what specific plugin needs it. In my case, the issue is with rust-analyzer.

Log without the foreground hack (neither rustup or cargo are installed in the system, only in a project flake)

Support/Zed/languages/rust-analyzer/rust-analyzer-2024-06-11", working directory: "/Users/XXXXXX", args: []
2024-06-11T12:58:46+04:00 [ERROR] Language server rust-analyzer-2024-06-11 (id 1) status update: Failed to load workspaces.
2024-06-11T12:58:46+04:00 [ERROR] Language server rust-analyzer-2024-06-11 (id 1) status update: Failed to load workspaces.
2024-06-11T12:58:47+04:00 [INFO] Language server with id 0 sent unhandled notification LogMessage:
{
  "level": 0,
  "message": "[DEBUG] [agent] [2024-06-11T08:58:47.175Z] Telemetry initialized",
  "metadataStr": "[DEBUG] [agent] [2024-06-11T08:58:47.175Z]",
  "extra": [
    "Telemetry initialized"
  ]
}
2024-06-11T12:58:47+04:00 [INFO] reload git repository ".git"
2024-06-11T12:58:47+04:00 [INFO] reload git repository ".git"
2024-06-11T12:58:47+04:00 [INFO] add connection to peer
2024-06-11T12:58:47+04:00 [INFO] add_connection;
2024-06-11T12:58:47+04:00 [INFO] waiting for server hello
2024-06-11T12:58:47+04:00 [INFO] got server hello
2024-06-11T12:58:47+04:00 [INFO] set status to connected (connection id: ConnectionId { owner_id: 0, id: 0 }, peer id: PeerId { owner_id: 443, id: 2668270 })
2024-06-11T12:58:47+04:00 [INFO] set status on client 153260: Connected { peer_id: PeerId { owner_id: 443, id: 2668270 }, connection_id: ConnectionId { owner_id: 0, id: 0 } }
2024-06-11T12:58:50+04:00 [ERROR] crates/languages/src/rust.rs:443: Os { code: 2, kind: NotFound, message: "No such file or directory" }
2024-06-11T12:58:51+04:00 [WARN] request completed with error: request or operation took longer than the configured timeout time
2024-06-11T12:58:51+04:00 [ERROR] crates/client/src/telemetry.rs:492: request or operation took longer than the configured timeout time

Caused by:
    [28] Timeout was reached
2024-06-11T12:58:51+04:00 [WARN] request completed with error: request or operation took longer than the configured timeout time
2024-06-11T12:58:54+04:00 [ERROR] crates/languages/src/rust.rs:443: Os { code: 2, kind: NotFound, message: "No such file or directory" }
2024-06-11T12:58:55+04:00 [ERROR] crates/languages/src/rust.rs:443: Os { code: 2, kind: NotFound, message: "No such file or directory" }
2024-06-11T12:58:57+04:00 [ERROR] crates/languages/src/rust.rs:443: Os { code: 2, kind: NotFound, message: "No such file or directory" }
mrnugget commented 5 months ago

In my case, the issue is with rust-analyzer.

That should then be possible with the upcoming Preview release and when setting {"rust-analyzer": {"binary": {"path_lookup": true}}}.

jeremylightsmith commented 5 months ago

Alright, I want to show you where it's not working for me. You can see that when I go into my project directory, direnv runs and we are finding elixir from nix. But after launching zed, even with foreground, within a task, it finds elixir from asdf :(. Which means all my tests fail and I can't do things like mix test

2024-06-11 09 52 46

For VSCode, it works because the first time I run a task, it opens and fails, then I can manually type direnv allow, and then future runs of the task use that same environment that has been direnv'd.

mrnugget commented 5 months ago

within a task

Yeah, that's not supported yet.

zimbatm commented 4 months ago

This is a bit related to #5347 in the sense that you want to:

  1. "remote" into a per-project shell session.
  2. manage the lifecycle of that session (e.g., stop/start/reload).
  3. make sure every program invocation in the code has a layer of indirection to execute in that session.
  4. lean on the PATH of said session to find tools instead of complicated/static lookup/installation logic.

So far, all the IDEs I have seen do this wrong as they try to resolve and execute binaries from the IDE's context. Sometimes, they allow updating the IDE's ENV, and then it breaks down when working on two different projects at the same time.

Creating this architecture is also helpful for other things, like moving to a future where project code is sandboxed correctly and doesn't have arbitrary access to all of $HOME.

musjj commented 3 months ago

It looks like that direnv is now officially supported: https://github.com/zed-industries/zed/blob/main/docs/src/configuring-zed.md#direnv-integration I was trying out the latest release and was pleasantly surprised that direnv just works (without having to manually trigger it in the shell).

misuzu commented 3 months ago

It looks like that direnv is now officially supported: https://github.com/zed-industries/zed/blob/main/docs/src/configuring-zed.md#direnv-integration I was trying out the latest release and was pleasantly surprised that direnv just works (without having to manually trigger it in the shell).

Does it work with multiple direnv environments loaded at the same time?

jeremylightsmith commented 3 months ago

Direnv does NOT just work. It mostly doesn't seem to work at all, and if you read the fine print on the docs, it says:

direnv integration currently only means that the environment variables set by a direnv configuration can be used to detect some language servers in $PATH instead of installing them.

I need it to find other things, like elixir in a path modified by direnv. It doesn't have the path as direnv sets it :(

bjeanes commented 3 months ago

I need it to find other things, like elixir in a path modified by direnv. It doesn't have the path as direnv sets it :(

This is still true after https://github.com/zed-industries/zed/commit/0360cda5434f019ea3bda2f77c573b959249f9b9 ?

From the changelog on v0.147.0-pre:

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 (https://github.com/zed-industries/zed/issues/12125).

Check out the video!

jeremylightsmith commented 3 months ago

I'm still on Zed 0.146.5, so looks like I need to wait till 147.2 hits? That's super exciting!

jeremylightsmith commented 3 months ago

Hmm, I installed zed preview, which is 147.2, and when I run this task:

  {
    "label": "show path",
    "command": "echo $PATH",
    "reveal": "always"
  }

it still shows the path without direnv's changes to it. I'm using nix, if it matters.

:(

mrnugget commented 3 months ago

How do you use direnv?

Here's what I use with Zed Preview 0.147.2:

  1. Create the project:
    mkdir ~/tmp/testing-direnv
    cd ~/tmp/testing-direnv
    echo export FOO=foo > .envrc
    direnv allow .
  2. Open Zed Preview
  3. task: Spawn
  4. Type in echo $FOO, hit opt-return
  5. Task is spawned, prints foo

See: screenshot-2024-08-07-13 58 34@2x

So, how exactly is your direnv setup working, how do you start Zed and how to you open that project?

jeremylightsmith commented 3 months ago

Thanks for taking the time to try to help me out. I think I have things working now, but I did find a couple bugs along the way.

Here's a play project that someone can hopefully repro them with:

testing-zed.zip

It has this .envrc file:

export FOO=foo
export PATH=/first:$PATH:/last

And this .zed/tasks.json file:

[
  {
    "label": "show path",
    "command": "echo \"FOO=$FOO and PATH=$PATH\"",
    "reveal": "always"
  }
]

When I open zed and run the show path task, the behavior I see is:

  1. I was using zed . to open zed. When I do this, I do see $FOO as being defined, but I don't see /first or /last on the path. Weird. When I open the application from spotlight, then I do see /first and /last on the path

  2. /last is last on the path, but /first is not first. not sure why, but there's a bunch of stuff that comes first, including asdf in my actual project, which breaks which elixir I'm trying to use. (I worked around it by just removing asdf for now). On the command line, /first is first.

I'm on an M2 Max mac running Sonoma 14.6.

mrnugget commented 3 months ago

Okay, let me first answer (2), since (1) is tricky and I need to dive into it.

2. /last is last on the path, but /first is not first. not sure why, but there's a bunch of stuff that comes first, including asdf in my actual project, which breaks which elixir I'm trying to use. (I worked around it by just removing asdf for now). On the command line, /first is first.

That's because when spawning tasks, we spawn an interactive shell, which then sources your ~/.zshrc or ~/.bashrc or what you have.

Here, all the things that come before /first are things that I add in my .zshrc:

screenshot-2024-08-08-10 08 41@2x

That's reproducible when running the same command that Zed runs when executing the task:

screenshot-2024-08-08-10 12 13@2x

I'm not sure how to fix it yet. Workaround could be to change your shell config to detect when any of the ZED_* env vars are set and then not add to the PATH:

screenshot-2024-08-08-10 16 49@2x

mrnugget commented 3 months ago
  1. I was using zed . to open zed. When I do this, I do see $FOO as being defined, but I don't see /first or /last on the path. Weird. When I open the application from spotlight, then I do see /first and /last on the path

Okay, this is a bit of a nasty one. I think I know what's going on now:

So, what do we do about it?

I think there are a bunch of options and I need to think through the trade-offs of them, but here's what we/you could do, off the top of my head:

  1. We: when Zed.app is spawned from the CLI, we should detect that and not re-populate the environment from the home-dir.
    • Subtask: we should explicitly forward the environment from the CLI to the process, so that we can store that on environment on the project and reuse it, even if multiple Zed windows are open. That's the best solution.
  2. We could do a hack: remove DIRENV_* variables before spawning the login-shell in the project dir. Hacky, nasty. Temptingly easy though. Still: super nasty.
  3. You: instead of doing zed . you could do cd .. && zed my-dir, so that direnv is unloaded, the env vars removed and direnv is properly loaded.
  4. You: you can run zed --foreground . too, which should fix the issue. But I don't consider that a fix, more like a workaround.

We really should do (1) and the subtask, but we're very busy now with an upcoming launch, so I'm probably not going to get to that in the next couple of weeks.

domenkozar commented 3 months ago

Using 0.147.2, with direnv activated and rust-analyzer available in $PATH, Zed still downloads the binary on it's own and fails to run it. No error is reported in the editor.

I wish there way a way to disable auto-downloading all together and using only $PATH.

mrnugget commented 3 months ago

@domenkozar did you turn the PATH-lookup on for rust-analyzer? It's not the default for rust-analyzer since r-a behaves different than other language servers somehow.

{
  "lsp": {
      "rust-analyzer": {
          "binary": {"path_lookup": true}
      }
  }
}

(Edit: documented as part of our docs party today: 168318664ae3ba07abf390f90aad93979abb3af7)

domenkozar commented 3 months ago

Same result:

2024-08-08T13:17:54.685708953+01:00 [INFO] starting language server "rust-analyzer", path: "/home/domen/dev/lanza.events", id: 5
2024-08-08T13:17:54.727934758+01:00 [INFO] starting language server. binary path: "/home/domen/.local/share/zed/languages/rust-analyzer/rust-analyzer-2024-08-05", working directory: "/home/domen/dev/lanza.events", args: []
2024-08-08T13:17:54.728599637+01:00 [ERROR] failed to start language server "rust-analyzer": failed to spawn command. path: "/home/domen/.local/share/zed/languages/rust-analyzer/rust-analyzer-2024-08-05", working directory: "/home/domen/dev/lanza.events", args: []
2024-08-08T13:17:54.728706493+01:00 [ERROR] server stderr: Some("")
2024-08-08T13:17:54.7288086+01:00 [ERROR] Hit 4 reinstallation attempts for "rust-analyzer"

Note that I'd expect path_lookup to be a global setting that LSPs respect.

mrnugget commented 3 months ago

It works for me:

$ cd tucan
direnv: loading ~/code/projects/tucan/.envrc
direnv: using flake
[...]
direnv: nix-direnv: renewed cache
direnv: export +AR +AR_FOR_TARGET +AS +AS_FOR_TARGET +CC +CC_FOR_TARGET +CONFIG_SHELL +CXX +CXX_FOR_TARGET +HOST_PATH +IN_NIX_SHELL +LD +LD_DYLD_PATH +LD_FOR_TARGET +MACOSX_DEPLOYMENT_TARGET +NIX_BINTOOLS +NIX_BINTOOLS_FOR_TARGET +NIX_BINTOOLS_WRAPPER_TARGET_HOST_aarch64_apple_darwin +NIX_BINTOOLS_WRAPPER_TARGET_TARGET_aarch64_apple_darwin +NIX_BUILD_CORES +NIX_CC +NIX_CC_FOR_TARGET +NIX_CC_WRAPPER_TARGET_HOST_aarch64_apple_darwin +NIX_CC_WRAPPER_TARGET_TARGET_aarch64_apple_darwin +NIX_CFLAGS_COMPILE +NIX_CFLAGS_COMPILE_FOR_TARGET +NIX_DONT_SET_RPATH +NIX_DONT_SET_RPATH_FOR_BUILD +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_IGNORE_LD_THROUGH_GCC +NIX_LDFLAGS +NIX_LDFLAGS_FOR_TARGET +NIX_NO_SELF_RPATH +NIX_STORE +NM +NM_FOR_TARGET +PATH_LOCALE +RANLIB +RANLIB_FOR_TARGET +RUST_SRC_PATH +SIZE +SIZE_FOR_TARGET +SOURCE_DATE_EPOCH +STRINGS +STRINGS_FOR_TARGET +STRIP +STRIP_FOR_TARGET +XDG_DATA_DIRS +__darwinAllowLocalNetworking +__impureHostDeps +__propagatedImpureHostDeps +__propagatedSandboxProfile +__sandboxProfile +__structuredAttrs +buildInputs +buildPhase +builder +cmakeFlags +configureFlags +depsBuildBuild +depsBuildBuildPropagated +depsBuildTarget +depsBuildTargetPropagated +depsHostHost +depsHostHostPropagated +depsTargetTarget +depsTargetTargetPropagated +doCheck +doInstallCheck +dontAddDisableDepTrack +mesonFlags +name +nativeBuildInputs +out +outputs +patches +phases +preferLocalBuild +propagatedBuildInputs +propagatedNativeBuildInputs +shell +shellHook +stdenv +strictDeps +system ~PATH

$ which rust-analyzer
/nix/store/x4irwblcsfq8df4z5lvq8vzwxfxfprhm-rust-default-1.77.2/bin/rust-analyzer

Then I open Zed.app, open the project, and in the logs it says:

2024-08-08T13:14:53.836573Z [INFO] found user-installed language server for Rust. path: "/nix/store/x4irwblcsfq8df4z5lvq8vzwxfxfprhm-rust-default-1.77.2/bin/rust-analyzer", arguments: []
2024-08-08T13:14:53.836649Z [INFO] starting language server. binary path: "/nix/store/x4irwblcsfq8df4z5lvq8vzwxfxfprhm-rust-default-1.77.2/bin/rust-analyzer", working directory: "/Users/thorstenball/code/projects/tucan", args: []

Can you show your settings.json? Or do you have a project with which to reproduce this?

domenkozar commented 3 months ago
{
  "assistant": {
    "version": "2",
    "default_open_ai_model": null,
    "default_model": {
      "provider": "anthropic",
      "model": "claude-3-5-sonnet-20240620"
    }
  },
  "lsp": {
      "rust-analyzer": {
          "binary": {"path_lookup": true}
      }
  },
  "vim_mode": true,
  "base_keymap": "VSCode",
  "theme": "One Dark",
  "ui_font_size": 16,
  "buffer_font_size": 16
}
mrnugget commented 3 months ago

Nothing wrong there. With your settings it also works for me:

2024-08-08T16:07:52.718096Z [INFO] found user-installed language server for Rust. path: "/nix/store/x4irwblcsfq8df4z5lvq8vzwxfxfprhm-rust-default-1.77.2/bin/rust-analyzer", arguments: []
2024-08-08T16:07:52.718193Z [INFO] starting language server. binary path: "/nix/store/x4irwblcsfq8df4z5lvq8vzwxfxfprhm-rust-default-1.77.2/bin/rust-analyzer", working directory: "/Users/thorstenball/code/projects/tucan", args: []
domenkozar commented 3 months ago

If I launch Zed with rust-analyzer already available it works, but when it's only provisioned inside the terminal using direnv it tries to download it.

srid commented 3 months ago

Zed + direnv + Nix + Rust (rust-analyzer) has been consistently working well for me. Environment: macOS, with nothing (but Nix) globally installed.

The project in question: https://github.com/juspay/omnix

My Zed config:

{
  "base_keymap": "VSCode",
  "load_direnv": "shell_hook",
  "lsp": { "rust-analyzer": { "binary": { "path_lookup": true } } },
  "vim_mode": true,
}
image
srid commented 3 months ago

Incidentally, to get nixd (Nix language server) working, I had to add the following to Zed config:

{
  "lsp": {
    "rust-analyzer": { "binary": { "path_lookup": true } },
    "nix": { "binary": { "path_lookup": true } }
  },
}
image

Perhaps path_lookup should be the default for all language servers?

srid commented 3 months ago

One thing I noticed is this configuration works only if you put it in your user config (~/.config/zed/settings.json) but not the project config (./.zed/settings.json) -- as VSCode users do. That seems like a bug?

rawkode commented 3 months ago

Perhaps path_lookup should be the default for all language servers?

Yep! If the binary exists in the path, please just use it.

mrnugget commented 3 months ago

That seems like a bug?

Not strictly a bug, but: we only allow overwriting certain settings in project-specific settings. But yeah, agree, this should probably be one.

mrnugget commented 3 months ago

Yep! If the binary exists in the path, please just use it.

I agree, but last time I did that, lots of rust-analyzer stuff broke, I need to look into this again.

rome-user commented 3 months ago

@srid Which version of Zed are you using? I am using Zed 0.149.3 on Mac OS 14.6.1. My projects use Nix with direnv. Zed is able to find LSP servers like gopls, nixd, etc. in projects where they are specified in Nix flakes.

It was sufficient to set load_direnv to shell_hook in the settings.json.

srid commented 3 months ago

@rome-user 0.149.3 on macOS ARM 15.0 beta.

image

I definitely have to enable path_lookup for each LSP to get it to work (can confirm for rust and nixd).

domenkozar commented 3 months ago

A good step forward would be to have a global path_lookup flag that each LSP implementation needs to respect.

DrewBurkhart commented 3 months ago

In case it helps anyone else, this was my solution/workaround.


tl;dr

What was tripping me up is that it was saying Hit 4 reinstallation attempts for "rust-analyzer" (which I now realize I focused too heavily on as the final log message) but I think it was really 4 failed attempts to start rust-analyzer. Once I installed it via rustup, it worked just fine. That step wasn't required in VS Code so I just hadn't expected to need it. Not sure if that's expected or not.


My Problem when using nix + direnv + rust-analyzer (essentially just a restatement of earlier messages here) In various projects that work as is in VS code with no RA-related settings, I'm having trouble getting RA to run. I've added ``` { "load_direnv": "shell_hook", "lsp": { "rust-analyzer": { "binary": { "path_lookup": true } } } } ``` to `~/.config/zed/settings.json` but it appears as though one of two things happens depending on how I start Zed (which matches what was explained above): 1. If I open via CLI (`zed .`) it doesn't load the env in the terminal and rust-analyzer seems to start ok but then errors with "failed to load workspace" in the logs (with no other apparent context). 2. If I open via spotlight it loads the env correctly but the logs indicate that it's failing to install rust-analyzer repeatedly (same as @domenkozar noted above) ``` [INFO] found user-installed language server for Rust. path: "/nix/store/dlipzjnxcypn8sz8kgaqn7hw26j4shki-rustup-1.26.0/bin/rust-analyzer", arguments: [] [INFO] starting language server. binary path: "/nix/store/dlipzjnxcypn8sz8kgaqn7hw26j4shki-rustup-1.26.0/bin/rust-analyzer", working directory: "/Users/me/Development/proj", args: [] [ERROR] cannot read LSP message headers [ERROR] failed to start language server "rust-analyzer": oneshot canceled [ERROR] server stderr: Some("error: 'rust-analyzer' is not installed for the toolchain '1.80.0-aarch64-apple-darwin'\n") [ERROR] Hit 4 reinstallation attempts for "rust-analyzer" ``` This is the same behavior as when I follow the steps @mrnugget mentioned above > You: instead of doing zed . you could do cd .. && zed my-dir, so that direnv is unloaded, the env vars removed and direnv is properly loaded. You: you can run zed --foreground . too, which should fix the issue. But I don't consider that a fix, more like a workaround.

My Solution/Workaround Basically I eventually just followed the error I'd overlooked in the logs which states ``` [ERROR] server stderr: Some("error: 'rust-analyzer' is not installed for the toolchain '1.80.0-aarch64-apple-darwin'\n") ``` So I ran ``` rustup component add rust-analyzer --toolchain 1.80.0-aarch64-apple-darwin ``` and then ran ``` cd .. && zed proj ``` Works great now!
deng232 commented 3 months ago

in nixos. when I launch zed inside an manually launched development shell. direnv work properly. but with terminal direnv shell hook setup, zed termianl shows getting inside development shell but it is not develop environment. both "shell_hook" and "direct" setting behave the same. however going to directory using workspace:open has no problem.

mrnugget commented 3 months ago

@DrewBurkhart thanks so much for that debugging! That's going to help.

domenkozar commented 2 months ago

Currently, some LSPs call workspace.which(binary) to get the binary if it exists, while others use path_lookup settings (which is off by default).

Ideally, the extension API would provide find_executable(name) function and then have a global setting for path_lookup configurability for uniform experience.

Once that lands I'll write documentation for devenv.

mrnugget commented 2 months ago

Currently, some LSPs call workspace.which(binary) to get the binary if it exists, while others use path_lookup settings (which is off by default).

I'm in the process of turning path_lookup for rust-analyzer on by default. https://github.com/zed-industries/zed/pull/17926