Skip to main content

Architecture

{baler} internals explanations

Articles only suitable for developers

Workspace layout

{baler} is a CLI tool built in Rust and it is used to make {box} modules to be more manageable. What does the crates/ workspace looks like?

crates/
├── baler-cli/              # bin + lib crate: clap CLI, thin command wrappers
│   └── src/
│       ├── lib.rs                 # Cli struct, clap subcommands, run()
│       ├── bin/baler.rs           # fn main() { baler_cli::run(); }
│       └── commands/              # one file per subcommand, delegates to baler-core::ops
│           └── box_pkg/           # `baler box`, the one command with logic of its own (see below)
├── baler-core/             # lib crate: all real logic, reusable outside the CLI
│   └── src/
│       ├── baler_toml/            # baler.toml schema + parsing, one file per table
│       ├── lockfile.rs            # baler.lock schema, read/write
│       ├── version.rs             # semver-based version requirements (module versions)
│       ├── ver.rs                 # R-native package versions and requirements
│       ├── resolver.rs            # PubGrub-based dependency solver
│       ├── paths.rs               # install-dir / R-lib-dir resolution
│       ├── installed.rs           # reads the install dir back: .dist-info folders, list, find
│       ├── license.rs             # SPDX check of `license`, warnings for bundle
│       ├── project_files.rs       # finds a project's license files and README
│       ├── formats/               # tar.gz archive read/write
│       ├── cran/                  # R package fetch, resolve, install
│       │   ├── packages.rs        # PACKAGES.gz fetch + parse
│       │   ├── universe.rs        # the CRAN side of the solver: repositories and Archive/ lookups
│       │   ├── client.rs          # dependency resolution + install orchestration
│       │   ├── client/            # graph.rs (resolution), download.rs (source installs), git.rs (GitHub packages)
│       │   └── binary_install.rs  # binary-archive extraction
│       └── ops/                   # one module per CLI command's actual logic
│           ├── init.rs, bundle.rs, compile.rs, lock.rs, remove.rs
│           ├── resolve.rs, module_graph.rs, external_tools.rs
│           ├── list.rs, show.rs   # text of `baler list` and `baler show`
│           └── install.rs         # with install/: archive.rs, github.rs, native.rs
└── native-code-handler/    # the `baler-native` package: compiled-code scaffolding and build
    └── src/
        ├── detect.rs      # locates native-code dirs inside a module's source tree
        ├── scaffold.rs    # writes example native code for `baler init --native`
        ├── toolchain.rs   # R CMD SHLIB invocation, target/arch detection, .abi.json sidecars
        ├── cache.rs       # build cache, keyed by source hash, platform, and R version
        ├── lang.rs        # NativeLang / Backend enums
        └── templates/     # per-language scaffold templates (c, cpp, fortran, R glue)

baler-cli owns argument parsing and process exit codes. baler-core owns everything that could plausibly be reused by something other than the CLI, and the network feature flag lives on this crate. baler-native (the package in the native-code-handler/ directory) owns everything to do with locating, scaffolding, and building a module’s compiled code, independent of both the CLI and the packaging logic in baler-core.

baler box

baler box install and baler box status are the one command pair whose logic lives in baler-cli instead of baler-core::ops. Nothing in it is meant for reuse outside the CLI: it only composes public pieces of baler-core (paths::detect_r_platform, paths::resolve_r_lib_dir and cran::binary_install::install_binary_package) with a release lookup. If something else ever needs it, moving the module into baler-core/src/ops/ changes no command line.

commands/box_pkg/
├── mod.rs           # install and status: the flow, and the guards before anything is replaced
├── asset.rs         # picks the build for an R version and platform out of a release's file names
├── description.rs   # reads an installed {box}'s DESCRIPTION and baler-source.txt, no R needed
└── release.rs       # GitHub release lookup, downloads, SHA-256 check

asset.rs and description.rs do plain string and file work, so every platform and R version is unit tested without a network or R. tests/box_tests.rs runs the real binary against a local server that plays the release API, and a fake Rscript, so the whole flow is tested on Unix without leaving the machine.

The installer scripts never contain this logic. They only print a hint, or run baler box install when BALER_WITH_BOX=1, so there is one implementation of the platform and checksum rules, and it also serves people who installed baler with cargo install.

What goes into a bundle

baler bundle writes a <name>_<version>.tar.gz that contains a single top-level directory, <name>_<version>/:

tstk_0.1.0/
├── tstk/              # the module's source directory (the one with __init__.r)
│   ├── __init__.r
│   └── ...
├── baler.toml
├── baler.lock         # only if the project has one
├── LICENSE            # every license file the project has
└── README.md          # the README, if the project has one

baler.toml, baler.lock, the license files and the README are shipped as they are, not as a generated copy of them, much like a Python sdist ships its own pyproject.toml. baler install reads baler.toml back out of the archive, so the archive is self-describing without a second metadata file. Earlier versions embedded a generated manifest.json here, but it duplicated baler.toml and has been removed.

Which files are included:

  • Every file under the module’s source directory, except hidden (dot-prefixed) files and directories, which are never bundled.
  • The one exception is .lib/, which is dot-prefixed but is forced into the archive when binary = true is set under [compiled-code]. Binaries are stored per platform as .lib/<platform>/, each artifact with an .abi.json sidecar. See Binary bundles for what install does with them.
  • The license files and the README from the project root. The README is stored as README plus its ending, wherever the project kept it.
  • The native source, always. binary only decides whether compiled binaries ship along with it.

Path resolution

Two directories matter at runtime, and both follow an environment-variable-first, sensible-default-second pattern.

Under paths::resolve_install_dir():

  1. BALER_LIB, if set and non-empty.
  2. ~/.baler/modules.

Under paths::resolve_r_lib_dir():

  1. BALER_R_LIB, if set and non-empty. The explicit override, meant for renv/rv-style project-local libraries.
  2. R_LIBS_USER, if set and non-empty. R’s own variable.
  3. A subprocess fallback: Rscript -e "cat(.libPaths()[1])".

The third step is a real subprocess call. Invoking baler install --install-deps without R/Rscript on PATH, and without either environment variable set, fails there with an error asking whether R is installed and on PATH. If Rscript runs but reports no library path, the error points at BALER_R_LIB instead.

The build cache has its own override, BALER_CACHE_DIR, described under Build cache.