castwide / vscode-solargraph

A Visual Studio Code extension for Solargraph.
Other
425 stars 23 forks source link

Solargraph only works when running code from my shell #193

Closed tyler-boyd closed 4 years ago

tyler-boyd commented 4 years ago

Thanks for your work on this, it's a very cool extension!

Like #74 and #41, but with 0.21.1 of the extension.

I have zsh as my shell, and running solargraph from the VSCode integrated terminal works fine, but the language server itself crashes on boot as it uses system ruby instead of chruby.

I'm not sure if this is because the solargraph extension is using bash instead of zsh...but if I run code from a zsh terminal, everything works totally fine including the solargraph autocomplete etc.

Thoughts? How can I achieve the same effect as running code from a terminal?

Please let me know if you need any more information!

castwide commented 4 years ago

This appears to be a common problem with chruby. It might be that whatever script adds chruby to your environment, e.g., ~/.profile or ~/.zprofile, isn't getting loaded by VS Code. That would also explain why it works from a terminal; the correct environment gets passed into it.

tyler-boyd commented 4 years ago

Thanks for the tip, do you know exactly how VSCode loads the shell?

I can play around with adding the chruby startup script to other profile/rc files

castwide commented 4 years ago

It's liable to vary, but testing on a MacBook, it appears to load from a non-login bash shell. To get more information about the environment, you can run process.env from the developer console (Help -> Toggle Developer Tools).

Please let me know if you find a chruby setup that works. I'll at least get it documented, and I might be able to determine a way to make it work out of the box.

tyler-boyd commented 4 years ago

I think I found my problem. Nothing to do with VSCode itself (or its env); your logic here is properly adding the -l.

How to explain this clearly...the issue for me was related to https://github.com/postmodern/chruby/issues/142#issuecomment-18252495.

Basically, when running zsh -l -c 'echo $PATH', the chruby-selected ruby version came AFTER the default /usr/local/.... I fixed this by adding the following to my ~/.zprofile:

source /usr/local/opt/chruby/share/chruby/chruby.sh
source /usr/local/opt/chruby/share/chruby/auto.sh
chruby_auto

The key is that third line, chruby_auto, which will (try to) add the current directory's .ruby-version to the start of the PATH. The default chruby/auto hooks do NOT run at all for non-interactive shells (like your VSCode extension spawning a child process), so explicitly running it once as part of the login shell process fixes the issue.

Tl;dr add 3 magic lines to ~/.zprofile (likely the same for .bash_profile) and the extension will work perfectly!

tyler-boyd commented 4 years ago

I'm gonna close this issue as it is not an issue with this extension. Let me know if there would be a good place for me to add this in the docs, otherwise, hopefully people find this issue :slightly_smiling_face: