如何使用Foreman与子文件夹和嵌套的Gemfiles

时间:2018-02-20 17:34:38

标签: ruby-on-rails rake bundler foreman procfile

我正在开发一个大型项目,其中包含UI,Rails API以及位于monorepo中的其他一些基于Rails / Ruby的服务。我试图在项目的根部围绕Foreman设置一些Rake任务,以便在开发模式下启动项目。

repo的根包含一个带有Foreman和Rake的Gemfile和一个Procfile:

rails-api: sh -c 'cd ./rails-api && bundle && bundle exec rails server'

rails-api文件夹只是一个带有自己的Gemfile的标准Rails 5 API应用程序。

从repo root运行foreman start时,这是以下输出:

11:30:10 factory-api.1 | started with pid 38593
11:30:11 factory-api.1 | Using rake 12.3.0
11:30:11 factory-api.1 | Using bundler 1.16.1
11:30:11 factory-api.1 | Using colorize 0.8.1
11:30:11 factory-api.1 | Using dotenv 2.2.1
11:30:11 factory-api.1 | Using thor 0.19.4
11:30:11 factory-api.1 | Using foreman 0.84.0
11:30:11 factory-api.1 | Bundle complete! 4 Gemfile dependencies, 6 gems now installed.
11:30:11 factory-api.1 | Use `bundle info [gemname]` to see where a bundled gem is installed.
11:30:11 factory-api.1 | bundler: failed to load command: rails (/Users/matthew/.rbenv/versions/2.5.0/bin/rails)
11:30:11 factory-api.1 | Gem::Exception: can't find executable rails for gem railties. railties is not currently included in the bundle, perhaps you meant to add it to your Gemfile?
11:30:11 factory-api.1 |   /Users/matthew/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:458:in `block in replace_bin_path'
11:30:11 factory-api.1 |   /Users/matthew/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:478:in `block in replace_bin_path'
11:30:11 factory-api.1 |   /Users/matthew/.rbenv/versions/2.5.0/bin/rails:23:in `<top (required)>'
11:30:11 factory-api.1 | exited with code 1
11:30:11 system        | sending SIGTERM to all processes

如您所见,它捆绑了根Gemfile。从子文件夹手动捆绑工作就好了。即使手动执行sh -c 'cd ./rails-api && bundle && bundle exec rails server'也可以。

我还尝试在Procfile行中抛出pwd并返回子文件夹,但bundle仍然在根目录中运行。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

所以我发现了这个问题,这是因为我认为这篇文章并不是非常重要,但却被排除在最初的问题之外。

Foreman正在从回购根目录中的Rake任务开始捆绑:

exec('bundle exec foreman start -p Procfile.dev')

以这种方式调用确实使用了捆绑中的Foreman,但Bundler接管了所有内容。我将此行更改为:

Bundler.clean_system('foreman start -p Procfile.dev')

一切似乎都运转正常。