Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby environment activation fails in PowerShell #2434

Closed
slabstone opened this issue Aug 13, 2024 · 4 comments · Fixed by #2489
Closed

Ruby environment activation fails in PowerShell #2434

slabstone opened this issue Aug 13, 2024 · 4 comments · Fixed by #2489
Labels
bug Something isn't working help-wanted Extra attention is needed

Comments

@slabstone
Copy link

Description

Description

Ruby LSP fails to activate Ruby after plugin version 0.7.8

Environment

  • Windows 10 22H2
  • VS Code v1.92.1
  • Ruby LSP v0.17.12
  • Ruby LSP VS Code plugin v0.7.8 or later

Reproduction steps

  1. Start VS Code with Ruby LSP
  2. Observe the error
Automatic Ruby environment activation with rubyInstaller failed: Command failed: c:\Ruby33-x64\bin\ruby -W0 -rjson -e 'user_dir = Gem.user_dir;paths = Gem.path;if paths.length > 2;  paths.delete(Gem.default_dir);  paths.delete(Gem.user_dir);  if paths[0];    user_dir = paths[0] if Dir.exist?(paths[0]);  end;end;newer_gem_home = File.join(File.dirname(user_dir), "3.3.4");gems = (Dir.exist?(newer_gem_home) ? newer_gem_home : user_dir);data = { defaultGems: Gem.default_dir, gemHome: gems, yjit: !!defined?(RubyVM::YJIT), version: RUBY_VERSION };STDERR.print(JSON.dump(data))'
-e:1: unexpected fraction part after numeric literal
...n(File.dirname(user_dir), 3.3.4);gems = (Dir.exist?(newer_ge...
...                             ^~
c:\Ruby33-x64\bin\ruby.exe: compile error (SyntaxError)

VS Code output logs

Running command: `c:\Ruby33-x64\bin\ruby -W0 -rjson -e 'user_dir = Gem.user_dir;paths = Gem.path;if paths.length > 2;  paths.delete(Gem.default_dir);  paths.delete(Gem.user_dir);  if paths[0];    user_dir = paths[0] if Dir.exist?(paths[0]);  end;end;newer_gem_home = File.join(File.dirname(user_dir), "3.3.4");gems = (Dir.exist?(newer_gem_home) ? newer_gem_home : user_dir);data = { defaultGems: Gem.default_dir, gemHome: gems, yjit: !!defined?(RubyVM::YJIT), version: RUBY_VERSION };STDERR.print(JSON.dump(data))'` in ...\ruby-lsp-env using shell: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

This command indeed fails in PowerShell

PS ...\ruby-lsp-env> c:\Ruby33-x64\bin\ruby -W0 -rjson -e 'user_dir = Gem.user_dir;paths = Gem.path;if paths.length > 2;  paths.delete(Gem.default_dir);  paths.delete(Gem.user_dir);  if paths[0];    user_dir = paths[0] if Dir.exist?(paths[0]);  end;end;newer_gem_home = File.join(File.dirname(user_dir), "3.3.4");gems = (Dir.exist?(newer_gem_home) ? newer_gem_home : user_dir);data = { defaultGems: Gem.default_dir, gemHome: gems, yjit: !!defined?(RubyVM::YJIT), version: RUBY_VERSION };STDERR.print(JSON.dump(data))'
-e:1: unexpected fraction part after numeric literal
...n(File.dirname(user_dir), 3.3.4);gems = (Dir.exist?(newer_ge...
c:\Ruby33-x64\bin\ruby.exe: compile error (SyntaxError)

Additional information

This does not reproduce on VS Code plugin v0.7.5 or earlier. First version to have this bug is v0.7.8.

VS Code logs with plugin v0.7.5

Running command: `c:\Ruby33-x64\bin\ruby -W0 -rjson -e 'user_dir = Gem.user_dir;paths = Gem.path;if paths.length > 2;  paths.delete(Gem.default_dir);  paths.delete(Gem.user_dir);  if paths[0];    user_dir = paths[0] if Dir.exist?(paths[0]);  end;end;newer_gem_home = File.join(File.dirname(user_dir), RUBY_VERSION);gems = (Dir.exist?(newer_gem_home) ? newer_gem_home : user_dir);data = { defaultGems: Gem.default_dir, gemHome: gems, yjit: !!defined?(RubyVM::YJIT), version: RUBY_VERSION };STDERR.print(JSON.dump(data))'` in ...\ruby-lsp-env using shell: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Activated Ruby environment: defaultGems=C:/Ruby33-x64/lib/ruby/gems/3.3.0 gemHome=C:/Users/.../.local/share/gem/ruby/3.3.0 yjit=false
@slabstone slabstone added the bug Something isn't working label Aug 13, 2024
@andyw8
Copy link
Contributor

andyw8 commented Aug 14, 2024

Thank you for the report. It seems related to the quotes around the version number, but it's unclear on why exactly it's failing. Unfortunately none of the maintainers are Windows users so we will hope the community can assist with solving this.

@andyw8 andyw8 added the help-wanted Extra attention is needed label Aug 14, 2024
@slabstone
Copy link
Author

Simple escaping \"3.3.4\" worked for PowerShell

> c:\Ruby33-x64\bin\ruby -W0 -rjson -e 'user_dir = Gem.user_dir;paths = Gem.path;if paths.length > 2;  paths.delete(Gem.default_dir);  paths.delete(Gem.user_dir);  if paths[0];    user_dir = paths[0] if Dir.exist?(paths[0]);  end;end;newer_gem_home = File.join(File.dirname(user_dir), \"3.3.4\");gems = (Dir.exist?(newer_gem_home) ? newer_gem_home : user_dir);data = { defaultGems: Gem.default_dir, gemHome: gems, yjit: !!defined?(RubyVM::YJIT), version: RUBY_VERSION };STDERR.print(JSON.dump(data))'
{"defaultGems":"c:/Ruby33-x64/lib/ruby/gems/3.3.0","gemHome":"C:/Users/.../.local/share/gem/ruby/3.3.0","yjit":false,"version":"3.3.4"}

The original (unescaped) command also works in vanilla Command Prompt (cmd.exe), but escaped does not

>c:\Ruby33-x64\bin\ruby -W0 -rjson -e 'user_dir = Gem.user_dir;paths = Gem.path;if paths.length > 2;  paths.delete(Gem.default_dir);  paths.delete(Gem.user_dir);  if paths[0];    user_dir = paths[0] if Dir.exist?(paths[0]);  end;end;newer_gem_home = File.join(File.dirname(user_dir), \"3.3.4\");gems = (Dir.exist?(newer_gem_home) ? newer_gem_home : user_dir);data = { defaultGems: Gem.default_dir, gemHome: gems, yjit: !!defined?(RubyVM::YJIT), version: RUBY_VERSION };STDERR.print(JSON.dump(data))'
-e:1: syntax error, unexpected backslash, expecting ')'
....join(File.dirname(user_dir), \"3.3.4\");gems = (Dir.exist?(...
c:\Ruby33-x64\bin\ruby: compile error (SyntaxError)

I couldn't find a way to select shell for the activation command, so potential fixes could be escaping quotes in PowerShell or using Command Prompt.

@andyw8 andyw8 removed their assignment Aug 23, 2024
@SnowSzn
Copy link

SnowSzn commented Aug 25, 2024

I have the same problem (activation fails on PowerShell) running ruby-lsp with the same environment as @slabstone but on Windows 11.

In my case, The VSCode extension fails to activate and remains stuck as "Starting".

The output in VSCode looks like this:

2024-08-25 22:51:21.793 [info] (test) Checking if chruby is available on the path with command: chruby --version
2024-08-25 22:51:21.811 [info] (test) Checking if rbenv is available on the path with command: rbenv --version
2024-08-25 22:51:21.828 [info] (test) Checking if rvm is available on the path with command: rvm --version
2024-08-25 22:51:21.848 [info] (test) Checking if asdf is available on the path with command: asdf --version
2024-08-25 22:51:21.922 [info] (test) Discovered version manager rubyInstaller
2024-08-25 22:51:21.997 [info] (test) Running command: `c:\Ruby33-x64\bin\ruby.exe -W0 -rjson -e 'STDERR.print({ env: ENV.to_h, yjit: !!defined?(RubyVM::YJIT), version: RUBY_VERSION }.to_json)'` in d:\test using shell: C:\Users\ferna\AppData\Local\Microsoft\WindowsApps\Microsoft.PowerShell_8wekyb3d8bbwe\pwsh.exe
2024-08-25 22:51:23.085 [error] (test) Tried parsing invalid JSON environment: �[31;1mSet-PSReadLineOption: �[0mC:\Users\ferna\OneDrive\Documentos\PowerShell\Microsoft.PowerShell_profile.ps1:6�[0m
�[31;1m�[0m�[36;1mLine |�[0m
�[31;1m�[0m�[36;1m�[36;1m   6 | �[0m �[36;1mSet-PSReadLineOption -PredictionSource HistoryAndPlugin�[0m
�[31;1m�[0m�[36;1m�[36;1m�[0m�[36;1m�[0m�[36;1m     | �[31;1m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~�[0m
�[31;1m�[0m�[36;1m�[36;1m�[0m�[36;1m�[0m�[36;1m�[31;1m�[31;1m�[36;1m     | �[31;1mThe predictive suggestion feature cannot be enabled because the console output doesn't support virtual terminal�[0m
�[31;1m�[0m�[36;1m�[36;1m�[0m�[36;1m�[0m�[36;1m�[31;1m�[31;1m�[36;1m�[31;1m�[36;1m     | �[31;1mprocessing or it's redirected.�[0m
�[31;1mSet-PSReadLineOption: �[0mC:\Users\ferna\OneDrive\Documentos\PowerShell\Microsoft.PowerShell_profile.ps1:7�[0m
�[31;1m�[0m�[36;1mLine |�[0m
�[31;1m�[0m�[36;1m�[36;1m   7 | �[0m �[36;1mSet-PSReadLineOption -PredictionViewStyle ListView�[0m
�[31;1m�[0m�[36;1m�[36;1m�[0m�[36;1m�[0m�[36;1m     | �[31;1m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~�[0m
�[31;1m�[0m�[36;1m�[36;1m�[0m�[36;1m�[0m�[36;1m�[31;1m�[31;1m�[36;1m     | �[31;1mControlador no v�lido.�[0m

The problem appears when PowerShell tries to load the user profile (Microsoft.PowerShell_profile.ps1)

My Powershell profile looks like this:

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/easy-term.omp.json" | Invoke-Expression

Import-Module -Name Terminal-Icons
Import-Module -Name PSPasswordGenerator

Set-PSReadLineOption -PredictionSource HistoryAndPlugin
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineOption -EditMode Windows

winfetch

The extension fails to activate due to the Set-PSReadLineOption commands, it complains about the console output not supporting virtual terminal.

Once these lines are removed from the profile, the extension activates succesfully.

You can also force ruby-lsp to use another shell in a workspace changing the VSCode setting terminal.integrated.defaultProfile.windows to Window's default command prompt, so you can keep your PowerShell profile, but you will be forced to use that shell in the workspace.
For example, in VScode settings JSON:

{
  "terminal.integrated.defaultProfile.windows": "Command Prompt"
}

Making ruby-lsp avoid PowerShell will likely fix this issue.

You could also force PowerShell not to load the user profile with the argument -NoProfile when invoking PowerShell:

pwsh.exe -NoProfile -Command "Command-Here"

@andyw8 Is it possible to let the user choose/force which shell (shell path) to use on the ruby-lsp extension settings in VSCode? so we don't have to change the value of terminal.integrated.defaultProfile.windows and affect the whole workspace? In case the shell setting is left empty, the default shell can be used

@vinistock
Copy link
Member

Based on the context provided here, I believe #2489 will fix this.

Using the user's default shell to invoke the scripts is relevant for Linux and MacOS, but probably not for Windows. We can just standardize to always activate using cmd.exe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help-wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants