Gemfile.lock 应该包含在 .gitignore 中吗?

发布于 2024-10-01 16:06:59 字数 243 浏览 6 评论 0原文

我对捆绑器及其生成的文件有点陌生。我有一份来自 GitHub 的 git 存储库副本,该存储库由许多人贡献,因此我惊讶地发现捆绑程序创建了一个存储库中不存在且不在 .gitignore 列表。

因为我已经分叉了它,所以我知道将它添加到存储库不会破坏主存储库的任何内容,但如果我执行拉取请求,会导致问题吗?

Gemfile.lock 是否应该包含在存储库中?

I'm sort of new to bundler and the files it generates. I have a copy of a git repo from GitHub that is being contributed to by many people so I was surprised to find that bundler created a file that didn't exist in the repo and wasn't in the .gitignore list.

Since I have forked it, I know adding it to the repo won't break anything for the main repo, but if I do a pull request, will it cause a problem?

Should Gemfile.lock be included in the repository?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

离笑几人歌 2024-10-08 16:07:00

当您正在开发需要具有可配置数据库适配器的开源 Rails 应用程序时,真正的问题就会发生。我正在开发 Fat Free CRM 的 Rails 3 分支。
我的偏好是 postgres,但我们希望默认数据库是 mysql2。

在这种情况下,仍然需要使用默认的 gem 集签入 Gemfile.lock,但我需要忽略我在计算机上对其所做的更改。为了实现这一点,我运行:

git update-index --assume-unchanged Gemfile.lock

并反转:

git update-index --no-assume-unchanged Gemfile.lock

Gemfile 中包含类似以下代码的内容也很有用。这将根据您的database.yml 加载适当的数据库适配器gem。

# Loads the database adapter gem based on config/database.yml (Default: mysql2)
# -----------------------------------------------------------------------------
db_gems = {"mysql2"     => ["mysql2", ">= 0.2.6"],
           "postgresql" => ["pg",     ">= 0.9.0"],
           "sqlite3"    => ["sqlite3"]}
adapter = if File.exists?(db_config = File.join(File.dirname(__FILE__),"config","database.yml"))
  db = YAML.load_file(db_config)
  # Fetch the first configured adapter from config/database.yml
  (db["production"] || db["development"] || db["test"])["adapter"]
else
  "mysql2"
end
gem *db_gems[adapter]
# -----------------------------------------------------------------------------

我不能说这是否是既定的最佳实践,但它对我来说效果很好。

The real problem happens when you are working on an open-source Rails app that needs to have a configurable database adapter. I'm developing the Rails 3 branch of Fat Free CRM.
My preference is postgres, but we want the default database to be mysql2.

In this case, Gemfile.lock still needs be checked in with the default set of gems, but I need to ignore changes that I have made to it on my machine. To accomplish this, I run:

git update-index --assume-unchanged Gemfile.lock

and to reverse:

git update-index --no-assume-unchanged Gemfile.lock

It is also useful to include something like the following code in your Gemfile. This loads the appropriate database adapter gem, based on your database.yml.

# Loads the database adapter gem based on config/database.yml (Default: mysql2)
# -----------------------------------------------------------------------------
db_gems = {"mysql2"     => ["mysql2", ">= 0.2.6"],
           "postgresql" => ["pg",     ">= 0.9.0"],
           "sqlite3"    => ["sqlite3"]}
adapter = if File.exists?(db_config = File.join(File.dirname(__FILE__),"config","database.yml"))
  db = YAML.load_file(db_config)
  # Fetch the first configured adapter from config/database.yml
  (db["production"] || db["development"] || db["test"])["adapter"]
else
  "mysql2"
end
gem *db_gems[adapter]
# -----------------------------------------------------------------------------

I can't say if this is an established best practice or not, but it works well for me.

可爱暴击 2024-10-08 16:07:00

我和我的同事有不同的Gemfile.lock,因为我们使用不同的平台,windows和mac,而我们的服务器是linux。

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

My workmates and I have different Gemfile.lock, because we use different platforms, windows and mac, and our server is linux.

We decide to remove Gemfile.lock in repo and create Gemfile.lock.server in git repo, just like database.yml. Then before deploy it on server, we copy Gemfile.lock.server to Gemfile.lock on server using cap deploy hook

陈甜 2024-10-08 16:07:00

2021 年的简单答案:
Gemfile.lock 也应该在 Rubygems 的版本控制中。现在接受的答案是 11 岁。

这里有一些推理(从评论中挑选出来):

@josevalim https://github。 com/heartcombo/devise/pull/3147#issuecomment-52193788

Gemfile.lock 应保留在存储库中,因为贡献者和开发人员应该能够分叉该项目并使用保证工作的版本运行它。

@rafaelfranca https://github.com/rails/rails/pull/18951#issuecomment -74888396

我认为即使对于插件来说,忽略锁定文件也不是一个好主意。

