Skip to main content

buildl_core/
lib.rs

1//! Domain data, ports, and pure pipeline logic for the buildl build system.
2//!
3//! This crate is the centre buildl's other crates depend on. It depends on no other buildl
4//! crate, on no Lua runtime, and on no filesystem, process, thread, network, environment or
5//! clock API, so every pipeline flow can be exercised against in-memory implementations of its
6//! ports.
7//!
8//! # Responsibilities
9//!
10//! - The domain values the pipeline phases hand to one another.
11//! - The ports: traits through which the pipeline reaches build-file evaluation, storage,
12//!   hashing, and execution.
13//! - The pure logic of each phase: Load → Resolve → Plan → Execute → Record.
14//!
15//! # Non-responsibilities
16//!
17//! - Implementing any port. Concrete adapters live in the `buildl-lua` and `buildl` crates.
18//! - Reading or writing anything. A value arrives from a caller or through a port.
19//!
20//! # Where things live
21//!
22//! | Module | Holds |
23//! |---|---|
24//! | [`error`] | the one error enum and the crate-wide `Result` alias |
25//! | [`types`] | the validated domain values — names, identities, instants, declarations and the plugins, if any, whose code made each, the target graph, action keys, cache rows, the ceiling, the plugins a workspace names and what it lets each have, the plan, ready actions, outcomes, log events, what one run was asked for — and the as-written values a build file stages |
26//! | [`json`] | the one canonical serializer every byte of output goes through, and the one reader that takes such bytes back |
27//! | [`ports`] | every trait in the crate: the ports through which everything outside it is reached, and the bundle that names one implementation of each, with the set that holds one instance of each |
28//! | [`load`](mod@load) | Load, the first phase: build files, and the last result of each discovery target, to validated, sorted declarations |
29//! | [`resolve`](mod@resolve) | Resolve, the second phase: declarations to the target graph, that graph cut down to the targets one run was asked for, and the targets a query reaches |
30//! | [`plan`](mod@plan) | Plan, the third phase: the target graph to a verdict per target, with what the build wants |
31//! | [`execute`](mod@execute) | Execute and Record, the last phase: a plan to one outcome per target, each result recorded as the last step of its action and reported once it is logged |
32//! | `placement` | where every output lives and what a command's placeholders stand for once they are placed; used by Plan, Execute and verify, and not a phase |
33//! | [`grants`] | the build's authority: which programs a build made, everything a build wants, the wants a plugin's targets may not have, which plugins are behind each want, and the approver's answer; not a phase |
34//! | [`pipeline`] | the phases run in sequence over one bundle of ports, one method per command, and the commands that need only some of the ports as functions over those ports alone: the three that reach only build files and the state that holds discovery results, `doctor`, and `log` |
35//! | [`history`] | the read side of the build log: the newest run of a target that left output |
36//! | [`verify`](mod@verify) | verify, which serves one command and is not a phase: cached targets put back in place, a sample of them re-run, and each re-run compared with what was cached |
37//! | [`watch`](mod@watch) | what a watching run decides from the changes it sees: what a batch of changed paths starts, and the loop that builds, waits and reacts; serves one command and is not a phase |
38//! | `discovery` | the last result of each discovery target, read back from the build log and the content store for a second load of the build files; several pipeline functions use it, and it is not a phase |
39//! | `suggest` | the nearest declared name to one that names nothing, for any module that refuses a name |
40//!
41//! Each phase has its own module, named for the phase. Five rules keep that arrangement
42//! navigable:
43//!
44//! 1. No phase module names another. Phases communicate through values, and [`pipeline`] owns
45//!    the sequence, so a phase that imported the next would be bypassing it. [`history`],
46//!    [`verify`](mod@verify), [`watch`](mod@watch) and `discovery` are not phases: each serves a command, or several,
47//!    and may name a phase's internals. `placement` and [`grants`] are neither: several phases
48//!    name them, and they name no phase.
49//! 2. [`types`] holds no decisions between *different* concepts. A type there may decide things
50//!    about its own kind — [`Timestamp::duration_since`] decides whether two instants of the
51//!    same type went backwards, [`Label::resolve`] decides which of three reference forms it was
52//!    handed — but logic needing two different concepts to decide something belongs to the
53//!    phase that decides it.
54//! 3. One concept, one type. [`types`] is the only place a domain value is declared, so nothing
55//!    downstream invents a second spelling of a name that already exists.
56//! 4. [`ports`] never gains a production implementation. A real implementation means doing I/O,
57//!    which this crate does not do; the only implementations here are test fakes.
58//! 5. Every phase module is exercised against in-memory implementations of the ports it uses.
59//!    Its pure helpers, which need no port, are tested in the file under test; its flows through
60//!    a port are tested in this crate's integration tests, against fakes built from the public
61//!    API alone, so anything a fake does an adapter crate can do too.
62//!
63//! # Status
64//!
65//! Pre-release. Load is implemented: [`load`](fn@load) walks the workspace's build files
66//! through the [`DeclarationSource`] port and returns their declarations validated and sorted,
67//! and [`Pipeline::check`] runs it twice to catch a build file that declares differently on
68//! each evaluation. Resolve is implemented: [`resolve`](fn@resolve) turns those declarations
69//! into a [`TargetGraph`], refusing a name declared twice, a reference that names nothing or the
70//! wrong kind of thing, and a dependency cycle; [`Pipeline::graph`] runs both phases. Plan is
71//! implemented: [`plan`](fn@plan) refuses a build that wants more than its workspace's
72//! [`Ceiling`] allows, gives every target an [`ActionKey`] from what the ports report, and
73//! compares it with the key the target's last build recorded, so each target is clean, dirty
74//! with a reason, or conditional on a dependency's output; the [`Plan`] also holds the value of
75//! every build setting for the run, which is where Execute reads them; [`Pipeline::plan`] runs
76//! all three phases. Execute and Record are implemented: [`execute`](fn@execute) gives every
77//! output a path under the workspace's output directory, expands every command and holds its
78//! program to the [`Ceiling`] before anything runs, then runs the plan through the
79//! [`Dispatcher`] port. A target whose finished key equals the one its last build recorded,
80//! with every recorded output still in the [`ContentStore`], is cached; any other runs through
81//! the [`ExecStrategy`] port.
82//! An output is a regular file or a directory of regular files: its [`OutputManifest`] is stored
83//! beside the files it names, and the digest of that manifest is the output's digest.
84//! Recording is the last step of each action — its outputs into the content store, then its
85//! [`CacheRow`], then one [`OutcomeEvent`] in the [`EventLog`] — so a failure leaves no row.
86//! [`Pipeline::build`] runs all four phases and returns one [`ActionOutcome`] per target; what
87//! a failure does to the rest of the build is the caller's [`OnFailure`]. [`Pipeline::build`]
88//! and [`Pipeline::test`] put the plan's wanted set to the [`Approver`] port after planning,
89//! and [`negotiate`](fn@negotiate) turns a denial into [`Error::NotApproved`]: a refusal runs
90//! nothing, even when every target is cached. [`Pipeline::plan`] never asks.
91//!
92//! A plugin's declarations carry an [`Origin`]: the chain of [`PluginRef`]s, outermost first,
93//! whose code made them, and a graph [`Node`] keeps it. [`plan`](fn@plan) holds every want of a
94//! node with plugins to each plugin in its chain, the plugin's manifest and the workspace's
95//! [`PluginCap`] for it, before it asks the [`Ceiling`]: a want outside either is
96//! [`Error::PluginRefused`], and so is a target that reaches the network when the cap does not
97//! allow it. An [`Attribution`] says which plugins' targets are behind each want; the plan, the
98//! [`Approver`] and a [`Checkup`] carry it for display, and it is no part of an [`ActionKey`].
99//!
100//! A run works on the whole workspace or on the targets a [`Scope`] names, with everything they
101//! depend on: [`narrow`](fn@narrow) cuts the graph down before Plan, so [`Pipeline::plan`] and
102//! [`Pipeline::build`] neither plan nor build a target outside it, while two outputs claiming one
103//! place are still refused across the whole workspace. A build writes down the graph of the whole
104//! workspace through the [`GraphRecord`] port before it plans, and is shown through the
105//! [`Reporter`] port as it goes: that it began, each action as a worker begins it, each target's
106//! outcome with what its action printed — for a cached target as for one that ran — and the count
107//! last. Under [`LiveOutput::OneAction`] one running action at a time is shown as it prints. An
108//! action a signal ended fails its target as [`OutcomeClass::Killed`], and an input that is not a
109//! regular file is refused by Plan from what the [`Digester`] reports as an [`InputStat`].
110//! [`Hasher`] takes the digest of bytes handed in pieces. A [`Query`], `deps` or `rdeps` of one
111//! target, asks which targets that target needs or which need it: [`reach`](fn@reach) answers it
112//! over a [`TargetGraph`], in the [`Direction`] it names. [`pipeline::check`], [`pipeline::graph`]
113//! and [`pipeline::query`] run the three commands that reach only build files for a caller that
114//! holds a [`DeclarationSource`], a [`RunHistory`] and a [`ContentStore`]. A workspace that
115//! declares a discovery target is loaded again with the last result of each, read from those two
116//! ports, and the second load may not declare different discovery targets than the first
117//! ([`Error::DiscoveryChanged`]); [`Pipeline::plan`], [`Pipeline::verify`] and [`gc`](fn@gc) do
118//! the same, and [`Plan::stale_discovery`] names the discovery targets a plan finds not clean.
119//! [`Pipeline::build`] and [`Pipeline::test`] build the discovery targets first, as a build of
120//! just them, and load the second time with the results that leaves.
121//! A [`LoggedRun`] reads back from the build log through
122//! [`RunHistory`], and [`pipeline::log`](fn@pipeline::log) returns a target's last run with what it
123//! printed. [`pipeline::doctor`](fn@pipeline::doctor) reports what a workspace's build wants
124//! against its ceiling as a [`Checkup`], every want outside the ceiling listed and none refused,
125//! for a caller that holds a [`DeclarationSource`], a [`Manifest`], a [`RunHistory`] and a
126//! [`ContentStore`].
127//!
128//! A target no cache row stands for may be served by a cache other machines share, reached
129//! through the [`RemoteCache`] port. [`Pipeline::set_remote`] sets the [`RemotePolicy`] of a
130//! build and a test run: a [`RemoteUse`], a [`RemoteFailure`] and a [`RemoteExecution`]. With
131//! the remote on, a worker asks the port for the result of a [`RemoteAction`] built from the ready action before it runs
132//! anything. A [`RemoteHit`] is used only when it is a clean success for the declared outputs;
133//! every blob is checked against its digest and stored before the row is written, and the target
134//! ends as [`OutcomeClass::Fetched`]. A miss runs the action, and under [`RemoteUse::ReadWrite`]
135//! a run that succeeded is uploaded, its blobs read from the content store through
136//! [`BlobSource`]. A failed call warns once and turns the remote off for the rest of the command
137//! under [`RemoteFailure::Lenient`], and fails the command under [`RemoteFailure::Strict`]; a
138//! result that is not usable is a failure of the same kind. [`NoRemote`] is the port of a
139//! workspace that names none, and every command but `build` and `test` never calls it.
140//!
141//! A verify compares what cached targets produce when re-run with what was cached.
142//! [`Pipeline::verify`] plans as a build does, asks the [`Approver`] as a build does, then
143//! [`verify`](fn@verify) puts the outputs of every target whose last build still stands back in
144//! place and re-runs a [`Sample`] of those targets through the [`ExecStrategy`] port. A
145//! [`Verification`] holds a [`Check`] per target it looked at: the re-run reproduced the outputs,
146//! differed at the first output and file an [`OutputDifference`] names, failed, or was never begun
147//! because a target it needs was not put back. It counts the targets with nothing cached and the
148//! ones the sample left out. A verify records no row, forgets none, logs no event and reports
149//! nothing, and no output of a re-run is put in place.
150//!
151//! This file holds only module declarations and re-exports, so it carries no logic to
152//! unit-test.
153
154mod discovery;
155pub mod error;
156pub mod execute;
157pub mod grants;
158pub mod history;
159pub mod json;
160pub mod load;
161pub mod pipeline;
162mod placement;
163pub mod plan;
164pub mod ports;
165pub mod resolve;
166mod suggest;
167pub mod types;
168pub mod verify;
169pub mod watch;
170
171pub use error::{
172    ActionFound, DeclarationField, DeclarationSite, DeclaredKind, Error, EvaluationFailure,
173    EvaluationLimit, NameKind, PortName, RemoteFlagConflict, RemoteVariable, Result, TreeFault,
174    UnsandboxedRemote,
175};
176pub use execute::{ensure_sandboxed_remote, execute};
177pub use grants::negotiate;
178pub use history::LastRun;
179pub use load::load;
180pub use pipeline::{Collected, Pipeline, Shown, doctor, gc, log};
181pub use plan::plan;
182pub use ports::{
183    ActionCache, Approver, BlobSource, Changes, Clock, ContentStore, Cycles, DeclarationSource,
184    Digester, Dispatcher, EventLog, ExecStrategy, GraphRecord, Host, IgnoreRules, Manifest,
185    NoRemote, NothingIgnored, PortSet, Ports, RemoteCache, Reporter, RunHistory, StatCache, Sweep,
186    ToolResolver, WatchReport,
187};
188pub use resolve::{narrow, reach, resolve};
189pub use types::{
190    Action, ActionKey, ActionOutcome, Alias, Approval, ApproverMode, Argument, Attribution, Blob,
191    BuildFile, Built, CacheRow, Captured, Ceiling, ChangeBatch, ChangedPath, Channel, Check,
192    Checkup, Command, CommitId, Cycle, Declaration, DeclarationOrder, Declared, DecodedTree,
193    DenialReason, DepProducts, Description, Diagnostic, Difference, Digest, Direction, Directory,
194    DirtyReason, Discovered, EntryChange, EntryKind, EntryName, EnvMark, EnvName, EnvValue,
195    Evaluated, Executed, Execution, Exit, ExpandedDescription, FieldName, FileChange, FileMode,
196    FileStat, Found, FoundEntry, FoundKind, Freshness, GitUrl, Hasher, HeaderName, HostPath,
197    IgnoredPath, Ignoring, InfraKind, InputDigest, InputKind, InputStat, InstanceName, Isolation,
198    KeyComponents, Label, LiveOutput, LiveSink, Loaded, LogLine, LoggedRun, MachineFolders,
199    NetworkAccess, Node, NodeId, OnFailure, Origin, OutcomeClass, OutcomeEvent, OutputDifference,
200    OutputManifest, OutputName, OutputPath, OutputRefusal, OutputSource, OwnComponents, Plan,
201    PlannedKey, Platform, PlatformProperties, PlatformPropertyName, PluginCap, PluginChain,
202    PluginName, PluginNetwork, PluginRef, PluginSource, PluginVersion, PluginViolation, Produced,
203    Program, Provenance, Purpose, Query, RawOutcome, Reaction, ReadyAction, Reference,
204    RemoteAction, RemoteCall, RemoteContent, RemoteExecution, RemoteFailure, RemoteFile, RemoteHit,
205    RemoteJobs, RemoteNotice, RemoteOutput, RemoteOutputDirectory, RemoteOutputFile,
206    RemotePlatform, RemotePolicy, RemoteScheme, RemoteSettings, RemoteSettingsBuilder,
207    RemoteSwitch, RemoteTree, RemoteTreeFile, RemoteUpload, RemoteUrl, RemoteUse, Rerun,
208    RetryCount, Rev, RootPath, Rule, RunChoices, Sample, Scope, Setting, SettingName, SettingValue,
209    SourcePath, StagedAlias, StagedDeclaration, StagedEnv, StagedFile, StagedItem, StagedMark,
210    StagedRule, StagedSetting, StagedSubdir, StagedTarget, StatEntry, StoredFile, Summary, TagName,
211    Tally, Target, TargetGraph, TargetName, TargetRole, Timeout, Timestamp, TokenSource,
212    ToolIdentity, Tree, TreeEntry, TreePath, Trigger, Turn, Unfinished, Uploaded, Verdict,
213    Verification, Want, WantedSet, WatchLayout, Watched, Workers, Workspace, Written,
214};
215pub use verify::verify;
216pub use watch::{react, watch};