Fix: Rails main Rubocop Error on Windows

In an earlier post, we saw how to install and use Rails main on Windows for developing your web application. This post describes an error related to Rubocop and how to fix it.

Note: This issue was present at least till 20th April, 2024. If you’re trying after this, let’s hope it’s already been fixed but if you’re here, maybe it has not yet.

The Problem

If you run on Rails main (as of 20th April 2024, which would give you Rail 7.2.0-alpha) on Windows, you might encounter an error as below when you try to generate a controller, or scaffold, etc. Since this is a basic thing to do, you will run into the issue really early on when you start using Rails. This is what it looks like.

$ ruby bin\rails g controller home welcome
      create  app/controllers/home_controller.rb
       route  get "home/welcome"
      invoke  erb
      create    app/views/home
      create    app/views/home/welcome.html.erb
      invoke  helper
      create    app/helpers/home_helper.rb
D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/configuration.rb:138:in `system': 
    Exec format error - bin/rubocop -A --fail-level=E app/controllers/home_controller.rb app/helpers/home_helper.rb (Errno::ENOEXEC)
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/configuration.rb:138:
        in `block in apply_rubocop_autocorrect_after_generate!'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/generators.rb:317:
        in `block in run_after_generate_callback'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/generators.rb:316:
        in `each' from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/generators.rb:316:in `
        run_after_generate_callback'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/generators.rb:263:in `invoke'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/commands/generate/generate_command.rb:26:
        in `perform'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/command.rb:28:in `run'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/invocation.rb:127:in `invoke_command'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/command/base.rb:178:in `invoke_command'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor.rb:527:in `dispatch'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/command/base.rb:73:in `perform'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/command.rb:71:in `block in invoke'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/command.rb:149:in `with_argv'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/command.rb:69:in `invoke'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/bundler/gems/rails-284baa18c2c6/railties/lib/rails/commands.rb:18:in `<main>'
        from D:/Ruby33-x64/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from D:/Ruby33-x64/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from D:/Ruby33-x64/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from bin/rails:4:in `<main>'

Starting out, this can be intimidating because it feels like everything failed, and a search online won’t get you much and you’ll basically be told not to waste time trying to run Rails on Windows.

The good news is that this is not a big deal. If you look at the messages carefully, the issue is:

Exec format error - bin/rubocop -A --fail-level=E app/controllers/home_controller.rb app/helpers/home_helper.rb (Errno::ENOEXEC)

Rails 8 will ship with a Rubocop configuration and will use Rubocop to have more consistent style. The error has to do with running Rubocop. To fix it, this is what you need to do on Windows.

The Fix

The fix is basically related to setting up the binstubs for rubocop on Windows to run correctly. You need to do this:

$ bundle binstubs rubocop --force

You won’t see any output – that’s normal.

Now, try doing the same step again (I added --force so that Rails generates everything again properly).

$ ruby bin\rails g controller home welcome --force
   identical  app/controllers/home_controller.rb
       route  get "home/welcome"
      invoke  erb
       exist    app/views/home
   identical    app/views/home/welcome.html.erb
      invoke  helper
   identical    app/helpers/home_helper.rb
Inspecting 2 files
..

2 files inspected, no offenses detected

At the end of the text above, you see the output from Rubocop assuring you that 2 files were inspected and no offenses were found! Yay, it all works.

I have opened an issue on the Rails GitHub issue tracker for this issue.

comments powered by Disqus