<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yann PETITJEAN</title>
    <description>The latest articles on DEV Community by Yann PETITJEAN (@yann120).</description>
    <link>https://dev.to/yann120</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F162329%2F5e2cc6c6-dad4-4f07-bd62-df8ec7147c17.jpg</url>
      <title>DEV Community: Yann PETITJEAN</title>
      <link>https://dev.to/yann120</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yann120"/>
    <language>en</language>
    <item>
      <title>Make rubocop 20x faster in 5 min</title>
      <dc:creator>Yann PETITJEAN</dc:creator>
      <pubDate>Thu, 13 Aug 2020 13:24:56 +0000</pubDate>
      <link>https://dev.to/doctolib/make-rubocop-20x-faster-in-5-min-4pjo</link>
      <guid>https://dev.to/doctolib/make-rubocop-20x-faster-in-5-min-4pjo</guid>
      <description>&lt;p&gt;If you want to have your code formatted by rubocop without waiting for seconds, you are reading the right article.&lt;/p&gt;

&lt;p&gt;At Doctolib, we have a lot of rubocop rules to keep our Ruby code readable by everyone.&lt;br&gt;
But not everyone is familiar with all these rules, and fixing them by launching the rubocop script in the terminal can be a nightmare.&lt;/p&gt;

&lt;p&gt;That’s why having the rubocop extension in VScode changes your life, you have immediate feedback of your errors, and it can automatically format the code for some rules.&lt;/p&gt;
&lt;h1&gt;
  
  
  Immediate feedback? 🤔 Not so immediate
&lt;/h1&gt;

&lt;p&gt;I’ve peer coded with my colleague Lionel and I saw that he deactivated the auto format function for Ruby because it takes up to 4 seconds to save a simple file.&lt;/p&gt;

&lt;p&gt;Rather than removing this super cool feature of VScode, I tried to find a way to make it faster.&lt;/p&gt;

&lt;p&gt;And I found this gem which is underestimated in the Ruby community, and makes rubocop much faster &lt;a href="https://github.com/fohte/rubocop-daemon"&gt;rubocop-daemon&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  It’s 5 to 20 times faster to run rubocop
&lt;/h1&gt;

&lt;p&gt;I made some speed tests on big files and the difference is breathtaking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On the first save, it takes on average 2.7s with the normal rubocop for a file of 1K+ lines of code, and only 0.57s with the rubocop-daemon. It’s 5x faster!&lt;/li&gt;
&lt;li&gt;Both tools are caching the file, so if you relaunch it for the same file, it will take 1.49s with the normal rubocop, and 0.07s with rubocop-daemon. It’s 20x faster!&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  It only takes 5 minutes to set up
&lt;/h1&gt;
&lt;h3&gt;
  
  
  1. Add the gem to the Gemfile of your project
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'rubocop-daemon'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;require: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;and launch&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;bundle &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or if you don’t want to add it only on your computer, just launch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;gem &lt;span class="nb"&gt;install &lt;/span&gt;rubocop-daemon
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Launch it
&lt;/h3&gt;

&lt;p&gt;Each time you need it, start the server, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;rubocop-daemon start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The rubocop server will run in the background until you switch off your computer.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Replace the rubocop command
&lt;/h3&gt;

&lt;p&gt;Now, we want to replace the old command rubocop with the rubocop-daemon-wrapper, so you will be able to launch rubocop the same way as before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;curl https://raw.githubusercontent.com/fohte/rubocop-daemon/master/bin/rubocop-daemon-wrapper &lt;span class="nt"&gt;-o&lt;/span&gt; /tmp/rubocop-daemon-wrapper
&lt;span class="nb"&gt;sudo mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /usr/local/bin/rubocop-daemon-wrapper
&lt;span class="nb"&gt;sudo mv&lt;/span&gt; /tmp/rubocop-daemon-wrapper /usr/local/bin/rubocop-daemon-wrapper/rubocop
&lt;span class="nb"&gt;sudo chmod&lt;/span&gt; +x /usr/local/bin/rubocop-daemon-wrapper/rubocop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We now need to add the command to our terminal.&lt;/p&gt;

&lt;p&gt;Add this line at the end of your .zshrc or .bashrc file and save it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/usr/local/bin/rubocop-daemon-wrapper:&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Relaunch your terminal.&lt;/p&gt;

&lt;p&gt;Last step: Override rubocop with a symlink to rubocop-daemon-wrapper&lt;/p&gt;

&lt;p&gt;If you use rvm, run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ln&lt;/span&gt; &lt;span class="nt"&gt;-fs&lt;/span&gt; /usr/local/bin/rubocop-daemon-wrapper &lt;span class="si"&gt;$(&lt;/span&gt;which rubocop&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or if you use rbenv:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ln&lt;/span&gt; &lt;span class="nt"&gt;-fs&lt;/span&gt; /usr/local/bin/rubocop-daemon-wrapper &lt;span class="si"&gt;$(&lt;/span&gt;rbenv which rubocop&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that’s it! &lt;/p&gt;

&lt;h3&gt;
  
  
  4. Configure VScode
&lt;/h3&gt;

&lt;p&gt;You can now run rubocop as before, and VScode will use the fast rubocop.&lt;/p&gt;

&lt;p&gt;Don’t forget to add the &lt;a href="https://marketplace.visualstudio.com/items?itemName=misogi.ruby-rubocop"&gt;rubocop VScode extension&lt;/a&gt; if you don't have it.&lt;br&gt;
And activate the “format on save” and “format on paste”:&lt;/p&gt;

&lt;p&gt;Open preferences with &lt;code&gt;cmd&lt;/code&gt; + &lt;code&gt;,&lt;/code&gt; (or &lt;code&gt;ctrl&lt;/code&gt; + &lt;code&gt;,&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Search for &lt;code&gt;Format on save&lt;/code&gt; / &lt;code&gt;Format on paste&lt;/code&gt; and activate them.&lt;/p&gt;

&lt;p&gt;Ta-da, your code will be automagically formatted by rubocop at the lightning speed 🚀&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>rubocop</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
