elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.52k stars 296 forks source link

vscode-neovim doesn't work with Elvish as default shell #1804

Closed tmpm697 closed 1 month ago

tmpm697 commented 1 month ago

What happened, and what did you expect to happen?

I have this script in PATH variable and used for vscode to spawn neovim instance to use with vscode-neovim extension:

#!/usr/bin/env bash
NVIM_APPNAME=vscode-neovim nvim "$@"

but it fail to start (my current default shell is elvish) Screenshot 2024-05-04 at 5 42 55 PM

before switched to elvish, i use zsh and the above script just work out-of-box

Output of "elvish -version"

0.20.1

Code of Conduct

xiaq commented 1 month ago

Elvish is not responsible for interpreting the shebang line, so there's something else going on.

How did you set Elvish to be your default shell?

xiaq commented 1 month ago

Also - what's the name of your script and can you elaborate why it's necessary for your setup?

I took a look at the vscode-neovim extension and it doesn't mention that you need to create a custom script to use it, so I suppose it's some custom setup you're using?

tmpm697 commented 1 month ago

Elvish is not responsible for interpreting the shebang line, so there's something else going on.

How did you set Elvish to be your default shell?

I set default shell by issue this command: sudo chsh -s $(which elvish) $USER note that i use macos sonoma and nix to install elvish.

which elvish resolve to smth like /Users/user/.nix-profile/bin/elvish but this should not be any problem, just to verbose more.

I did re-login/out before doing any test.

Also - what's the name of your script and can you elaborate why it's necessary for your setup?

it's named vscode-neovim.sh as in the image.

I took a look at the vscode-neovim extension and it doesn't mention that you need to create a custom script to use it, so I suppose it's some custom setup you're using?

you can check it here: https://github.com/milanglacier/nvim#seamless-integration-with-vscode

this script due to users want to use a separate neovim config for vscode-neovim instance and the default neovim that load ~/.config/nvim

so i have another folder ~/.config/vscode-neovim that store neovim config for neovim instance of vscode-neovim.

xiaq commented 1 month ago

OK thanks, I understand what you are trying to do now.

There seems to be quite a few moving parts in this setup you are using, and any component could be passing code through the login shell (rather than the correct thing, using an API that maps to a direct exec call without going through the shell), so making sure that the vscode-neovim script is using bash may not be sufficient, as is apparently the case here...

xiaq commented 1 month ago

Some things to check:

xiaq commented 1 month ago

Hmm looking at the error code you're getting (ENOENT) - did you set vscode-neovim.neovimExecutablePaths.darwin to the full absolute path of your script?

krader1961 commented 1 month ago

before switched to elvish, i use zsh and the above script just work out-of-box

Note that Zsh and Bash are both POSIX compatible shells (more or less). I would expect that you would experience similar problems if you used another non-POSIX shell such as Fish or Xonsh. This type of situation is why the best practice is to leave your "login" shell something like Zsh or Bash and configure your terminal emulator to spawn Elvish instead of your login shell; see https://elv.sh/get/default-shell.html. As @hanche suggested in the IM chat it a useful experiment is to try invoking the relevant programs with something like export SHELL=/bin/bash to ensure the default shell for Vscode or the Nvim plugin is POSIX compatible.

My primary OS is also macOS and it is very frustrating that it doesn't support a usable strace or truss command. It's dtruss and dtrace tools are useful but not for situations like this. What I typically do is create a "wrapper" script that looks something like this to capture the arguments to a program:

#!/bin/sh
echo "$@" > /tmp/$$.args
exec /path/to/real-program "$@"

Where the original program is renamed to /path/to/real-program and the script is installed as the original /path/to/program whose invocation we want to track.

tmpm697 commented 1 month ago

Some things to check:

  • Does your setup work correctly if you change your login shell back to bash or zsh?
  • Did you try the alternative approach of separating the config directory (setting vscode-neovim.NVIM_APPNAME)?

yes, default shell is zsh which work out-of-box I don't have another approach to this.

vscode-neovim.neovimExecutablePaths.darwin

this set to vscode-neovim.sh and it's avail when typing in terminal --> vscode-neovim.sh is avail in PATH for use. (with elvish as default shell)

tmpm697 commented 1 month ago

before switched to elvish, i use zsh and the above script just work out-of-box

