【问题标题】:Bundle install runs from incorrect directory捆绑安装从错误的目录运行
【发布时间】:2023-03-15 18:24:01
【问题描述】:

我正在为一些内部项目构建一个简单的基于thor 的生成器,但似乎无法从正确的目录运行bundle install

当我运行新的 [APP_NAME] 函数时,它应该创建目录和文件,然后运行 ​​bundle install 来安装应用程序所需的 gem。

生成器函数的来源:

def create
  puts "Creating application #{name}"

  directory 'application', "#{name}"

  Dir.chdir("#{Dir.pwd}/#{name}") do
    puts `bundle install`
  end
end

以及运行调用此创建方法的命令的控制台输出:

$ bundle exec bin/my_gem new test_app
Creating application test_app
      create  test_app
      create  test_app/Gemfile
      create  test_app/Guardfile
      create  test_app/README.md
      create  test_app/app/controllers
      create  test_app/app/helpers
      create  test_app/app/models
      create  test_app/app/views
Using thor (0.14.6) 
Using my_gem (0.0.1) 
Using bundler (1.1.3) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

如您所见,它正在运行bundle install,但它在我的当前目录(thorbundlermy_gem)中运行,而不是test_app 目录(guard、@ 987654332@、guard-less 等)。

运行lspwd 等其他命令会得到预期的结果:

Gemfile
Guardfile
README.md
app

/Users/davidlumley/Development/Gems/my_gem/test_app

不确定这是否有任何区别,但我使用 RVM 来管理我的红宝石。

【问题讨论】:

  • "不确定是否有任何区别,但我使用 RVM 来管理我的红宝石。" - 是的:)
  • 是的,它当然会管理 ruby​​,但在 rvm 的帮助下,您应该创建 gemset 并使用该 gemset 目录来安装 gem,您现在不会遇到任何问题..

标签: ruby gem rvm bundler thor


【解决方案1】:

听起来您的应用已经在使用 bundler 并且您遇到了 bundler-inside-bundler 问题。试试这个:

Bundler.with_clean_env do
  puts `bundle install`
end

我猜发生的事情是您的外部捆绑器将 BUNDLE_GEMFILE 环境变量设置为您应用的 Gemfile,然后您的内部捆绑器最终继承了它。

【讨论】:

    【解决方案2】:

    请试试这个,我一定能帮到你。

          rvm gemset create app_name
    
    
          rvm use ruby-1.9.2@app_name
    

    Ruby 版本你还用什么。

    然后安装捆绑器 gem

          gem install bundler.           
    

    然后捆绑安装并运行服务器..

    【讨论】:

    • 我目前正在使用新的 gemset,它安装了 thorbundler。问题不是没有安装包,而是我的生成器没有在适当的目录中运行命令,因此它使用了 wrong Gemfile。
    • 哦,我遇到了这个问题,然后我使用了这个技巧。通过命令行转到应用程序目录并一一安装所有 gem,并确保在任何 gem 之后将其安装在正确的 gemset 目录中.
    • 我正在尝试自动化,这就是我尝试以编程方式运行 bundle install 的原因,类似于 rails 在其生成器中的运行方式。