zed icon indicating copy to clipboard operation
zed copied to clipboard

Direnv doesn't load with `load_direnv="direct"` with `Direnv returned invalid json`

Open AKarbas opened this issue 9 months ago • 2 comments

Summary

Direnv doesn't load with load_direnv="direct" with Direnv returned invalid json if direnv returns empty string.

Steps to reproduce:

  1. ensure direnv is installed
  2. set up a sandbox dir as follows:
    cd $(mktemp -d)
    echo 'export foo=bar' > .envrc
    direnv allow
    mkdir -p .zed
    echo '{ "load_direnv": "direct" }' > .zed/settings.json
    
  3. run zed there (zed .) or open the dir in a new zed window from the GUI
  4. look at logs to find something like 2025-05-08T12:28:00-07:00 ERROR [project::environment] Direnv returned invalid json. See logs for more info

Expected Behavior: no error seen Actual Behavior: error seen :shrug:

I expect this is because of https://github.com/direnv/direnv/issues/809 — running direnv exec / direnv export json instead of direnv export json should fix this — or accepting an empty string as valid if direnv successfully exited, in case the env vars are already loaded (didn't know how to check that in zed).

Zed Version and System Specs

Zed: v0.185.13 (Zed) OS: macOS 15.4.1 Memory: 36 GiB Architecture: aarch64

AKarbas avatar May 08 '25 19:05 AKarbas

Hey, I tried running your reproduction steps, and couldn't seem to reproduce the error as you described. Would you mind verifying that running the steps exactly as described reproduces the issue?

probably-neb avatar May 16 '25 09:05 probably-neb

Hey, I'm having a hard time reproducing too, maybe got fixed. But also I cannot verify that direnv is even loading/working — e.g., if I set up like this instead:

cd $(mktemp -d)
echo 'PATH_add /foo' > .envrc
direnv allow
echo $PATH # /foo:...
mkdir -p .zed
echo '{ "load_direnv": "direct" }' > .zed/settings.json

and then open Zed (zed .), the logs don't show /foo at the beginning of the list in this log line:

2025-05-27T14:30:33-07:00 INFO  [util] set environment variables from shell:/bin/zsh, path:/Users/amin/.nix-profile/bin:...

AKarbas avatar May 27 '25 21:05 AKarbas

This is probably fixed by #32559, since you are getting a json parse error.

I believe that this isn't related to https://github.com/direnv/direnv/issues/809 as the empty output case is handled properly. direnv export json provides changes to the current environment, so it usually outputs nothing when run from a shell with direnv hooks configured.

mgsloan avatar Jun 11 '25 17:06 mgsloan