这意味着“git clone;bundle;rake test”序列不能保证通过,因为您的数十个依赖项之一已升级并使您的代码中断。另外,正如 @chancancode 所说,这使得平分变得更加困难。

Rails 在 git 中也有 Gemfile.lock:

Simple answer in the year 2021:
Gemfile.lock should be in the version control also for Rubygems. The accepted answer is now 11 years old.

Some reasoning here (cherry-picked from comments):

@josevalim https://github.com/heartcombo/devise/pull/3147#issuecomment-52193788

The Gemfile.lock should stay in the repository because contributors and developers should be able to fork the project and run it using versions that are guaranteed to work.

@rafaelfranca https://github.com/rails/rails/pull/18951#issuecomment-74888396

I don't think it is a good idea to ignore the lock file even for plugins.

This mean that a "git clone; bundle; rake test" sequence is not guarantee to be passing because one of yours dozens of dependencies were upgraded and made your code break. Also, as @chancancode said, it make a lot harder to bisect.

Also Rails has Gemfile.lock in git:

祁梦 2024-10-08 16:07:00

同意 r-dub 的观点,将其保留在源代码控制中,但对我来说,真正的好处是:

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

更糟糕的是,这种情况发生在服务器上,除非受到纪律处分并安装确切的版本,否则会获得未经测试的版本。 Gemfile.lock 使这一点变得明确,并且它会明确告诉您您的版本不同。

注意:记住将内容分组,如:开发和:测试

Agreeing with r-dub, keep it in source control, but to me, the real benefit is this:

collaboration in identical environments (disregarding the windohs and linux/mac stuff). Before Gemfile.lock, the next dude to install the project might see all kinds of confusing errors, blaming himself, but he was just that lucky guy getting the next version of super gem, breaking existing dependencies.

Worse, this happened on the servers, getting untested version unless being disciplined and install exact version. Gemfile.lock makes this explicit, and it will explicitly tell you that your versions are different.

Note: remember to group stuff, as :development and :test

仅冇旳回忆 2024-10-08 16:07:00

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

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

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

请参阅“将代码签入版本控制”部分:

开发应用程序一段时间后,签入
应用程序以及 Gemfile 和 Gemfile.lock 快照。现在,
您的存储库记录了所有 gem 的确切版本
您上次使用时您确定该应用程序
工作了。请记住,虽然您的 Gemfile 仅列出了三个 gem
(版本严格程度不同),您的应用程序取决于
在几十种宝石上,一旦你考虑到所有的
您所依赖的 gem 的隐式要求。

这很重要:Gemfile.lock 使您的应用程序成为一个单一的
您自己的代码和上次运行的第三方代码的包
到了你确定一切正常的时候了。指定准确的
您在 Gemfile 中依赖的第三方代码的版本将
不提供相同的保证,因为宝石通常声明一个范围
其依赖项的版本。

下次您在同一台计算机上运行捆绑安装时,捆绑程序将
看到它已经拥有您需要的所有依赖项,然后跳过
安装过程。

不要检入 .bundle 目录或其中的任何文件。
这些文件特定于每台特定机器,用于
在捆绑安装运行之间保留安装选项
命令。

如果你运行了bundle pack,gems(虽然不是git gems)
您的捆绑包所需的内容将被下载到供应商/缓存中。捆绑器
如果满足以下条件,则无需连接到互联网(或 RubyGems 服务器)即可运行
您需要的所有宝石都存在于该文件夹中并签入到
你的源代码控制。这是一个可选步骤,不推荐,
由于源代码控制存储库的大小增加。

The Bundler docs address this question as well:

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

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

See the section called "Checking Your Code into Version Control":

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.

兔姬 2024-10-08 16:07:00

没有 Gemfile.lock 意味着:

  • 新贡献者无法运行测试,因为奇怪的事情会失败,因此他们不会贡献或获得失败的 PR...糟糕的第一次体验。
  • 您将无法返回到斧头年前的项目并修复错误,而无需更新/重写项目 ->

如果您丢失了本地 Gemfile.lock , 始终检查 Gemfile.lock,如果您想更彻底,请让 travis 删除它 https://grosser.it/2015/08/14/check-in-your-gemfile-lock/

No Gemfile.lock means:

  • new contributors cannot run tests because weird things fail, so they won't contribute or get failing PRs ... bad first experience.
  • you cannot go back to a x year old project and fix a bug without having to update/rewrite the project if you lost your local Gemfile.lock

-> Always check in Gemfile.lock, make travis delete it if you want to be extra thorough https://grosser.it/2015/08/14/check-in-your-gemfile-lock/

笑忘罢 2024-10-08 16:07:00

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

当您构建 Rails 应用程序时,您将在本地计算机中使用某些版本的 gem。如果您想避免生产模式和其他分支中的错误,则必须在各处使用该 Gemfile.lock 文件,并告诉捆绑器在每次更改时bundle 重建 gem。

