buildl-core 0.3.1

Domain data, ports, and pure pipeline logic for the buildl build system — no I/O, no Lua.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! A task described as a remote cache is asked about it.
//!
//! Its own file because the remote cache is keyed by a description of the work, not by buildl's
//! own key, and building that description reads the content store: every file a dependency
//! produced is part of the input root, and the key holds only the digest of each output's
//! manifest. The fold from a task and the execution settings into that description is pure
//! apart from those reads, so it is kept apart from running anything.
//!
//! Responsibilities: [`remote_action`].
//!
//! Non-responsibilities: asking the remote, encoding the description for the wire, and reading
//! the bytes of a declared input, which are the remote adapter's.

use std::collections::BTreeMap;

use crate::error::{Error, PortName, Result};
use crate::execute::perform::Task;
use crate::ports::ContentStore;
use crate::types::{
    ActionKey, Diagnostic, Digest, Execution, OutputManifest, OutputPath, PlatformProperties,
    RemoteAction, RemoteFile, RemotePlatform, RootPath,
};

/// The `RemoteAction` of `task`, run under `execution`.
///
/// The environment is the action's own `key` variables with the variables the strategy pins laid
/// over them, as the strategy sets them where the command runs. An `unkeyed` variable is left
/// out: its value is the machine's, and the description is what the remote caches by. The
/// outputs are sorted and hold no path twice. A target whose output is its standard output keeps
/// its path among them: the command is
/// asked for a file there, so that a server reports one the command wrote, and the result is
/// then refused for it. The files are every declared input, with no mode, and every file of
/// every output of every dependency, with the mode its manifest records, all sorted by path.
/// Each dependency output's manifest is read from `cas` by the digest the key holds for it. The
/// `properties` are the workspace's own, carried as they are; they take no part in the action
/// key.
///
/// # Errors
///
/// Returns [`Error::Adapter`], naming [`PortName::ContentStore`], when the task's action and
/// its key do not name the same number of dependency outputs, or when a dependency output's
/// manifest is not held, cannot be read, or is not a manifest; and whatever `cas` returns.
pub(crate) fn remote_action<C: ContentStore>(
    task: &Task,
    execution: &Execution,
    properties: &PlatformProperties,
    cas: &C,
) -> Result<RemoteAction> {
    let (ready, key) = (&task.action, &task.key);
    let mut env: BTreeMap<_, _> = ready
        .env
        .iter()
        .filter(|(name, _)| !ready.unkeyed.contains(*name))
        .map(|(name, value)| (name.clone(), value.clone()))
        .collect();
    env.extend(execution.strategy.pinned());

    let mut outputs = ready.outputs.clone();
    outputs.sort();
    outputs.dedup();

    let components = key.components();
    let mut files: Vec<RemoteFile> = components
        .own
        .inputs
        .iter()
        .map(|input| RemoteFile {
            path: RootPath::from(&input.path),
            digest: input.digest,
            mode: None,
        })
        .collect();
    let produced: Vec<_> = components
        .deps
        .iter()
        .flat_map(|dep| &dep.outputs)
        .collect();
    if produced.len() != ready.deps.len() {
        return Err(Error::Adapter {
            port: PortName::ContentStore,
            diagnostic: Diagnostic::new(format!(
                "the action of {} names {} dependency outputs but its key holds {}",
                ready.label,
                ready.deps.len(),
                produced.len()
            )),
        });
    }
    for (at, produced) in ready.deps.iter().zip(produced) {
        files.extend(output_files(at, &produced.digest, cas)?);
    }
    files.sort_by(|left, right| left.path.cmp(&right.path));

    Ok(RemoteAction {
        salt: key.digest(),
        command: ready.command.clone(),
        env,
        outputs,
        files,
        platform: platform_of(key),
        timeout: execution.timeout,
        properties: properties.clone(),
    })
}

