A file that should exist in all your ruby projects

Alright, the title is maybe a little more general than is strictly valid. I’m making the assumption that if you’re writing ruby then you are, like me, the sort of trend bucking nonconformist that does exactly the same thing as all the other trend bucking non-conformists.

Specifically I’m assuming that if you are using ruby you’re also using bundler (you should be. It is the way and the truth and the light. There is no salvation save through bundler), and you are using git. If you’re not using the former you should fix that (did I mention you should fix that?). If you’re not using the latter then this might be worth reading anyway, but the specific file you’re going to need is different.

Anyway, at the root of your git repo you should have a file called “.gitattributes”. That file may contain various things, but the line it needs to contain is

Gemfile.lock -merge


What’s going on here?

Well, the Gemfile.lock is basically a compiled and pinned down version of your Gemfile. You’re supposed to commit it to your repo so as to get a consistent gem environment across all the different platforms you run on.

The problem comes when you’re working with other people, or even just on different branches, and you make changes to the Gemfile (or even just the Gemfile.lock) on each of those different branches. You might get away with it, but there’s a good chance that when you merge the branches it will silently just merge your Gemfile.lock files. This is because the lock file is a text format so git assumes it’s safe to merge.

Sometimes this will cause you no problems, or will cause you problems that you notice very quickly. The problem is that often it will produce a Gemfile.lock that confuses bundler into working in some cases and much later down the line you will get really confusing bundler errors when you try to use it in a slightly different context (we’ve e.g. found that if you have two incompatible versions of a gem in your Gemfile.lock it can cause confusingly different results depending on what’s installed on your system).

So what does this .gitattributes do? Simple: It tells git never to merge your Gemfile.locks. For merge purposes it treats them as binary files and will generate a conflict at this point, thus localising the error to where the problem occurred instead of some distant point down the line.

This entry was posted in programming on by .