如果 Gemfile.lock 在您的生产机器上发生了更改,并且 Git 不允许您 git pull,您应该将 git reset --hard 写入避免文件更改并再次写入 git pull 。

A little late to the party, but answers still took me time and foreign reads to understand this problem. So I want to summarize what I have find out about the Gemfile.lock.

When you are building a Rails App, you are using certain versions of gems in your local machine. If you want to avoid errors in the production mode and other branches, you have to use that one Gemfile.lock file everywhere and tell bundler to bundle for rebuilding gems every time it changes.

If Gemfile.lock has changed on your production machine and Git doesn't let you git pull, you should write git reset --hard to avoid that file change and write git pull again.

和我恋爱吧 2024-10-08 16:07:00

这里的其他答案是正确的:是的,您的 Ruby 应用程序(不是您的 Ruby gem)应该在存储库中包含 Gemfile.lock 。要详细了解为什么应该这样做,请继续阅读:

我错误地认为每个环境(开发、测试、登台、产品...)都执行了捆绑安装 构建自己的 Gemfile.lock。我的假设是基于 Gemfile.lock 不包含任何分组数据的事实,例如 :test、:prod 等。这个假设是错误的,正如我在一个痛苦的本地问题中发现的那样。

经过仔细调查,我很困惑为什么我的 Jenkins 构建显示成功获取特定的 gem(ffaker,FWIW),但是当应用程序加载并需要 ffaker 时,它说找不到文件。搞什么?

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

首先,它使用 Gemfile.lock 来获取所有 gem,甚至是那些不会在该特定环境中使用的 gem。 然后它使用 Gemfile 来选择在这个环境中实际使用那些获取的 gem。

因此,尽管它在第一步中基于 Gemfile.lock 获取了 gem,但它并未包含在我的 :test 环境中(基于 Gemfile 中的组)。

修复(在我的例子中)是将 gem 'ffaker' 从 :development 组移动到主组,这样所有的环境都可以使用它。 (或者,根据情况仅将其添加到 :development、:test 中)

The other answers here are correct: Yes, your Ruby app (not your Ruby gem) should include Gemfile.lock in the repo. To expand on why it should do this, read on:

I was under the mistaken notion that each env (development, test, staging, prod...) each did a bundle install to build their own Gemfile.lock. My assumption was based on the fact that Gemfile.lock does not contain any grouping data, such as :test, :prod, etc. This assumption was wrong, as I found out in a painful local problem.

Upon closer investigation, I was confused why my Jenkins build showed fetching a particular gem (ffaker, FWIW) successfully, but when the app loaded and required ffaker, it said file not found. WTF?

A little more investigation and experimenting showed what the two files do:

First it uses Gemfile.lock to go fetch all the gems, even those that won't be used in this particular env. Then it uses Gemfile to choose which of those fetched gems to actually use in this env.

So, even though it fetched the gem in the first step based on Gemfile.lock, it did NOT include in my :test environment, based on the groups in Gemfile.

The fix (in my case) was to move gem 'ffaker' from the :development group to the main group, so all env's could use it. (Or, add it only to :development, :test, as appropriate)

静若繁花 2024-10-08 16:06:59

2022 年更新,来自 TrinitronX

快进到 2021 年,现在Bundler 文档< /a> [web archive] 现在说将 Gemfile.lock 提交到 gem 中... ́_(ツ)_/́ 我想这对于开发人员来说很有意义,并且在开始项目时易于使用。但是,现在 CI 作业需要确保删除任何杂散的 Gemfile.lock 文件以针对其他版本进行测试。

遗留答案 ~2010

假设您没有编写 ruby​​gem,Gemfile.lock 应该位于您的存储库中。它用作所有所需 gem 及其依赖项的快照。这样,bundler 就不必在每次部署等时重新计算所有 gem 依赖项。

来自下面的牛仔编码的评论:

如果您正在处理 gem,请不要签入您的 Gemfile.lock。如果您正在开发 Rails 应用程序,请检查您的 Gemfile.lock。

这是一篇不错的文章解释锁定文件是什么。

Update for 2022 from TrinitronX

Fast-forward to 2021 and now Bundler docs [web archive] now say to commit the Gemfile.lock inside a gem... ¯_(ツ)_/¯ I guess it makes sense for developers and ease of use when starting on a project. However, now CI jobs need to be sure to remove any stray Gemfile.lock files to test against other versions.

Legacy answer ~2010

Assuming you're not writing a rubygem, Gemfile.lock should be in your repository. It's used as a snapshot of all your required gems and their dependencies. This way bundler doesn't have to recalculate all the gem dependencies each time you deploy, etc.

From cowboycoded's comment below:

If you are working on a gem, then DO NOT check in your Gemfile.lock. If you are working on a Rails app, then DO check in your Gemfile.lock.

Here's a nice article explaining what the lock file is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文