Skip to main content

Lockfile for {baler}

baler.lock is a lockfile (like Cargo.lock or poetry.lock from Python) that pins the exact R package versions (and repos, or commits for packages from GitHub) that {baler} resolved the last time you ran baler lock. Commit it next to baler.toml so later installs, either by you or by anyone else, will reuse those pins instead of re-resolving against CRAN.

Three kinds of R packages are locked: runtime packages under [dependencies], build-time packages under [compiled-code]’s build_deps, and packages under [extras]. Each entry records which of these it is needed for (see groups below). {box} modules under [dependencies.baler] that declare a source are locked too, as [[module]] entries (see Modules). External tools under [extras.external] are never locked either, since {baler} only checks for them.

Format

The file is TOML. A lock with no modules and no GitHub packages is written as format version 2, and a lock with [[module]] entries or a package from GitHub as version 3. Locks written with format version 1 are still read as they are, since a package without groups means the same thing in both. A lock in a format newer than the {baler} reading it knows is rejected with a message to update {baler}. The table below summarizes the blocks and fields under baler.lock:

Field Meaning
version Lock format version (not a package version). Defaults to 2 if omitted when reading.
r_version The R version that produced this lock, e.g. "4.2.0". This is totally optional, and it has rather no use. This is only written when baler lock is run with --with-rver. The constraint actually enforced lives in baler.toml’s r_version field, checked separately at lock and install time (see below).
[[package]] One entry per pinned R package. Entries are written sorted by name so the file stays stable and diffable.
name Package name as it appears on the repo.
version Exact version baler resolved (e.g. "1.2.2"). It is stored as a semver string, so a CRAN version such as 0.3-1 is written 0.3.1. Baler works out the published spelling again from the index when it installs.
repo Repo URL used for that resolution (default CRAN mirror, or an explicit repo from [dependencies]). For a package declared with git, it is the GitHub source instead, written git+<url>[@<ref>]#<commit> (see Packages from GitHub).
groups What the package is needed for: runtime, build (from [compiled-code]), or extras (from [extras]). When omitted, the package is needed at runtime only, which is the common case. A package needed by more than one group lists all of them, runtime included. An unknown group name is a parse error, so a typo fails loudly.
[[module]] One entry per {box} module under [dependencies.baler] that declares a source. Written sorted by name.
name, version (module) The module’s name, and the version its own baler.toml declared when the lock was written.
git, ref, module_dir For a git source: the repository, the branch, tag or rev exactly as written in baler.toml, and the directory holding the module. Only the keys that were set are written.
rev The commit ref pointed at when the lock was written.
url For a url source: the archive URL. The URL is the pin, so there is no rev.

Here’s an example:

version = 2

[[package]]
name = "Rcpp"
version = "1.0.12"
repo = "https://cloud.r-project.org"
groups = ["runtime", "build"]

[[package]]
name = "dplyr"
version = "1.1.3"
repo = "https://cloud.r-project.org"

[[package]]
name = "rlang"
version = "1.2.0"
repo = "https://cloud.r-project.org"

[[package]]
name = "testthat"
version = "3.2.1"
repo = "https://cloud.r-project.org"
groups = ["extras"]

Groups are worked out per dependency set, not only for the packages you declare yourself. A package that is only pulled in by testthat is extras too, and a package that both a runtime dependency and a build dependency need is tagged with both.

r_version isn’t in the example above since it’s not written by default. This field is totally optional and only generated when you pass --with-rver to baler lock (see below). Here’s what it looks like when you do:

version = 2
r_version = "4.2.0"

[[package]]
name = "dplyr"
version = "1.1.3"
repo = "https://cloud.r-project.org"

Packages from GitHub

A package declared with git in baler.toml is locked with its source and the commit it was resolved to, in repo:

version = 3

[[package]]
name = "BH"
version = "1.87.0-1"
repo = "git+https://github.com/eddelbuettel/bh#0123456789abcdef0123456789abcdef01234567"
groups = ["build"]

