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

Go to definition not working with custom Gemfile #1706

Closed
1 task done
backpackerhh opened this issue Nov 17, 2023 · 5 comments · Fixed by #1203
Closed
1 task done

Go to definition not working with custom Gemfile #1706

backpackerhh opened this issue Nov 17, 2023 · 5 comments · Fixed by #1203
Labels
bug Something isn't working transferred This issue was transferred from vscode-ruby-lsp

Comments

@backpackerhh
Copy link

Operating System

Ubuntu MATE 22.04

Ruby version

3.2.2

Project has a bundle

  • Has bundle

Ruby version manager being used

rvm

Description

Some notes:

  • The custom Gemfile is successfully recognized, unlike Trying to work with a custom gemfile doesn`t work #1724
  • The project is running in Docker.
  • I've created an RVM gemset (named ruby-lsp) where all gems are installed.
  • The directory .ruby-lsp does not exist in my working directory. Maybe is the expected behavior?
  • RuboCop offenses are properly highlighted.
  • Go to definition does not work most of the time.

And that last point is the one that I'm trying to solve or at least understand how to benefit from that feature.

VSCodium version

1.84.2

VSCodium extension version

0.4.13

VSCodium settings

{
  "[ruby]": {
        "editor.defaultFormatter": "Shopify.ruby-lsp",
        "editor.formatOnSave": false,
        "editor.formatOnType": false,
        "editor.tabSize": 2,
        "editor.insertSpaces": true,
        "editor.semanticHighlighting.enabled": true,
    },
    "rubyLsp.rubyVersionManager": "rvm",
    "rubyLsp.formatter": "rubocop",
    "rubyLsp.enableExperimentalFeatures": true,
    "rubyLsp.bundleGemfile": "<absolute/path>/ruby-lsp/Gemfile",
    "rubyLsp.enabledFeatures": {
        "codeActions": true,
        "diagnostics": true,
        "documentHighlights": true,
        "documentLink": true,
        "documentSymbols": true,
        "foldingRanges": true,
        "formatting": false,
        "hover": true,
        "inlayHint": true,
        "onTypeFormatting": false,
        "selectionRanges": true,
        "semanticHighlighting": true,
        "completion": true,
        "codeLens": true,
        "definition": true,
        "workspaceSymbol": true,
        "references": true
    },
    "rubyLsp.yjit": true,
}

ruby-lsp gem version

0.12.3

Custom Gemfile content

# frozen_string_literal: true

source "https://rubygems.org"

gem "ruby-lsp"
gem "rubocop", "~> 1.57"
gem "rubocop-performance"
gem "rubocop-rspec"

gem "hanami", "~> 2.0"
gem "hanami-controller", "~> 2.0"
gem "hanami-router", "~> 2.0"
gem "hanami-validations", "~> 2.0"
gem "hanami-reloader"
gem "hanami-rspec"

gem "rom", "~> 5.3"
gem "rom-sql", "~> 3.6"

gem "dry-types", "~> 1.0", ">= 1.6.1"

Ruby LSP output in VSCodium

Ruby LSP> Trying to activate Ruby environment with command: /usr/bin/zsh -ic 'rvm-auto-ruby -rjson -e "printf(%{RUBY_ENV_ACTIVATE%sRUBY_ENV_ACTIVATE}, JSON.dump(ENV.to_h))"' inside directory: /media/david/Documents/Projects/ruby-lsp
Starting Ruby LSP v0.12.3...
Ruby LSP is ready

Project's Gemfile

Screenshot at 2023-11-17 13-36-09

Only some gems present in the custom Gemfile seems to be recorgnized. For instance, only hanami-rspec from all Hanami-related gems used in the project.

@backpackerhh backpackerhh added the bug Something isn't working label Nov 17, 2023
@vinistock
Copy link
Member

Thank you for the report. When say, "doesn't work most of the time", are you talking about go to definition for methods?

We haven't finished adding support for methods yet (#899). However, it should be working properly for classes, modules and constants. Are you able to go to definition on those?

Also, you can always find details about our supported features in the documentation.

@backpackerhh
Copy link
Author

Thank you for the report. When say, "doesn't work most of the time", are you talking about go to definition for methods?

We haven't finished adding support for methods yet (Shopify/ruby-lsp#899). However, it should be working properly for classes, modules and constants. Are you able to go to definition on those?

Also, you can always find details about our supported features in the documentation.

Thanks for your answer, @vinistock.

Yes, I was mostly talking about go to definition for methods, although I've found some unexpected behavior (for me, of course) with some classes/modules. I need to check it again so I can provide with some examples.

Anyway, I've checked the list of supported features included in the documentation link you provided and I can confirm that completion doesn't work for me with code such as the one in the image (obviously requiring classes from my project):

https://shopify.github.io/ruby-lsp/completion.gif

Additionally, I'd like to confirm some details:

  • Should native Ruby classes and modules be working right now? I guess those parts of the code written in C won't work, but what about the ones written in Ruby?
  • Why .ruby-lsp directory is not present in my project folder?

@vinistock
Copy link
Member

Anyway, I've checked the list of supported features included in the documentation link you provided and I can confirm that completion doesn't work for me with code such as the one in the image (obviously requiring classes from my project):

I don't understand what's not working on that GIF. Completion is showing all classes available in the LOAD_PATH. You're seeing results from gems and standard libraries that are present in the LOAD_PATH.

Do you mean that you expected to only see results for the one file you required? If that's the case, that is really hard to do in Ruby. Requiring any file makes all constants defined in it available globally everywhere in the project. To accurately determine if a constant is present in an exact point in the code would require typechecking the entire codebase in the exact order files are required - which would most definitely be extremely slow.

We might be able to do better sorting of the completion results. Are you looking for constants defined in your app and then finding gem results appear before them?

Should native Ruby classes and modules be working right now? I guess those parts of the code written in C won't work, but what about the ones written in Ruby?

Not yet. We still need to add support for those, which will likely require our indexer to consume RBS files so that we know which declarations exist in C code.

Why .ruby-lsp directory is not present in my project folder?

Because you're using a custom Gemfile. In that case, there's no need to create the .ruby-lsp, we can always just boot the server directly.

@backpackerhh
Copy link
Author

I don't understand what's not working on that GIF. Completion is showing all classes available in the LOAD_PATH. You're seeing results from gems and standard libraries that are present in the LOAD_PATH.

Sorry, I didn't make myself clear.

Let me show you what is currently present in my LOAD_PATH, inside the Docker container where my code is running:

(hanami console) puts $LOAD_PATH
/usr/app/lib
/usr/local/bundle/gems/bundler-2.4.22/lib
/usr/local/bundle/gems/rubocop-rspec-2.25.0/lib
/usr/local/bundle/gems/rubocop-performance-1.19.1/lib
/usr/local/bundle/gems/rubocop-factory_bot-2.24.0/lib
/usr/local/bundle/gems/rubocop-capybara-2.19.0/lib
/usr/local/bundle/gems/rubocop-1.57.2/lib
/usr/local/bundle/gems/unicode-display_width-2.5.0/lib
/usr/local/bundle/gems/ruby-progressbar-1.13.0/lib
/usr/local/bundle/gems/rubocop-ast-1.30.0/lib
/usr/local/bundle/gems/rom-sql-3.6.1/lib
/usr/local/bundle/gems/rom-factory-0.11.0/lib
/usr/local/bundle/gems/rom-5.3.0/lib
/usr/local/bundle/gems/rom-repository-5.3.0/lib
/usr/local/bundle/gems/rom-changeset-5.3.0/lib
/usr/local/bundle/gems/rom-core-5.3.0/lib
/usr/local/bundle/gems/transproc-1.1.1/lib
/usr/local/bundle/gems/rexml-3.2.6/lib
/usr/local/bundle/gems/regexp_parser-2.8.2/lib
/usr/local/bundle/gems/rainbow-3.1.1/lib
/usr/local/bundle/gems/rack-test-2.1.0/lib
/usr/local/bundle/gems/pg-1.5.4/lib
/usr/local/bundle/extensions/x86_64-linux-musl/3.2.0/pg-1.5.4
/usr/local/bundle/gems/parser-3.2.2.4/lib
/usr/local/bundle/gems/racc-1.7.3/lib
/usr/local/bundle/extensions/x86_64-linux-musl/3.2.0/racc-1.7.3
/usr/local/bundle/gems/parallel-1.23.0/lib
/usr/local/bundle/gems/language_server-protocol-3.17.0.3/lib
/usr/local/lib/ruby/gems/3.2.0/gems/json-2.6.3/lib
/usr/local/lib/ruby/gems/3.2.0/extensions/x86_64-linux-musl/3.2.0/json-2.6.3
/usr/local/bundle/gems/hanami-validations-2.0.1/lib
/usr/local/bundle/gems/hanami-rspec-2.0.1/lib
/usr/local/bundle/gems/rspec-3.12.0/lib
/usr/local/bundle/gems/rspec-mocks-3.12.6/lib
/usr/local/bundle/gems/rspec-expectations-3.12.3/lib
/usr/local/bundle/gems/rspec-core-3.12.2/lib
/usr/local/bundle/gems/rspec-support-3.12.1/lib
/usr/local/bundle/gems/hanami-router-2.0.2/lib
/usr/local/bundle/gems/mustermann-contrib-3.0.0/lib
/usr/local/bundle/gems/hansi-0.2.1/lib
/usr/local/bundle/gems/mustermann-3.0.0/lib
/usr/local/lib/ruby/gems/3.2.0/gems/ruby2_keywords-0.0.5/lib
/usr/local/bundle/gems/hanami-reloader-2.0.2/lib
/usr/local/bundle/gems/hanami-controller-2.0.2/lib
/usr/local/bundle/gems/rack-2.2.8/lib
/usr/local/bundle/gems/hanami-2.0.3/lib
/usr/local/bundle/gems/hanami-utils-2.0.3/lib
/usr/local/bundle/gems/hanami-cli-2.0.3/lib
/usr/local/bundle/gems/guard-puma-0.8.1/lib
/usr/local/bundle/gems/puma-6.4.0/lib
/usr/local/bundle/extensions/x86_64-linux-musl/3.2.0/puma-6.4.0
/usr/local/bundle/gems/nio4r-2.5.9/lib
/usr/local/bundle/extensions/x86_64-linux-musl/3.2.0/nio4r-2.5.9
/usr/local/bundle/gems/guard-compat-1.2.1/lib
/usr/local/bundle/gems/guard-2.18.1/lib
/usr/local/bundle/gems/thor-1.3.0/lib
/usr/local/bundle/gems/pry-0.14.2/lib
/usr/local/bundle/gems/method_source-1.0.0/lib
/usr/local/bundle/gems/notiffany-0.1.3/lib
/usr/local/bundle/gems/shellany-0.0.1/lib
/usr/local/bundle/gems/nenv-0.3.0/lib
/usr/local/bundle/gems/lumberjack-1.2.9/lib
/usr/local/bundle/gems/listen-3.8.0/lib
/usr/local/bundle/gems/rb-inotify-0.10.1/lib
/usr/local/bundle/gems/rb-fsevent-0.11.2/lib
/usr/local/bundle/gems/formatador-1.1.0/lib
/usr/local/bundle/gems/ffi-1.16.3/lib
/usr/local/bundle/extensions/x86_64-linux-musl/3.2.0/ffi-1.16.3
/usr/local/bundle/gems/faker-2.23.0/lib
/usr/local/bundle/gems/i18n-1.14.1/lib
/usr/local/bundle/gems/dry-validation-1.10.0/lib
/usr/local/bundle/gems/dry-transformer-1.0.1/lib
/usr/local/bundle/gems/dry-system-1.0.1/lib
/usr/local/bundle/gems/dry-struct-1.6.0/lib
/usr/local/bundle/gems/ice_nine-0.11.2/lib
/usr/local/bundle/gems/dry-schema-1.13.3/lib
/usr/local/bundle/gems/dry-types-1.7.1/lib
/usr/local/bundle/gems/dry-monitor-1.0.1/lib
/usr/local/bundle/gems/dry-logic-1.5.0/lib
/usr/local/bundle/gems/dry-logger-1.0.3/lib
/usr/local/bundle/gems/dry-initializer-3.1.1/lib
/usr/local/bundle/gems/dry-inflector-1.0.0/lib
/usr/local/bundle/gems/dry-files-1.1.0/lib
/usr/local/bundle/gems/dry-events-1.0.1/lib
/usr/local/bundle/gems/dry-configurable-1.1.0/lib
/usr/local/bundle/gems/dry-cli-1.0.0/lib
/usr/local/bundle/gems/dry-auto_inject-1.0.1/lib
/usr/local/bundle/gems/dry-core-1.0.1/lib
/usr/local/bundle/gems/zeitwerk-2.6.12/lib
/usr/local/bundle/gems/dotenv-2.8.1/lib
/usr/local/bundle/gems/diff-lcs-1.5.0/lib
/usr/local/lib/ruby/gems/3.2.0/gems/debug-1.7.1/lib
/usr/local/lib/ruby/gems/3.2.0/extensions/x86_64-linux-musl/3.2.0/debug-1.7.1
/usr/local/lib/ruby/gems/3.2.0/gems/irb-1.6.2/lib
/usr/local/lib/ruby/gems/3.2.0/gems/reline-0.3.2/lib
/usr/local/lib/ruby/gems/3.2.0/gems/io-console-0.6.0/lib
/usr/local/lib/ruby/gems/3.2.0/extensions/x86_64-linux-musl/3.2.0/io-console-0.6.0
/usr/local/bundle/gems/database_cleaner-sequel-2.0.2/lib
/usr/local/bundle/gems/sequel-5.74.0/lib
/usr/local/bundle/gems/database_cleaner-core-2.0.1/lib
/usr/local/bundle/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby
/usr/local/bundle/gems/coderay-1.1.3/lib
/usr/local/bundle/gems/bigdecimal-3.1.4/lib
/usr/local/bundle/extensions/x86_64-linux-musl/3.2.0/bigdecimal-3.1.4
/usr/local/bundle/gems/ast-2.4.2/lib
/usr/local/bundle/gems/rake-13.1.0/lib
/usr/local/lib/ruby/site_ruby/3.2.0
/usr/local/lib/ruby/site_ruby/3.2.0/x86_64-linux-musl
/usr/local/lib/ruby/site_ruby
/usr/local/lib/ruby/vendor_ruby/3.2.0
/usr/local/lib/ruby/vendor_ruby/3.2.0/x86_64-linux-musl
/usr/local/lib/ruby/vendor_ruby
/usr/local/lib/ruby/3.2.0
/usr/local/lib/ruby/3.2.0/x86_64-linux-musl

The application code is located in /usr/app/lib (first entry in previous list). Inside there are some other directories and files:

/usr/app/lib/fintech/merchants/application/create_merchant_use_case.rb
/usr/app/lib/fintech/merchants/domain/merchant_entity.rb
/usr/app/lib/fintech/merchants/infrastructure/postgres_merchant_repository.rb
...
/usr/app/lib/types.rb

What I meant before is that I cannot see any of those files when I type require "fintech/":

Screenshot at 2023-11-21 12-19-19

Not sure if ruby-lsp is able to recognize when a file is autoloaded or not, but I'm thinking that maybe is because the code within lib/fintech is supposed to be autoloaded by Hanami?

https://guides.hanamirb.org/v2.0/app/autoloading/#autoloading-in-the-lib-directory

Zeitwerk is used under the hood.

We might be able to do better sorting of the completion results. Are you looking for constants defined in your app and then finding gem results appear before them?

I don't think so.

Not yet. We still need to add support for those, which will likely require our indexer to consume RBS files so that we know which declarations exist in C code.

Because you're using a custom Gemfile. In that case, there's no need to create the .ruby-lsp, we can always just boot the server directly.

Great. Thanks for your answer.

@vinistock
Copy link
Member

Oh, you mean the require path completion not the constant completion. Yeah, we noticed it stopped working suddenly and submitted a report to the LSP VS Code package.

It might be a bug in VS Code itself given that we're returning proper completion items and it just never shows the selection to the user. Still trying to figure that one out.

@st0012 st0012 transferred this issue from Shopify/vscode-ruby-lsp Mar 18, 2024
@st0012 st0012 added the transferred This issue was transferred from vscode-ruby-lsp label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working transferred This issue was transferred from vscode-ruby-lsp
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants