ruby-lsp icon indicating copy to clipboard operation
ruby-lsp copied to clipboard

Error `uninitialized constant RUBY_LSP_ACTIVATION_SEPARATOR` with PowerShell (Windows)

Open ynty opened this issue 1 year ago • 7 comments

Description

Ruby LSP Information

Ruby LSP Information

VS Code Version

1.94.2

Ruby LSP Extension Version

0.8.8

Ruby LSP Server Version

Unknown

Ruby LSP Addons

Ruby Version

Unknown

Ruby Version Manager

none

Installed Extensions

Click to expand
  • latex-workshop (10.5.5)
  • vscode-clangd (0.1.29)
  • vscode-language-pack-zh-hans (1.94.2024101609)
  • black-formatter (2024.4.0)
  • debugpy (2024.12.0)
  • pylint (2024.0.0)
  • python (2024.16.1)
  • vscode-pylance (2024.10.1)
  • jupyter (2024.9.1)
  • jupyter-keymap (1.1.2)
  • jupyter-renderers (1.0.19)
  • remote-ssh (0.115.0)
  • remote-ssh-edit (0.87.0)
  • cmake-tools (1.19.52)
  • hexeditor (1.10.0)
  • powershell (2024.2.2)
  • remote-explorer (0.4.3)
  • material-icon-theme (5.12.0)
  • vscode-xml (0.27.1)
  • vscode-yaml (1.15.0)
  • ruby-extensions-pack (0.1.12)
  • ruby-lsp (0.8.8)
  • sorbet-vscode-extension (0.3.37)
  • even-better-toml (0.19.2)
  • cmake (0.0.17)
  • markdown-all-in-one (3.6.2)

Ruby LSP Settings

Click to expand
Workspace
{
  "rubyVersionManager": {
    "identifier": "none"
  }
}
User
{
  "enableExperimentalFeatures": false,
  "enabledFeatures": {
    "codeActions": true,
    "diagnostics": true,
    "documentHighlights": true,
    "documentLink": true,
    "documentSymbols": true,
    "foldingRanges": true,
    "formatting": true,
    "hover": true,
    "inlayHint": true,
    "onTypeFormatting": true,
    "selectionRanges": true,
    "semanticHighlighting": true,
    "completion": true,
    "codeLens": true,
    "definition": true,
    "workspaceSymbol": true,
    "signatureHelp": true,
    "typeHierarchy": true
  },
  "featuresConfiguration": {},
  "addonSettings": {},
  "rubyVersionManager": {
    "identifier": "none"
  },
  "customRubyCommand": "",
  "formatter": "auto",
  "linters": null,
  "bundleGemfile": "",
  "testTimeout": 30,
  "branch": "",
  "pullDiagnosticsOn": "both",
  "useBundlerCompose": false,
  "bypassTypechecker": false,
  "rubyExecutablePath": "",
  "indexing": {},
  "erbSupport": true
}

Reproduction steps

  1. Start the Ruby LSP using a certain editor on Windows.
  2. Open a Ruby file.
  3. Command ruby -W0 -rjson -e 'STDERR.print("RUBY_LSP_ACTIVATION_SEPARATOR" + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + "RUBY_LSP_ACTIVATION_SEPARATOR")' failed and the LSP won't start.

When trying to run this command on PowerShell 7, I get the Error Message:

 ruby -W0 -rjson -e 'STDERR.print("RUBY_LSP_ACTIVATION_SEPARATOR" + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + "RUBY_LSP_ACTIVATION_SEPARATOR")'
-e:1:in `<main>': uninitialized constant RUBY_LSP_ACTIVATION_SEPARATOR (NameError)

STDERR.print(RUBY_LSP_ACTIVATION_SEPARATOR + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + RUBY_LSP_ACTIVATION_SEPARATOR)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ynty avatar Oct 24 '24 12:10 ynty

I've renamed the issue for easier identification (was previously "The Closed Issue #2592 Can Still be Reproduced").

andyw8 avatar Oct 24 '24 13:10 andyw8

Thank you for the bug report. We never run anything on PowerShell for activation exactly because of escaping issues, so the fact that running the command manually fails on it is expected (we always run on cmd.exe).

Can you share which errors you see in the Ruby LSP channel under the Output tab? Also, which version manager are you using (RubyInstaller or something else)?

vinistock avatar Oct 24 '24 17:10 vinistock

@vinistock

which version manager are you using (RubyInstaller or something else)?

I'm using rbenv for Windows. It is similar to the rbenv on Unix-like systems but there are still differences, for example it has no rbenv exec command, which will be run to activate LSP when rubyVersionManager.identifier is set to "rbenv", so I set it to "none".


Can you share which errors you see in the Ruby LSP channel under the Output tab?

I can only get this under Output tab:

2024-10-25 20:37:02.347 [info] (hello-ruby) Running command: `ruby -W0 -rjson -e 'STDERR.print("RUBY_LSP_ACTIVATION_SEPARATOR" + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + "RUBY_LSP_ACTIVATION_SEPARATOR")'` in d:\Code\hello-ruby using shell: undefined

And I receive an error notification:

Automatic Ruby environment activation with none failed: Command failed: ruby -W0 -rjson -e 'STDERR.print("RUBY_LSP_ACTIVATION_SEPARATOR" + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + "RUBY_LSP_ACTIVATION_SEPARATOR")'
-e:1:in `<main>': uninitialized constant RUBY_LSP_ACTIVATION_SEPARATOR (NameError)
 
STDERR.print(RUBY_LSP_ACTIVATION_SEPARATOR + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + RUBY_LSP_ACTIVATION_SEPARATOR)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Image

ynty avatar Oct 25 '24 12:10 ynty

And I receive an error notification:

Automatic Ruby environment activation with none failed: Command failed: ruby -W0 -rjson -e 'STDERR.print("RUBY_LSP_ACTIVATION_SEPARATOR" + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + "RUBY_LSP_ACTIVATION_SEPARATOR")'
-e:1:in `<main>': uninitialized constant RUBY_LSP_ACTIVATION_SEPARATOR (NameError)
 
STDERR.print(RUBY_LSP_ACTIVATION_SEPARATOR + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + RUBY_LSP_ACTIVATION_SEPARATOR)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

~~THERE IS A BAD NEWS. I cannot reproduce this error notification. The error message will become unreadable every time opening this folder in VSCode after the first time.~~

Sorry that I forgot to open VSCode in my PowerShell so rbenv init doesn't work. 🫠

ynty avatar Oct 25 '24 12:10 ynty

So, there are two issues here:

The first one is that the Windows version of rbenv doesn't provide the same commands as the Unix version. This aspect, you can probably get around by using our custom activation.

Essentially, you need to define what shell script has to be invoked to ensure that the right Ruby environment is activated.

{
  // Set the manager to custom
  "rubyLsp.rubyVersionManager": {
    "identifier": "custom",
  },

  // A shell script that will be executed before trying to invoke Ruby.
  // Invoke the Windows version of rbenv here to set the correct environment variables
  // for the Ruby version. For example, something like this
  "rubyLsp.customRubyCommand": "rbenv activate"
}

The second part, which I frankly don't understand as of right now, is the escaping issue. Based on the output you shared, we are not setting any shells to invoke the Ruby command, which should default to using cmd.exe and the escaping should work properly. It's as if, despite not setting the shell, we're somehow still invoking PowerShell under the hood.

vinistock avatar Oct 25 '24 17:10 vinistock

I published a pre-release version of the extension v0.8.11 which includes the fix in #2785.

@tn-072 can you give it a try and report back if it fixes the issue?

vinistock avatar Oct 25 '24 20:10 vinistock

I published a pre-release version of the extension v0.8.11 which includes the fix in #2785.

@tn-072 can you give it a try and report back if it fixes the issue?

@vinistock

Seems that the command was run under cmd.exe, but it still failed.

Automatic Ruby environment activation with none failed: Command failed: ruby -W0 -rjson -e 'STDERR.print("RUBY_LSP_ACTIVATION_SEPARATOR" + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + "RUBY_LSP_ACTIVATION_SEPARATOR")'
-e:1:in `<main>': uninitialized constant RUBY_LSP_ACTIVATION_SEPARATOR (NameError)

STDERR.print(RUBY_LSP_ACTIVATION_SEPARATOR + { env: ENV.to_h, yjit: !!defined?(RubyVM:: YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + RUBY_LSP_ACTIVATION_SEPARATOR)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Image Image

ynty avatar Oct 26 '24 10:10 ynty

I think setting it to cmd.exe didn't actually make exec use it under the hood, given that you're hitting the same PowerShell escaping issue. We may need to full path for it to work properly.

I'm not sure how to detect what is the default shell configured in Windows or how to find the exact location of cmd.exe. Any help from Windows users here is greatly appreciated.

vinistock avatar Oct 30 '24 18:10 vinistock

This issue is being marked as stale because there was no activity in the last 2 months

github-actions[bot] avatar Dec 30 '24 12:12 github-actions[bot]

This issue is being marked as stale because there was no activity in the last 2 months

github-actions[bot] avatar Mar 04 '25 12:03 github-actions[bot]