direnv / direnv-vscode

unclutter your .profile
https://marketplace.visualstudio.com/items?itemName=mkhl.direnv
BSD Zero Clause License
163 stars 11 forks source link

Extension Prevents `.envrc` being loaded in terminal. #561

Open s0kil opened 1 year ago

s0kil commented 1 year ago

In my .bashrc I hook in direnv as:

# direnv
eval "$(direnv hook bash)"

When I open a new integrated terminal window in VS Code, the env is loaded:

direnv: loading ~/Lab/Zetta-Pow/command/.envrc
direnv: loading https://raw.githubusercontent.com/nix-community/nix-direnv/2.3.0/direnvrc (sha256-Dmd+j63L84wuzgyjITIfSxSD57Tx7v51DMxVZOsiUD8=)
direnv: using flake . --impure --accept-flake-config
direnv: nix-direnv: using cached dev shell
direnv: export +ADMIN_PASS +ADMIN_USER +C_INCLUDE_PATH +DEVENV_DOTFILE +DEVENV_PROFILE +DEVENV_ROOT +DEVENV_STATE +FILEBASE_KEY +FILEBASE_SECRET +IHP +IHP_LIB +IN_NIX_SHELL +LD_LIBRARY_PATH +LIBRARY_PATH +LOCALE_ARCHIVE +PKG_CONFIG_PATH +name ~PATH ~XDG_CONFIG_DIRS ~XDG_DATA_DIRS
danielsokil@system76:~/Lab/Zetta-Pow/command$

However, when I enable the direnv-vscode extension, the env is no longer loaded when opening a new terminal window.

mkhl commented 1 year ago

How are you determining that the .envrc isn't loaded?

s0kil commented 1 year ago

The binary paths that are available when .envrc is loaded, are not available when it's not loaded.

mkhl commented 1 year ago

What's the output of direnv status in that case?

s0kil commented 1 year ago

When opening terminal:

$ direnv status
direnv exec path /nix/store/6b56l3300y961gpfhznw833hqglwn27k-direnv-2.32.3/bin/direnv
DIRENV_CONFIG /home/danielsokil/.config/direnv
bash_path /nix/store/xfb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15/bin/bash
disable_stdin false
warn_timeout 5s
whitelist.prefix []
whitelist.exact map[]
Loaded RC path /home/danielsokil/Lab/Zetta-Pow/command/.envrc
Loaded watch: ".envrc" - 2023-07-19T23:36:02-04:00
Loaded watch: "../../../.local/share/direnv/allow/6b615a5bc9017297c6c186f046eff003b768f3f47d5cd5f2cdfa4f541bfe3446" - 2023-07-24T15:17:55-04:00
Loaded watch: "../../../.direnvrc" - 1969-12-31T19:00:00-05:00
Loaded watch: "../../../.config/direnv/direnvrc" - 1969-12-31T19:00:00-05:00
Loaded watch: "flake.nix" - 2023-07-23T01:07:10-04:00
Loaded watch: "flake.lock" - 2023-07-19T23:35:38-04:00
Loaded watch: "devshell.toml" - 1969-12-31T19:00:00-05:00
Loaded RC allowed false
Loaded RC allowPath 
Found RC path /home/danielsokil/Lab/Zetta-Pow/command/.envrc
Found watch: ".envrc" - 2023-07-19T23:36:02-04:00
Found watch: "../../../.local/share/direnv/allow/6b615a5bc9017297c6c186f046eff003b768f3f47d5cd5f2cdfa4f541bfe3446" - 2023-07-24T15:17:55-04:00
Found RC allowed true
Found RC allowPath /home/danielsokil/.local/share/direnv/allow/6b615a5bc9017297c6c186f046eff003b768f3f47d5cd5f2cdfa4f541bfe3446

Then I run direnv allow:

$ direnv allow
direnv: loading ~/Lab/Zetta-Pow/command/.envrc
direnv: loading https://raw.githubusercontent.com/nix-community/nix-direnv/2.3.0/direnvrc (sha256-Dmd+j63L84wuzgyjITIfSxSD57Tx7v51DMxVZOsiUD8=)
direnv: using flake . --impure --accept-flake-config
direnv: nix-direnv: using cached dev shell
direnv: export +ADMIN_PASS +ADMIN_USER +C_INCLUDE_PATH +DATABASE_URL +DEVENV_DOTFILE +DEVENV_PROFILE +DEVENV_ROOT +DEVENV_STATE +FILEBASE_KEY +FILEBASE_SECRET +IHP +IHP_LIB +IN_NIX_SHELL +LD_LIBRARY_PATH +LIBRARY_PATH +LOCALE_ARCHIVE +PKG_CONFIG_PATH +name ~PATH ~XDG_CONFIG_DIRS ~XDG_DATA_DIRS

Status After:

$ direnv status
direnv exec path /nix/store/6b56l3300y961gpfhznw833hqglwn27k-direnv-2.32.3/bin/direnv
DIRENV_CONFIG /home/danielsokil/.config/direnv
bash_path /nix/store/xfb3ykw9r5hpayd05sr0cizwadzq1d8q-bash-5.2-p15/bin/bash
disable_stdin false
warn_timeout 5s
whitelist.prefix []
whitelist.exact map[]
Loaded RC path /home/danielsokil/Lab/Zetta-Pow/command/.envrc
Loaded watch: ".envrc" - 2023-07-19T23:36:02-04:00
Loaded watch: "../../../.local/share/direnv/allow/6b615a5bc9017297c6c186f046eff003b768f3f47d5cd5f2cdfa4f541bfe3446" - 2023-07-26T12:43:00-04:00
Loaded watch: "../../../.direnvrc" - 1969-12-31T19:00:00-05:00
Loaded watch: "../../../.config/direnv/direnvrc" - 1969-12-31T19:00:00-05:00
Loaded watch: "flake.nix" - 2023-07-23T01:07:10-04:00
Loaded watch: "flake.lock" - 2023-07-19T23:35:38-04:00
Loaded watch: "devshell.toml" - 1969-12-31T19:00:00-05:00
Loaded RC allowed false
Loaded RC allowPath 
Found RC path /home/danielsokil/Lab/Zetta-Pow/command/.envrc
Found watch: ".envrc" - 2023-07-19T23:36:02-04:00
Found watch: "../../../.local/share/direnv/allow/6b615a5bc9017297c6c186f046eff003b768f3f47d5cd5f2cdfa4f541bfe3446" - 2023-07-26T12:43:00-04:00
Found RC allowed true
Found RC allowPath /home/danielsokil/.local/share/direnv/allow/6b615a5bc9017297c6c186f046eff003b768f3f47d5cd5f2cdfa4f541bfe3446
lakim commented 1 year ago

I have the exact same issue.

# .envrc
if ! has nix_direnv_version || ! nix_direnv_version 2.3.0; then
  source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.3.0/direnvrc" "sha256-Dmd+j63L84wuzgyjITIfSxSD57Tx7v51DMxVZOsiUD8="
fi
use nix

When the direnv-vscode extension is disabled and I open an integrated terminal, I get the following output and my PATH is setup as expected:

direnv: loading ~/path/to/project/.envrc                                                                                    
direnv: loading https://raw.githubusercontent.com/nix-community/nix-direnv/2.3.0/direnvrc (sha256-Dmd+j63L84wuzgyjITIfSxSD57Tx7v51DMxVZOsiUD8=)
direnv: using nix
direnv: nix-direnv: using cached dev shell
direnv: export +AR +AS +BUNDLE_JOBS +BUNDLE_PATH +BUNDLE_RETRY +CC +CONFIG_SHELL +CXX +DATABASE_URL +GEM_PATH +GETTEXTDATADIRS_FOR_BUILD +HOST_PATH +IN_NIX_SHELL +LD +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_BUILD_CORES +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_CFLAGS_COMPILE +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_LDFLAGS +NIX_PKG_CONFIG_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu +NIX_STORE +NM +NODE_ENV +NODE_PATH +OBJCOPY +OBJDUMP +PKG_CONFIG +PKG_CONFIG_PATH +RAILS_ENV +RANLIB +READELF +REDIS_URL +RUBYLIB +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +XDG_DATA_DIRS +__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 ~LANG ~PATH

However, as soon as I enable the direnv-vscode extension, when I open an integrated terminal, none of the above output is displayed and my PATH is not setup as expected. I need to run direnv allow to get the same output and have my PATH setup properly.

Note that everything works correctly from a custom task defined in tasks.json. The issue seems to be only with the integrated terminal.

lakim commented 1 year ago

Same as @s0kil my direnv status is the same before and after executing direnv allow:

direnv exec path /usr/local/bin/direnv
DIRENV_CONFIG /home/lakim/.config/direnv
bash_path /nix/store/a7f7xfp9wyghf44yv6l6fv9dfw492hd3-bash-5.2-p15/bin/bash
disable_stdin false
warn_timeout 5s
whitelist.prefix []
whitelist.exact map[]
Loaded RC path /home/lakim/path/to/project/.envrc
Loaded watch: ".envrc" - 2023-07-26T18:49:22+02:00
Loaded watch: "../../../.local/share/direnv/allow/9a07ed4dc4c70bdc97e6b3b9f4610122a4f4cf459a224dd3751f9e591af799ea" - 2023-07-26T19:11:03+02:00
Loaded watch: "../../../.direnvrc" - 1970-01-01T01:00:00+01:00
Loaded watch: "../../../.config/direnv/direnvrc" - 1970-01-01T01:00:00+01:00
Loaded watch: "shell.nix" - 2023-07-23T16:18:29+02:00
Loaded watch: "default.nix" - 1970-01-01T01:00:00+01:00
Loaded RC allowed false
Loaded RC allowPath 
Found RC path /home/lakim/path/to/project/.envrc
Found watch: ".envrc" - 2023-07-26T18:49:22+02:00
Found watch: "../../../.local/share/direnv/allow/9a07ed4dc4c70bdc97e6b3b9f4610122a4f4cf459a224dd3751f9e591af799ea" - 2023-07-26T19:11:03+02:00
Found RC allowed true
Found RC allowPath /home/lakim/.local/share/direnv/allow/9a07ed4dc4c70bdc97e6b3b9f4610122a4f4cf459a224dd3751f9e591af799ea
s0kil commented 1 year ago

It's so strange that direnv status is the same, however the programs I run cannot see certain paths unless I do, direnv allow.

s0kil commented 1 year ago

Another thing, when I open a regular terminal, and go to the project directory: It's visible that the direnv is being loaded:

$ cd Lab/Zetta-Pow/command/
direnv: loading ~/Lab/Zetta-Pow/command/.envrc
direnv: loading https://raw.githubusercontent.com/nix-community/nix-direnv/2.3.0/direnvrc (sha256-Dmd+j63L84wuzgyjITIfSxSD57Tx7v51DMxVZOsiUD8=)
direnv: using flake . --impure --accept-flake-config
direnv: nix-direnv: using cached dev shell
direnv: export +ADMIN_PASS +ADMIN_USER +C_INCLUDE_PATH +DEVENV_DOTFILE +DEVENV_PROFILE +DEVENV_ROOT +DEVENV_STATE +FILEBASE_KEY +FILEBASE_SECRET +IHP +IHP_LIB +IN_NIX_SHELL +LD_LIBRARY_PATH +LIBRARY_PATH +LOCALE_ARCHIVE +PKG_CONFIG_PATH +name ~PATH ~XDG_CONFIG_DIRS ~XDG_DATA_DIRS

However, when I open a VS Code Terminal in the project directory, There is no output: When I run direnv allow, then I see the output expected:

danielsokil@system76:~/Lab/Zetta-Pow/command$ direnv allow
direnv: loading ~/Lab/Zetta-Pow/command/.envrc
direnv: loading https://raw.githubusercontent.com/nix-community/nix-direnv/2.3.0/direnvrc (sha256-Dmd+j63L84wuzgyjITIfSxSD57Tx7v51DMxVZOsiUD8=)
direnv: using flake . --impure --accept-flake-config
direnv: nix-direnv: using cached dev shell
direnv: export +ADMIN_PASS +ADMIN_USER +C_INCLUDE_PATH +DEVENV_DOTFILE +DEVENV_PROFILE +DEVENV_ROOT +DEVENV_STATE +FILEBASE_KEY +FILEBASE_SECRET +IHP +IHP_LIB +IN_NIX_SHELL +LD_LIBRARY_PATH +LIBRARY_PATH +LOCALE_ARCHIVE +PKG_CONFIG_PATH +name ~PATH ~XDG_CONFIG_DIRS ~XDG_DATA_DIRS

Screenshot from 2023-07-26 13-33-28 Screenshot from 2023-07-26 13-31-19

mkhl commented 1 year ago

FWIW, that the direnv: loading etc. output doesn't appear is correct and expected. That output appears when direnv loads a custom environment, but in your case direnv has already loaded the environment by way of the extension, so it doesn't do so again. That's why I asked how you determined that it isn't loaded.

My best guess at this point is that your shell setup undoes what your .envrc does, so let's check that:

s0kil commented 1 year ago

Yes, direnv reload works as expected:

Here are the outputs:

Before (new integrated terminal):

/home/danielsokil/.fly/bin:/home/danielsokil/.wasmtime/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.opam/4.14.0/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.cabal/bin:/home/danielsokil/.ghcup/bin:/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/bin:/home/danielsokil/.fly/bin:/home/danielsokil/.wasmtime/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.opam/4.14.0/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.cargo/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.asdf/shims:/home/danielsokil/.asdf/bin:/home/danielsokil/.cabal/bin:/home/danielsokil/.ghcup/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.local/bin:/home/danielsokil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/danielsokil/snap/flutter/common/flutter/.pub-cache/bin:/home/danielsokil/Downloads/platform-tools_r33.0.1-linux/platform-tools:/home/danielsokil/Downloads/commandlinetools-linux-8092744_latest/cmdline-tools/bin:/home/danielsokil/Android/Sdk/tools:/home/danielsokil/Android/Sdk/platform-tools:/usr/local/go/bin:/home/danielsokil/go/bin:/home/danielsokil/Lab/odin-lang/Odin:/home/danielsokil/.nimble/bin:/home/danielsokil/.local/share/coursier/bin:/home/danielsokil/.local/share/JetBrains/Toolbox/scripts:/opt/mssql-tools18/bin:/home/danielsokil/snap/flutter/common/flutter/.pub-cache/bin:/home/danielsokil/Downloads/platform-tools_r33.0.1-linux/platform-tools:/home/danielsokil/Downloads/commandlinetools-linux-8092744_latest/cmdline-tools/bin:/home/danielsokil/Android/Sdk/tools:/home/danielsokil/Android/Sdk/platform-tools:/usr/local/go/bin:/home/danielsokil/go/bin:/home/danielsokil/Lab/odin-lang/Odin:/home/danielsokil/.nimble/bin:/home/danielsokil/.local/share/coursier/bin:/home/danielsokil/.local/share/JetBrains/Toolbox/scripts:/opt/mssql-tools18/bin
{
  "p": {
    "PATH": "/home/danielsokil/.fly/bin:/home/danielsokil/.wasmtime/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.opam/4.14.0/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.cargo/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.asdf/shims:/home/danielsokil/.asdf/bin:/home/danielsokil/.cabal/bin:/home/danielsokil/.ghcup/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.local/bin:/home/danielsokil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/danielsokil/snap/flutter/common/flutter/.pub-cache/bin:/home/danielsokil/Downloads/platform-tools_r33.0.1-linux/platform-tools:/home/danielsokil/Downloads/commandlinetools-linux-8092744_latest/cmdline-tools/bin:/home/danielsokil/Android/Sdk/tools:/home/danielsokil/Android/Sdk/platform-tools:/usr/local/go/bin:/home/danielsokil/go/bin:/home/danielsokil/Lab/odin-lang/Odin:/home/danielsokil/.nimble/bin:/home/danielsokil/.local/share/coursier/bin:/home/danielsokil/.local/share/JetBrains/Toolbox/scripts:/opt/mssql-tools18/bin",
    "XDG_CONFIG_DIRS": "/etc/xdg/xdg-pop:/etc/xdg",
    "XDG_DATA_DIRS": "/usr/share/pop:/usr/share/gnome:/home/danielsokil/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop"
  },
  "n": {
    "C_INCLUDE_PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/include:",
    "DEVENV_DOTFILE": "/home/danielsokil/Lab/Zetta-Pow/command/.devenv",
    "DEVENV_PROFILE": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile",
    "DEVENV_ROOT": "/home/danielsokil/Lab/Zetta-Pow/command",
    "DEVENV_STATE": "/home/danielsokil/Lab/Zetta-Pow/command/.devenv/state",
    "DIRENV_ACTIVE": "/home/danielsokil/Lab/Zetta-Pow/command:",
    "DIRENV_DIR": "-/home/danielsokil/Lab/Zetta-Pow/command",
    "DIRENV_FILE": "/home/danielsokil/Lab/Zetta-Pow/command/.envrc",
    "DIRENV_WATCHES": "eJyskU1O7DAQBu_i9bzYmTg_zhnegjWIRafdJlbaNopNZiTE3VkghIBR0AguUF3V392zeIQyi1HIOQWSFqInzmnxLP_DJG-pFPh3k04SUwgQrawobiuKgwjJFh9IjHVnlG76oRkOgs4-lyzGsj7Ry2EHXnFCYJlnWElav1LcJDCnk6xJH0GhQphc3w4T1IBkajfBBOD0UVnXW2VahCNSrY1pbNfoVvWEfTe4S2qt0deovel8jlQfAAecfyBgis4_vHf9mnd5CcewUBX9-XtxO3TXjLHH54TL1wONMfoPDlja8kzMVUmBd59z_xoAAP__p6bmgw==",
    "IHP": "/nix/store/bi05sv3ms22j5v3cla85bvam8xfcrsxf-source/lib/IHP",
    "IHP_LIB": "IHP/lib/IHP",
    "IN_NIX_SHELL": "impure",
    "LD_LIBRARY_PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/lib:",
    "LIBRARY_PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/lib:",
    "LOCALE_ARCHIVE": "/nix/store/7n67sxgrwchfjpwjady8rlmp350jzi3n-glibc-locales-2.37-8/lib/locale/locale-archive",
    "PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/bin:/home/danielsokil/.fly/bin:/home/danielsokil/.wasmtime/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.opam/4.14.0/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.cargo/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.asdf/shims:/home/danielsokil/.asdf/bin:/home/danielsokil/.cabal/bin:/home/danielsokil/.ghcup/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.local/bin:/home/danielsokil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/danielsokil/snap/flutter/common/flutter/.pub-cache/bin:/home/danielsokil/Downloads/platform-tools_r33.0.1-linux/platform-tools:/home/danielsokil/Downloads/commandlinetools-linux-8092744_latest/cmdline-tools/bin:/home/danielsokil/Android/Sdk/tools:/home/danielsokil/Android/Sdk/platform-tools:/usr/local/go/bin:/home/danielsokil/go/bin:/home/danielsokil/Lab/odin-lang/Odin:/home/danielsokil/.nimble/bin:/home/danielsokil/.local/share/coursier/bin:/home/danielsokil/.local/share/JetBrains/Toolbox/scripts:/opt/mssql-tools18/bin",
    "PKG_CONFIG_PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/lib/pkgconfig:",
    "XDG_CONFIG_DIRS": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/etc/xdg:/etc/xdg/xdg-pop:/etc/xdg",
    "XDG_DATA_DIRS": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/share:/usr/share/pop:/usr/share/gnome:/home/danielsokil/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share:/var/lib/snapd/desktop",
    "name": "devenv-shell-env"
  }
}

