Melange: Apko's Dependency Solver


You're working on a project using Apko, and you're curious about how it handles dependency resolution. Apko is a tool for building Alpine Linux packages, and it uses a dependency solver named Melange to ensure that your project's dependencies are correctly resolved during the build process. In this example, we'll explore Melange and learn how it works.


First, let's take a look at the Apko project structure:

├── config/
├── docs/
├── examples/
├── hack/
├── internal/
├── mac/
├── pkg/
├── Makefile
├── go.mod
└── go.sum

Melange is a part of the Apko project, and it's located in the internal/cli/ directory. Let's explore some of the relevant files:

├── build.go
├── build_test.go
├── build_implementation.go
└── lock.go

The build.go file contains the main logic for building and resolving dependencies using Melange. The lock.go file is responsible for managing the lockfile, which stores the resolved dependencies and their versions.

Now, let's see how Melange works by creating a simple example. Let's assume we have the following dependencies in our config/task.yaml file:

name: my-project
version: 0.1.0

  - my-dep: ">= 1.0.0 < 2.0.0"
  - another-dep: ">= 0.5.0 < 1.0.0"

To build our project using Apko, we can run the following command:

apk add -R .

Apko will then use Melange to resolve the dependencies based on the version ranges specified in the config/task.yaml file. Melange will consider all direct and transitive dependencies and their version range requirements to find the best possible resolution.

To verify the resolved dependencies, we can check the generated pkg/lock/ directory:

├── my-project-0.1.0.lock
└── my-dep-1.1.0.lock

The my-project-0.1.0.lock file contains the resolved dependencies for our project, and the my-dep-1.1.0.lock file contains the resolved dependencies for my-dep.


To verify the dependency resolution, you can check the generated lockfiles against the expected versions. For example, you can create a test file tests/ with the following content:




if [ ! -f "$my_project_lock" ]; then
  echo "Error: my-project lockfile not found."
  exit 1

if [ ! -f "$my_dep_lock" ]; then
  echo "Error: my-dep lockfile not found."
  exit 1

if [ ! -f "$another_dep_lock" ]; then
  echo "Error: another-dep lockfile not found."
  exit 1

my_project_version=$(grep "version:" "$my_project_lock" | awk '{print $2}')
my_dep_version=$(grep "version:" "$my_dep_lock" | awk '{print $2}')
another_dep_version=$(grep "version:" "$another_dep_lock" | awk '{print $2}')

if [ "$my_project_version" != "0.1.0" ]; then
  echo "Error: my-project version mismatch. Expected: 0.1.0, Found: $my_project_version"
  exit 1

if [ "$my_dep_version" != "$expected_my_dep_version" ]; then
  echo "Error: my-dep version mismatch. Expected: $expected_my_dep_version, Found: $my_dep_version"
  exit 1

if [ "$another_dep_version" != "$expected_another_dep_version" ]; then
  echo "Error: another-dep version mismatch. Expected: $expected_another_dep_version, Found: $another_dep_version"
  exit 1

echo "All dependencies resolved correctly."

You can then run the test script using the following command:

sh tests/

If the test passes, it means that Melange has correctly resolved the dependencies according to the specified version ranges.