Skip to content

fix(apk): accept a !name entry in the world - #2695

Merged
uhryniuk merged 5 commits into
chainguard-dev:mainfrom
uhryniuk:world-conflicts
Nov 25, 2026
Merged

uhryniuk merged 5 commits into
chainguard-dev:mainfrom
uhryniuk:world-conflicts

Conversation

@uhryniuk

@uhryniuk uhryniuk commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What

A !name entry in the package list excludes name, as in an apk world file. Nothing is installed for the entry, and name is reported as a conflict, the same as a !name dependency of a package.

  • Resolver. constrain already disqualified the providers of name. The install loop then looked for a package named !name and failed with nothing provides "!name". The loop now drops the entry after constrain.
  • Lock. LockImageConfiguration checked every entry for a locked version and failed with unable to lock packages to a consistent version: map[!musl:...]. It now keeps a !name entry as is in every locked list.
  • Lockfile. A lockfile held only name=version entries, so a build from a lockfile dropped the exclusion. The lockfile now records !name entries under excluded, and a build from it adds them back.
  • Installed database. A !name entry in the world keeps name out when it is pulled in as a dependency, but an explicit apk add name replaces the entry. The build now records a package, .apko-excluded, that depends on each !name entry, as glibc depends on !musl, and pins it in the world. apk then refuses name even on an explicit add.

Why

A !name dependency of a package works today. The same exclusion in the package list failed. TestWorldConflict resolves glibc with !ld-linux: without the exclusion glibc gets ld-linux, and with it glibc gets musl, with ld-linux as the conflict. A bare ! names no package and fails. A TestUnify case and a lockfile case cover the lock and the lockfile.

Tested by hand with apk-tools 2.14.13 on Wolfi, for builds from an image config and from a lockfile: the world has the entry and the pinned .apko-excluded, apk add git picks another libcurl provider, an explicit apk add libcurl-openssl4 is refused, and apk fix keeps the block.

Notes

.apko-excluded appears in the image SBOM as a package. The installed-database commit has no unit test yet.

@astrojerms astrojerms left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Review summary

The resolver change works. A top-level !name disqualifies the providers of name, installs nothing for that entry, and reports name as a conflict, the same as a package-declared conflict. It reuses the exclusion constrain already does instead of adding a second mechanism.

The main gap is between the description and apko's build path. The description gives an image configuration as the use case, but apko build/publish still fail on !musl (SR001). The new test also doesn't exercise the exclusion itself (SR002).

Should address

ID Severity File:Line Issue Suggestion
SR001 Medium pkg/apk/apk/repo.go:738-745 The resolver now accepts !name, but the image-configuration path the description cites still fails. LockImageConfiguration calls unify (pkg/build/lock.go:180-206), which adds !musl to the set of original packages. No resolved package matches it, so unify errors with unable to lock packages to a consistent version: map[!musl:[ (amd64)]] (lock.go:246-281). I reproduced this at the PR head. Every apko build/publish without a lockfile goes through this path. This isn't a regression, since the input failed before this PR too. Either (a) carry !-prefixed entries through the build path: skip them in unify's missing check, append them to each per-arch locked list, cover the lockfile branch (SR010), and add a TestUnify case. Or (b) if your caller uses the resolver directly, narrow the description to resolver-level callers and track build support separately. Which caller is the motivating one?
SR002 Low pkg/apk/apk/repo_test.go:830-853 TestWorldConflict doesn't show that !musl changes which provider gets picked. In this fixture, ["glibc"] alone already resolves to ld-linux. If a later change moved the new DeleteFunc ahead of constrain, the package assertions would still pass. The test does guard the new skip and the conflict report. Exclude the provider the solver picks by default. {"glibc","!ld-linux"} should give musl-1.23-r4.apk, glibc-2.38-r10.apk and conflicts {"ld-linux"}.
SR005 Low pkg/apk/apk/repo.go:739 A bare ! entry used to fail with nothing provides. It's now accepted silently and returns the conflict "", so a typo or templating bug in a config does nothing. Reject an empty name after CutPrefix, with an error that names the entry.

