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

Header must provide a Content-Length property #1723

Closed
1 task done
jcagarcia opened this issue Oct 19, 2023 · 27 comments · Fixed by #1199
Closed
1 task done

Header must provide a Content-Length property #1723

jcagarcia opened this issue Oct 19, 2023 · 27 comments · Fixed by #1199
Labels
bug Something isn't working help-wanted Extra attention is needed pinned This issue or pull request is pinned and won't be marked as stale transferred This issue was transferred from vscode-ruby-lsp

Comments

@jcagarcia
Copy link

Operating System

OSX Sonoma 14.0

Ruby version

3.2.2

Project has a bundle

  • Has bundle

Ruby version manager being used

rvm

Description

After opening a Ruby project using VSCode, I'm getting the following error when trying to initialize the Ruby LSP extension

Ruby LSP> Skipping custom bundle setup since /Users/Juancarlos.Garcia/code/my-project/.ruby-lsp/Gemfile.lock already exists and is up to date
Ruby LSP> Running bundle install for the custom bundle. This may take a while...
The Gemfile's dependencies are satisfied
Fetching gem metadata from https://rubygems.org/.......
Resolving dependencies...
Bundler attempted to update ruby-lsp but its version stayed the same
Bundler attempted to update debug but its version stayed the same
Bundle updated!
Starting Ruby LSP...
[Error - 20:47:53] Client Ruby LSP: connection to server is erroring.
Header must provide a Content-Length property.
{"resolving dependencies...\ncontent-length":"1539"}
Shutting down server.
[Error - 20:47:53] Stopping server failed
Error: Client is not running and can't be stopped. It's current state is: starting
	at $d.shutdown (/Users/Juancarlos.Garcia/.vscode/extensions/shopify.ruby-lsp-0.4.10/out/extension.js:41:8468)
	at $d.stop (/Users/Juancarlos.Garcia/.vscode/extensions/shopify.ruby-lsp-0.4.10/out/extension.js:41:8047)
	at $d.stop (/Users/Juancarlos.Garcia/.vscode/extensions/shopify.ruby-lsp-0.4.10/out/extension.js:44:38188)
	at $d.handleConnectionError (/Users/Juancarlos.Garcia/.vscode/extensions/shopify.ruby-lsp-0.4.10/out/extension.js:43:63)
	at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[Error - 20:47:53] Client Ruby LSP: connection to server is erroring.
Header must provide a Content-Length property.
{"{\"id\"":"0,\"result\":{\"capabilities\":{\"positionEncoding\":\"utf-16\",\"textDocumentSync\":{\"openClose\":true,\"change\":2},\"completionProvider\":{\"triggerCharacters\":[\"/\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\",\"K\",\"L\",\"M\",\"N\",\"O\",\"P\",\"Q\",\"R\",\"S\",\"T\",\"U\",\"V\",\"W\",\"X\",\"Y\",\"Z\"],\"completionItem\":{\"labelDetailsSupport\":true}},\"hoverProvider\":{},\"definitionProvider\":true,\"documentHighlightProvider\":true,\"documentSymbolProvider\":{\"symbolKind\":{\"value_set\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},\"hierarchicalDocumentSymbolSupport\":true},\"codeActionProvider\":{\"resolveProvider\":true},\"codeLensProvider\":{},\"documentLinkProvider\":{},\"documentFormattingProvider\":true,\"documentOnTypeFormattingProvider\":{\"firstTriggerCharacter\":\"{\",\"moreTriggerCharacter\":[\"\\n\",\"|\"]},\"foldingRangeProvider\":{\"lineFoldingOnly\":true},\"selectionRangeProvider\":true,\"semanticTokensProvider\":{\"documentSelector\":{\"scheme\":\"file\",\"language\":\"ruby\"},\"legend\":{\"tokenTypes\":[\"namespace\",\"type\",\"class\",\"enum\",\"interface\",\"struct\",\"typeParameter\",\"parameter\",\"variable\",\"property\",\"enumMember\",\"event\",\"function\",\"method\",\"macro\",\"keyword\",\"modifier\",\"comment\",\"string\",\"number\",\"regexp\",\"operator\",\"decorator\"],\"tokenModifiers\":[\"declaration\",\"definition\",\"readonly\",\"static\",\"deprecated\",\"abstract\",\"async\",\"modification\",\"documentation\",\"default_library\"]},\"range\":true,\"full\":{\"delta\":false}},\"inlayHintProvider\":{},\"diagnosticProvider\":{\"interFileDependencies\":false,\"workspaceDiagnostics\":false},\"workspaceSymbolProvider\":true}},\"jsonrpc\":\"2.0\"}Content-Length: 242"}
Shutting down server.
[Error - 20:47:53] Stopping server failed
Error: Client is not running and can't be stopped. It's current state is: starting
	at $d.shutdown (/Users/Juancarlos.Garcia/.vscode/extensions/shopify.ruby-lsp-0.4.10/out/extension.js:41:8468)
	at $d.stop (/Users/Juancarlos.Garcia/.vscode/extensions/shopify.ruby-lsp-0.4.10/out/extension.js:41:8047)
	at $d.stop (/Users/Juancarlos.Garcia/.vscode/extensions/shopify.ruby-lsp-0.4.10/out/extension.js:44:38188)
	at $d.handleConnectionError (/Users/Juancarlos.Garcia/.vscode/extensions/shopify.ruby-lsp-0.4.10/out/extension.js:43:63)
	at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
