Shopify / ruby-lsp

An opinionated language server for Ruby
https://shopify.github.io/ruby-lsp/
MIT License
1.39k stars 128 forks source link

Visual Studio Code 0.7.4 appears to be unable to initialize rbenv #2202

Open justinbburris opened 2 weeks ago

justinbburris commented 2 weeks ago

Description

When I attempt to load a codebase with ruby LSP and the Visual Studio Code integration (at version 0.7.4), the extension fails to load.

It works when using the extension at 0.7.2

Some initial investigation turned up this change to the rbenv (and other manager) initialization. Seems like it could be related?

Error

VS Code error popup

Automatic Ruby environment activation with rbenv failed: 
Command failed: rbenv exec ruby -W0 -rjson -e 'STDERR.print({env: ENV.to_h,yjit:!!defined?(RubyVM::YJIT),version:RUBY_VERSION}.to_json)' 
zsh:1: command not found: rbenv

Output error

2024-06-17 21:25:52.034 [info] (smallwins) Running command: `rbenv exec ruby -W0 -rjson -e 'STDERR.print({env: ENV.to_h,yjit:!!defined?(RubyVM::YJIT),version:RUBY_VERSION}.to_json)'` in /Users/justinburris/dev/saishori/smallwins using shell: /bin/zsh

Debugging

Here's the output from running the intialization command from the shell directly

$ /bin/zsh -ic 'rbenv exec ruby -rjson -e "puts JSON.dump(ENV.to_h)"'

{"RBENV_VERSION":"3.3.0",...}

Reproduction steps

Configuration

Processor: Apple M2 Max ruby version: 3.3.0 & 3.0.3 rbenv version: 1.2.0 (installed via brew and configured per usual method) shell: zsh editor: VS Code 1.90.1 No vs code configuration

Project setup

Any ruby project with a Gemfile that has the ruby-lsp gem

If it helps, here's a snippet of my .zshrc

eval "$(/opt/homebrew/bin/brew shellenv)"

# rbenv
# To enable shims and autocompletion add to your profile:
eval "$(rbenv init - zsh)"
FPATH=~/.rbenv/completions:"$FPATH"
vinistock commented 2 weeks ago

Thank you for the bug report! We recently pushed changes to decouple Ruby environment activation from shells as much as possible because it caused headaches for a number of users.

I suspect the reason for not finding rbenv is because it's only present if you source ~/.zshrc, which we don't do by default.

If you source rbenv in your profile file (whatever the zsh equivalent of .profile is), does it work?

justinbburris commented 2 weeks ago

I appreciate y'alls work here. It's tough dealing with all of the different configurations out there.

It looks like moving it to the ~/.zshenv file did the trick!

Per these docs, that file is sourced very early in the initialization, and appears to work!

Would y'all be open to me updating the troubleshooting guide to indicate this solution?

vinistock commented 2 weeks ago

Yes, definitely! We should include that in the documentation.

xeron commented 4 days ago

I see similar issues activating asdf if vscode was not started from the shell.

vinistock commented 2 days ago

@xeron that's a bit surprising to me since we search for the asdf script file and manually source it to ensure it's available before invoking it.

Do you want to create a separate issue for that and describe your exact setup? It seems like a different issue related only to ASDF integration.

xeron commented 2 days ago

@xeron that's a bit surprising to me since we search for the asdf script file and manually source it to ensure it's available before invoking it.

Do you want to create a separate issue for that and describe your exact setup? It seems like a different issue related only to ASDF integration.

I think you're right, I created a new issue – https://github.com/Shopify/ruby-lsp/issues/2247