rake assets:precompile undefined method directory? for nil:NilClass - ruby-on-rails-3

While trying to do an assets:precompile for a production website I've encountered this error for the first time.
rake assets:precompile undefined method directory? for nil:NilClass
I have successfully updated the website and done a assets:precompile many times before.
The full example:
# RAILS_ENV=production rake assets:precompile --trace
/usr/local/rvm/gems/ruby-1.9.2-p290#pm/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53: warning: already initialized constant WFKV_
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/usr/local/rvm/gems/ruby-1.9.2-p290#pm/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53: warning: already initialized constant WFKV_
rake aborted!
undefined method `directory?' for nil:NilClass
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
I'm looking forward to someone's insight in this one, I've been racking my brain and Googling answers for hours.

This is what happens when the file system can't find one of the assets it thinks its supposed to precompile. This happened to me when my VM could not find one of the files in my shared folder (Weird VMware issue where it appeared in the directory listing but the file did not actually exist)

I had created a symbolic link to a non-existent file by mistake in the assets/javascript directory. Removing the link fixed the problem. Note that I do have other links in the assets directory so links in general are not a problem.

In my case, I had this issue when deploying an application in Heroku.
And one of my files was symlinked to somewhere outside the GIT repository, so the file was not present int 'master'.
I then had to remove the symbolic link (rm .../file) then copy the original file (cp file1 .../file) and finally add it to the repository (git -a -m "Added a forgotten file")

Related

Running into issues installing gitlab-shell

I am trying to install Gitlab CE from source and following this installation guide.
When I attempt to install the gitlab-shell using the command provided.
sudo -u git -H bundle exec rake gitlab:shell:install REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=production SKIP_STORAGE_VALIDATION=true
I received the following stack trace error.
WARNING: This version of GitLab depends on gitlab-shell 4.1.1, but you're running Unknown. Please update gitlab-shell.
rake aborted!
Gitlab::TaskFailedError: Gitlab::TaskFailedError
/home/git/gitlab/lib/tasks/gitlab/task_helpers.rb:87:in `run_command!'
/home/git/gitlab/lib/tasks/gitlab/task_helpers.rb:164:in `clone_repo'
/home/git/gitlab/lib/tasks/gitlab/task_helpers.rb:157:in `checkout_or_clone_tag'
/home/git/gitlab/lib/tasks/gitlab/shell.rake:16:in `block (3 levels) in <top (required)>'
/usr/local/bin/bundle:22:in `load'
/usr/local/bin/bundle:22:in `<main>'
Tasks: TOP => gitlab:shell:install
What config file should I be modifying to fix my issue? Do I need to modify the Redis URL even though I am using the default configuration? By the way, this install is on an Ubuntu 16.04 server.
This might be caused by using a version of git that is too old.
To debug: append the --trace option to bundle exec to get more info on where this error is coming from and inspect the source.

Trying to precompile assets on AWS Elastic Beanstalk

2013-11-03 20:30:48,342 [INFO] (30173 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Output from script: /usr/bin/ruby1.9 /usr/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted!
Command failed with status (): [/usr/bin/ruby1.9 /usr/bin/rake assets:prec...]
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
Rake task failed to run, skipping asset compilation.
How do I precompile assets on AWS Elastic Beanstalk ?
I am getting following error
Precompilation of assets can be done at deployment time, and to do it, go to the root of your Rails project and create a .ebextensions directory with a .config file in it. You can specify precompilation there. I'm running a Rails app with Elastic Beanstalk and had problems with fonts not being shown. Here's my .ebextensions/ruby.config file:
packages:
yum:
git: []
container_commands:
01_assets:
command: RAILS_ENV=production bundle exec rake assets:precompile
leader_only: true
You can customise the system by running scripts before or after your application deployment.
Basically, you need to create a .ebextensions directory in your source code and insert your commands and customisation steps in a YAML syntax.
The documentation explains the details.
Beware that these commands are run as 'root' with no environment variable setup. If you need variables like PATH or other, it is up to you to position them.
Here is a sample I wrote for an application which required a NodeJS module installation from the source.
packages:
yum:
gcc: []
gcc-c++: []
container_commands:
10_pre_install_sqlite3:
command: "/opt/elasticbeanstalk/node-install/node-v0.10.21-linux-x64/bin/npm install sqlite3#2.1.16 --build-from-source"
cwd: "/tmp/deployment/application"
env:
HOME: "/root"
PATH: "/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin"

How to call a rake task in rspec

I am trying to invoke a rake task in in my rspec.
require "rake"
rake = Rake::Application.new
Rake.application = rake
rake.init
rake.load_rakefile
rake['rake my:task'].invoke
But i am getting error
Failure/Error: rake['rake db:migrate'].invoke
RuntimeError:
Don't know how to build task 'rake db:migrate'
Does anyone have a idea how we can invoke rake task in rspec code.
Any help would be highly appreciated.
Small namespacing issue, the task is db:migrate not rake db:migrate like the command line usage.
So changing it to this should help:
rake['db:migrate'].invoke
A simpler solution for Rails with Rspec :
In your spec_helper (or rails_helper for newer versions of rspec-rails) :
require "rake"
Rails.application.load_tasks
Then when you want to invoke your task you can do the following :
Rake::Task['my:task'].invoke
To pass in the arguments in square brackets to invoke:
rake sim:manual_review_referral_program[3,4]
becomes:
rake['sim:manual_review_referral_program'].invoke(3,4)
If your args are in an array, you can do the following:
args = [3,4]
rake['sim:manual_review_referral_program'].invoke(*args)
More info at this StackOverflow question: How to run Rake tasks from within Rake tasks?.

Capistrano can't find a JavaScript runtime

I have a problem with Capistrano deploying to a server with RVM and gemsets
executing `deploy:assets:precompile'
* executing "cd /var/app/releases/20111229233555 && rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile"
*** [err ::] rake aborted!
*** [err ::] Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
*** [deploy:update_code] rolling back
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '1.9.2#mygemset' -c 'cd /var/app/releases/20111229234715 && rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile'"
This is my Capfile
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require 'rvm/capistrano'
set :rvm_bin_path, "$HOME/.rvm/bin"
set :rvm_ruby_string, '1.9.2#mygemset'
set :rvm_type, :user
load 'deploy/assets'
When I log in as the very same user which is used by Capistrano the failing command is executed without any problems.
It seems that Capistrano is using some other environment.
Here is my ~/.rvmrc file:
rvm_gemset_create_on_use_flag=1 rvm_trust_rvmrcs_flag=1
Is there a way how to check which ruby and gemset is used at the execution time?
node.js is and V8 are installed on the system, and all needed gems are in Gemfile:
here is my rvm info:
ruby:
interpreter: "ruby"
version: "1.9.2p290"
date: "2011-07-09"
platform: "x86_64-linux"
patchlevel: "2011-07-09 revision 32553"
full_version: "ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]"
homes:
gem: "/home/capistrano/.rvm/gems/ruby-1.9.2-p290#mygemset"
ruby: "/home/capistrano/.rvm/rubies/ruby-1.9.2-p290"
binaries:
ruby: "/home/capistrano/.rvm/rubies/ruby-1.9.2-p290/bin/ruby"
irb: "/home/capistrano/.rvm/rubies/ruby-1.9.2-p290/bin/irb"
gem: "/home/capistrano/.rvm/rubies/ruby-1.9.2-p290/bin/gem"
rake: "/home/capistrano/.rvm/gems/ruby-1.9.2-p290#mygemset/bin/rake"
setting up PATH in the deploy.rb solved this issue:
default_environment['PATH'] = "/usr/local/bin:/usr/bin:/bin:/opt/bin:$PATH"
it's a Gentoo distribution and the PATH var seems to be empty when using cap deploy, so I copied the content of server PATH and now it works
This issue went away for me when I installed nodejs (using apt-get) on my server. As I stated in this Stackoverflow answer, it's the most practical choice because you don't need to include any dependencies in your Gemfile.

warbled gems not seen by Rakefile

I have s sinatra app warbled and deployed on glassfish. I have used bundler to manage dependencies and warbler has included those gems in WEB-INF/gems directory.
But when i try to run a rake task from WEB-INF directory, that task is not running.
gfish#server2:~/glassfish/domains/domain1/applications/sinatra_app/WEB-INF$ rake resque:work
(in /home/gfish/glassfish/domains/domain1/applications/sinatra_app/WEB-INF)
rake aborted!
Could not find gem 'sinatra (>= 0, runtime)' in any of the gem sources listed in your Gemfile.
/home/gfish/glassfish/domains/domain1/applications/sinatra_app/WEB-INF/Rakefile:5:in `(root)'
(See full trace by running task with --trace)
gfish#server2:~/glassfish/domains/domain1/applications/sinatra_app/WEB-INF$ **ls -l** gems/gems/
total 104
...
drwxr-xr-x 4 root root 4096 2011-02-24 14:38 sinatra-1.1.3
I finally found it. I used the following code to do it.
# Add warbled gems to the $LOAD_PATH
if ENV['RACK_ENV'] == "production"
puts "Adding warbled gems to the load path..."
local_gems_path = Dir[File.expand_path(".") + "/gems/gems/*"]
local_gems_path.each do |g|
$LOAD_PATH.unshift "#{g}/lib/"
end
end