@jcagarcia jcagarcia added the bug Something isn't working label Oct 19, 2023
@vinistock
Copy link
Member

Thank you for the bug report!

It seems that some bundler output is getting mixed with the Ruby LSP's responses in STDOUT based on this line, which should only have included the content length.

{"resolving dependencies...\ncontent-length":"1539"}

Do you have any bundler configuration by any chance? I'm not sure how that bundler output ended up mixed in there - especially given that we redirect all bundler output to STDERR on the server.

@jderose9
Copy link

I'm experiencing the same issue, and also using rvm.

@vinistock vinistock added help-wanted Extra attention is needed pinned This issue or pull request is pinned and won't be marked as stale labels Oct 26, 2023
@bricedurand
Copy link

I have the same issue after migrating from the Ruby and VSCode Ruby extensions.

I did remove the old configuration (starting with "ruby.") and followed the Troubleshooting section.

@vinistock
Copy link
Member

Folks, we'll need more information to be able to understand what's going on. If someone who can reproduce the issue help debug it would help a lot.

The following script will run our custom bundle logic and capture anything that got printed to STDOUT. We need to understand what's being printed to STDOUT, because that's how the output would get mixed into responses that we send back to the editor.

# Require the Ruby LSP's custom bundle logic
require "ruby_lsp/setup_bundler"

# Require StringIO
require "stringio"

# Save the current $stdout in a variable
current_stdout = $stdout

# Set $stdout to be a StringIO so that we can capture it
$stdout = StringIO.new

# Run the Ruby LSP's custom bundle logic
_bundle_gemfile, _bundle_path = RubyLsp::SetupBundler.new(Dir.pwd, **{}).setup!

# Get everything that was printed to STDOUT
content = $stdout.string

# Restore the original STDOUT
$stdout = current_stdout

# Show contents that were printed to STDOUT
puts "*" * 80
puts "STDOUT"
puts content

@jderose9
Copy link

jderose9 commented Oct 30, 2023

@vinistock Happy to help but I don't understand what context this is supposed to run in? In the [project]/.ruby-lsp directory? Or replace something in the extension code?

@vinistock
Copy link
Member

I think just running it in a terminal inside the project's directory ought to be enough (not .ruby-lsp). If we find that nothing gets printed to STDOUT from the terminal, then the issue is likely not in the server, but in the extension instead.

@jderose9
Copy link

I don't get any STDOUT:

Ruby LSP> Skipping custom bundle setup since /Users/jason/code/my-project/.ruby-lsp/Gemfile.lock already exists and is up to date
Ruby LSP> Running bundle install for the custom bundle. This may take a while...
Ruby LSP> Command: (bundle check || bundle install) 1>&2
The Gemfile's dependencies are satisfied
********************************************************************************
STDOUT

@vinistock
Copy link
Member

Interesting...still not sure how other output is getting mixed up. Do you have any bundler configurations?

@bricedurand are you also using RVM? Trying to understand if this problem is somehow related to RVM specifically.

@jderose9
Copy link

@vinistock I didn't dig into the extension code to see what it's doing, but reading just the console output, it seems like it gets past the bundle configuration. It says it's skipping the bundle setup as that has already been completed. So I'm not sure this is bundler output. It appears that the extension has both a client and server component, and the message is output when the extension is attempting to connect to the server.

@vinistock
Copy link
Member

Based on the backtrace posted, it looks like bundler output is getting mixed into the responses we send back to the editor somehow.

{"resolving dependencies...\ncontent-length":"1539"}

That line should only contain the content-length, so that the server knows how many bytes to read from the STDIN pipe. I have no idea where the resolving dependencies... part is coming from or how that's getting mixed up. Isn't that from Bundler? Does RVM print things to stdout when you run rvm-auto-ruby?

@andyw8
Copy link
Contributor

andyw8 commented Nov 1, 2023

It looks like the message originates here, we might want to look at Bundler.ui.info to see if something else might cause it to write to stdout.

@jderose9
Copy link

jderose9 commented Nov 1, 2023

Switching to asdf did resolve this for me:

Ruby LSP> Checking if asdf is available on the path with command: /bin/zsh -ic 'asdf --version'
Ruby LSP> Discovered version manager asdf
Ruby LSP> Trying to activate Ruby environment with command: /bin/zsh -ic 'asdf exec ruby -rjson -e "printf(%{RUBY_ENV_ACTIVATE%sRUBY_ENV_ACTIVATE}, JSON.dump(ENV.to_h))"' inside directory: /Users/jason/code/myproject
Ruby LSP> Running bundle install for the custom bundle. This may take a while...
Ruby LSP> Command: (bundle check || bundle install) 1>&2
Resolving dependencies...
The Gemfile's dependencies are satisfied
Starting Ruby LSP v0.12.2...
Ruby LSP is ready

@vinistock
Copy link
Member

Thank you very much for helping diagnose this. It seems that it might be somehow related to RVM, although it's still a bit unclear why it's the only version manager that gets us into this state.

I created a fix to guarantee that we never print anything to STDOUT and always use STDERR for Ruby activation. This should hopefully make any extraneous output go to STDERR and not mess up the LSP's communication.

@vinistock
Copy link
Member

I released https://github.com/Shopify/vscode-ruby-lsp/releases/tag/v0.4.14 as a prerelease with the fix. If someone could please install the prerelease version and let us know if the fix worked, I'd really appreciate it.

@marciotoshio
Copy link

Hi, I was facing the same issue and I can confirm that 0.4.14 is working. 👍🏽
Screenshot 2023-11-12 at 20 29 37

@flooguu
Copy link

flooguu commented Nov 13, 2023

I released https://github.com/Shopify/vscode-ruby-lsp/releases/tag/v0.4.14 as a prerelease with the fix. If someone could please install the prerelease version and let us know if the fix worked, I'd really appreciate it.

Works for me, thanks!

@bricedurand
Copy link

Interesting...still not sure how other output is getting mixed up. Do you have any bundler configurations?

@bricedurand are you also using RVM? Trying to understand if this problem is somehow related to RVM specifically.

Sorry for the late reply, I am using RVM indeed.
I will install the prerelease and let you know if that works for me.

@vinistock
Copy link
Member

Happy to hear that it worked. I'll close this for now, but please let us know if you find any other issues.

@bricedurand
Copy link

bricedurand commented Nov 22, 2023

Hi @vinistock, unfortunately the pre release v0.4.14 still isn't working for me.

Here is the output I get

Ruby LSP> Trying to activate Ruby environment with command: /bin/zsh -ic 'rvm-auto-ruby -rjson -e "STDERR.printf(%{RUBY_ENV_ACTIVATE%sRUBY_ENV_ACTIVATE}, JSON.dump(ENV.to_h))"' inside directory: /Users/brice/Documents/dev/collectif-objets
Ruby LSP> Skipping custom bundle setup since /Users/brice/Documents/dev/collectif-objets/.ruby-lsp/Gemfile.lock already exists and is up to date
Ruby LSP> Running bundle install for the custom bundle. This may take a while...
Ruby LSP> Command: (bundle check || bundle install) 1>&2
Found no changes, using resolution from the lockfile
The Gemfile's dependencies are satisfied
Starting Ruby LSP v0.12.3...
[Error - 09:31:31] Client Ruby LSP: connection to server is erroring.
Header must provide a Content-Length property.
{"found no changes, using resolution from the lockfile\ncontent-length":"1539"}
Shutting down server.
[Error - 09:31:31] Stopping server failed
Error: Client is not running and can't be stopped. It's current state is: starting
	at Hd.shutdown (/Users/brice/.vscode/extensions/shopify.ruby-lsp-0.4.14/out/extension.js:41:8468)
	at Hd.stop (/Users/brice/.vscode/extensions/shopify.ruby-lsp-0.4.14/out/extension.js:41:8047)
	at Hd.stop (/Users/brice/.vscode/extensions/shopify.ruby-lsp-0.4.14/out/extension.js:44:38188)
	at Hd.handleConnectionError (/Users/brice/.vscode/extensions/shopify.ruby-lsp-0.4.14/out/extension.js:43:63)
	at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

@vinistock
Copy link
Member

@bricedurand thank you for the backtrace, I think I figured out the problem now. You must have Bundler's log level configured to debug, because you're getting debug messages like this one printed to stdout (which messes up client/server communication).

I put up a PR on the server to override the log level to silent, which I believe will fix the issue. You can also verify if this is the case by changing your log level configuration.

@bricedurand
Copy link

@vinistock It is unlikely that I have Bundler's log level set to debug, I couldn't even find in the docs how to change it.
If you look a @jcagarcia 's output (who opened the issue), it looks similar.

@vinistock
Copy link
Member

You have found no changes, using resolution from the lockfile in your backtrace.

There's only one place in the entire rubygems codebase where that string is printed, which is the point in the code that I linked to.

It uses Bundler.ui.debug, which only prints if the level is set to debug. The only way that string is getting printed is if the log level is set to debug, I don't see any alternative.

That said, there are multiple ways to configure it, one of them being the verbose flag. The resolver also accepts some environment variables

@eneagoe
Copy link

eneagoe commented Nov 30, 2023

If it helps anyone, I had the same issue resolved by updating bundler to 2.4.22

@bricedurand
Copy link

bricedurand commented Dec 11, 2023

@vinistock I checked bundle's config and the environment variables but couldn't find anything that sets the log level to debug. Tried uninstalling bundler and upgrading to the newest version but no luck either.

EDIT : I finally got it to work after doing a fresh install of mac OS

@e-smirnov735
Copy link

I have the same issue after migrating from the Ruby and VSCode Ruby extensions.

I did remove the old configuration (starting with "ruby.") and followed the Troubleshooting section.

I released https://github.com/Shopify/vscode-ruby-lsp/releases/tag/v0.4.14 as a prerelease with the fix. If someone could please install the prerelease version and let us know if the fix worked, I'd really appreciate it.

Works!

@st0012 st0012 added the transferred This issue was transferred from vscode-ruby-lsp label Mar 18, 2024
@st0012 st0012 transferred this issue from Shopify/vscode-ruby-lsp Mar 18, 2024
@thatbudakguy
Copy link

I seem to be having this same issue, but the setup is quite different:

  • using rbenv, not rvm
  • using extension version v0.7.11 with bundler 2.5.14
  • using vscode 1.91.0

The LSP starts up fine and correctly detects my shell, version manager, etc. The problem only occurs when I ask it to format a file using the VSCode command palette:

2024-07-10 13:43:35.668 [info] (geoblacklight) [Error - 1:43:35 PM] Client Ruby LSP: connection to server is erroring.
Header must provide a Content-Length property.
{"inspecting 1 file\n\n\n1 file inspected, no offenses detected\ncontent-length":"39"}
Shutting down server.
2024-07-10 13:43:35.669 [info] (geoblacklight) Shutting down Ruby LSP...

2024-07-10 13:43:35.669 [info] (geoblacklight) [Error - 1:43:35 PM] Client Ruby LSP: connection to server is erroring.
Header must provide a Content-Length property.
{"{\"id\"":"41,\"result\":null,\"jsonrpc\":\"2.0\"}Content-Length: 39"}
Shutting down server.
2024-07-10 13:43:37.670 [info] (geoblacklight) [Error - 1:43:37 PM] Stopping server timed out
2024-07-10 13:43:37.674 [info] (geoblacklight) [Error - 1:43:37 PM] Stopping server failed
2024-07-10 13:43:37.675 [info] (geoblacklight) Error: Stopping the server timed out

It looks like the output of rubocop is being mixed in, but:

  • I don't have the rubocop extension installed or configured
  • This project uses standardrb, and the LSP correctly detected that

Could this be a problem with the Standard Ruby LSP addon? I noticed that I don't encounter this problem on other projects that do use the LSP but use rubocop instead of standardrb.

@andyw8
Copy link
Contributor

andyw8 commented Jul 10, 2024

@thatbudakguy could you create a separate issue for this please? It could be due to something quite different, and there's been a lot of changes since this was originally opened.

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 pinned This issue or pull request is pinned and won't be marked as stale transferred This issue was transferred from vscode-ruby-lsp
Projects
None yet
Development

Successfully merging a pull request may close this issue.