If you go with SR001 option (a):

  • SR010 (pkg/apk/apk/repo.go:738): with an existing lockfile, Arch2LockedPackages returns only name=version entries (pkg/lock/lock.go:91-96), and those replace Contents.Packages (pkg/build/lock.go:104). !musl never reaches the image's /etc/apk/world, so a later runtime apk add isn't constrained by it. Fixing unify alone leaves this path broken.
  • Document the syntax in docs/apko_file.md once apko build accepts it.

Optional:

  • Add table cases for a world that both requests and excludes a name ({"glibc","!musl","musl"}, which errors with excluded by !musl) and for a versioned conflict (!musl>2.0).
  • Add a sentence to the GetPackagesWithDependencies doc comment saying that a !name entry installs nothing and comes back in conflicts.

CI: Enforce - Commit Signing fails because 48e428da is unsigned.

What's well done

  • pkg/apk/apk/repo.go:736-737: The comment explains why dropping the entry is only safe after constrain has run, and ties it to apk world semantics. You can't tell that ordering from the deletion alone.
  • pkg/apk/apk/repo.go:738-745: This reuses disqualifyProviders through constrain instead of adding a second exclusion mechanism. World entries go through the same conflicts/uniqify path as dependency-declared !name, so duplicates are collapsed for free.
  • pkg/apk/apk/repo_test.go:830-853: The test mirrors TestExcludedDeps, which makes the world form and the dependency form easy to compare. It also puts the require.Equal arguments in (expected, actual) order, which its neighbor gets backwards.
  • The description clearly states the old failure, the new semantics, and the test that covers them.

@uhryniuk
uhryniuk marked this pull request as ready for review November 25, 2026 15:09
tomgeorge
tomgeorge previously approved these changes Nov 25, 2026

@tomgeorge tomgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The resolver change does what the description says; the remaining points from the earlier review are non-blocking.

