A small quality of life improvement to the rails runner command that gives a more helpful error message when given a path as input to a file that doesn’t exist:
Before the proposed change:
$ rails runner foo.rb
Please specify a valid ruby command or the path of a script to run.
Run 'rails runner -h' for help.
undefined local variable or method `foo' for main:Object
After the proposed change:
$ rails runner foo.rb
The file foo.rb could not be found, please check and try again.
Run 'rails runner -h' for help.
rails:main
← tekin:rails-runner-spots-missing-files
opened 03:09PM - 23 Aug 22 UTC
### Summary
This updates The Rails runner command to give a more helpful err… or message when given a path as input to a file that doesn't exist.
### Before this change:
```
$ rails runner foo.rb
Please specify a valid ruby command or the path of a script to run.
Run 'rails runner -h' for help.
undefined local variable or method `foo' for main:Object
```
### After this change:
```
$ rails runner foo.rb
The file foo.rb could not be found, please check and try again.
Run 'rails runner -h' for help.
```
### Other Information
Distinguishing a valid file path from valid Ruby syntax is hard, if not impossible, so I've gone with the simplest approach I could think of that will catch a file path most of the time: the input ends with `".rb"`. This isn't a perfect approach – there's nothing stopping someone passing a script without an extension, and it's also possible to have valid Ruby syntax that ends in `".rb" – but it does feel "good enough" and will make the Rails runner slightly more user-friendly for those working with file paths without negatively impacting those that are passing in Ruby code.
I considered more sophisticated approaches to identifying potential file paths like looking for the presence of slashes and the lack of unescaped white space, but these come with their own drawbacks that make them not worth the cost in complexity and potential false-positives.
sdubois
(Sébastien Dubois)
August 24, 2022, 7:39am
2
I believe the recommendation to get attention on PRs is to submit them in the discord channel.