machdocs
Home Ecosystem Changelog

Dependencies

A mach project declares its dependencies in mach.toml, and a dependency is the files in dep/<id>/. A git dependency selects releases by version range, or one exact commit, tag or branch by ref. A path dependency is a copy of another project tree. mach dep resolves and realizes that tree, and mach build reads what it holds, with no network.

Declaring a dependency

A dependency is named by its project id, and that one name appears in three places: the manifest key [dep.<id>], the directory dep/<id>/, and the first segment of every module path the dependency exposes (use <id>.x;). So dep/<id>/mach.toml must declare id = "<id>". A dependency whose id is the declaring project's own is refused where it is declared.

# in mach.toml
[dep.std]
git     = "https://github.com/briar-systems/mach-std"
version = "^5.3.0"                # any release from 5.3.0 up to, not including, 6.0.0

[dep.gfx]
git = "https://example.com/gfx"
ref = "tag/v1.4.0"                 # one exact tag

[dep.util]
path = "../util"                   # a local project tree, never fetched
KeyTypeMeaning
gitstringGit URL. The dependency is checked out at dep/<id>/. Requires version or ref.
versionstringA version range over the dependency's releases. Valid only with git.
refstringExact selector for git: branch/<name>, tag/<name>, or commit/<full-object-id>.
pathstringLocal project tree, resolved relative to this manifest's directory. Never fetched. Forbids version and ref.

A stanza names exactly one source, git or path. A git stanza also names exactly one selector, and naming both is refused:

error[manifest.key_conflict] mach.toml: [dep.both] names both 'ref' and 'version'. keep 'version' for a release range or 'ref' for one exact or branch selector

version selects among releases. ref selects one exact tag or commit, or follows a branch. Any other ref spelling is rejected: there are no bare names, short hashes, or empty refs.

A path dependency is copied into dep/<id>/, without the source's own dep/ or Git metadata. The copy is refreshed only by mach dep pull and mach dep update, each of which says what it did with it:

$ mach dep add . helper --path ../helper
  realized  helper  ./../helper  (copy made from its source)
     added  [dep.helper]
$ mach dep pull .
note path dependency './dep/helper' replaced 'src/lib/helper.mach' with its source's content
  realized  helper  ./../helper  (copy refreshed from its source)

A build reads the copy as it is and never syncs it, so an edit to the source reaches the build after the next pull. A local edit to the copy does not survive one.

Version ranges

[dep.<id>].version and the compiler range [project].mach share one range grammar:

range   = clause *( "," clause )        ; the intersection of every clause
clause  = op partial
op      = "^" / "~" / ">=" / ">" / "<=" / "<" / "="
partial = major [ "." minor [ "." patch [ "-" pre ] ] ]

A version satisfies a range when it satisfies every clause. Each clause names its operator, so a bare 1.2 is refused (a clause needs an operator, such as ^1.2 or >=1.2). Whitespace is allowed around , and between an operator and its version, and nowhere else. A missing component is 0.

ClauseMeans
^1.2.3>=1.2.3, <2.0.0
^1.2>=1.2.0, <2.0.0
^1>=1.0.0, <2.0.0
~1.2.3>=1.2.3, <1.3.0
~1.2>=1.2.0, <1.3.0
~1>=1.0.0, <2.0.0
>=1.2, >1.2, <=1.2, <2the bound with missing components as 0, so >1.2 is >1.2.0
=1.2.3exactly 1.2.3. = needs all three components.

Below 1.0 a minor release is breaking, so caret fixes everything up to the first nonzero component:

ClauseMeans
^0.4.2>=0.4.2, <0.5.0
^0.4>=0.4.0, <0.5.0
^0.0.3=0.0.3
^0.0>=0.0.0, <0.1.0
^0>=0.0.0, <1.0.0

A pre-release version (1.3.0-rc.1) satisfies a range only when one of its clauses names a pre-release of that same release. So ^1.2 never selects 1.3.0-rc.1, and >=1.3.0-rc.1, <2 does. Build metadata (+...) is refused in a range and ignored in a release's version. There is no * and no ||.

Releases and resolution

A release of a git dependency is a tag vX.Y.Z (or vX.Y.Z-pre) together with the mach.toml at that tag. A tag whose manifest does not load, or whose [project].version differs from the tag name, is not a candidate.

Resolution runs in mach dep add, mach dep update and mach dep outdated, and in mach init when the std selection it declares is a range. Builds never resolve. For every identity in the closure that some manifest selects by version, resolution picks one release such that:

  1. every requirer's range contains it,
  2. its own [project].mach contains the running compiler, and
  3. the closure its own manifest implies also resolves.

