Help setting up and using Rubocop on Mac

Hi all,
After several abortive attempts and wading through lots of different documentation, I am finally getting closer to running Rubocop! I have pasted below the process I followed, for the benefit of anyone else going through this learning curve.

Initial attempts to install on top of the system Ruby (2.6.0 on Mac OS 12.7) failed due to permissions errors. Further research suggested using sudo to force this was not a good idea, the consensus seems to be don’t mess with the system Ruby; it is better to use a version manager, or at least Homebrew to make a separate Ruby installation.
After initially trying the RVM version manager, I had a better a experience with rbenv. They provide a good comparison of different version managers here: Comparison of version managers · rbenv/rbenv Wiki · GitHub

  1. install Homebrew; install .pkg from Releases · Homebrew/brew · GitHub
  2. install rbenv using Homebrew
    $ brew install rbenv
  3. install Ruby Build for rbenv
    $ brew install ruby-build
  4. install Ruby 3.2.2 using ruby-build
    $ rbenv install 3.2.2
  5. Install Rubocop and Rubocop-SketchUp gems using Bundler
    $ bundle install in project dir containing Gemfile

Gemfile

    # frozen_string_literal: true
    source "https://rubygems.org"
    git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
    gem 'rubocop', '>= 0.82', '< 2.0', require: false
    gem 'rubocop-sketchup', '~> 1.4.0', require: false

I ended up with the following environment:

$ gem env

  - RUBYGEMS VERSION: 3.4.10
  - RUBY VERSION: 3.2.2 (2023-03-30 patchlevel 53) [x86_64-darwin21]
  - INSTALLATION DIRECTORY: /Users/dave/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0
  - USER INSTALLATION DIRECTORY: /Users/dave/.gem/ruby/3.2.0
  - RUBY EXECUTABLE: /Users/dave/.rbenv/versions/3.2.2/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /Users/dave/.rbenv/versions/3.2.2/bin
  - SPEC CACHE DIRECTORY: /Users/dave/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/dave/.rbenv/versions/3.2.2/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - x86_64-darwin-21
  - GEM PATHS:
     - /Users/dave/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0
     - /Users/dave/.gem/ruby/3.2.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => true
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/dave/.rbenv/versions/3.2.2/bin
     - /usr/local/Cellar/rbenv/1.3.2/libexec
     - /Users/dave/.rbenv/shims
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /Users/dave/.rvm/bin

Rubocop configuration

In my project directory I have .rubocop.yml

require: rubocop-sketchup

AllCops:
  DisabledByDefault: true

SketchupDeprecations:
  Enabled: true

SketchupPerformance:
  Enabled: true

SketchupRequirements:
  Enabled: true

SketchupSuggestions:
  Enabled: true

SketchupBugs:
  Enabled: true

Run Rubocop

In the project directory;

If rubocop Gems were installed directly, (eg by gem install rubocop):
$ rbenv exec rubocop

If rubocop Gems were installed using Bundler:
$ bundle exec rubocop

So I finally got Rubocop to run, but as I am retrofitting this to a project that has some parts of the code that goes back at least 10 years, I was immediately confronted by several 100 ‘offenses’ …

So to my first question:

To limit the offenses, I tried running as minimal as possible:
$ bundle exec rubocop -r rubocop-sketchup --only SketchupRequirements

The first issue is this, from rubocop/sketchup/cop.rb:55:

warning: Inheriting from RuboCop::Cop::Cop is deprecated. Use RuboCop::Cop::Base instead.
For more information, see v1 Upgrade Notes :: RuboCop Docs.

Does this mean that the rubocop-sketchup Gem needs to be updated? Can I just do that myself in my local copy?

I thought that @tt_su had provided a lightweight ruleset somewhere ?
EDIT: Perhaps this …
https://github.com/SketchUp/rubocop-sketchup/blob/main/sketchup-style.yml

It is warning not an exception. And it really has nothing to do with your code.

But it likely means that at some point the gem will need to be updated.

Sure you could. Use it as a test. If you get it right then you could open an issue in the GitHub - SketchUp/rubocop-sketchup repo and provide the update (Thomas might even appreciate a pull request.)

1 Like

Yes, I was correct. I remember Thomas explaining this, and found it with a forum search …

This whole post and likely topic is very informative.

I realised that most of the offences were related to the fact I had my unit tests folder inside the folder that Rubocop was inspecting and it was complaining about alternative definitions of the root namespace :blush:

Modifying < Cop to < Base did make those warnings go away, but now I seem to have more errors coming from Rubocop :thinking: I guess there is a bit more to it.

Thanks Dan. I did come across that post in my travels and it was very helpful, but haven’t tried that particular config yet - will give it a go.

When I developed code on a multi-person team, I found rubocop to be valuable for getting style consistent across the team. That greatly improved our ability for one dev to understand what another was doing. But we found the “standard” cops to hold opinions we often did not agree with, more like petty preferences elevated to rules. Like Thomthom, we pared them down to the minimum that actually improved our code.

Today, working as a team of one, I use a very minimal set.

1 Like