6. Versioning Policies
A versioning policy is merely a set of simple rules governing how version numbers are allocated. It can be very simple (e.g. the version number is a single number starting with 1 and incremented for each successive version), or it can be really strange (Knuth’s[#knuth] TeX project had version numbers: 3, 3.1, 3.14, 3.141, 3.1415; each successive version added another digit to PI).
Because RubyGems provides support for version comparisons, we want to pick a policy that works well with the RubyGems comparisons and gives the end user what they expect. We call such a policy “rational”. Also, if we call non-working policies “irrational”, then we apply a little bit of social engineering to gently prod offenders to conform.
By the way, Knuth’s versioning policy (mentioned above) is not only irrational, it is also transcendental.
Users expect to be able to specify a version constraint that gives them some reasonable expectation that new versions of a library will work with their software if the version constraint is true, and not work with their software if the version constraint is false. In other words, the perfect system will accept all compatible versions of the library and reject all incompatible versions.
Libraries change in 3 ways (well, more than 3, but stay focused here!).
- The change may be an implementation detail only and have no effect on the client software.
- The change may add new features, but do so in a way that client software written to an earlier version is still compatible.
- The change may change the public interface of the library in such a way that old software is no longer compatible.
Some examples are appropriate at this point. Suppose I have a Stack class that supports a push and a pop method.
Examples of Category 1 changes:
- Switch from an array based implementation to a linked-list based implementation.
- Provide an automatic (and transparent) backing store for large stacks.
Examples of Category 2 changes might be:
- Add a depth method to return the current depth of the stack.
- Add a top method that returns the current top of stack (without changing the stack).
- Change push so that it returns the item pushed (previously it had no usable return value).
Examples of Category 3 changes might be:
- Changes pop so that it no longer returns a value (you must use top to get the top of the stack).
- Rename the methods to push_item and pop_item.
The RationalVersioningPolicy provides the following guidelines:
- Versions shall be represented by three non-negative integers, separated by periods (e.g. 3.1.4). The first integers is the ’’’major’’’ version number, the second integer is the ’’’minor’’’ version number, and the third integer is the ’’’build’’’ number.
- A category 1 change (implementation detail) will increment the build number.
- A category 2 change (backwards compatible) will increment the minor version number and reset the build number.
- A category 3 change (incompatible) will increment the major build number and reset the minor and build numbers.
- Any ’’public’’ release of a gem should have a different version. Normally that means incrementing the build number. This means a developer can generate builds all day long for himself, but as soon as he/she makes a public release, the version must be updated.
That’s it. It’s not too difficult.
Let’s work through a project lifecycle using our Stack example from above.
- Version 0.0.1: The initial Stack class is release.
- Version 0.0.2: Switched to a linked=list implementation because it is cooler.
- Version 0.1.0: Added a depth method.
- Version 1.0.0: Added top and made pop return nil (pop used to return the old top item).
- Version 1.1.0: push now returns the value pushed (it used it return nil).
- Version 1.1.1: Fixed a bug in the linked list implementation.
- Version 1.1.2: Fixed a bug introduced in the last fix.
Client A needs a stack with basic push/pop capability. He writes to the original interface (no top), so his version constraint looks like …
gem 'stack', '>= 0.0' # However, in this case, it's sufficient just to skip the gem call, and require the library: require 'stack'
Essentially, any version is OK with Client A. An incompatible change to the library will cause him grief, but he is willing to take the chance (we call Client A optimistic).
Client B is just like Client A except for two things: (1) He uses the depth method and (2) he is worried about future incompatibilities, so he writes his version constraint like this:
gem 'stack', '>=0.1', '< 1.0' require 'stack'
The depth method was introduced in version 0.1.0, so that version or anything later is fine, as long as the version stay below version 1.0 where incompatibilities are introduced. We call Client B pessimistic because he is worried about incompatible future changes (it is OK to be pessimistic!).
Client B could have written the his pessimistic constraint like this …
gem 'stack', '~> 0.1' require 'stack'
This uses the pessimistic comparison operator and short hand for the previous version (see PessimisticVersionConstraint).
Although RubyGems provides no mechanism to enforce versioning policy, we feel that this is an important issue. And it will become more important as the number of gems increases and the number of versions proliferate. So we strongly encourage developers to follow the RationalVersioningPolicy, or at least one of the VersioningPolicyVariations.
There are several good ways to manage complex versioning manifests for gem dependencies, and these packages are available as gems:
bundler # "Semantic Versioning" http://gembundler.com/ isolate # "KISS Versioning" http://github.com/jbarnette/isolate