Among the choices that satisfy all three, it takes the highest release of each identity, and pins it like any other dependency. mach dep update <path> <name> keeps every other identity at its pinned release while that release still fits, so an update moves as little as it can. --all resolves from scratch. When nothing fits, the error lists every requirement that took part, a release that needs a newer compiler among them:

error[dep.unsatisfiable] no set of releases satisfies every requirement
    demo requires vlib ^0.2

When the identity at fault is one the root does not declare itself, the error also names the root declaration that would settle it. Resolution never silently settles for a lower release than the ranges allow.

Root declarations: narrowing and overriding

A root version for an identity narrows: it is intersected with every requirer's range, and resolution and verification hold the pin to all of them. A root ref or path overrides: the requirers' ranges and selectors for that identity no longer apply. pull, update and add print a note for each requirement a root declaration replaced:

note dependency 'vlib' is declared by the root as ref = "tag/v0.2.0", overriding conf -> liba -> vlib, which requires ref = "tag/v0.1.0". nothing checks that 'vlib' accepts the root's selection
Overrides are unchecked

Because the closure is flat, a requirer's use b.* binds to whatever the root selected, even a major that requirer was never built or tested against. A passing build only shows that the code it reached compiled. mach dep verify prints the same note for every edge an override replaced, without failing. Treat each note as a claim to confirm.

A release selects only releases

A dependency reached through a release (a version range or an exact tag/) may itself select dependencies only by version or tag/, so a release is reproducible from its tag all the way down. A dependency reached through branch/ or commit/ is in development, and its manifest may use any selector. A release that breaks the rule is refused wherever it is reached, naming the chain and the offending line, unless the root declares the offending dependency itself. mach dep verify <path> --release holds the project itself to the same rule, so a library's release workflow catches the mistake before it tags:

error[dep.release_selector] project 'mf' is a release (checked by `mach dep verify --release`) whose manifest selects [dep.std] by ref = "branch/dev", so it cannot be reproduced from its tag. select [dep.std] by `version` or an exact `tag/` before tagging

The mach dep command

mach dep <command> <path> manages the tree under dep/. Every command takes the project path first and accepts --quiet / -q, and writes its progress lines to stdout.

CommandEffect
add <path> <name>Declare a dependency and realize it. Takes --git <url> with an optional --range <range> or --ref <ref>, or --path <dir>. Without a selector it writes the caret range of the highest compatible release.
update <path> (<name> | --all)The only command that moves a pin. Advances branch refs to their remote tips, moves exact refs to the commit they name, resolves version ranges, and refreshes path copies. Takes --lowest and --offline.
outdated <path>Report each version-selected dependency's pinned, highest compatible, and latest release.
pull <path>Realize every dependency at its pin without moving any pin, and sync each path copy with its source.
verify <path>Check the realized dependencies against the manifests without changing anything. --release also applies the release rule to the project itself.
remove <path> <name>Drop the entry from mach.toml. --purge also deletes dep/<name>/.
list <path>Print each declared entry with its source, selection, recorded pin, and whether it is realized or missing.
# add std at the caret range of its newest compatible release
mach dep add . std --git https://github.com/briar-systems/mach-std

# or state the range, or pin one exact tag
mach dep add . std --git https://github.com/briar-systems/mach-std --range "^9.0"
mach dep add . std --git https://github.com/briar-systems/mach-std --ref tag/v9.0.0

# see what newer releases exist, then move to them
mach dep outdated .
mach dep update . std

pull looks at what a git dependency's dep/<id> holds together with its record and takes the one step that brings it to its pin. A recorded gitlink with nothing checked out, as on a fresh clone, is initialized in place, a checkout at the wrong commit is moved to its pin, a clean unregistered checkout is registered, and with none of these the dependency is checked out at its selector. A symlink, a file, a directory that is not a checkout of its own, and a dirty checkout are refused and left alone. A version-selected dependency that has no pin yet is refused, with the mach dep update command to run.

update <name> looks the name up in the whole closure, so it can re-pin a transitive dependency the root does not declare. For an identity reached by more than one path, the root's selector wins if the root declares it. Otherwise the requirers must agree, or the command stops, prints both chains, and names the root declaration that would decide.

$ mach dep update . vlib
   updated  vlib  e651f9fe6e01 -> 8570e112d508  (pinned to the exact selector)

Pins

A dependency is the files in dep/<id>/ after mach dep pull. In a Git repository a git dependency is a submodule, and the record of which commit it is at is the gitlink the repository commits, generated into .gitmodules by mach dep. A project need not be a repository: in a plain directory, or a project nested inside an unrelated repository, git dependencies are plain checkouts whose own commits are verified. There is no lock file, and a mach.lock in the project root is an unrelated file no command reads. The verifier reads the Git index, so a freshly realized dependency is verifiable before it is committed. Path dependencies are verified from their copies.

