Recently we upgraded ruby to 3.2. We started getting the following error:

/Users/deepak/.rvm/gems/ruby-3.2.2@my-project/gems/pundit-2.3.0/lib/generators/rspec/templates/policy_spec.rb:1:in `template':
undefined method `exists?' for File:Class (NoMethodError)

Did you mean?  exist?

This is how we traced it and fixed it:

Solution

Updated on 20th July 2023

Pundit v2.3.1 is released and updating to the new version should solve this issue.

# Gemfile

# https://github.com/varvet/pundit
gem 'pundit', '~> 2.3.1'

OR

You can just update pundit using

bundle update pundit

Original Solution

We found that the error was raised from pundit gem from this location:

pundit-2.3.0/lib/generators/rspec/templates/policy_spec.rb

Turned out that File.exists? was first deprecated and later removed in Ruby 3.2 - Ref bf97415c

We also found that it has been fixed in this pull request varvet/pundit#754 but at the time of writing this article this change is not yet released

How we fixed it in our project:

# Gemfile

# https://github.com/varvet/pundit
gem 'pundit', git: 'https://github.com/varvet/pundit.git', ref: '5b22078'

Note that ref is optional but it is recommended to add it. Having ref avoids getting unstable changes from the main branch.

References