require_tree argument must be a directory in a Rails 5 upgraded app
Asked Answered
V

5

40

I just upgraded my app from Rails 4.2.7 to Rails 5.0.0.1. I used RailsDiff to make sure I had everything covered and I believe I did. So far everything has worked well up until the loading of my app.

Now I am seeing this error:

Sprockets::ArgumentError at /
require_tree argument must be a directory

This is my application.css:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
 * files in this directory. Styles in this file should be added after the last require_* statement.
 * It is generally better to create a new file per style scope. *
 *= require_tree .
 *= require_self
 */

This is my application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

This is what the server log looks like:

Started GET "/" for ::1 at 2016-09-02 09:08:19 -0500
  ActiveRecord::SchemaMigration Load (1.5ms)  SELECT "schema_migrations".* FROM "schema_migrations"
  User Load (1.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
Processing by ProfilesController#index as HTML
  Rendering profiles/index.html.erb within layouts/application
  Profile Load (1.6ms)  SELECT "profiles".* FROM "profiles"
  Rendered profiles/index.html.erb within layouts/application (45.8ms)
Completed 500 Internal Server Error in 367ms (ActiveRecord: 6.3ms)


DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from initialize at /.rvm/gems/ruby-2.3.1@myapp/gems/better_errors-2.1.1/lib/better_errors/raised_exception.rb:7)
DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from initialize at /.rvm/gems/ruby-2.3.1myapp/gems/better_errors-2.1.1/lib/better_errors/raised_exception.rb:8)

Sprockets::ArgumentError - require_tree argument must be a directory:
  sprockets (3.7.0) lib/sprockets/directive_processor.rb:182:in `rescue in block in process_directives'
  sprockets (3.7.0) lib/sprockets/directive_processor.rb:179:in `block in process_directives'
  sprockets (3.7.0) lib/sprockets/directive_processor.rb:178:in `process_directives'

I am using no plugins of any kind. It is a fairly simple/vanilla app. The only styling is from the default scaffold.scss.

What could be causing this?

Velum answered 2/9, 2016 at 14:30 Comment(0)
V
84

I finally figured it out. So because I am doing the upgrade, RailsDiff didn't tell me that I was missing something.

So the error message wasn't incorrect, however, what I forgot to do was to create an empty directory.

In my app/assets/javascripts/cable.js, I had the following:

//= require_tree ./channels

However, I forgot to actually create that folder.

So to fix this, all I had to do was create an empty folder within app/assets/javascripts called channels. Also, because git ignores empty directories, within that newly created folder, I also had to create an empty file called .keep.

So once I did the following, everything worked like a charm:

  • Create folder: app/assets/javascripts/channels
  • Create empty file within that folder: app/assets/javascripts/channels/.keep

Everything works perfectly now.

Velum answered 3/9, 2016 at 23:23 Comment(6)
Still working in 2018 - thanks for posting this - worked for me!Lettielettish
In my case, I converted from javascript sprockets to webpacker and I forgot to remove a line referencing the old javascript location in app/assets/config/manifest.jsThoracotomy
Still working in 2021, I was upgrading from Rails 4.2.11.1 to Rails 5.0.1. Thanks for the clarification. The error in the console was wrong as you mentioned.Windward
thanks man, save my timeDisyllable
In my case, a javascript file was looking for files in my (empty) vendor folder, which I had dockerignored by mistakeBackbreaking
Thanks man, this helped. When I created my repository, I noticed git created .keep files which I deleted, as I was fine with not pushing empty folders. Never knew this would affect the production deployment.Wolverine
C
8

Faced a similar, but not the same issue. During upgrade of Rails from 5.2.3 to 5.2.4.1

$ rails s returned:

Expected to find a manifest file in `app/assets/config/manifest.js` (Sprockets::Railtie::ManifestNeededError)
But did not, please create this file and use it to link any assets that need to be rendered by your app:

Example:
  //= link_tree ../images
  //= link_directory ../javascripts .js
  //= link_directory ../stylesheets .css
and restart your server

Ok, followed the instructions, created manifest.js with the above content, then

$ rails s returned:

Sprockets::ArgumentError at / link_tree argument must be a directory

Fix:

create empty .keep file in a new images folder (which was possibly deleted at some point in the past without any immediate consequences):

app/assets/images/.keep

Churl answered 23/1, 2020 at 20:51 Comment(1)
Thank you I was not aware of these files I removed them and had a lots of problems with deployment.Quoth
E
6

The problem occurs when using rails new appname --skip-keeps flag - it still tries to require non-existing files and generally is a mistake on Rails team side.

This is just a different approach to the described problem, the main solution works perfectly;

  1. Open app/assets/javascripts/cable.js
  2. Remove autogenerated //= require_tree ./channels, from line 6

Keep your codebase as small as possible, someone skipped the .keeps for a reason.

Entity answered 26/11, 2018 at 15:16 Comment(1)
I agree with keeping codebase as small as possible. I had the issue when I upgraded rails.Ommatidium
A
1

This happened to me too because I have messed with my app files. I first created it locally and then uploaded the files to my GitHub repo and then cloned it to the original location after removing local files. That made some directories in the assets directory and vendor directory missing.

What I have done is recreate them related to the manifest.js path.

so since manifest.js path is app/assets/config/manifest.js

eg:

//= link_tree ../images

create images directory inside assets directory

//= link_tree ../../../vendor/javascript .js

create vendor directory outside app directory and create javascript directory inside vendor directory

Amyloid answered 20/6, 2023 at 16:31 Comment(0)
M
0

If you are not using ActionCable then removed all content inside app/assets/javascripts/cable.js

Marciano answered 16/6, 2020 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.