[[package]]
name = "cpp11"
version = "0.5.2"
repo = "git+https://github.com/r-lib/cpp11@main#89abcdef0123456789abcdef0123456789abcdef"

The @<ref> part is the branch or tag exactly as written in baler.toml, and it is left out when none was set. A package pinned with rev has no @<ref>, only the commit. The #<commit> part is always the full commit that was read, and it is what gets installed.

A plain baler lock keeps that commit even if the branch has moved, and baler lock . --update BH moves it. baler install --install-deps downloads that commit. If git, the ref or the rev changes in baler.toml, the old pin no longer matches: baler lock resolves the package again, and an install stops with a message that the lock is stale. Changing a package between CRAN and GitHub counts as a change in the same way.

Modules

A module under [dependencies.baler] that declares a source gets a [[module]] entry, and the lock is then format version 3:

version = 3

[[module]]
name = "rcalc"
version = "0.1.0"
git = "https://github.com/baler-dev/baler"
ref = "main"
module_dir = "examples/modules/rcalc"
rev = "9fceb02"

[[module]]
name = "tools"
version = "1.0.0"
url = "https://github.com/user/repo/releases/download/v1.0.0/tools_1.0.0.tar.gz"

rev is the commit that ref pointed at when the lock was written. A plain baler lock keeps it even if main has moved, and baler lock . --update rcalc moves it. baler install --install-deps fetches that commit. If git, ref or module_dir changes in baler.toml, the old pin no longer matches and is ignored. A url module has nothing to pin beyond its URL.

Writing the lock

Generating the lockfile runs a simple command through baler lock <path/of/the/proj>, just isn’t the same process as Cargo.lock being generated through builds. Run the following command and this will generate the certain lockfile that pins the dependencies:

baler lock .

What happens is that {baler} will read and validate baler.toml (the same checks baler install makes: a valid semver version, consistent keys in each [dependencies.baler] entry, and valid entries under [extras.external]), then resolves the runtime packages, the [compiled-code] build deps, and the [extras] packages, each on its own (honoring any existing lock unless you pass --update). The three sets are merged into one entry per package, and baler.lock is written next to baler.toml. If the sets resolve the same package to different versions, baler lock stops with an error instead of picking one. Align the version constraints in baler.toml and run it again. You are also allowed to re-resolve the pinned dependencies under the lockfile by passing --update. Here’s an example:

baler lock . --update

--update discards the existing pins and re-resolves everything from scratch. Use it when you intentionally want newer upstream versions. To move only some packages or modules, name them, and every other pin stays:

baler lock . --update purrr cli

This is also how a package from GitHub is moved to the newest commit of its branch.

A project with no dependencies resolves to an empty set: the lock is still there written, but with no [[package]] entries.

baler lock also fetches the modules under [dependencies.baler] that declare a source, so it needs network access in that case. It locks the R packages those modules need together with your own, and pins each module. A module that declares no source is skipped with a warning, since there is nothing to fetch.

Running it again

A plain baler lock never moves a pin that still satisfies baler.toml, including the transitive packages you never declared. A module stays at its pinned commit even if its branch has moved. If the lock already matches, the file isn’t touched at all, not even its modification time. If baler.toml changed, {baler} keeps what still fits, adds or drops what the manifest changed, re-resolves only a package whose repo or constraint no longer fits its pin, and prints what moved. This also means two machines with different R or CRAN states get the same lock, as long as baler.toml is the same.

Checking the lock in CI

baler lock . --check

--check never writes. It fails if baler.lock is missing, or if a normal baler lock would change it, and lists the differences. It can’t be combined with --update, --with-rver or --remove.

Recording the R version

By default, the lock doesn’t record which R wrote it. Let’s say two people on different R versions running baler lock against the same baler.toml should get the exact same [[package]] output, and if r_version was always written that’d turn into a pointless diff every time. Pass --with-rver if you want it recorded anyway:

baler lock . --with-rver

A plain run leaves an existing r_version exactly as it is, since it describes whichever machine wrote it. --with-rver is only allowed when the lock is being created or updated; on an existing lock, use baler lock . --update --with-rver. A full --update refreshes r_version only if the old lock recorded one.

