I’m currently looking into Rails 7 and found an odd behaviour when creating a new Rails app. Before creating a Rails app a few other things need to be in place, most notably Ruby, and _a_ database. My set up is this:
The simplest way to create a new Rails app is this:
rails new app_name
You can also specify various options, including, for example
--css=tailwind, to use Tailwind for CSS. Since that’s what is used in a book I’m reading (‘Agile Web Development with Rails 7’ by Sam Ruby & Dave Thomas), I ran this:
rails new app_name --css=tailwind
Now I got an exception logged in the output, while also the exit code of the command was 0. Interesting. Here’s the command and the output (lots of output omitted for brevity):
rails new a_new_app --css=tailwind create … create Gemfile run git init from "." Initialized empty Git repository in /Users/stephan/dev/tmp/a_new_app/.git/ create app … create config/master.key append .gitignore create config/boot.rb … remove config/initializers/new_framework_defaults_7_0.rb run bundle install Fetching gem metadata from https://rubygems.org/........... Resolving dependencies.... Using rake 13.0.6 Using minitest 5.15.0 … Using rails 7.0.2 Bundle complete! 16 Gemfile dependencies, 75 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. run bundle binstubs bundler rails importmap:install … Add Tailwindcss include tags and container element in application layout insert app/views/layouts/application.html.erb insert app/views/layouts/application.html.erb insert app/views/layouts/application.html.erb … Add default Procfile.dev create Procfile.dev Ensure foreman is installed run gem install foreman from "." Successfully installed foreman-0.87.2 /Users/stephan/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/yard-0.9.27/lib/yard/rubygems/hook.rb:88:in `require': cannot load such file -- yard (LoadError) from ….rbenv/…/ruby/…/yard-0.9.27/lib/yard/rubygems/hook.rb:88:in `load_yard' from ….rbenv/…/ruby/…/yard-0.9.27/lib/yard/rubygems/hook.rb:163:in `setup' from ….rbenv/…/ruby/…/yard-0.9.27/lib/yard/rubygems/hook.rb:152:in `generate' from ….rbenv/…/ruby/…/yard-0.9.27/lib/yard/rubygems/hook.rb:63:in `block in generation_hook' from ….rbenv/…/ruby/…/yard-0.9.27/lib/yard/rubygems/hook.rb:52:in `each' from ….rbenv/…/ruby/…/yard-0.9.27/lib/yard/rubygems/hook.rb:52:in `generation_hook' from ….rbenv/…/ruby/…/request_set.rb:311:in `block in install_hooks' from ….rbenv/…/ruby/…/request_set.rb:310:in `each' from ….rbenv/…/ruby/…/request_set.rb:310:in `install_hooks' from ….rbenv/…/ruby/…/request_set.rb:209:in `install' from ….rbenv/…/ruby/…/commands/install_command.rb:210:in `install_gem' from ….rbenv/…/ruby/…/commands/install_command.rb:226:in `block in install_gems' from ….rbenv/…/ruby/…/commands/install_command.rb:219:in `each' from ….rbenv/…/ruby/…/commands/install_command.rb:219:in `install_gems' from ….rbenv/…/ruby/…/commands/install_command.rb:167:in `execute' from ….rbenv/…/ruby/…/command.rb:323:in `invoke_with_build_args' from ….rbenv/…/ruby/…/command_manager.rb:180:in `process_args' from ….rbenv/…/ruby/…/command_manager.rb:149:in `run' from ….rbenv/…/ruby/…/gem_runner.rb:53:in `run' from ….rbenv/versions/3.1.0/bin/gem:13:in `<main>' Add bin/dev to start foreman create bin/dev Compile initial Tailwind build run rails tailwindcss:build from "." + /Users/stephan/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/tailwindcss-rails-2.0.5-x86_64-darwin/exe/x86_64-darwin/tailwindcss -i /Users/stephan/dev/tmp/a_new_app/app/assets/stylesheets/application.tailwind.css -o /Users/stephan/dev/tmp/a_new_app/app/assets/builds/tailwind.css -c /Users/stephan/dev/tmp/a_new_app/config/tailwind.config.js Done in 228ms.
This is … interesting, since
yard is found, when listing matching gems:
> gem list yard *** LOCAL GEMS *** yard (0.9.27)
However, the command that is running at this time is (apparently) executed in the context of Bundler. Since
yard isn’t listed in the
Gemfile, Bundler won’t find or use it:
bundle info yard Could not find gem 'yard'.
yard, creating a new Rails app works fine and doesn’t log an exception.
While this solves the issue of getting the exception, it was still unclear why this happened in the first place.
Now, what is
YARD is a documentation generation tool for the Ruby programming language.https://github.com/lsegal/yard
Aha, looking into my
.gemrc (which is used to configure Rubygems), I found these two lines (Yes, I like to have the documentation generated 🙂):
install: --rdoc --ri --document=yri update: --rdoc --ri --document=yri
After removing the
--document=yri, creating a new Rails app worked fine even with yard being installed.
I still don’t completely understand, why this is an issue when specifying a CSS processor, but not otherwise. This may be topic for another post.
If you have an idea about why this happens, please let me know.
Did you try removing Gemfile.lock and then bundle install, it worked for me once when i faced similar issue, no need to create new app
Thanks for your comment.
Yes, deleting ‘Gemfile.lock’ and (re-) bundling is one thing that can help in a number of situations.
However, neither of the installed gems or its versions caused the problem in this case: It was the ‘.gemrc’ file (and the way it configured to creation of the documentation), which caused the issue.