'Rails 7 Ruby 3.1 LoadError: cannot load such file -- net/smtp

I upgraded to Rails 7 and Ruby 3.1. While trying to run tests with rspec I got the error below. How can I fix it?

An error occurred while loading rails_helper.
Failure/Error: require File.expand_path('../config/environment', __dir__)

LoadError:
  cannot load such file -- net/smtp
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:34:in `require'
# .../gems/zeitwerk-2.5.2/lib/zeitwerk/kernel.rb:36:in `require'
# .../gems/mail-2.7.1/lib/mail.rb:9:in `<module:Mail>'
# .../gems/mail-2.7.1/lib/mail.rb:3:in `<main>'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:100:in `register'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
# .../gems/zeitwerk-2.5.2/lib/zeitwerk/kernel.rb:36:in `require'
# .../bundler/gems/rails-6a0f6c4d70b1/actionmailbox/lib/action_mailbox/mail_ext.rb:3:in `<main>'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:100:in `register'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
# .../gems/zeitwerk-2.5.2/lib/zeitwerk/kernel.rb:36:in `require'
# .../bundler/gems/rails-6a0f6c4d70b1/actionmailbox/lib/action_mailbox.rb:3:in `<main>'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:100:in `register'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
# .../gems/zeitwerk-2.5.2/lib/zeitwerk/kernel.rb:36:in `require'
# .../bundler/gems/rails-6a0f6c4d70b1/actionmailbox/lib/action_mailbox/engine.rb:9:in `<main>'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:100:in `register'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
# .../gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
# .../gems/zeitwerk-2.5.2/lib/zeitwerk/kernel.rb:36:in `require'
# ./config/application.rb:11:in `<top (required)>'
# ./config/environment.rb:2:in `require_relative'
# ./config/environment.rb:2:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `require'
# ./spec/rails_helper.rb:4:in `<top (required)>'
No examples found.


Finished in 0.00005 seconds (files took 0.57758 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples


Solution 1:[1]

UPD: on January 6th, 2022 Rails 7.0.1 was released:

The focus of this release is bring support to Ruby 3.1

Amongh other Ruby 3.1-related issues it brought a fix for this problem. So upgrade to Rails >= 7.0.1.


Add gem 'net-smtp', require: false to your Gemfile and run bundle.

Similarly I assume you may have problems with net-imap and net-pop and so have to add them until a new mail gem version is released.

Related pull requests and issues:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1