/// The files of the dependency output at `at`, whose manifest `cas` holds under `manifest`.
fn output_files<C: ContentStore>(
    at: &OutputPath,
    manifest: &Digest,
    cas: &C,
) -> Result<Vec<RemoteFile>> {
    let unreadable = |what: &str| Error::Adapter {
        port: PortName::ContentStore,
        diagnostic: Diagnostic::new(format!("the manifest of {at} {what}: {manifest}")),
    };
    let bytes = cas
        .get(manifest)?
        .ok_or_else(|| unreadable("is not held"))?;
    let parsed =
        OutputManifest::from_bytes(&bytes).ok_or_else(|| unreadable("is not a manifest"))?;
    Ok(match parsed {
        OutputManifest::File(file) => vec![RemoteFile {
            path: RootPath::from(at),
            digest: file.digest,
            mode: Some(file.mode),
        }],
        OutputManifest::Tree(tree) => tree
            .entries()
            .iter()
            .map(|entry| RemoteFile {
                path: RootPath::from(&at.entry(&entry.path)),
                digest: entry.digest,
                mode: Some(entry.mode),
            })
            .collect(),
    })
}

/// The machine `key` was made for, in the words a remote cache tells machines apart by.
fn platform_of(key: &ActionKey) -> RemotePlatform {
    RemotePlatform::of(&key.components().own.platform)
}

