What is ~ (til) and (circumflex accent) in the Composer versioning scheme for?

Asked

Viewed 732 times

9

In some cases, I see that some libraries that use the Composer, place ~ or ^ at the beginning of the library version, thus:

"orchestra/testbench" : "^3.6"

Or

"phpunit/phpunit"    : "~7.0"

I wanted to know what is the purpose of each of them.

  • 5

    To whom it concerns, follows the documentation, I only ask, please, not just copy and paste as an answer.

  • 1

    @Andersoncarloswoss you got the spirit of it. I looked for some content about it on the Internet, in Portuguese, and I didn’t find anything very interesting.

  • If I’m not mistaken, it’s like Cocoapods, whatever

  • 4

    Just to comment, some devs do not follow the semver.org, here it comes, from version 1.0.1 to 1.0.2 has something that changes the behavior abruptly, there goes and fails everything in your project, I’m so afraid that only use "x.x.x", no range :P

  • I know that the NPM uses the same notation. I just don’t know if it would be appropriate to add this to the response.

  • Actually, there are subtleties between NPM and Composer. Deviation from npm/semver in Tilde range Constraint.

Show 1 more comment

1 answer

7


Summary

Consider the model of semantic versioning: MAJOR.MINOR.PATCH

The operator (~) allows last digit to be incremented

~MAJOR.MINOR.PATCH  >=MAJOR.MINOR.PATCH e < MAJOR.(MINOR+1).0

That is, all Patchs of the MAJOR.MINOR version

example: ~ 1.2.3 will match all Patchs released after version 1.2.3. >1.2.3 and <=1.3.0

Other examples:

"vendor/package": "~1.3.2", // >=1.3.2 <1.4.0
"vendor/package": "~1.3", // >=1.3.0 <2.0.0

The circumflex operator (^) find the latest major version (MAJOR).

There is an exception if the major version is zero. In this case the behavior is to search for even the latest minor version. This exception exists to respect the rule 4 of semantic versioning:

At the beginning of development, the Major version MUST be zero (0.y.z). Anything can change at any time. The public API shall not be considered stable."

example: 1.2.3 will match any 1.x. x version, including 1.3.0, but will hang in 2.0.0.

Other examples

"vendor/package": "^1.3.2", // >=1.3.2 <2.0.0
"vendor/package": "^0.3.2", // >=0.3.2 <0.4.0 // exceção se a versão maior (MAJOR) é 0

Explaining the examples of the question:

"orchestra/testbench" : " 3.6" - indicates which package has a dependency on this library and ensures compatibility with all versions equal to or greater than version 3.6, but smaller than version 4.minor

"phpunit/phpunit": "~7.0" - indicates that the package has a dependency at least equal to 7.0, but allows all 7.minor. patch

Documentation

According to the documentation of Composer:

Tilde Version Range (~)

The ~ Operator is best Explained by example: ~1.2 is equivalent to >=1.2 <2.0.0, while ~1.2.3 is equivalent to >=1.2.3 <1.3.0. As you can see it is Mostly Useful for Projects respecting semantic versioning. A common Usage would be to mark the minimum minor version you Depend on, like ~1.2 (which Allows Anything up to, but not including, 2.0). Since in Theory there should be no Backwards Compatibility breaks until 2.0, that Works well. Another way of Looking at it is that using ~ specifies a minimum version, but Allows the last Digit specified to go up.

Caret Version Range (^)

The Operator behaves very similarly but it sticks Closer to semantic versioning, and will Always allow non-breaking updates. For example 1.2.3 is equivalent to >=1.2.3 <2.0.0 as None of the releases until 2.0 should break Backwards Compatibility. For pre-1.0 versions it also Acts with Safety in Mind and Treats 0.3 as >=0.3.0 <0.4.0.

This is the Recommended Operator for Maximum Interoperability when writing library code.

Translating:

Til Operator Version Track (~)

The ~ operator is best explained by the example: ~ 1.2 is equivalent to> = 1.2 <2.0.0, while ~ 1.2.3 is equivalent to> = 1.2.3 <1.3.0. As you can see, it is more useful for projects that respect semantic versions. A common use would be to mark the minimum secondary version you depend on, such as ~ 1.2 (which allows anything up to, but not including, 2.0). Since, in theory, there should be no compatibility intervals with previous versions until the 2.0, this works well. Another way to see this is to use ~ specifies a minimal version, but allows the last specified digit to go up.

Version Range with Circumflex Operator(^)

The operator behaves very similar (to the ~operator), but is closer to semantic versions and will always allow uninterrupted updates. For example, 1.2.3 is equivalent to> = 1.2.3 <2.0.0, since none of the releases up to 2.0 should break backwards compatibility. For pre-1.0 versions, it also acts with a security target and treats 0.3 as> = 0.3.0 <0.4.0.

This is the recommended operator for maximum interoperability when writing library code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.