[RUBY] Rubocop-daemon as a countermeasure for the problem that RuboCop starts slowly

This article is the 17th day article of Opt Technologies Advent Calendar 2020 This was also late. Nothing is in time (death)

The article on the 16th day is @ checche's tips for writing analysis code or implementation of statistical modeling (unpublished). The article on the 18th day is about to make horse racing AI by @___uhu (unpublished)


There is a de facto RuboCop gem as a Linter and Formatter for Ruby. It's kind of helpful, but from the perspective of running Lint/Format every time I save a file using Prettier or ESLint on the front end, it's too slow to start, but every time I save a file I wasn't ready to do it It seems that there are people in the world who have a similar sense of challenge, and I came across a tool called rubocop-daemon, so I will introduce it.

What is rubocop-daemon?

rubocop-daemon makes RuboCop faster.

That's right

RuboCop seems to be slow due to high startup cost, and as the name suggests, this gem is a library that resides in the process as a daemon and takes measures there. Reference: https://github.com/rubocop-hq/rubocop/issues/6492#issuecomment-447598459

If you want to use it at the minimum, it's like starting with rubocop-daemon start and executing rubocop with rubocop-daemon exec.

Useful with an editor

Of course, I want to make it convenient to use in the editor, but it is difficult to make rubocop-daemon exec available from the editor after doing rubocop-daemon start every time I want to play rubocop, so" Introduce rubocop-daemon-wrapper that will do" run rubocop while starting if daemon is not running "

You can't install it just by installing with gem, you need to install it while using curl by yourself. It is OK if you execute the command as follows according to this item of the document and add the location where rubocop-daemon-wrapper is placed in the PATH.

curl https://raw.githubusercontent.com/fohte/rubocop-daemon/master/bin/rubocop-daemon-wrapper -o /tmp/rubocop-daemon-wrapper
sudo mkdir -p /usr/local/bin/rubocop-daemon-wrapper
sudo mv /tmp/rubocop-daemon-wrapper /usr/local/bin/rubocop-daemon-wrapper/rubocop
sudo chmod +x /usr/local/bin/rubocop-daemon-wrapper/rubocop
export PATH="/usr/local/bin/rubocop-daemon-wrapper:$PATH"

Now you can hit rubocop to run rubocop-daemon-wrapper to launch the daemon and run rubocop. All you have to do is hit this from the editor

I say ... but it's surprisingly annoying. Introducing VS Code and Ruby Mine that I usually use

VSCode

VSCode doesn't support the ability of the vscode-ruby plugin to replace the path of the rubycop command, so you'll need to overwrite the rubocop command with a symlink to rubocop-daemon-wrapper.

The specific method is to look for the path of rubocop and replace the symlink while referring to this item in the document.

which rubocop  #rvm etc.
rbenv which rubocop # rbenv
# => <HOME>/.rvm/gems/ruby-x.y.z/bin/rubocop

ln -fs /usr/local/bin/rubocop-daemon-wrapper $HOME/.rvm/gems/ruby-x.y.z/bin/rubocop

Follow the above steps to enable formatOnSave in VS Code and use it.

RubyMine

In the first place, RubyMine requires a lot of work because it is necessary to insert a plug-in in order to process something when saving a file.

--Based on the Run RuboCop on opened file document, set Tools> External Tools to the command to run RuboCop with the following settings. image.png

-Register a macro that only executes the command set in ↑ --Register the macro registered in ↑ from Appearance & Behavior> Quick Lists (The image in ↓ only registers the macro named exec rubocop macro)

image.png

-Insert Save Actions, a plug-in that executes commands when saving. --Register the process registered in Quick Lists in the Build Actions item on the Save Actions setting screen.

image.png

At last, you can execute RuboCop auto correct when saving a file. (Please tell me if there is another easier way.)

in conclusion

That's why it was the introduction and introduction method of rubocop-daemon. I hope this helps you to lead a comfortable Ruby life.

Recommended Posts

Rubocop-daemon as a countermeasure for the problem that RuboCop starts slowly
Access with Selenium as a countermeasure for navigator.webdriver
[Java] The problem that true was returned as a result of comparing Integer with ==
Mechanism for converting to a language that the browser can recognize
A story that I finally understood Java for statement as a non-engineer
A memorandum of the FizzBuzz problem
Dispatcher Pattern (a new design pattern that solves the Visitor Pattern's Expression Problem)