#[cfg(test)]
mod tests {
    #![expect(
        clippy::unwrap_used,
        reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
    )]

    use std::collections::{BTreeMap, BTreeSet};

    use super::remote_action;
    use crate::error::{Error, PortName, Result};
    use crate::execute::assembly::ready as ready_on;
    use crate::execute::fixtures::{
        command, digest_of, file, label, node, output_path, own, path, tree,
    };
    use crate::execute::perform::Task;
    use crate::ports::two_machines::TwoMachines;
    use crate::ports::{ContentStore, Host};
    use crate::types::{
        ActionKey, DepProducts, Digest, EnvName, EnvValue, Execution, FileMode, InputDigest,
        Isolation, KeyComponents, NetworkAccess, NodeId, OutputManifest, OutputName, OutputPath,
        OutputSource, Platform, PlatformProperties, PlatformPropertyName, Produced, ReadyAction,
        RemoteAction, RemoteFile, RemotePlatform, RootPath, StoredFile, TargetRole, Timeout,
        TreePath,
    };

    /// A store that holds exactly the manifests it is given.
    struct Manifests(BTreeMap<Digest, Vec<u8>>);

    impl Manifests {
        fn holding(manifests: &[&OutputManifest]) -> Self {
            Self(
                manifests
                    .iter()
                    .map(|manifest| (digest_of(manifest), manifest.to_bytes().unwrap()))
                    .collect(),
            )
        }
    }

    impl ContentStore for Manifests {
        fn put(&self, bytes: &[u8]) -> Result<Digest> {
            Ok(Digest::of(bytes))
        }

        fn contains(&self, digest: &Digest) -> bool {
            self.0.contains_key(digest)
        }

        fn get(&self, digest: &Digest) -> Result<Option<Vec<u8>>> {
            Ok(self.0.get(digest).cloned())
        }

        fn clear(&self, _at: &OutputPath) -> Result<()> {
            Ok(())
        }

        fn place(&self, _digest: &Digest, _mode: FileMode, _at: &OutputPath) -> Result<()> {
            Ok(())
        }
    }

    fn env(pairs: &[(&str, &str)]) -> BTreeMap<EnvName, EnvValue> {
        pairs
            .iter()
            .map(|(name, value)| {
                (
                    EnvName::parse(*name).unwrap(),
                    EnvValue::parse(*value).unwrap(),
                )
            })
            .collect()
    }

    fn produced(name: &str, manifest: &OutputManifest) -> Produced {
        Produced {
            name: OutputName::parse(name).unwrap(),
            digest: digest_of(manifest),
        }
    }

    fn dependency(raw: &str, outputs: Vec<Produced>) -> DepProducts {
        DepProducts {
            label: label(raw),
            outputs,
        }
    }

    /// A ready action running `cc` with the environment `env`, no input and no dependency,
    /// leaving `outputs`.
    fn ready(env: BTreeMap<EnvName, EnvValue>, outputs: Vec<OutputPath>) -> ReadyAction {
        ReadyAction {
            label: label("//app:app"),
            command: command(&["cc"]),
            description: None,
            env,
            unkeyed: BTreeSet::new(),
            inputs: Vec::new(),
            deps: Vec::new(),
            outputs,
            network: NetworkAccess::Sealed,
            output_source: OutputSource::Command,
        }
    }

    fn key(platform: &str, inputs: Vec<InputDigest>, deps: Vec<DepProducts>) -> ActionKey {
        let mut own = own(&["app"]);
        own.platform = Platform::parse(platform).unwrap();
        own.inputs = inputs;
        ActionKey::new(KeyComponents { own, deps }).unwrap()
    }

    fn plain_key() -> ActionKey {
        key("linux/x86_64", Vec::new(), Vec::new())
    }

    fn execution(strategy: Isolation) -> Execution {
        Execution {
            strategy,
            ..Execution::default()
        }
    }

    fn task(action: ReadyAction, key: ActionKey) -> Task {
        Task {
            node: NodeId::new(0),
            role: TargetRole::Build,
            key,
            action,
        }
    }

    fn describe(ready: ReadyAction, key: ActionKey, execution: &Execution) -> RemoteAction {
        remote_action(
            &task(ready, key),
            execution,
            &PlatformProperties::default(),
            &Manifests::holding(&[]),
        )
        .unwrap()
    }

    #[test]
    fn the_salt_is_the_buildl_key() {
        let key = plain_key();
        let action = describe(
            ready(BTreeMap::new(), Vec::new()),
            key.clone(),
            &Execution::default(),
        );
        assert_eq!(action.salt, key.digest());
        assert_eq!(action.command, command(&["cc"]));
    }

    #[test]
    fn pinned_variables_override_declared_ones_under_the_sandbox() {
        let declared = || ready(env(&[("TZ", "X"), ("KEEP", "1")]), Vec::new());

        let sandboxed = describe(declared(), plain_key(), &execution(Isolation::Sandbox));
        assert_eq!(
            sandboxed.env,
            env(&[
                ("KEEP", "1"),
                ("LC_ALL", "C"),
                ("SOURCE_DATE_EPOCH", "315532800"),
                ("TZ", "UTC"),
            ])
        );

        let bare = describe(declared(), plain_key(), &execution(Isolation::Bare));
        assert_eq!(bare.env, env(&[("KEEP", "1"), ("TZ", "X")]));
    }

    #[test]
    fn outputs_are_sorted_and_deduplicated() {
        let b = output_path("//app:app", "b");
        let a = output_path("//app:app", "a");
        let action = describe(
            ready(BTreeMap::new(), vec![b.clone(), a.clone(), b.clone()]),
            plain_key(),
            &Execution::default(),
        );
        assert_eq!(action.outputs, vec![a, b]);
    }

    #[test]
    fn a_stdout_target_keeps_its_path_in_the_outputs() {
        let at = output_path("//app:app", "pkgs");
        let mut action = ready(BTreeMap::new(), vec![at.clone()]);
        action.output_source = OutputSource::Stdout;

        let described = describe(action, plain_key(), &Execution::default());

        assert_eq!(described.outputs, vec![at]);
    }

    #[test]
    fn inputs_and_dependency_files_form_one_sorted_list() {
        let lib = file("lib");
        let headers = tree(&[("b.h", "b"), ("a/a.h", "a")]);
        let bin = OutputManifest::File(StoredFile {
            digest: Digest::of(b"bin"),
            mode: FileMode::Executable,
        });
        let data = file("data");
        let held = Manifests::holding(&[&lib, &headers, &bin, &data]);

        let lib_at = output_path("//a:a", "lib");
        let headers_at = output_path("//a:a", "inc");
        let bin_at = output_path("//b:b", "bin");
        let data_at = output_path("//b:b", "data");
        let mut action = ready(BTreeMap::new(), Vec::new());
        action.deps = vec![
            lib_at.clone(),
            headers_at.clone(),
            bin_at.clone(),
            data_at.clone(),
        ];
        let key = key(
            "linux/x86_64",
            vec![
                InputDigest {
                    path: path("src/main.c"),
                    digest: Digest::of(b"main"),
                },
                InputDigest {
                    path: path("Makefile"),
                    digest: Digest::of(b"make"),
                },
            ],
            vec![
                dependency(
                    "//a:a",
                    vec![produced("lib", &lib), produced("inc", &headers)],
                ),
                dependency(
                    "//b:b",
                    vec![produced("bin", &bin), produced("data", &data)],
                ),
            ],
        );

        let described = remote_action(
            &task(action, key),
            &Execution::default(),
            &PlatformProperties::default(),
            &held,
        )
        .unwrap();

        let entry = |name: &str, content: &str| RemoteFile {
            path: RootPath::from(&headers_at.entry(&TreePath::parse(name).unwrap())),
            digest: Digest::of(content.as_bytes()),
            mode: Some(FileMode::Plain),
        };
        let output = |at: &OutputPath, content: &str, mode: FileMode| RemoteFile {
            path: RootPath::from(at),
            digest: Digest::of(content.as_bytes()),
            mode: Some(mode),
        };
        let input = |name: &str, content: &str| RemoteFile {
            path: RootPath::from(&path(name)),
            digest: Digest::of(content.as_bytes()),
            mode: None,
        };
        let mut expected = vec![
            input("src/main.c", "main"),
            input("Makefile", "make"),
            output(&lib_at, "lib", FileMode::Plain),
            entry("a/a.h", "a"),
            entry("b.h", "b"),
            output(&bin_at, "bin", FileMode::Executable),
            output(&data_at, "data", FileMode::Plain),
        ];
        expected.sort_by(|left, right| left.path.cmp(&right.path));
        assert_eq!(described.files, expected);
    }

    #[test]
    fn the_platform_maps_os_and_arch() {
        for (platform, os_family, isa) in [
            ("macos/aarch64", "macos", "arm-a64"),
            ("linux/x86_64", "linux", "x86-64"),
            ("linux/riscv64", "linux", "riscv64"),
        ] {
            let described = describe(
                ready(BTreeMap::new(), Vec::new()),
                key(platform, Vec::new(), Vec::new()),
                &Execution::default(),
            );
            assert_eq!(described.platform.os_family(), os_family, "{platform}");
            assert_eq!(described.platform.isa(), isa, "{platform}");
        }
    }

    /// A task with one dependency output, `lib` of `//a:a`, whose manifest is `lib`.
    fn task_with_a_dependency(lib: &OutputManifest) -> Task {
        let mut action = ready(BTreeMap::new(), Vec::new());
        action.deps = vec![output_path("//a:a", "lib")];
        let key = key(
            "linux/x86_64",
            Vec::new(),
            vec![dependency("//a:a", vec![produced("lib", lib)])],
        );
        task(action, key)
    }

    /// Asserts that `error` is a content store failure whose message holds `part`.
    fn assert_content_store_error(error: Error, part: &str) {
        match error {
            Error::Adapter { port, diagnostic } => {
                assert_eq!(port, PortName::ContentStore);
                assert!(diagnostic.to_string().contains(part), "{diagnostic}");
            }
            other => unreachable!("expected Adapter, got {other:?}"),
        }
    }

    #[test]
    fn a_dependency_manifest_not_held_is_a_content_store_error() {
        let lib = file("lib");
        let error = remote_action(
            &task_with_a_dependency(&lib),
            &Execution::default(),
            &PlatformProperties::default(),
            &Manifests::holding(&[]),
        )
        .unwrap_err();
        assert_content_store_error(error, "is not held");
    }

    #[test]
    fn a_dependency_manifest_that_is_not_a_manifest_is_a_content_store_error() {
        let lib = file("lib");
        let held = Manifests(BTreeMap::from([(
            digest_of(&lib),
            b"not a manifest".to_vec(),
        )]));
        let error = remote_action(
            &task_with_a_dependency(&lib),
            &Execution::default(),
            &PlatformProperties::default(),
            &held,
        )
        .unwrap_err();
        assert_content_store_error(error, "is not a manifest");
    }

    #[test]
    fn an_action_and_a_key_naming_different_dependency_outputs_is_an_error() {
        let lib = file("lib");
        let mut mismatched = task_with_a_dependency(&lib);
        mismatched.action.deps.clear();
        let error = remote_action(
            &mismatched,
            &Execution::default(),
            &PlatformProperties::default(),
            &Manifests::holding(&[&lib]),
        )
        .unwrap_err();
        assert_content_store_error(error, "names 0 dependency outputs but its key holds 1");
    }

    #[test]
    fn the_action_carries_the_execution_timeout() {
        let execution = Execution {
            timeout: Timeout::parse("90s").unwrap(),
            ..Execution::default()
        };
        let described = describe(ready(BTreeMap::new(), Vec::new()), plain_key(), &execution);
        assert_eq!(described.timeout, Timeout::parse("90s").unwrap());
    }

    #[test]
    fn the_workspace_properties_are_carried() {
        let properties = PlatformProperties::new(BTreeMap::from([(
            PlatformPropertyName::parse("pool").unwrap(),
            "large".to_owned(),
        )]));
        let described = remote_action(
            &task(ready(BTreeMap::new(), Vec::new()), plain_key()),
            &Execution::default(),
            &properties,
            &Manifests::holding(&[]),
        )
        .unwrap();
        assert_eq!(described.properties, properties);
    }

    #[test]
    fn no_properties_leave_the_description_as_before() {
        let key = plain_key();
        let described = describe(
            ready(BTreeMap::new(), Vec::new()),
            key.clone(),
            &execution(Isolation::Bare),
        );
        assert_eq!(
            described,
            RemoteAction {
                salt: key.digest(),
                command: command(&["cc"]),
                env: BTreeMap::new(),
                outputs: Vec::new(),
                files: Vec::new(),
                platform: RemotePlatform::of(&Platform::parse("linux/x86_64").unwrap()),
                timeout: Execution::default().timeout,
                properties: PlatformProperties::default(),
            }
        );
    }

    /// The ready action and key of the cargo target of `machine`: `RUSTFLAGS` keyed, `HOME` and
    /// `PATH` unkeyed.
    fn cargo_task_on(machine: &TwoMachines) -> Task {
        let name = |text: &str| EnvName::parse(text).unwrap();
        let mut own = own(&["app"]);
        own.env = BTreeMap::from([(name("RUSTFLAGS"), machine.env(&name("RUSTFLAGS")).unwrap())]);
        own.unkeyed = BTreeSet::from([name("HOME"), name("PATH")]);
        let node = node("//app:app", |_| {});
        let action = ready_on(
            &node,
            &own,
            machine,
            command(&["cc"]),
            None,
            &[],
            Vec::new(),
        )
        .unwrap();
        let key = ActionKey::new(KeyComponents {
            own,
            deps: Vec::new(),
        })
        .unwrap();
        task(action, key)
    }

    #[test]
    fn two_machines_give_the_same_remote_action() {
        let describe_on = |machine: &TwoMachines| {
            let task = cargo_task_on(machine);
            let command_env = task.action.env.clone();
            let described = remote_action(
                &task,
                &execution(Isolation::Sandbox),
                &PlatformProperties::default(),
                &Manifests::holding(&[]),
            )
            .unwrap();
            (command_env, described)
        };

        let (first_command_env, first) = describe_on(&TwoMachines::first());
        let (second_command_env, second) = describe_on(&TwoMachines::second());

        assert_ne!(
            first_command_env, second_command_env,
            "each command runs with its own machine's HOME and PATH"
        );
        assert_eq!(first, second);
        assert_eq!(
            first.env,
            env(&[
                ("LC_ALL", "C"),
                ("RUSTFLAGS", "-Cdebuginfo=0"),
                ("SOURCE_DATE_EPOCH", "315532800"),
                ("TZ", "UTC"),
            ])
        );
    }
}