1

I have a regular Rails project that is up and running fine, using Rails 5.1.3.

I recently discovered Rails runner and tried it along the documentation (https://guides.rubyonrails.org/command_line.html#rails-runner).

However, my Rails runner seems to be incapable of coping with files. I have a file lib/manual_exports/my_export.rb, and it really exists. I tried running

rails runner lib/manual_exports/my_export.rb

and

rails runner ./lib/manual_exports/my_export.rb

I always get the response

Please specify a valid ruby command or the path of a script to run.
Run 'bin/rails runner -h' for help.

undefined local variable or method `lib' for #<Rails::Command::RunnerCommand:0x00007fb13008b310> 
# (or `.' instead of `lib')

This sounds as if runner thought my path was a Ruby command, which shouldn't be the case.

When I try reading in the file directly like

rails runner "eval(File.read 'lib/manual_exports/my_export.rb')"

or

rails runner "eval(File.read '#{Rails.root}/lib/manual_exports/my_export.rb')"

I get no answer at all, until I press ctrl+C, which gives me the stack trace

/Users/marion/.rvm/gems/ruby-2.4.2/gems/railties-5.1.3/lib/rails/commands/runner/runner_command.rb:37:in `read': No such file or directory @ rb_sysopen - lib/manual_exports/my_export.rb (Errno::ENOENT)

I tried running a command directly (rails runner 'puts "hi"'), which worked. However, it took incredibly long - about two seconds. Can anybody explain to me what is happening here? Thank you!

MDickten
  • 105
  • 1
  • 10
  • Are you 100% sure the file is there? Everything is pointing to it not being there. The long time is normal for a large rails app. It's booting up the whole rails architecture. Not meant for fast scripts. – Siim Liiser Jul 29 '20 at 12:27
  • @Siim Liiser About the file: Yes, I am. Really I am. I can include a screenshot if you want :-). I read all the bits about putting it in lib and I put it there. What I am wondering is, why does the error message say "unknown local variable" instead of "File not found"? BTW, thanks for pointing out why runner takes so long to start. Of course. It's a largish project. -- As for now, I've switched to making a rake task instead of fiddling with runner. This works :-) – MDickten Jul 30 '20 at 16:54

0 Answers0