Version Ranges & Conflicts
Pinning every requirement to an exact version (fmt/12.1.0) is predictable but tedious: every bug-fix release needs an edit. A version range lets Conan pick the newest version that satisfies a constraint. Ranges are convenient for libraries (don't force your users onto one patch release), and dangerous for applications unless you also use a lockfile.
Range syntax
A range goes in square brackets in place of the version:
| Range | Matches |
|---|---|
fmt/[>=12.0 <13] |
12.0 up to, but not including, 13 |
fmt/[~12.1] |
12.1.x ("tilde": patch updates only) |
fmt/[^12.1] |
12.x from 12.1 on ("caret": compatible updates) |
fmt/[>=12.1.0] |
12.1.0 or newer, with no upper bound (avoid) |
fmt/[*] |
any version |
fmt/[>=1.0 <2.0 || >=3.0] |
either interval |
fmt/[>=12.0 <13, include_prerelease] |
also accept 12.2.0-rc1 and similar |
Conan resolves a range against the versions it can see: the local cache first, then the remotes. It does not contact remotes again for a range already satisfied in the cache unless you pass --update, so two machines with different caches can resolve the same range differently. That's exactly the problem lockfiles solve.
Watching a range resolve
To have several versions to choose from, this session exports four versions of a tiny header-only recipe into the cache (conan export stores a recipe without building it). ConanCenter is disabled so Conan only looks at the cache:
conan remote disable conancenter > /dev/null
mkdir -p mylib && cd mylib
cat > conanfile.py <<'PY'
from conan import ConanFile
class MyLib(ConanFile):
name = "mylib"
package_type = "header-library"
PY
for v in 1.0.0 1.1.0 1.1.3 2.0.0; do conan export . --version=$v > /dev/null 2>&1; done
conan list "mylib/*" 2>&1 | grep 'mylib/'
for range in "[>=1.0 <2]" "[~1.0]" "[^1.1]" "[>=1.1]" "[<1.1]" "[>=3]"; do
printf '%-12s -> ' "$range"
conan graph info --requires="mylib/$range" --filter=label 2>&1 | grep -E '^ label: mylib|ERROR' | sed 's/ label: //'
doneFound 4 pkg/version recipes matching mylib/* in local cache
mylib/1.0.0
mylib/1.1.0
mylib/1.1.3
mylib/2.0.0
[>=1.0 <2] -> mylib/1.1.3
[~1.0] -> mylib/1.0.0
[^1.1] -> mylib/1.1.3
[>=1.1] -> mylib/2.0.0
[<1.1] -> mylib/1.0.0
[>=3] -> ERROR: Package 'mylib/[>=3]' not resolved: Version range '>=3' from requirement 'mylib/[>=3]' required by 'None' could not be resolved. Required by 'cli'
The newest matching version wins, and an unsatisfiable range is an error at graph time, before anything is built. [~1.0] means "1.0.x", so it took 1.0.0 even though 1.1.3 exists.
Conflicts
Two paths through the graph can ask for incompatible versions of the same package. Here the app wants mylib/1.0.0 while a dependency, engine, was built against mylib/1.1.3:
conan remote disable conancenter > /dev/null
mkdir -p mylib engine app
cat > mylib/conanfile.py <<'PY'
from conan import ConanFile
class MyLib(ConanFile):
name = "mylib"
package_type = "header-library"
PY
for v in 1.0.0 1.1.3; do conan export mylib --version=$v > /dev/null 2>&1; done
cat > engine/conanfile.py <<'PY'
from conan import ConanFile
class Engine(ConanFile):
name = "engine"
version = "3.0.0"
package_type = "header-library"
def requirements(self):
self.requires("mylib/1.1.3")
PY
conan export engine > /dev/null 2>&1
cd app
printf '[requires]\nengine/3.0.0\nmylib/1.0.0\n' > conanfile.txt
conan graph info . 2>&1 | grep -A3 '^ERROR'
echo "--- with an override"
printf '[requires]\nengine/3.0.0\nmylib/1.1.3\n' > conanfile.txt
conan graph info . --filter=label 2>&1 | grep ' label:'ERROR: Version conflict: Conflict between mylib/1.1.3 and mylib/1.0.0 in the graph.
Conflict originates from engine/3.0.0
Run 'conan graph info ... --format=html > graph.html' and open 'graph.html' to inspect the conflict graphically.
--- with an override
label: conanfile.txt
label: engine/3.0.0
label: mylib/1.1.3
C++ can't link two versions of one library into one program, so Conan refuses instead of guessing. The fixes, from best to worst:
- Agree on one version: here the app moved up to 1.1.3, the version
engineneeds. - Use ranges in libraries (
enginerequiringmylib/[>=1.0 <2]), so the graph can find a version both paths accept. - Force it in a conanfile.py:
self.requires("mylib/1.0.0", force=True)(oroverride=True, which only applies if something else requires mylib). This overrulesengine's requirement, so you're responsible for checking thatenginestill works with the older version.
A runnable model: resolving a range
Version ranges come down to parsing versions and comparing them as tuples of numbers, never as strings ("1.10" < "1.9" as text). This model implements >=, <, ~ and ^ the way the table above describes:
def ver(s):
return tuple(int(x) for x in s.split("."))
def matches(version, rng):
v = ver(version)
for cond in rng.split():
if cond.startswith(">="):
ok = v >= ver(cond[2:])
elif cond.startswith("<"):
ok = v < ver(cond[1:])
elif cond.startswith("~"): # same major.minor
base = ver(cond[1:])
ok = v >= base and v[:2] == (base + (0,))[:2]
elif cond.startswith("^"): # same major
base = ver(cond[1:])
ok = v >= base and v[0] == base[0]
else:
ok = v == ver(cond)
if not ok:
return False
return True
def resolve(available, rng):
found = [v for v in available if matches(v, rng)]
return max(found, key=ver) if found else "no match"
available = ["1.0.0", "1.1.0", "1.1.3", "1.9.0", "1.10.0", "2.0.0"]
for rng in [">=1.0 <2", "~1.1", "^1.1", "<1.1", ">=3"]:
print(f"{rng:<10} -> {resolve(available, rng)}")
print("as strings, max is", max(available), "(wrong)")>=1.0 <2 -> 1.10.0
~1.1 -> 1.1.3
^1.1 -> 1.10.0
<1.1 -> 1.0.0
>=3 -> no match
as strings, max is 2.0.0 (wrong)
Test yourself
Which versions does [~1.1] accept?
- Any 1.x from 1.1
- 1.1.x only
- Exactly 1.1
- Anything below 1.1
Why can two developers resolve fmt/[>=12 <13] to different versions?
- Conan picks randomly
- Ranges are resolved against what each machine's cache (and remotes) contain; a cached match is used without checking remotes unless --update is given
- Different Python versions
- They can't
The app requires mylib/1.0.0 and engine requires mylib/1.1.3. What does Conan do?
- Links both
- Picks the newest silently
- Stops with a version conflict until you resolve it
- Picks the oldest silently