Existing findings confirmed (from #2695 (review))

  • SR001, still open, non-blocking: apko build without a lockfile still fails on !musl in contents.packages, because unify treats !musl as a missing package (pkg/build/lock.go:180-206, error at lock.go:280). This failed before the PR too, and the current description no longer claims image-config support, so I'm reading this PR as resolver/world-file only. Worth saying so in the description, since "package list" reads like contents.packages to apko users.
  • SR002, still open, nit: TestWorldConflict passes without the exclusion doing anything. I ran {"glibc"} alone and it already resolves to ld-linux. {"glibc","!ld-linux"} gives musl-1.23-r4.apk, glibc-2.38-r10.apk with conflicts ["ld-linux"], which would actually catch a regression.
  • SR005, still open, nit: a bare ! entry now resolves silently with conflict "" (pkg/apk/apk/repo.go:739). Rejecting an empty name after CutPrefix would surface a templating mistake.

What I verified

  • constrain disqualifies the providers of a !name entry before the new DeleteFunc removes it (pkg/apk/apk/repo.go:732-744), so nextPackage never sees !name.
  • The second loop skips ! entries (repo.go:770), so GetPackageWithDependencies isn't called with !musl.
  • The conflict string is name with any version suffix kept, the same as for a dependency-declared !name (repo.go:984), and uniqify dedups the two.
  • Targeted run at 13561f2: {"glibc","!musl","musl"} errors with excluded by !musl, {"!musl"} returns no packages and conflict musl, and !musl>2.0 returns conflict musl>2.0.
  • FixateWorld checks each conflict against the installed DB (pkg/apk/apk/implementation.go:942), so a world with !musl on a system that already has musl fails with "cannot install due to conflict with musl", as in apk.
  • CI is green at 13561f2 (gh pr checks), including commit signing.

astrojerms
astrojerms previously approved these changes Nov 25, 2026
@uhryniuk
uhryniuk dismissed stale reviews from astrojerms and tomgeorge via 7bbd192 November 25, 2026 15:43
astrojerms
astrojerms previously approved these changes Nov 25, 2026
@xnox

xnox commented Nov 25, 2026

Copy link
Copy Markdown
Member

Does apk-tools support !pkg syntax in the world file? If yes, can we ensure to add it to the world file?

This is to ensure that subsequent usage of "apk add" continues to respect the original negative package selection.

If it doesn't support this, it would help to synthesise a package that is declared in the installed database with a depends on "!pkg" - as we know for sure that apk-tools respects this; as we use that to block installing musl today.

tomgeorge
tomgeorge previously approved these changes Nov 25, 2026

@tomgeorge tomgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed after 7bbd192. LGTM, same non-blocking nits as before.

  • SR001: addressed for builds without a lockfile. unify now keeps !name in every locked list (pkg/build/lock.go:183-187, :319, :338), and the new TestUnify "excluded package" case covers it.
  • SR010: still open, non-blocking. With a lockfile, Arch2LockedPackages only emits name=version (pkg/lock/lock.go:89-100), so !musl never reaches /etc/apk/world. The image contents are still right, but a later apk add musl inside the image isn't blocked.
  • SR002 (nit): TestWorldConflict passes even if the exclusion does nothing. Using {"glibc","!ld-linux"} would make it fail when the exclusion breaks.
  • SR005 (nit): a bare ! still resolves silently, with conflict "".

What I verified

  • The resolver change in pkg/apk/apk/repo.go hasn't changed since 13561f2.
  • A locked list like ["!bar", "foo=1.2.3"] goes back through ResolveWorld, which drops !bar after constrain (pkg/apk/apk/repo.go:736-744).

@uhryniuk

Copy link
Copy Markdown
Contributor Author

Does apk-tools support !pkg syntax in the world file? If yes, can we ensure to add it to the world file?

This is to ensure that subsequent usage of "apk add" continues to respect the original negative package selection.

If it doesn't support this, it would help to synthesise a package that is declared in the installed database with a depends on "!pkg" - as we know for sure that apk-tools respects this; as we use that to block installing musl today.

@xnox

I tested this locally with apk-tools 2.14.13 from wolfi-base and apko built from this branch. It works partly. A !pkg entry in the world file keeps the package out when apk pulls it in as a dependency, but it doesn't stop someone who installs the package by name.

Where it passes:

  • apk add '!curl' writes !curl to /etc/apk/world.
  • With !libcurl-openssl4 in the world, apk add git picks libcurl-rustls4 instead.
  • apko from this branch writes !pkg to /etc/apk/world for builds from an image config, and a later apk add git still avoids the excluded provider.

Where it fails:

  • An explicit apk add curl replaces the !curl entry and installs curl.
  • Builds from an apko lockfile drop the !pkg entry, because the lockfile has no place for it.

A package that depends on !pkg blocks even the explicit add. I tested that too. I can take a look at adding that package to block the install, but I think that includes a lockfile update as well.

@uhryniuk
uhryniuk dismissed stale reviews from tomgeorge and astrojerms via dea6e18 November 25, 2026 17:06
tomgeorge
tomgeorge previously approved these changes Nov 25, 2026

@tomgeorge tomgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed after 86edfa4 and dea6e18. LGTM, with one non-blocking edge case for base images (inline).

  • SR010 (lockfile builds drop !name): addressed. apko lock records ! entries in Contents.Excluded (internal/cli/lock.go:171-175), and Arch2LockedPackages appends them to each arch (pkg/lock/lock.go:103-105). TestArch2LockedPackagesExcluded covers it.
  • xnox's runtime apk add concern: addressed by blockExcluded (pkg/build/build_implementation.go:571-602).
  • SR002, SR005: still open, nits as before.

Nits (non-blocking)

  • With a base image built with ! entries, the derived image gets a second .apko-excluded record, and apk-tools then enforces neither one (pkg/build/build_implementation.go:596).
  • .apko-excluded will show up in the SPDX SBOM as pkg:apk/<os>/.apko-excluded@0-r0, since sbom.go:109 lists everything in the installed DB. Fine by me, but worth knowing.

What I verified

  • I generated the record with PackageToInstalled at dea6e18, appended it to a wolfi root's installed DB, and added !curl and .apko-excluded=0-r0 to the world. With apk-tools 2.14.13, apk add curl fails with breaks: .apko-excluded-0-r0[!curl], and both apk upgrade -a and apk fix keep the package.
  • The checksum is derived from the sorted ! entries, so the record is the same on every build.
  • blockExcluded runs after all three install paths (pre-resolved, lockfile, FixateWorld) (pkg/build/build_implementation.go:327).

Dependencies: excluded,
Checksum: sum[:],
}
if _, err := bc.apk.AddInstalledPackage(pkg, nil); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: with a base image whose installed DB already has .apko-excluded (the base was built with a ! entry), this appends a second record with the same name and a different checksum. buildImage copies the base records in first, and the world also gets .apko-excluded=0-r0 twice. I tried that against apk-tools 2.14.13 with one record carrying D:!curl and the other D:!wget: apk add curl and apk add wget both succeeded, and apk upgrade -a replaced the package. So the derived image loses the base's block as well as its own. Merging the base record's ! dependencies into this one, and replacing that record rather than appending another, would fix it. baseimage is experimental, so I'm fine with this as a follow-up.