After (direnv reload):

/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/bin:/home/danielsokil/.fly/bin:/home/danielsokil/.wasmtime/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.opam/4.14.0/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.cargo/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.asdf/shims:/home/danielsokil/.asdf/bin:/home/danielsokil/.cabal/bin:/home/danielsokil/.ghcup/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.local/bin:/home/danielsokil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/danielsokil/snap/flutter/common/flutter/.pub-cache/bin:/home/danielsokil/Downloads/platform-tools_r33.0.1-linux/platform-tools:/home/danielsokil/Downloads/commandlinetools-linux-8092744_latest/cmdline-tools/bin:/home/danielsokil/Android/Sdk/tools:/home/danielsokil/Android/Sdk/platform-tools:/usr/local/go/bin:/home/danielsokil/go/bin:/home/danielsokil/Lab/odin-lang/Odin:/home/danielsokil/.nimble/bin:/home/danielsokil/.local/share/coursier/bin:/home/danielsokil/.local/share/JetBrains/Toolbox/scripts:/opt/mssql-tools18/bin
{
  "p": {
    "PATH": "/home/danielsokil/.fly/bin:/home/danielsokil/.wasmtime/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.opam/4.14.0/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.cargo/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.asdf/shims:/home/danielsokil/.asdf/bin:/home/danielsokil/.cabal/bin:/home/danielsokil/.ghcup/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.local/bin:/home/danielsokil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/danielsokil/snap/flutter/common/flutter/.pub-cache/bin:/home/danielsokil/Downloads/platform-tools_r33.0.1-linux/platform-tools:/home/danielsokil/Downloads/commandlinetools-linux-8092744_latest/cmdline-tools/bin:/home/danielsokil/Android/Sdk/tools:/home/danielsokil/Android/Sdk/platform-tools:/usr/local/go/bin:/home/danielsokil/go/bin:/home/danielsokil/Lab/odin-lang/Odin:/home/danielsokil/.nimble/bin:/home/danielsokil/.local/share/coursier/bin:/home/danielsokil/.local/share/JetBrains/Toolbox/scripts:/opt/mssql-tools18/bin",
    "XDG_CONFIG_DIRS": "/etc/xdg/xdg-pop:/etc/xdg",
    "XDG_DATA_DIRS": "/usr/share/pop:/usr/share/gnome:/home/danielsokil/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop"
  },
  "n": {
    "C_INCLUDE_PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/include:",
    "DEVENV_DOTFILE": "/home/danielsokil/Lab/Zetta-Pow/command/.devenv",
    "DEVENV_PROFILE": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile",
    "DEVENV_ROOT": "/home/danielsokil/Lab/Zetta-Pow/command",
    "DEVENV_STATE": "/home/danielsokil/Lab/Zetta-Pow/command/.devenv/state",
    "DIRENV_ACTIVE": "/home/danielsokil/Lab/Zetta-Pow/command:",
    "DIRENV_DIR": "-/home/danielsokil/Lab/Zetta-Pow/command",
    "DIRENV_FILE": "/home/danielsokil/Lab/Zetta-Pow/command/.envrc",
    "DIRENV_WATCHES": "eJyskU1O7DAQBu_i9bzYmTg_zhnegjWIRafdJlbaNopNZiTE3VkghIBR0AguUF9V992zeIQyi1HIOQWSFqInzmnxLP_DJG-pFPh3k04SUwgQrawobiuKgwjJFh9IjHVnlG56Y8xB0NnnksVY1id6OezAK04ILPMMK0nrV4qbBOZ0kjXpIyhUCJPr22GCGpBM7SaYAJw-Kut6q0yLcESqtTGN7Rrdqp6w7wZ3Sa01-hq1N53PkeoD4IDzDwRM0fmH965f8y5_wjEsVEV__l7cDt1wRfEenxMuXwcaY_QfDFja8kzMVUmBd49z_xoAAP__uobmiw==",
    "IHP": "/nix/store/bi05sv3ms22j5v3cla85bvam8xfcrsxf-source/lib/IHP",
    "IHP_LIB": "IHP/lib/IHP",
    "IN_NIX_SHELL": "impure",
    "LD_LIBRARY_PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/lib:",
    "LIBRARY_PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/lib:",
    "LOCALE_ARCHIVE": "/nix/store/7n67sxgrwchfjpwjady8rlmp350jzi3n-glibc-locales-2.37-8/lib/locale/locale-archive",
    "PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/bin:/home/danielsokil/.fly/bin:/home/danielsokil/.wasmtime/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.opam/4.14.0/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.cargo/bin:/home/danielsokil/.local/share/pnpm:/home/danielsokil/.asdf/shims:/home/danielsokil/.asdf/bin:/home/danielsokil/.cabal/bin:/home/danielsokil/.ghcup/bin:/home/danielsokil/.nix-profile/bin:/home/danielsokil/.local/bin:/home/danielsokil/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/danielsokil/snap/flutter/common/flutter/.pub-cache/bin:/home/danielsokil/Downloads/platform-tools_r33.0.1-linux/platform-tools:/home/danielsokil/Downloads/commandlinetools-linux-8092744_latest/cmdline-tools/bin:/home/danielsokil/Android/Sdk/tools:/home/danielsokil/Android/Sdk/platform-tools:/usr/local/go/bin:/home/danielsokil/go/bin:/home/danielsokil/Lab/odin-lang/Odin:/home/danielsokil/.nimble/bin:/home/danielsokil/.local/share/coursier/bin:/home/danielsokil/.local/share/JetBrains/Toolbox/scripts:/opt/mssql-tools18/bin",
    "PKG_CONFIG_PATH": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/lib/pkgconfig:",
    "XDG_CONFIG_DIRS": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/etc/xdg:/etc/xdg/xdg-pop:/etc/xdg",
    "XDG_DATA_DIRS": "/nix/store/cdp99l37xzciik0ljr89wn4b4ya26pn5-devenv-profile/share:/usr/share/pop:/usr/share/gnome:/home/danielsokil/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share:/var/lib/snapd/desktop",
    "name": "devenv-shell-env"
  }
}
s0kil commented 1 year ago

