简体   繁体   中英

Version numbers with caret and tilde in composer.json

I wonder what is the difference between caret and tilde in composer.json. Can I say it like this: caret (^) lock the first and the second version number (the 1.2 in 1.2.3) and tilde(~) lock only the first version number (the 1 in 1.2.3)?

https://getcomposer.org/doc/articles/versions.md#next-significant-release-operators

From the documentation you linked:

~1.2 is equivalent to >=1.2 <2.0.0, while ~1.2.3 is equivalent to >=1.2.3 <1.3.0

^1.2.3 is equivalent to >=1.2.3 <2.0.0

The tilde depends on how many digits in the version number are given. The last digit given can vary.

The caret is almost always the better choice because it acts similarly enough to be a direct replacement ( ~1.2 is the same as ^1.2 or ^1.2.0 ), but offer better flexibility when addressing non-zero patch versions ( ^1.2.3 is NOT the same as ~1.2.3 , because the tilde version only allows updates below 1.3.0, the caret allows updates below 2.0.0).

The only reason why one would use the tilde as version requirement is if you have to deal with "zero" versions that get compatible updates. The tilde does not differ between ~0.1 and ~1.1 , in both cases it will allow updates up to the next major version number (below 1.0 or 2.0 respectively). The caret operator will disallow minor updates in this range: ^0.1 does not allow updates to 0.2, because in semantic versioning a zero-dot-something version may introduce incompatible changes when going to zero-dot-something+1.

Summary:

  • Prefer the caret operator - it's the easiest way to force minimum patch versions.
  • Prefer versions above 0.x (start with 1.0.0) and use semantic versioning for your own code.
  • For the development phase, you can use alpha , beta or rc stability together with the intended final version, ie 1.0.0-alpha1 would be a rough outline of what 1.0.0 will be in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM