我对捆绑器和它生成的文件有点陌生。我有一个副本的git回购从GitHub,这是由许多人贡献,所以我很惊讶地发现,捆绑器创建了一个文件,不存在的回购,不在.gitignore列表。

因为我已经fork了它,我知道将它添加到回购中不会破坏主回购的任何东西,但如果我做一个pull请求,它会引起问题吗?

应该Gemfile。锁包含在存储库中?


当前回答

Bundler文档也解决了这个问题:

原:http://gembundler.com/v1.3/rationale.html

编辑:http://web.archive.org/web/20160309170442/http: / / bundler.io / v1.3 / rationale.html

请参阅“将代码检入版本控制”一节:

After developing your application for a while, check in the application together with the Gemfile and Gemfile.lock snapshot. Now, your repository has a record of the exact versions of all of the gems that you used the last time you know for sure that the application worked. Keep in mind that while your Gemfile lists only three gems (with varying degrees of version strictness), your application depends on dozens of gems, once you take into consideration all of the implicit requirements of the gems you depend on. This is important: the Gemfile.lock makes your application a single package of both your own code and the third-party code it ran the last time you know for sure that everything worked. Specifying exact versions of the third-party code you depend on in your Gemfile would not provide the same guarantee, because gems usually declare a range of versions for their dependencies. The next time you run bundle install on the same machine, bundler will see that it already has all of the dependencies you need, and skip the installation process. Do not check in the .bundle directory, or any of the files inside it. Those files are specific to each particular machine, and are used to persist installation options between runs of the bundle install command. If you have run bundle pack, the gems (although not the git gems) required by your bundle will be downloaded into vendor/cache. Bundler can run without connecting to the internet (or the RubyGems server) if all the gems you need are present in that folder and checked in to your source control. This is an optional step, and not recommended, due to the increase in size of your source control repository.

其他回答

同意r-dub,把它放在源代码控制中,但对我来说,真正的好处是:

在相同的环境下协作(忽略windows和linux/mac的东西)。Gemfile之前。Lock,下一个安装项目的人可能会看到各种令人困惑的错误,责怪自己,但他只是一个幸运的人,得到了下一个版本的super gem,打破了现有的依赖关系。

更糟糕的是,这发生在服务器上,得到未经测试的版本,除非遵守规则并安装准确的版本。Gemfile。Lock使这个显式,它会显式地告诉你你的版本不同。

注意:记得把东西分类,比如:development和:test

这里的其他答案是正确的:是的,你的Ruby应用程序(不是你的Ruby gem)应该包含Gemfile。锁定回购。要详细说明为什么它应该这样做,请继续阅读:

我错误地认为,每个环境(开发、测试、阶段、测试……)都进行了捆绑安装来构建自己的Gemfile.lock。我的假设是基于Gemfile。Lock不包含任何分组数据,如:test、:prod等。这种假设是错误的,因为我在一个令人痛苦的地方问题中发现了这一点。

经过进一步调查,我很困惑为什么我的Jenkins构建显示获取一个特定的宝石(ffaker, FWIW)成功,但当应用程序加载并需要ffaker时,它说文件未找到。WTF ?

更多的调查和实验显示了这两个文件的功能:

首先,它使用Gemfile。锁定去获取所有的宝石,即使是那些不会使用在这个特定的环境。然后它使用Gemfile选择在这个环境中实际使用哪些获取的宝石。

因此,尽管它在第一步中基于Gemfile获取了宝石。锁,它不包括在我的:测试环境,基于组在Gemfile。

修复(在我的情况下)是移动宝石'ffaker'从:开发组到主组,所以所有的env都可以使用它。(或者,将其添加到:development,:test,视情况而定)

Bundler文档也解决了这个问题:

原:http://gembundler.com/v1.3/rationale.html

编辑:http://web.archive.org/web/20160309170442/http: / / bundler.io / v1.3 / rationale.html

请参阅“将代码检入版本控制”一节:

After developing your application for a while, check in the application together with the Gemfile and Gemfile.lock snapshot. Now, your repository has a record of the exact versions of all of the gems that you used the last time you know for sure that the application worked. Keep in mind that while your Gemfile lists only three gems (with varying degrees of version strictness), your application depends on dozens of gems, once you take into consideration all of the implicit requirements of the gems you depend on. This is important: the Gemfile.lock makes your application a single package of both your own code and the third-party code it ran the last time you know for sure that everything worked. Specifying exact versions of the third-party code you depend on in your Gemfile would not provide the same guarantee, because gems usually declare a range of versions for their dependencies. The next time you run bundle install on the same machine, bundler will see that it already has all of the dependencies you need, and skip the installation process. Do not check in the .bundle directory, or any of the files inside it. Those files are specific to each particular machine, and are used to persist installation options between runs of the bundle install command. If you have run bundle pack, the gems (although not the git gems) required by your bundle will be downloaded into vendor/cache. Bundler can run without connecting to the internet (or the RubyGems server) if all the gems you need are present in that folder and checked in to your source control. This is an optional step, and not recommended, due to the increase in size of your source control repository.

有点晚了,但答案仍然花了我时间和外国阅读来理解这个问题。因此,我想总结一下我对Gemfile.lock的了解。

当您构建Rails应用程序时,您正在本地机器中使用特定版本的gems。如果希望避免在生产模式和其他分支中出现错误,就必须使用那个Gemfile。锁文件无处不在,并告诉捆绑器捆绑重建宝石每次它的变化。

如果Gemfile。你的生产机器上的锁已经改变了,Git不允许你Git pull,你应该写Git reset——很难避免文件改变和再次写Git pull。

我的同事和我有不同的Gemfile。锁,因为我们使用不同的平台,Windows和mac,我们的服务器是linux。

我们决定删除Gemfile。锁在repo并在git repo中创建Gemfile.lock.server,就像database.yml一样。然后,在将其部署到服务器之前,我们将Gemfile.lock.server复制到Gemfile。使用cap部署钩子锁定服务器