It is added as part of proposal, and this can be mostly useful for debugging. Say someone reports a weird install and you want to know what R wrote their lock. It still doesn’t change or dictate what’s enforced at install, that’s still baler.toml’s r_version job.

Removing the lock

You can manually delete baler.lock whatever, whenever, and however you want. {baler} just adds another option on baler lock command through passing --remove, which deletes baler.lock and stops there. Nothing will be resolved on {baler} once baler.lock, but nothing is broken, so rest assured. Also, --remove can’t be combined with --update, --with-rver or --check. There’s a nuance and that is to safely remove the lockfile. To repeat, a missing lock just means {baler} resolves [dependencies] under baler.toml fresh, same as a project that never had one.

baler lock . --remove

How installs use the lock

Almost nothing throws a failure when the module doesn’t have a lockfile. When we say “almost”, {baler} still conveniently resolves the usual way to install the package dependencies, although having a lockfile is nice-to-have for better reproducibility. The problem is that when you DO have a lockfile but malformed and/or unreadable will throw a failure. That’s why a broken lock file fails loudly instead of being silently ignored.

When baler.lock is present in the project root:

  • Every runtime package listed in the lock, including the transitive ones you never declared, is installed at that exact version and repo. A package from GitHub is installed at its locked commit, and an install is skipped only when that version and commit are already installed.
  • Packages tagged only build or extras stay pinned in the lock, but a normal install does not install them. Build packages are installed when the module’s native code is built, and [extras] packages are installed only with baler install --install-deps --extras, at their locked versions.
  • A package declared in baler.toml but missing from the lock, or one whose repo or version constraint no longer fits its pin, is an error telling you the lock is stale. Run baler lock and commit the new lock.
  • A module that is not installed yet is installed by baler install --install-deps from its declared source, at the pinned commit when the lock has an entry for that source. If git, ref or module_dir changed in baler.toml, that entry is ignored and the declared source is used.
  • baler install never writes the lock.

baler bundle checks the lock the same way before packaging: it warns when the lock is missing or stale, and baler bundle --locked fails instead. A lock that holds modules is also stale when a module that declares a source is not pinned in it, or is pinned at a version baler.toml no longer accepts. A lock written before modules were pinned is not asked for them.

R version

{baler} still cares about the R version, just not through the lock. baler.toml’s [project] r_version is a version constraint, like ">=4.4.0" or "^4.4.0", written the same way you’d write a [dependencies] version. Both baler lock and baler install check the R on PATH against this constraint before doing anything else, and if it doesn’t match, that’s a hard error, not a warning.

baler.lock’s r_version field is different. It just records what R wrote the lock, rather than some constraints, and only if you passed --with-rver. What’s actually enforced is always baler.toml’s floor, checked against whatever R you have right now. What you need to do is to write it as a floor like ">=4.4.0", not an exact pin. An exact pin means only people on that one exact R version can even install the module.

Design notes

  1. Packages are sorted by name on write so routine re-locks do not produce noisy diffs when the resolved graph is unchanged.
  2. An invalid version string inside a lock entry fails hard on lookup rather than falling through to a fresh resolve.
  3. The lock format version field exists so future schema changes can be detected and handled without silently misreading old locks. Version 2 only added the optional groups field, and version 3 added [[module]] entries and packages from GitHub. A lock with neither is still written as version 2, so a project that does not use them keeps a lock that older {baler} can read.
  4. r_version is opt-in (--with-rver) rather than always written, for the same diff-stability reason as (1): it depends on the machine running baler lock, not on baler.toml, so recording it unconditionally would defeat (1) for any two contributors on different R installs.
  5. groups is omitted for runtime-only packages, so a project with no [compiled-code] build deps and no [extras] writes the same entries as before.
  6. Versions of external tools are never written to the lock. They depend on the machine, not on baler.toml, which is the same reasoning as (4).
  7. A lock in a format newer than {baler} knows is rejected with a message to update {baler}, instead of failing on a field it does not recognize.