TExcelTAD
Home › C/C++ Packages: Conan 2, vcpkg & CPM.cmake › Field Notes: Pinning & Caching

Field Notes: Pinning & Caching

Practical lessons from teams that run C/C++ package managers at scale. Most "the build broke and nobody changed anything" incidents trace back to something that wasn't pinned, or a cache that was trusted when it shouldn't have been.

Pin everything that can move

A C++ build has more moving inputs than people expect. Write them down:

Input Pin it with
Conan itself pip install conan==2.33.0 (in a requirements.txt for CI)
Conan dependency versions + revisions conan.lock, committed
vcpkg port versions builtin-baseline (and registry baselines), committed
vcpkg tool + scripts a vcpkg checkout at a fixed tag/commit (submodule, or git checkout in CI)
CPM packages exact Git tags or commit hashes, never branch names
Compiler and CMake a CI container image by digest, or a Conan tool_requires for CMake/Ninja
Profiles / triplets committed to the repository, passed explicitly in CI (-pr:h ci/profiles/linux-gcc13)

Then make CI fail when something unpinned sneaks in. Conan's lockfile is strict by default, and vcpkg refuses version constraints without a baseline:

bash · try it yourself
mkdir -p app && cd app
printf '[requires]\nfmt/12.1.0\n' > conanfile.txt
conan lock create . > /dev/null 2>&1
echo "--- someone adds spdlog but forgets the lockfile"
printf '[requires]\nfmt/12.1.0\nspdlog/1.17.0\n' > conanfile.txt
conan install . --lockfile=conan.lock 2>&1 | grep ERROR
echo "--- the deliberate fix: extend the lockfile, review, commit"
conan install . --lockfile=conan.lock --lockfile-partial --lockfile-out=conan.lock 2>&1 | tail -1
grep -o '"[a-z]*/[0-9.]*' conan.lock
echo "--- vcpkg: a constraint without a baseline"
mkdir -p ../v && cd ../v
echo '{ "dependencies": [ { "name": "fmt", "version>=": "12.2.0#1" } ] }' > vcpkg.json
vcpkg install 2>&1 | head -1
--- someone adds spdlog but forgets the lockfile
ERROR: Requirement 'spdlog/1.17.0' not in lockfile 'requires'
--- the deliberate fix: extend the lockfile, review, commit
Generated lockfile: /home/learner/app/conan.lock
"spdlog/1.17.0
"fmt/12.1.0
--- vcpkg: a constraint without a baseline
error: /home/learner/v/vcpkg.json was rejected because it uses "version>=" and does not have a "builtin-baseline". This can be fixed by removing the uses of "version>=" or adding a "builtin-baseline".

Never use unbounded ranges in applications

fmt/[>=12] will one day resolve to fmt 13 with a breaking change, on a Friday, on one CI agent whose cache happened to be empty. In libraries, use bounded ranges ([>=12.1 <13]) so consumers can resolve conflicts. In applications, use exact versions or ranges plus a lockfile.

Update on a schedule, not by accident

Pinning without updating is how you end up three years behind with a painful migration and unpatched CVEs. Put updates on a calendar:

  • A weekly or monthly job runs conan lock create . --update (or vcpkg x-update-baseline), builds, tests, and opens a pull request.
  • One baseline/lockfile update per pull request, so a failure points at one change.
  • Security fixes skip the queue: a targeted conan lock add / override, then catch up properly.

Cache binaries, and treat the cache as production data

A binary cache is a performance tool and a trust boundary: every build that downloads from it runs code it didn't compile.

  • Write from trusted CI only. Developers and pull-request builds read; only builds of protected branches write (Conan: upload only from main; vcpkg: read for PRs, readwrite for main).
  • Keep caches per toolchain image. A compiler update changes vcpkg's ABI hashes anyway; with Conan, a profile change does the same. Old binaries become dead weight, so set retention (conan remove "*" --lru=4w -c on build agents, retention policies on Artifactory).
  • Measure hit rates. A cache that misses every night (an unpinned compiler, a timestamp in a profile) costs storage and saves nothing. Conan's - Build / - Download lines and vcpkg's Restored N package(s) are easy to grep in CI logs.
  • Warm the cache once per configuration: a nightly job that builds every supported profile/triplet means daytime builds only download.

Keep the cache out of your repository

Don't commit vcpkg_installed/, build/, CMakeUserPresets.json or Conan's generated files. Do commit conan.lock, vcpkg.json, vcpkg-configuration.json, profiles, triplets and overlay ports. A useful .gitignore for this course's projects:

text
build/
build-*/
vcpkg_installed/
CMakeUserPresets.json

Air-gapped and offline builds need planning

Everything in this course ran with no network because the caches were filled first. Real offline environments work the same way:

  • Conan: an Artifactory remote that proxies ConanCenter, or conan cache save/restore archives carried across.
  • vcpkg: asset caching (X_VCPKG_ASSET_SOURCES with x-block-origin) for sources and tools, plus a binary cache.
  • CPM: a pre-filled CPM_SOURCE_CACHE, or local mirrors of each repository.

Test the offline path regularly (--network none in a container, like this course), or it will rot.

Test yourself

Someone adds a requirement but doesn't update conan.lock. What does conan install --lockfile=conan.lock do?

  1. Silently adds it
  2. Fails: the requirement isn't in the lockfile
  3. Ignores the lockfile
  4. Deletes the lockfile

Who should be allowed to write to a shared binary cache?

  1. Everyone
  2. Only CI builds of protected branches
  3. Only pull requests
  4. Nobody, it's read-only forever

Why are unbounded ranges like fmt/[>=12] risky in applications?

  1. They are slower
  2. They will eventually resolve to a new major version with breaking changes, differently on different machines
  3. Conan rejects them
  4. They disable caching