skip to content

A library in the vcpkg registry needs a local bug fix before your release; how do you patch it with an overlay port, and what changes about its versioning?

level: seniorimportance: should knowfreq 30%

answer

  1. copy the port directory
  2. a .patch in PATCHES
  3. overlays win name resolution
  4. version constraints no longer apply

basics

~20 s

Copy the port's directory into an overlay folder in your repository, add your .patch to the PATCHES list of its source-fetching call, and register the folder in overlay-ports. The overlay then wins name resolution, and version constraints and overrides stop applying.

solid answer

~50 s

A **port** is a directory with `vcpkg.json` (metadata, dependencies, features) and `portfile.cmake` (the build recipe), plus any patch files. To fix a library locally, copy the registry's directory for that port, at your baseline's version so you change only the bug, into something like `vcpkg-overlays/<port>`. Generate a patch: `vcpkg install <port> --editable` in classic mode keeps the extracted sources under `buildtrees/<port>/src`, where `git init`, your edit and `git diff --output` produce it. Add the file to `PATCHES` in the `vcpkg_from_github` call and bump `port-version` so the build is visibly different. Register the folder with `overlay-ports` in `vcpkg-configuration.json`, `--overlay-ports` or `VCPKG_OVERLAY_PORTS`. Overlays win name resolution over every registry, and the resolver applies no version constraints to them, so the baseline, `version>=` and overrides no longer move that library: you own its upgrades until the fix lands upstream and the overlay is deleted.

code

cmake · 11 lines
cmake
# vcpkg-overlays/zlib/portfile.cmake (excerpt; copied from the 2026.10.16 registry)
vcpkg_from_github(
    OUT_SOURCE_PATH SOURCE_PATH
    REPO madler/zlib
    REF v${VERSION}
    SHA512 16fea4df307a68cf0035858abe2fd550250618a97590e202037acd18a666f57afc10f8836cbbd472d54a0e76539d0e558cb26f059d53de52ff90634bbf4f47d4
    HEAD_REF master
    PATCHES
        0001-no-version-script-on-ohos.patch
        fix-crash.patch
)

go deeper

for a junior

Recall that a port is vcpkg.json plus portfile.cmake and that an overlay lets you use your own copy of one.

for a middle

Explain overlay locations and their search order, and how PATCHES in vcpkg_from_github applies a fix to the extracted sources.

for a senior

Walk through producing a patch with --editable and git diff, and explain why the overlay is frozen against baseline upgrades and how the ABI hash keeps caches honest.

for a principal

Set a policy for overlays: an owner, a ticket to upstream the fix, and a removal trigger, so local forks do not quietly outlive their reason.

## What a port is A **port** is the recipe vcpkg follows to produce one package: - `vcpkg.json`: name, version and `port-version`, description, license, dependencies and features; - `portfile.cmake`: the build script, which downloads sources, applies patches, configures, builds, installs and fixes up the result; - optional files: patches, a `usage` file, CMake wrapper scripts. A typical portfile from the 2026.10.16 registry (zlib) fetches the sources with `vcpkg_from_github(OUT_SOURCE_PATH SOURCE_PATH REPO madler/zlib REF v${VERSION} SHA512 ... HEAD_REF master PATCHES 0001-no-version-script-on-ohos.patch)`, then calls `vcpkg_cmake_configure`, `vcpkg_cmake_install`, `vcpkg_cmake_config_fixup` and `vcpkg_install_copyright`. The `PATCHES` list is applied to the extracted source tree before configuring. ## What an overlay port is An **overlay port** is a port read from a directory you supply instead of from a registry. It can replace an existing port or add one that no registry has. When vcpkg resolves a package name, overlays are consulted **first**, before any `packages` pattern or the default registry, and the first overlay location containing that name wins. Overlay locations are searched in this order: 1. `--overlay-ports` on the command line; 2. `overlay-ports` in `vcpkg-configuration.json` (paths relative to that file); 3. the `VCPKG_OVERLAY_PORTS` environment variable. A path may point at one port directory, or at a directory of ports named after them; in the latter case each subdirectory's `vcpkg.json` name must match its folder. ## Patching a library, step by step 1. **Copy** `ports/<port>` from the vcpkg registry, at the commit your manifest's baseline names, into `vcpkg-overlays/<port>` in your repository, so the only difference is your fix. 2. **Get editable sources**: run `vcpkg install <port> --overlay-ports=vcpkg-overlays --editable`. `--editable` (classic mode only) stops vcpkg from purging and re-extracting the source, which it prints under `buildtrees/<port>/src/...`. 3. **Make a patch**: in that source directory run `git init`, commit the pristine tree, edit, and run `git diff --output <overlay>/<port>/fix-crash.patch`. 4. **Apply it**: add `fix-crash.patch` to the `PATCHES` list of `vcpkg_from_github` (or the port's other source-fetching helper). 5. **Mark it**: increase `port-version` in the overlay's `vcpkg.json`, so logs and SBOMs show a modified build. 6. **Register** the overlay in `vcpkg-configuration.json` and build without `--editable`, so the patch is applied to clean sources. ## How the overlay changes versioning | Aspect | Registry port | Overlay port | |---|---|---| | Source of port files | git history at the selected version | the overlay directory as it is | | Baseline and `version>=` | applied | ignored | | `overrides` | applied | ignored | | Upgrades | move the baseline | edit the overlay | The vcpkg-tool resolver explicitly skips version constraints for a node that is overlaid or overridden. The overlay is therefore **frozen**: moving the baseline upgrades every other library but not this one. That is the point for a hot fix and the risk afterwards. ## Binary caching and hygiene - Every file in the port directory enters the package's **ABI hash**, so the patched port produces a new binary package automatically; consumers of your binary cache cannot receive the unpatched build by mistake. - Keep the patch minimal and send it upstream; when a released version contains the fix, delete the overlay and let the baseline take over again. - Record why the overlay exists, next to it, so nobody keeps a stale copy alive for years. - An overlay can also be empty of build steps to make vcpkg use a system library instead, which the CMake integration docs now recommend over a deprecated variable. ## What the patch should and should not touch The cleanest overlay differs from the registry copy by one patch file, one line in `PATCHES` and one `port-version` bump. That keeps the review small and makes the later removal obvious. Things to avoid: - **Changing the upstream version in the overlay** to pick up an unreleased commit. Point `REF` at a commit only when the fix exists upstream already, and then set a matching `version-date` or version string, update `SHA512`, and expect the existing patches to need rework. - **Editing sources under `buildtrees` and building without `--editable`.** vcpkg purges and re-extracts sources on a normal build, so those edits silently disappear. - **Copying the port from a different registry commit** than the one the manifest's baseline names. The overlay would then bring in a different version as well as your fix. When several projects need the same fix, the overlay directory can be shared, but at that point a registry is usually the better home.

  • Why bump port-version in the overlay copy if versions are ignored for overlays?
    Resolution ignores it, but humans and tools do not: the installed package's version, `vcpkg list`, logs and the SBOM show it, so a modified build is distinguishable from upstream. The ABI hash already differs because the port files differ.
  • When is a custom registry better than an overlay port?
    When the patched or private ports must be shared across many repositories with versioned history, baselines and version constraints. Overlays suit one repository's short-lived fix; a Git registry lets each consumer pin and upgrade the team's ports like any other.

  • You must fork the whole vcpkg registry to patch one library.
  • An overlay port still follows the manifest's baseline and overrides.
  • Editing files under buildtrees is enough; vcpkg keeps your changes on the next build.
  • Registries are searched before overlays, so the overlay is only a fallback.
  • A patched overlay port reuses the cached binary of the original port.