How to use `cpl` executable from Rake if not in Gemfile

Situation:

irb(main):019> output = system('/usr/local/bundle/bin/cpl help') irb(main):020> puts output /usr/local/bundle/gems/bundler-2.3.22/lib/bundler/rubygems_integration.rb:308:in `block in replace_bin_path': can't find executable cpl for gem cpl. cpl is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
  • cpl is installed on the docker image or with gem install cpl
  • but the rails console wants to use the gemfile which is causing an error
  • I cannot add the cpl gem to the gemfile as that causes conflicts

AI provided an excellent answer:

It seems like you’re trying to run a command that is not part of your current bundle. Bundler is trying to find the cpl command in the gems specified in your Gemfile, but it’s not there.

One way to run commands that are not part of your bundle is to use Bundler.with_unbundled_env or Bundler.with_clean_env (depending on your Bundler version). This will temporarily clean your environment of bundler settings while running the block.

Here’s how you can do it:

Bundler.with_unbundled_env do
  output = system('/usr/local/bundle/bin/cpl help')
  puts output
end

This will run the cpl help command in an environment where Bundler has not modified any variables, allowing the system to find the cpl command in its usual places.