The flat dep tree

The root's dep/ holds every identity in its transitive closure, one directory each, one level deep. The root manifest declares only what the root uses directly, plus any override. A dependency's own dependencies reach the root's dep/ through closure computation and need no declaration in the consumer. With a root that declares a, and a that declares b, the layout is dep/a/ and dep/b/, and a's use b.* resolves against the root's dep/b/. Git materializes a's own gitlink as an empty dep/a/dep/b/ that is neither realized, verified, nor descended into.

A build locates each dependency as the directory its key names under dep/ and reads its mach.toml for its id and src. That is all it does with dependencies: it never fetches, never writes under dep/, runs no Git, and checks no pin. A dependency with nothing checked out is refused naming mach dep pull.

One identity, one commit

One identity resolves to exactly one commit, with no exception for majors. Two different selections of one identity is a clash, and the diagnostic prints both chains and the root declaration that would resolve it (<url> stands for the repository URL as declared):

error[dep.conflict] project id 'vlib' is reached with two different selections
    conf -> liba -> vlib requires git <url> @ tag/v0.1.0
    conf -> libc -> vlib requires git <url> @ tag/v0.2.0
  the root decides by declaring the identity itself, for example
    [dep.vlib]
    git = "<url>"
    ref = "tag/v0.2.0"

The root's declaration may point at upstream or at a fork carrying the same id, with no change to any consumer. Two unrelated packages claiming one id is a collision and is rejected. URL disagreement is a mirror, not a conflict: the root's declared URL wins, else the first declaring path's, and verification compares commits, never URLs.

Removed forms

pull, verify, add and update refuse two shapes of a realized closure, and build and check refuse the first:

What mach dep verify checks

A build checks none of this. It builds what exists, so a stale or drifted dependency surfaces as a build error, and mach dep pull brings it back to its pin. mach dep verify checks, offline, that:

  1. every git dependency is a clean checkout at its applicable pin, and every path dependency is a contained tree without repository metadata,
  2. its project id equals its directory name,
  3. the closure computed from the realized manifests equals the set of directories under dep/, with nothing missing and nothing extra,
  4. there are no cycles,
  5. every realized manifest's [project].mach accepts the running compiler,
  6. for every identity selected by version, the pinned commit carries a release tag that is inside every requirer's range, the root's included, and every release in the closure selects only releases, and
  7. no identity is pinned at two commits across the closure.

Each failure names the identity and the command that fixes it. A checkout that drifted from the root's selection reads:

error[dep.ref_mismatch] dependency 'vlib' has exact ref 'tag/v0.2.0', required by conf -> vlib, which resolves to '8570e112d5084292ac472bd372f61d8f9fbb0f71', but the realized commit is 'e651f9fe6e01af77716ab37a5e58dda6b895296e'. run `mach dep update <path> vlib` for project '.' to re-pin it to the root's selection

For an identity the root does not declare, every requirer's exact tag/ or commit/ selector must match the realized commit. A root ref or path is the override, so the requirers' selectors are not checked against it. A branch/ selector is an input to update, never something verify checks.

Note

mach build never requires the network: a project whose dep/ tree is present builds on a bare machine. Only add, update, outdated, and pull reach a remote, through git found on PATH. A failed git command names the command, its exit status, and git's own message.

Importing a dependency

Once realized, a dependency's modules are imported by their full path: the first segment is the dependency's [project].id, and the rest is the module's path under its src.

use std.print;
use std.types.result.res;
use helper: helper.lib.helper;   # src/lib/helper.mach of project helper

A one-segment path names a project and no module, so use helper; is refused (use path 'helper' does not name a module). A library artifact's entry and export describe the prebuilt library the project ships and play no part in how a use path resolves.

Cascading link requirements

A dependency declares its own external link requirements as [link.X] entries. An entry marked export = true also applies to every project that depends on it, so a platform link requirement lives once in the manifest that needs it and out of every consumer. A standalone build and a consumed build read the same entries, so nothing behaves differently as a dependency. std's own entries are why a new project links on darwin and windows with no [link.*] table.

# in the dependency's manifest:
[link.kernel32]
source = "system"
name   = "kernel32.dll"
sys    = ["windows"]        # filtered out on any non-windows cell
arch   = ["*"]
abi    = ["*"]
export = true                # and cascades to every consumer

An entry applies to a build cell when all three of its sys, arch and abi axes match. Target names are local to each manifest and are never matched on.

See also

  • Manifest - the full mach.toml reference, including [project].mach, [dep.*], and [link.*]
  • CLI - every mach command, including mach dep
  • Modules - how source files map to importable module paths
  • Project layout - where dep/ and the manifest live in a project