@uhryniuk

Copy link
Copy Markdown
Contributor Author

I added two commits for the world-file gap, as separate commits so either can be dropped.

  • fix(lock): keep !name entries in the lockfile records !name entries under excluded in the lockfile and adds them back on a build from it. Without it, a lockfile build loses the exclusion.
  • feat(build): block the !name entries of the world in the installed database records a package, .apko-excluded, that depends on each !name entry, the same way glibc depends on !musl. It is pinned in the world, so apk keeps it. The gosec fix after it belongs with it.

I tested both by hand with apk-tools 2.14.13 on Wolfi, for builds from an image config and from a lockfile:

  • the world has !libcurl-openssl4 and .apko-excluded=0-r0
  • apk add git picks libcurl-rustls4
  • an explicit apk add libcurl-openssl4 is refused: breaks: .apko-excluded-0-r0[!libcurl-openssl4]
  • apk fix keeps the block

Two things to flag. .apko-excluded shows up in the image SBOM as a package. The installed-database commit has no unit test yet; I'll add one if we keep it.

A world entry of the form !name is a conflict in apk: the providers of
name are disqualified and nothing is installed for the entry. constrain
already disqualified the providers, but the install loop then looked
for a package named !name and failed with "nothing provides". The loop
now drops the entry after constrain and reports name as a conflict, as
a !name dependency of a package already does.

Signed-off-by: Dylan Uhryniuk <dylan.uhryniuk@chainguard.dev>
The lock step checked every entry of the package list for a locked
version. A !name entry installs nothing, so the lock failed with
"unable to lock packages to a consistent version". The lock now keeps
the entry as is in every locked list.

Signed-off-by: Dylan Uhryniuk <dylan.uhryniuk@chainguard.dev>
A lockfile held only name=version entries, so a build from a lockfile
dropped the !name entries of the package list and wrote no exclusion to
the apk world. The lockfile now records them under excluded, and a build
from it adds them back to the package list of every architecture.

Signed-off-by: Dylan Uhryniuk <dylan.uhryniuk@chainguard.dev>
…tabase

A !name entry in the world keeps a package out when it is pulled in as a
dependency, but an explicit apk add of the package replaces the entry.
The build now records a package, .apko-excluded, that depends on each
!name entry, as glibc depends on !musl, and pins it in the world. apk
then refuses the package even on an explicit add.

Signed-off-by: Dylan Uhryniuk <dylan.uhryniuk@chainguard.dev>
apk package checksums are SHA-1, so the import and the call are marked
the same way as the other apk code.

Signed-off-by: Dylan Uhryniuk <dylan.uhryniuk@chainguard.dev>

@tomgeorge tomgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at 2e37097. The history was rewritten, but the tree only differs from 90857b1 in pkg/apk/apk/repo.go and its test. LGTM.

  • SR002 (the test didn't exercise the exclusion): addressed. TestWorldConflict now excludes ld-linux, the provider picked by default, and expects musl (pkg/apk/apk/repo_test.go:839-848).
  • SR005 (a bare ! was accepted silently): addressed. It's now rejected up front with an error naming the entry (pkg/apk/apk/repo.go:732-736), and the same test covers it.
  • Base-image duplicate .apko-excluded record (pkg/build/build_implementation.go:596): still open, non-blocking.
  • Nit: as you said, blockExcluded has no unit test yet. One that checks the installed-DB record and the world entry would be enough.

What I verified

  • The rejection runs before constrain, so ! never reaches disqualifyProviders.
  • blockExcluded, the lockfile excluded field, and the unify passthrough are unchanged from dea6e18, which I checked against apk-tools 2.14.13.

@uhryniuk
uhryniuk merged commit 825cab2 into chainguard-dev:main Nov 25, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants