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

Rails support #87

Open
philevans opened this issue Sep 17, 2018 · 104 comments
Open

Rails support #87

philevans opened this issue Sep 17, 2018 · 104 comments

Comments

@philevans
Copy link

@philevans philevans commented Sep 17, 2018

I know Rails support is only partial at the moment, but I have installed the Solargraph gem, and am using the VSCode extension, and when I am in a Rails application, I am not getting any Rails-related completions/suggestions whatsoever.

I don't even get a suggestion for the "Rails" class. I just want to make sure I am not getting the wrong end of the stick in terms of what amount of Rails support Solargraph actually has? Solargraph itself is working fine - I am getting standard Ruby suggestions no problem.

@castwide
Copy link
Owner

@castwide castwide commented Sep 17, 2018

There are a couple of things you can try.

  1. Make sure you have gem documentation installed by running yard gems.

  2. Add a .solargraph.yml file to the app's root directory (you can generate a default one by running solargraph config) and make the following change to the require section:

require:
- actioncable
- actionmailer
- actionpack
- actionview
- activejob
- activemodel
- activerecord
- activestorage
- activesupport

That should get you closer, but it's still far from perfect. Improving Rails support has been on the roadmap for a while. The amount of runtime magic involved makes it challenging, but I have some updates in the works that should help.

@andersennl
Copy link

@andersennl andersennl commented Oct 16, 2018

I hope it's fine if I add my thoughts here. First of all, thanks for your work! I've found this thread after noticing that Solargraph wouldn't know how to handle a goto definition click on has_many :users, where I'd expect it to open the User class. I'm not sure how much runtime magic is involved here but having this would be a big improvement.

@tvallois
Copy link

@tvallois tvallois commented Jun 13, 2019

Hi, any news on this?

Is there any dedicated branch for Rails where we could help you to implement it?

@castwide
Copy link
Owner

@castwide castwide commented Jun 14, 2019

Solargraph's YARD support is at a point where some of the missing Rails intellisense can be filled with a small @!parse directive. I created a gist for it here: https://gist.github.com/castwide/28b349566a223dfb439a337aea29713e

I'm looking for ways to improve it, including ways to make it easy to install and maintain. Any suggestions are welcome.

@mathieushopify
Copy link

@mathieushopify mathieushopify commented Jun 18, 2019

@castwide Using the gist above, it works well in many cases... thank you! However, in the 'activesupport' gem, there is a method blank? which is not getting picked up... is there a workaround? (.gem/ruby/2.5.3/gems/activesupport-5.2.2.1/lib/active_support/core_ext/object/blank.rb)

@castwide
Copy link
Owner

@castwide castwide commented Jun 18, 2019

@mathieushopify It works in circumstances where a type can be inferred:

obj = Object.new
obj.b # <= suggests blank?

str = 'str'
str.b # <= suggests blank?

If those don't work, make sure you have the latest gem version (0.33.0 was released this morning) and run yard gems to make sure they're documented.

Completion won't work on on unknown types, which is unfortunately the status of most of the Rails API; for example, completion will suggest ApplicationRecord.find, but doesn't know what type find returns, so it won't make any suggestions for methods chained to it.

@mathieushopify
Copy link

@mathieushopify mathieushopify commented Jun 18, 2019

Makes sense, thanks for the prompt reply! :-)

@dgutov
Copy link

@dgutov dgutov commented Jun 18, 2019

@castwide Don't most types inherit from Object, though?

Although the lack of completions it can be a useful indicator that the type is in not inferred. "Some completions" is not always better than "no completions".

@castwide
Copy link
Owner

@castwide castwide commented Jun 18, 2019

@dgutov Yeah, my personal preference is for undefined types to return no completions instead of assuming Object. It's less ambiguous about whether a type was detected.

If anyone finds it more useful to assume Object for untagged methods, we might be able to make it a configuration option.

@castwide
Copy link
Owner

@castwide castwide commented Jun 25, 2019

Version 0.34.0 introduces some new features that should add some improvements to Rails support.

Quick Start

  1. Install the latest Solargraph gem.
  2. Run solargraph bundle from your Rails app's root directory.
  3. Add the most recent version of this file: https://gist.github.com/castwide/28b349566a223dfb439a337aea29713e

How It Works

  • solargraph bundle makes sure that YARD documentation is installed for all your gems. Rails gems get parsed with RDoc first, then converted to YARD and saved in Solargraph's cache directory.
  • The YardMap detects that the code calls Bundler.require and automatically adds gem dependencies to the map, so the @!parse require directives are no longer necessary.
  • The gist file adds explicit references to a few more modules that Rails automagically includes and extends.
  • The @!override in the gist applies overloaded methods with return types to ActiveRecord::FinderMethods#find.

Known Issues

  • Although this process fills in a lot more methods, practically none of them have documented return types. The @!override directive might be a way to fix that, but I'm not sure if it'll be a long-term solution.
  • Using go-to-definition on a symbol with custom documentation should open the correct file, but it always goes to the first line. (Go-to-definition on other gems still works correctly.)
  • The RDoc-to-YARD conversion is necessary so Rails magic methods documented in RDoc will be visible in Solargraph. It might be a good idea to implement this feature as a YARD plugin.
  • If the custom documentation gives you any trouble, you can run solargraph clear && solargraph download-core to reset it.
  • There are a lot more includes and extends that could be added to the gist. Any help is appreciated.
@minkir014
Copy link

@minkir014 minkir014 commented Jun 25, 2019

When you intend to fix this issue?

Using go-to-definition on a symbol with custom documentation should open the correct file, but it always goes to the first line. (Go-to-definition on other gems still works correctly.)

I think it should go to the correct line.

@mathieushopify
Copy link

@mathieushopify mathieushopify commented Jun 25, 2019

@castwide thanks for the update!

@dirksierd
Copy link

@dirksierd dirksierd commented Jun 25, 2019

Great stuff! Thanks!

@minkir014
Copy link

@minkir014 minkir014 commented Jun 27, 2019

@castwide can you tell me that if that was an ordinary work or it have an issue??

@castwide
Copy link
Owner

@castwide castwide commented Jun 27, 2019

@minkir014 I'm sorry, I don't know what you mean.

@castwide
Copy link
Owner

@castwide castwide commented Jun 27, 2019

Gem 0.34.1 fixes some issues that occurred in certain bundler environments.

@minkir014
Copy link

@minkir014 minkir014 commented Jun 28, 2019

As I know about go to definition that it must go to a certain line that the class or any other thing is defined and you said

Using go-to-definition on a symbol with custom documentation should open the correct file, but it always goes to the first line. (Go-to-definition on other gems still works correctly.)

That means that some gems have problems with go to definition. So, when do you intend to fix this issue???

@castwide
Copy link
Owner

@castwide castwide commented Jun 29, 2019

@minkir014 I don't know when it will be fixed because I don't even know what the root cause is. When the RdocToYard component generates an RDoc store, all the code objects have nil line numbers.

@minkir014
Copy link

@minkir014 minkir014 commented Jun 29, 2019

You could file an issue against RdocToYard gem. It maybe a problem with it.

@tvallois
Copy link

@tvallois tvallois commented Sep 5, 2019

Is there a way to manage models values with Solargraph?
E.g for the User model, to have autocompletion on user.id, user.created_at, etc...

@castwide
Copy link
Owner

@castwide castwide commented Sep 7, 2019

@tvallois I'm working on a Rails convention that should be able to map model attributes from the database schema.

Right now the only other option is to document them yourself, e.g., with the @!attribute directive:

class User < ActiveRecord::Base
  # @!attribute id
  #   @return [Integer]
  # @!attribute name
  #   @return [String]
end
@Zeko369
Copy link

@Zeko369 Zeko369 commented Sep 24, 2019

@castwide Hey, I was thinking of building a script that uses annotations built by annotate gem to add types to models. You said that you're working on something similar? Have you pushed any code towards that? I would like to help and start building it myself if you haven't 👨‍💻

# == Schema Information
#
# Table name: groups
#
#  id           :bigint(8)        not null, primary key
#  name         :string
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#  namespace_id :bigint(8)        indexed
#

class Group < ApplicationRecord
  # @!attribute id
  #   @return [Integer]
  # @!attribute name
  #   @return [String]
  # @!attribute created_at
  #   @return [Date]
  # @!attribute updated_at
  #   @return [Date]
  # @!attribute namespace_id
  #   @return [Integer]

  ...
end
@castwide
Copy link
Owner

@castwide castwide commented Sep 25, 2019

@Zeko369 I don't have anything substantial. If you can build something, that would be awesome. I'm not sure how integration will work yet, but we can worry about those details after we have a working implementation.

@amirhosseinak20
Copy link

@amirhosseinak20 amirhosseinak20 commented Nov 10, 2020

Hi, I still can't up and run completion for AR attributes, i run the above script and i have this structure.
before:
Screenshot_20201110_114112
after:
Screenshot_20201110_114116

@guledali
Copy link

@guledali guledali commented Nov 12, 2020

The problem is that you have to configure the rails.rb

# The following comments fill some of the gaps in Solargraph's understanding of
# Rails apps. Since they're all in YARD, they get mapped in Solargraph but
# ignored at runtime.
#
# You can put this file anywhere in the project, as long as it gets included in
# the workspace maps. It's recommended that you keep it in a standalone file
# instead of pasting it into an existing one.
#
# @!parse
#   class ActionController::Base
#     include ActionController::MimeResponds
#     extend ActiveSupport::Callbacks::ClassMethods
#     extend AbstractController::Callbacks::ClassMethods
#   end
#   class ActiveRecord::Base
#     extend ActiveRecord::QueryMethods
#     extend ActiveRecord::FinderMethods
#     extend ActiveRecord::Associations::ClassMethods
#     extend ActiveRecord::Inheritance::ClassMethods
#     include ActiveRecord::Persistence
#   end
# @!override ActiveRecord::FinderMethods#find
#   @overload find(id)
#     @param id [Integer]
#     @return [self]
#   @overload find(list)
#     @param list [Array]
#     @return [Array<self>]
#   @overload find(*args)
#     @return [Array<self>]
#   @return [self, Array<self>]

This does not cover rails at all, so many autocompletion missing both in the controller and model, obviously you could extend that file but who has the time for that. Alternative there is free version for rubymine https://www.jetbrains.com/ruby/nextversion/

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Nov 12, 2020

@amirhosseinak20 looks like you are expecting your editor to give you completion about AR attributes on the class. They are instance level attributes, so you need to try completion on an instance. What happens if you type User.new.? Do you see the attributes then?

@amirhosseinak20
Copy link

@amirhosseinak20 amirhosseinak20 commented Nov 14, 2020

@amirhosseinak20 looks like you are expecting your editor to give you completion about AR attributes on the class. They are instance level attributes, so you need to try completion on an instance. What happens if you type User.new.? Do you see the attributes then?

When i type sth like "User.fi" it doesn't show any completion (i expect to show first), but when i type "a = User.new; a.em" it shows email.

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Nov 16, 2020

Help is on the way in progress. I've started https://github.com/iftheshoefritz/solargraph_rails which is a SG plugin that parses Annotate comments to give magical IDE help on ActiveRecord attributes. It does this without generating any local files that confuse go-to-definition. Associations will come later, but those are IMO an easier problem to solve.

It relies on an unreleased feature of Solargraph (Conventions), but if you're willing to build things from source you can use it now. I've tested it on a Rails API that is a few years old and it does what I need. The API is far from the biggest, dirtiest Rails app that I work on, so there are probably lots more issues to find, and plenty of refactoring and performance stuff to think about.

(shoutout to @castwide for implementing Conventions in master, do we have any timelines on when v.0.40 will be released?)

@pavelz
Copy link

@pavelz pavelz commented Nov 16, 2020

Help is on the way in progress. I've started [iftheshoefritz/solargraph_rails](https://github.com

on here be dragons where does one find solargraph 40.1 ? reading shoutout, want to forgo unnecessary yak shaving.
Suppose I can push version to 40.1 by hand. Not sure if that what you have done.

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Nov 16, 2020

on here be dragons where does one find solargraph 40.1 ? reading shoutout, want to forgo unnecessary yak shaving.

You have to build solargraph from source, and set the version number to that (which I chose arbitrarily, Castwide actually said the next version would be v0.40.0).

@pavelz
Copy link

@pavelz pavelz commented Nov 17, 2020

on here be dragons where does one find solargraph 40.1 ? reading shoutout, want to forgo unnecessary yak shaving.

You have to build solargraph from source, and set the version number to that (which I chose arbitrarily, Castwide actually said the next version would be v0.40.0).

Also probably better find a different name, there is already solargraph-rails, which I have installed instead of this one on the first go.

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Nov 17, 2020

Also probably better find a different name, there is already solargraph-rails

🤦 and I see it is owned by Castwide himself

@castwide
Copy link
Owner

@castwide castwide commented Nov 17, 2020

@iftheshoefritz The current solargraph-rails gem is a legacy Rails app that I've been meaning to deprecate. I'm on board with publishing your plugin under that name to stay consistent with existing plugin conventions.

@castwide
Copy link
Owner

@castwide castwide commented Nov 17, 2020

(shoutout to @castwide for implementing Conventions in master, do we have any timelines on when v.0.40 will be released?)

I delayed it while I was researching a wart in how convention changes get applied to api maps. Then I got temporarily sidetracked with other work. 🤦 I'll try to have an update and an ETA before the end of the week.

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Nov 18, 2020

@castwide Thanks! Is the convention solargraph-rails or solargraph_rails (minus or underscore)?

@RobertLowe
Copy link

@RobertLowe RobertLowe commented Nov 18, 2020

@iftheshoefritz dash imo

@castwide
Copy link
Owner

@castwide castwide commented Nov 18, 2020

Yep, solargraph-rails. Current examples include solargraph-reek and solargraph-standardrb.

@castwide
Copy link
Owner

@castwide castwide commented Dec 19, 2020

With the release of gem v0.40.0, I think we're about ready to develop a proper Rails plugin for Solargraph. My proposal is to combine the Rails supplemental file with iftheshoefritz's model annotation. This would take a lot of the maintenance out of users' hands while still allowing them to apply their own supplemental file if necessary.

@iftheshoefritz, any thoughts on this? As I said earlier, I'd be happy to yank the legacy solargraph-rails gem so we can repurpose the name. That just leaves the question of how to coordinate collaboration and maintenance.

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Dec 20, 2020

@castwide that sounds good! If you could remove solargraph-rails, I'll rename my project and upload version 0.1.1pre or some such to rubygems.org

@castwide
Copy link
Owner

@castwide castwide commented Dec 20, 2020

@iftheshoefritz is your RubyGems username also iftheshoefritz? I figure the safest way to switch is for me to make you an owner, so it doesn't get squatted mid-transfer. You can also email me so we can coordinate more directly (my email is in my profile).

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Dec 22, 2020

@castwide yes, I am iftheshoefritz there too :)

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Jan 12, 2021

The Rails plugin (and solargraph) can now be installed with a simple gem install solargraph-rails --pre and adding the plugin to .solargraph.yml. Please have a look, the more eyeballs, the more edge cases and bugs we can find.

@RobertLowe
Copy link

@RobertLowe RobertLowe commented Jan 12, 2021

@kaspergrubbe
Copy link

@kaspergrubbe kaspergrubbe commented Jan 12, 2021

Thank you, can't wait to try it out!

@gaultierq
Copy link

@gaultierq gaultierq commented Mar 15, 2021

I am struggling make the auto-completion working on active record finder methods.
I tried the following :

  • gem 'solargraph', group: :development in my Gemfile
  • ./rails.rb copied from here
  • bundle exec yard gems && bundle exec solargraph bundle

I am expecting to have the find_by suggested on my models, but I do not have them (in vim and vscode). Am I missing something ?

Screenshot 2021-03-15 at 19 26 07

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Mar 26, 2021

@gaultierq can you tell us what version of Rails you're using? That gist has worked for me in the past, but it's not working in Rails 6.1.3 for me.

Also to be clear that gist should work with or without solargraph-rails, which only adds ActiveRecord model attributes.

@gaultierq
Copy link

@gaultierq gaultierq commented Mar 26, 2021

I tried with rails 6.1.1
All other versions can be seen in the bottom right corner of my attached screenshot

@typoon
Copy link

@typoon typoon commented Jun 3, 2021

I am having the same issue as @gaultierq . Using rails 6.1.3.2 with solargraph 0.41.1 and ruby 2.7.3
When I write MyModel.f I do not get a suggestion to the find_by method (or the find method, or any of the methods that would come from ActiveRecord whatsoever)

Also, I am using the ruby.rb file from the gist mentioned in the comments above, and if I do

m = MyModel.find(1)
m.

I don't get any suggestions (even though I'd expect at least the custom methods I have created in MyModel to show up. (I do have solargraph-rails installed as well)

If I do:

m = MyModel.new
m.

Then I get completions as expected.

It seems like Solargraph is having trouble with the completions that would be generated by the use of that ruby.rb file.

I am not sure how to debug it though, so I haven't been able to provide more information than this. Any pointers?

Thanks!

@typoon
Copy link

@typoon typoon commented Jun 3, 2021

Well, I guess I got this working now. Not sure why this happens though. The way I was generating all the data needed by solargraph was like this:

solargraph clear
bundle update solargraph
bundle exec yard gems
solargraph bundle

Turns out that solargraph bundle for some reason messes things up. If I run all 4 commands above and then save the output of solargraph scan -v to a file, this is what I get when I search for ActiveRecord::Base

ActiveRecord::Base (/home/gilgamesh/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/base.rb 264)
ActiveRecord::Base | Object
...
ActiveRecord::Base (/home/gilgamesh/work/code/security-rails/config/solargraph_definitions.rb 6)
ActiveRecord::Base | ActiveRecord::FinderMethods (/home/gilgamesh/work/code/security-rails/config/solargraph_definitions.rb 7)

