开发者

Whether to specify Gem versions compulsorily in Gemfile or not for rails application

开发者 https://www.devze.com 2023-04-02 20:45 出处:网络
I want to know should I specify gem version for each gem I add to Gemfile or not. Earlier with few of my projects I didn\'t specified any versions for all the gems and bundler took care of it, which w

I want to know should I specify gem version for each gem I add to Gemfile or not. Earlier with few of my projects I didn't specified any versions for all the gems and bundler took care of it, which worked quite well as well.

But recently I got to work on few project which were under development for last 6 month. In that project, many of gem versions were specified in Gemfile only and Gemfile.lock was ignored. That caused a lot headache to finally resolve version conflicts and upgrade few gems.

Also got to know that it's bad practice to remove Gemfile.lock from application version control - nice article by yehuda - http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/

So, my question is should I specify version for each and every gem I specifiy in Gemfile or just开发者_JS百科 specify name and bundler will take care of version ? What is best practice to handle this ?

UPDATE -

Updating this question to correctly specify the problem, as a Gemfile sovles many problems :)

Prob 1 - Every developer should have same version of gems. Actually adding Gemfile.lock into version control solved this problem. Developers just have to take care that they run 'bundle install'/'bundle' rather than 'bundle update' as this will update versions as well.

Prob 2 - Some gems version, if changed, brakes application code. Actually with omniauth,there are this type of issues, as API are changed from one version to another. And yes, to keep application working, versions will need to be specified for this gems.

My Prob. - So, in my gemfile, as versions for both A and B are strictly specified, and as they both depend on different versions of Z, which is there dependency, I even can't run the bundle install or bundle update. The only solution was to remove versions and let bundler to take the call. That's why I had question like - Gemfile.version_specification_mandatory? #=> true/false


I think it's best to not specify gem versions in the Gemfile. On rare occasions, it may be necessary to specify a version--e.g., when a newer version breaks your app. But specifying versi­ons for all of your gems is usually overkill. The Gemfile.lo­ck file (which you don't edit, but you do check into version control) will keep newer releases of gems from being used in your app, until you explicitly upgrade to them.


If you are using the gem for something that is available only in a specific version, you need to specify the version.

Bundler installs the latest version or uses the available version on the system if no version is specified. This works for the developer because the latest version has the feature she needs. But if the feature gets lost in the future versions of the same gem and the version is not specified in the Gemfile, all subsequent installations of the gem for different people or different machines will produce undesired effects.

I have faced these problems particularly for will paginate 3's release candidate versions.


I don't recall how Bundler worked back in 2011, but in 2021, if you don't specify a version in your Gemfile, you can't assume that Bundler will always automatically install the latest version. Instead, Bundler will try to find the right combination of versions to make all your gems compatible with one another. This could lead to some gems being downgraded, which is probably not what you want or expect. Bundler will do this silently, without a warning message, which you could argue is a broken user experience.

On the other hand, when you specify a version, if there is a conflict, Bundler will let you know and you can then decide how you want to proceed.

I recorded a screencast recently to show a real example of an older version of a gem being installed when the Gemfile didn't specify a version number.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号