Yes, When I source . ~/.bashrc file, it unloads current project .envrc.

s0kil commented 1 year ago

So, it seems the solution is as simple as adding direnv reload right after it's hooked with: eval "$(direnv hook bash)":

# direnv
eval "$(direnv hook bash)"
direnv reload

To the ~/.bashrc file.

lakim commented 1 year ago

Same as @s0kil, my PATH order changes before and after direnv reload. In my case, I'm expecting to have /nix/store/... paths at the beginning of the PATH. It's not the case before, but it's fixed after direnv reload. I'll try to investigate my .zshrc. Let me know if you have an idea of a possible cause.

lakim commented 1 year ago

I use asdf for some projects (don't have a choice) and nix-shell for my personal project. My issue was related to asdf shims path overriding the nix paths.

I've solved this by using asdf-direnv:

asdf plugin-add direnv
asdf direnv setup --shell zsh --version system

In my .zshrc, I've changed . $HOME/.asdf/asdf.sh to PATH="$HOME/.asdf/bin:$PATH" to only add the asdf bin to the path and not the shims path.

Lastly, I've created the following .envrc in the projects using asdf instead of nix-shell:

use asdf

This way, I use direnv for everything. Some projects use asdf and others use nix, avoiding path conflicts.

Hope this helps.

regadas commented 1 year ago

Facing the same issue here, where new integrated terminal doesn't have the env loaded.

burdiyan commented 10 months ago

I'm facing the same issue starting a few months ago. It used to work just fine: the environment was correctly loaded in the IDE and in the integrated terminal. But since a few months ago, I constantly need to reload in the integrated terminal.

I definitely can see it not working in the integrated terminal, because it doesn't pick up the correct version of the binary that is loaded inside my .envrc. But it does appear after I do direnv reload.

Looks like there was some optimization put in place so that the environment doesn't load again in the terminal and attempts to reuse the one that's already loaded by the IDE extension. But seems like it doesn't work correctly.

Before that, whenever I open integrated terminal I always used to see the activation message (which I understand wouldn't appear after the optimization was put in place).

I uninstalled the extension, and things started working as expected in the integrated terminal, but of course nothing is loading inside the IDE.

After installing the extension again things went back being bad: the environment doesn't load in the integrated terminal until I explicitly reload.

burdiyan commented 10 months ago

OK, looks like the problem is a bit more complex. Turns out that if I just export some variable in my .envrc it does work inside the integrated terminal just fine (as noticed previously I still don't see how direnv loads the environment, because it's already loaded by the extension).

The problem is that I'm not just exporting some static variables. I used to do use nix to pick up the environment from my shell.nix file, and it's not doing it correctly. It used to, but it stopped some months ago.

Now, I'm switching from Nix to Pixi, and even though it doesn't have a built in support for Direnv, I basically do eval "$(my-script-that-prints-some-variables)" inside of my.envrc` file. And those variables are not picked up in the integrated terminal if I have the VS Code extension installed.

Actually, it looks like most variables do appear, except the one for PATH.

burdiyan commented 10 months ago

Just checked again. Indeed, it seems like only the PATH variable is getting mangled somehow after direnv activation. But most other things evaluated in .envrc do get picked up by the integrated terminal. Unfortunately, the PATH variable is probably the most important one for my use case :)

burdiyan commented 10 months ago

Testing even further. I'm now just doing export PATH="/bin:/usr/bin:/usr/local/bin" in my .envrc file, and opening integrated terminal, when I do echo $PATH I see a different value. So clearly something modifies the PATH after the direnv extension does the initial loading.

I could assume that it might be VS Code doing its environment resolution thing. But I wonder why it used to work just fine (although I was seeing the direnv loading the environment each time I open the terminal, which I now don't).

burdiyan commented 10 months ago

To clarify, none of my VS Code settings touch the PATH variables.

mkhl commented 10 months ago

@burdiyan did you read the earlier comments on this?

What's happening is this:

Setting up the environment inside VSCode is not an optimization, it is necessary to make sure VSCode Tasks can even get the direnv environment, and it has been working like this since the first version.

The easiest workaround is having your shell run direnv reload late during startup, maybe conditional on DIRENV_DIFF being set.

I'm still thinking about what I can do from the extension side to make that workaround not necessary.

burdiyan commented 10 months ago

I've read the conversation but it's quite hard to follow, and I haven't realized that the problem is what you now explain (which now makes sense).

I wonder why this wasn't a problem before (a few months ago)? Something had to change I presume, either inside direnv, VS Code, or the extension?

burdiyan commented 10 months ago

I added this snippet to my shell profile to mitigate the problem (in case it helps anyone else), but I hope it could be fixed on some other level so this trick isn't necessary.

# This is a workaround to make direnv work with VS Code's integrated terminal
# when using the direnv extension, by making sure to reload
# the environment the first time terminal is opened.
#
# See https://github.com/direnv/direnv-vscode/issues/561#issuecomment-1837462994.
#
# The variable VSCODE_INJECTION is apparently set by VS Code itself, and this is how
# we can detect if we're running inside the VS Code terminal or not.
if [[ -n "$VSCODE_INJECTION" && -z "$VSCODE_TERMINAL_DIRENV_LOADED" && -f .envrc ]]; then
  direnv reload
  export VSCODE_TERMINAL_DIRENV_LOADED=1
fi

I'm using this with zsh but it should probably work with bash and other compatible shells.

Maatteogekko commented 6 months ago

using @burdiyan workaround caused the direnv extension to continually reload. This is my version that seems to work better.

# This is a workaround to make direnv work with VS Code's integrated terminal
# when using the direnv extension, by making sure to reload
# the environment the first time terminal is opened.
#
# See https://github.com/direnv/direnv-vscode/issues/561#issuecomment-1837462994.
#
# The variable VSCODE_INJECTION is apparently set by VS Code itself, and this is how
# we can detect if we're running inside the VS Code terminal or not.
if [[ -n "$VSCODE_INJECTION" && -z "$VSCODE_TERMINAL_DIRENV_LOADED" && -f .envrc ]]; then
    cd ..
    cd -
    export VSCODE_TERMINAL_DIRENV_LOADED=1
fi
WaffleLapkin commented 1 month ago

Translated @Maatteogekko's solution to fish:

# This is a workaround to make direnv work with VS Code's integrated terminal
# when using the direnv extension, by making sure to reload
# the environment the first time terminal is opened.
#
# See <https://github.com/direnv/direnv-vscode/issues/561#issuecomment-2310756248>.
#
# The variable VSCODE_INJECTION is apparently set by VS Code itself, and this is how
# we can detect if we're running inside the VS Code terminal or not.
if test -n "$VSCODE_INJECTION"
    and test -z "$VSCODE_TERMINAL_DIRENV_LOADED"
    and test -f .envrc

    # fish only emits `fish_prompt` in interactive mode,
    # so emit it explicitly to trigger direnv to reload.
    cd ..
    emit fish_prompt
    cd -
    emit fish_prompt

    # I'm not sure if this is actually helpful?
    # new terminals created by vscode don't inheret env variables,
    # so all new terminals will still re-execute this
    export VSCODE_TERMINAL_DIRENV_LOADED=1
end