Note that Zsh and Bash are both POSIX compatible shells (more or less). I would expect that you would experience similar problems if you used another non-POSIX shell such as Fish or Xonsh. This type of situation is why the best practice is to leave your "login" shell something like Zsh or Bash and configure your terminal emulator to spawn Elvish instead of your login shell; see https://elv.sh/get/default-shell.html. As @hanche suggested in the IM chat it a useful experiment is to try invoking the relevant programs with something like export SHELL=/bin/bash to ensure the default shell for Vscode or the Nvim plugin is POSIX compatible.

My primary OS is also macOS and it is very frustrating that it doesn't support a usable strace or truss command. It's dtruss and dtrace tools are useful but not for situations like this. What I typically do is create a "wrapper" script that looks something like this to capture the arguments to a program:

#!/bin/sh
echo "$@" > /tmp/$$.args
exec /path/to/real-program "$@"

Where the original program is renamed to /path/to/real-program and the script is installed as the original /path/to/program whose invocation we want to track.

this can be more useful than force macos user default shell but when i try to set vscode's terminal default shell, i can't: Screenshot 2024-05-05 at 12 10 34 PM "terminal.integrated.shell.osx": is hidden and not seem to work. is there a way to change vscode's default shell?

tmpm697 commented 1 month ago

ok i got that, it's smth like "terminal.integrated.defaultProfile.osx": "elvish",

xiaq commented 1 month ago

@tmpm697 Glad to know you worked around the issue. I've updated https://elv.sh/get/default-shell.html to include instruction for using Elvish from VS Code's terminal and documented vscode-neovim's incompatibility issue.

tmpm697 commented 1 month ago

@tmpm697 Glad to know you worked around the issue. I've updated https://elv.sh/get/default-shell.html to include instruction for using Elvish from VS Code's terminal and documented vscode-neovim's incompatibility issue.

thanks, if having elvish shell this way, is that it will first spawn zsh and then do exec elvish?

if true, then elvish will get all exported env vars from zsh?

krader1961 commented 1 month ago

thanks, if having elvish shell this way, is that it will first spawn zsh and then do exec elvish?

if true, then elvish will get all exported env vars from zsh?

Yes, because that is how env vars work; at least on Unix like operating systems such as macOS and Linux. Env vars are private to each process. When a process, such as zsh, spawns a new process it normally tells the OS to pass a copy of its env vars to the new process.

xiaq commented 1 month ago

@tmpm697 Glad to know you worked around the issue. I've updated https://elv.sh/get/default-shell.html to include instruction for using Elvish from VS Code's terminal and documented vscode-neovim's incompatibility issue.

thanks, if having elvish shell this way, is that it will first spawn zsh and then do exec elvish?

if true, then elvish will get all exported env vars from zsh?

I don't think any terminal has the literal behavior you're describing. Normally the login shell won't be involved. tmux is an exception - if you configured tmux to launch Elvish, you'll still get your login shell in the terminal, which will pass its environment variables to tmux and then to Elvish.

AFAICT some Linux desktop environments involve the login shell at some point during the login process, so you can end up getting environment variables from the profile file of the login shell, but you're using macOS so that's not relevant to you.

thanks, if having elvish shell this way, is that it will first spawn zsh and then do exec elvish? if true, then elvish will get all exported env vars from zsh?

Yes, because that is how env vars work; at least on Unix like operating systems such as macOS and Linux. Env vars are private to each process. When a process, such as zsh, spawns a new process it normally tells the OS to pass a copy of its env vars to the new process.

I think you're answering @tmpm697's second question assuming that the answer to the first question is yes, which I believe is not the case.

tmpm697 commented 1 month ago

vscode's settngs:

  "terminal.integrated.defaultProfile.osx": "elvish",

no idea but this is number of vars in elvish and zsh:

new vscode terminal --> starting shell is elvish:
~/s/nix ➜ e:env | wc -l
47
~/s/nix ➜ zsh
~/s/nix ➜ env | wc -l 
47

compare zsh's env | rg test_var vs elvish's e:env | rg test_var look the same.

so it seems that vscode on macos spawn zsh first and then exec elvish that automatically import all exported vars from zsh?

I don't set-env in my elvish config, but surprisingly if u set same var in evlish via set-env, it'll overwrite exported var from zsh but seem expected.