Dependency Resolution
{baler} internals explanations
{baler} internals explanations. An article for developers.
This article covers how {baler} resolves dependencies internally, following two paths: cran::client::resolve_all and the solver behind it for R package dependencies, and ops::module_graph::resolve_transitive for baler-installed module dependencies. Specifically: what runs when baler install --install-deps resolves and installs R packages, and what runs when a module’s [dependencies.baler] table gets walked transitively.
Fetching indexes
Package indexes are fetched lazily: one PACKAGES.gz per repository, at most once per run, and only when a package has to be looked up there. The fetch retries: three attempts, exponential backoff (2^attempt seconds) between them, on both non-2xx responses and request failures.
CRAN’s Archive/ listings and archived tarballs are fetched later and less often: only when the solver has to step back from a package’s newest release.
Resolution
All requested packages are solved together, as one graph, by a PubGrub-based solver (resolver.rs), driven by resolve_all in cran/client/graph.rs. Two things follow from that:
- A choice that leads to a conflict is undone and the next candidate version is tried, instead of the run failing on the first fit.
- Each candidate version is judged by its own dependencies, not by the newest release’s.
How the solver picks versions and explains a conflict is in The dependency solver. What matters here:
- Base and recommended R packages are never looked up.
- A package listed in
baler.tomlthat is missing from its index is an error naming the repository. - A dependency found in no repository is skipped with a warning, and
{baler}assumes it is already installed (a Bioconductor package, for example). Anything that only that package would have pulled in is skipped along with it. - A package’s
Imports,DependsandLinkingTofields are read whole before they are parsed. The index wraps long fields at a fixed width, even in the middle of an entry (lifecycle (>=on one line,1.0.3)on the next), so reading line by line tears one dependency into two bogus ones. A record whose dependencies cannot be read is skipped with a warning that says why.
Where packages come from
A package belongs to exactly one repository, decided in one place, so it never resolves differently depending on which package asked for it.
A package listed in
baler.tomlcomes from its declaredrepo, or the default CRAN mirror.Any other package is searched for in the default CRAN first, then in the declared repositories in alphabetical order. The first one that has it wins. A private repository therefore cannot take over a CRAN package name for a dependency nobody declared.
Two dependents asking for one package from different repositories is an error that names both. This is what stops a package pulled in from two repositories from silently landing on two different versions.
Version selection
For each package, the solver takes the first version that satisfies every constraint reaching it, in this order: the version baler.lock pins (when it comes from the same repository), the newest version in the index, then the newest archived version.
The archived versions come from fetch_archive_versions, which scrapes CRAN’s Archive/<pkg>/ HTML directory listing (a plain substring scan for href="...", not a full HTML parser). When the solver steps back to an archived release, fetch_archive_deps downloads that release’s tarball and reads the DESCRIPTION inside it, because the index only describes the newest release.
Versions compare the way R compares them (Ver in ver.rs), so 1.2.3.9000 is above 1.2.3 and 4.0-3 equals 4.0.3. The constraint syntax is in Version constraints.
Install order and installation
Resolved packages install dependencies first, in the order of the solver’s dependency edges, alphabetical among equals so the order is the same on every machine. For each one, an existing install whose version equals the resolved one under R’s ordering (0.3-1 and 0.3.1 are the same version) is left alone. Anything else gets (re)installed.
Installation prefers a binary archive over building from source, but what counts as “a binary is available” differs by platform, and by what repo you’ve configured.
Where the binary URL comes from
If you are on Windows,
{baler}tries to fetch atbin/windows/contrib/<r-version>/<pkg>_<version>.zip, against whatever repo is configured. CRAN’s default mirror has always served this.If you are on MacOS,
{baler}then tries to fetch atbin/macosx/<macos-arch>/contrib/<r-version>/<pkg>_<version>.tgz, also against the configured repo directly, no special repo required.<macos-arch>isbig-sur-arm64orbig-sur-x86_64, picked fromR.version$arch. This is deliberately not the flatbin/macosx/contrib/path some mirrors still serve: that legacy path is x86_64-only, and using it unconditionally hands an Intel binary to an Apple Silicon machine, which only fails later atdlopen()time, not at install time. An architecturebinary_url_fordoesn’t recognize skips the binary attempt entirely and goes straight to source, with a warning.If you work on some Linux distros, it only works against a Package Manager-style repo (Posit Package Manager, formerly RSPM), and only when a distro codename could be detected from
/etc/os-release’sVERSION_CODENAMEfield (Ubuntu and Debian set this; RHEL and openSUSE generally don’t). When both hold, the URL is<repo>/bin/linux/<distro>-<arch>/<r-version>/src/contrib/<pkg>_<version>.tar.gz. CRAN’s default mirror has no such tree at all, so on a plaincloud.r-project.orgrepo, or an unrecognized distro, this step never even fires. There’s no dependency oninstall.packages()’s usual user-agent sniffing here: distro, architecture, and R version are all spelled out in the URL itself, which is exactly why this scheme was picked over Package Manager’s other__linux__/<distro>URL format.
Verifying what came back
A downloaded binary archive is only trusted if it contains Meta/package.rds. A real, pre-built package always has it, a source tree never does. This catches a source tarball served where a binary was expected, or a truncated download, before either is ever unpacked into the library.
Every compiled library inside the archive (.so, .dll, .dylib) gets its actual architecture read straight out of its own header (ELF, PE, or Mach-O respectively) and checked against what the current machine needs. This isn’t redundant with picking the right URL: Package Manager has been observed returning HTTP 200 for an architecture-tagged URL even when the file it serves doesn’t match, instead of the clean 404 a plain CRAN mirror gives for the same kind of miss. A URL-only check would silently accept that.
Falling back to source
<repo>/src/contrib/<pkg>_<version>.tar.gz, then Archive/<pkg>/<pkg>_<version>.tar.gz on a 404, then <pkg>_<version>.9000.tar.gz (CRAN’s dev-version convention) on a second 404. Whichever tarball comes back gets handed to R CMD INSTALL --no-multiarch --no-docs --no-help.
A failed install of a directly requested package aborts the whole run. A failed install of a package that only showed up as a transitive dependency gets logged as a warning, and the run continues.
Archive extraction safety
Both the .zip and .tar.gz binary-extraction paths sanitize every entry path before writing anything to disk. Absolute paths and any .. component get rejected outright, and on Unix, a symlink target gets dropped if it’s absolute or contains a parent-directory component. Extraction always lands in a temp staging directory first. Only after Meta/package.rds is confirmed does the staged package get moved into the real library, via a rename, falling back to a recursive copy if staging and the library happen to live on different filesystems.
Module dependency graph
{baler}-installed modules (under [dependencies.baler] in baler.toml) resolve through a completely separate path from R packages: resolve_transitive in ops::module_graph. It shares no code with the CRAN resolution above. No repo grouping, no binary/source install step, no CRAN index. A module dependency gets fetched as a whole baler.toml from its declared git or url source (see the baler.toml reference), not resolved against a package index.
Why this walk is DFS, not BFS
The graph is walked depth-first on purpose, keeping an explicit ancestor path: the chain of module names from the root down to whatever’s currently being resolved. This isn’t an implementation detail you could swap out. A “have I seen this module name before” set on its own can’t tell a real dependency cycle (A depends on B depends on A) apart from an ordinary diamond dependency (two modules both depending on the same third module). Both just look like “this name showed up twice.” Only ancestry, whether the name is still an open call on the current path versus already finished and closed off, tells them apart. A queue-based (BFS) walk has no notion of ancestry, only of what’s already been dequeued. Only a call stack, or an explicit path Vec standing in for one, gives you that.
Resolution steps, per module dependency
- If the module name is already fully resolved, from an earlier branch of the walk, a diamond, its resolved version and source get checked for consistency against the current dependent’s constraint instead of being fetched again. A mismatched source or an incompatible version constraint fails loudly, and will name both requesters.
- If you have a module name that is already on the current ancestor path, that’s a real cycle. The walk fails with the full chain (e.g.
a -> b -> a) instead of looping forever. - A module dependency declared with neither
gitnorurl(a bare string, or a table with onlyversion) fails at this step, since there’s no default module registry to fall back to (unlike package deps, which default to CRAN). A table that combines contradictory keys, bothgitandurl, orbranch/tag/rev/module_dirwithoutgit, never reaches this walk at all: it fails whenbaler.tomlis loaded, before resolution starts. - Otherwise, the dependency’s
baler.tomlgets fetched from its declared source. Its declaredversionunder[project]is checked against the requesting constraint, its own R package entries under[dependencies]fold into the same package-resolution set used everywhere else, and its own[dependencies.baler]entries get recursed into.
What fetches a module
ModuleFetcher is a small trait: fn fetch(&self, source: &ModuleSource) -> Result<BalerToml>. resolve_transitive() only knows how to walk the graph, not how to fetch a baler.toml. ModuleSource is either a git source (a github.com URL, plus an optional ref and module_dir) or a url source (a direct link to an already-bundled .tar.gz).
The production implementation, GitHubFetcher, handles both. A git source builds the same GitHub tarball URL baler install --git uses, downloads and extracts it, then reads baler.toml from the resolved module_dir. A url source downloads the tarball directly and reads its baler.toml without extracting anything else. Both require the network feature, same as baler install’s own --git/--url handling (see CLI Commands).
A module dependency has exactly one candidate version, the one its source declares, so there is nothing for the solver to choose between. The module walk checks each module (source, version, cycles) and hands the R package constraints it collects to the solver.
baler lock walks the module dependencies that declare a source, locks the R packages they need, and pins each module at the commit its ref pointed at. A module dependency with no source is skipped with a warning.
baler install --install-deps installs the module dependencies that are not installed yet, each from its declared source, at the pinned commit when the lock has one. Before installing, it checks that the source is the module the entry names and that the version it declares fits the entry’s constraint. A module that is already installed is left alone.
The package dependencies collected along the way are merged: constraints on the same package are combined into one. Two dependents that each name a different repository for one package is an error, and a dependent that names no repository does not clash with one that does.