If I remove the solargraph bundle command and run only the first 3 ones, this is what I get in the output of `solargraph scan -v

ActiveRecord::Base (/home/gilgamesh/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/base.rb 264)
ActiveRecord::Base | ::Object
ActiveRecord::Base | ActiveModel::Naming
ActiveRecord::Base | ActiveSupport::Benchmarkable
ActiveRecord::Base | ActiveSupport::DescendantsTracker
ActiveRecord::Base | ActiveRecord::ConnectionHandling
ActiveRecord::Base | ActiveRecord::QueryCache::ClassMethods
ActiveRecord::Base | ActiveRecord::Querying
ActiveRecord::Base | ActiveRecord::Translation
ActiveRecord::Base | ActiveRecord::DynamicMatchers
ActiveRecord::Base | ActiveRecord::DelegatedType
ActiveRecord::Base | ActiveRecord::Explain
ActiveRecord::Base | ActiveRecord::Enum
ActiveRecord::Base | ActiveRecord::Delegation::DelegateCache
ActiveRecord::Base | ActiveRecord::Aggregations::ClassMethods
ActiveRecord::Base | ActiveRecord::Suppressor
ActiveRecord::Base | ActiveRecord::SignedId
ActiveRecord::Base | ActiveRecord::SecureToken
ActiveRecord::Base | ActiveRecord::Store
ActiveRecord::Base | ActiveRecord::Serialization
ActiveRecord::Base | ActiveRecord::Reflection
ActiveRecord::Base | ActiveRecord::NoTouching
ActiveRecord::Base | ActiveRecord::TouchLater
ActiveRecord::Base | ActiveRecord::Transactions
ActiveRecord::Base | ActiveRecord::NestedAttributes
ActiveRecord::Base | ActiveRecord::AutosaveAssociation
ActiveRecord::Base | ActiveModel::SecurePassword
ActiveRecord::Base | ActiveRecord::Associations
ActiveRecord::Base | ActiveRecord::Timestamp
ActiveRecord::Base | ActiveRecord::Callbacks
ActiveRecord::Base | ActiveRecord::AttributeMethods
ActiveRecord::Base | ActiveRecord::Locking::Pessimistic
ActiveRecord::Base | ActiveRecord::Locking::Optimistic
ActiveRecord::Base | ActiveRecord::Attributes
ActiveRecord::Base | ActiveRecord::CounterCache
ActiveRecord::Base | ActiveRecord::Validations
ActiveRecord::Base | ActiveRecord::Integration
ActiveRecord::Base | ActiveModel::Conversion
ActiveRecord::Base | ActiveRecord::AttributeAssignment
ActiveRecord::Base | ActiveRecord::Sanitization
ActiveRecord::Base | ActiveRecord::Scoping
ActiveRecord::Base | ActiveRecord::Inheritance
ActiveRecord::Base | ActiveRecord::ModelSchema
ActiveRecord::Base | ActiveRecord::ReadonlyAttributes
ActiveRecord::Base | ActiveRecord::Persistence
ActiveRecord::Base | ActiveRecord::Core
...
ActiveRecord::Base (/home/gilgamesh/work/code/security-rails/config/solargraph_definitions.rb 6)
ActiveRecord::Base | ActiveRecord::FinderMethods (/home/gilgamesh/work/code/security-rails/config/solargraph_definitions.rb 7)

For both cases described above, my solargraph_definitions.rb (which would be the ruby.rb file) was looking like this:

# @!parse
#   class ActiveRecord::Base
#     extend ActiveRecord::FinderMethods
#   end

The reason why it was so small was that I was trying to pinpoint where the issue was. I put back everything to the file and ran

solargraph clear
bundle update solargraph
bundle exec yard gems

Now everything works as expected :)

@iftheshoefritz
Copy link

@iftheshoefritz iftheshoefritz commented Jun 5, 2021

Sneak peak of associations coming to solargraph-rails: https://github.com/iftheshoefritz/solargraph-rails#associations-experimental. This will probably fall over with most real projects, but it is available as a prerelease gem to try it out.

@stanak
Copy link

@stanak stanak commented Jun 9, 2021

I've been trying to make solargraph rails-compatible, and no matter what I try, I can't get it to work. find method doesn't grow.
I referred to @typoon's post, but it still didn't work.
In a last hope, I lowered the version of solargraph from 0.41.1 to 0.40.0. Oh my god, it worked.
Is there something wrong with the latest version?

The actual command I entered

bundle exec solargraph clear
bundle exec yard gems --rebuild

ruby version is 2.7.2
yard version is 0.9.26
rails version is 5.2.4

@castwide
Copy link
Owner

@castwide castwide commented Jun 9, 2021

@stanak I don't know what the root cause of your specific issue is, but 0.41.2 fixes a bug that might have been causing lags or freezes in language server responses. If the problem persists, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet