piotrmurach/tty

Created new project, can't run `version` command for it

hasanen opened this issue · 3 comments

I created a new app with version from master. After running bin/setup I tried to run following command and got an expection

./exe/project version
/Users/hasanen/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- ../project/cli (LoadError)
        from /Users/hasanen/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from ./exe/project:5:in `<main>'

On the file exe/project in line 5 is following code:

require 'project/cli'

I changed it to following which solved the problem

require_relative '../lib/project/cli'

Is this correct way to fix this?

I would say that this isn't the correct way of fixing it but probably the best temporary way to develop your tool. Why is that? The reason that the require 'project/cli doesn't work is because it's not in the ruby lookup path. The way to add it to the path is usually done by installing the project as a gem. At this point the require will work just fine - don't take my word for it and just build a temporary gem and install it, this should make the project version command work from anywhere. For a concrete example of what I mean look at tty.gemspec.

Also anytime you run your command in development I would prefix it with bundle exec to let the bundler require all the necessary files for you.

Please let me know if you come up with the best way to make it work and we can document that in the readme as a 'gotcha'.

Tested by prefixing the command with bundle exec, works great with that! Need to change my habits and remember to use it :)

Hi Joni,

After a lot of deliberation I have changed the teletype executable and the generated executable template to automatically add the required path to ruby load paths and stop this error from happening.

I think this is much better user experience and will help to make the tools people build more resilient and may prevent people having problems and confusion and hence is worth adding.