oxdock-core 0.7.0-alpha

Core engine for OxDock's Dockerfile-inspired compile-time DSL, orchestrating workspace snapshots and asset embedding.
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
use anyhow::{Context, Result, anyhow, bail};
use std::sync::Arc;

use oxdock_fs::EntryKind;
use oxdock_parser::{IoBinding, IoStream, Step, StepKind, TemplateString, WorkspaceTarget};
use oxdock_process::{
    BackgroundHandle, CommandOptions, CommandResult, CommandStderr, CommandStdout, ProcessManager,
};
use sha2::{Digest, Sha256};

use super::fs_ops::{canonical_cwd, copy_entry, hash_path};
use super::io::write_stdout;
use super::steps::StepCtx;

pub(super) fn inherit_env<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    keys: &[String],
) -> Result<()> {
    // Resolve every key against the pre-mutation environment first; no
    // `CommandContext` (or any other `Arc` clone of `state.envs`) is alive
    // when we mutate, keeping `Arc::make_mut` O(1) in the common case.
    let mut removals: Vec<String> = Vec::new();
    let mut inserts: Vec<(String, String)> = Vec::new();
    for key in keys {
        if cx.state.io.inherit_env_is_removed(key) {
            removals.push(key.clone());
            continue;
        }
        if let Some(value) = cx.state.io.inherit_env_value(key).cloned() {
            inserts.push((key.clone(), value));
            continue;
        }
        if let Ok(value) = std::env::var(key) {
            inserts.push((key.clone(), value));
        }
    }
    let envs = Arc::make_mut(&mut cx.state.envs);
    for key in removals {
        envs.remove(&key);
    }
    for (key, value) in inserts {
        envs.insert(key, value);
    }
    Ok(())
}

pub(super) fn workdir<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    path: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(path, &ctx);
    cx.state.cwd = cx
        .state
        .fs
        .resolve_workdir(&cx.state.cwd, &rendered)
        .with_context(|| format!("step {}: WORKDIR {}", idx + 1, rendered))?;
    Ok(())
}

pub(super) fn workspace<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    target: &WorkspaceTarget,
) -> Result<()> {
    match target {
        WorkspaceTarget::Snapshot => {
            cx.state.fs.set_root(&cx.snapshot_root);
            cx.state.cwd = cx.state.fs.root().clone();
        }
        WorkspaceTarget::Local => {
            cx.state.fs.set_root(&cx.build_context);
            cx.state.cwd = cx.state.fs.root().clone();
        }
    }
    Ok(())
}

pub(super) fn env<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    key: &str,
    value: &TemplateString,
) -> Result<()> {
    // Scope the context so it drops before `Arc::make_mut`; holding its
    // `Arc` clone across the mutation would force an O(N) map clone.
    let rendered = {
        let ctx = cx.state.command_ctx()?;
        super::expand_template(value, &ctx)
    };
    Arc::make_mut(&mut cx.state.envs).insert(key.to_owned(), rendered);
    Ok(())
}

pub(super) fn run<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    cmd: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(cmd, &ctx);
    let step_stdin = if cx.expose_stdin {
        cx.stdin.clone()
    } else {
        None
    };

    // Check for an environment variable that forces stdout inheritance.
    // This is useful when we want to bypass output capturing (e.g. for build steps)
    // and stream directly to the terminal, even if a capture stream was provided.
    let inherit_override = cx
        .state
        .envs
        .get("OXDOCK_INHERIT_STDOUT")
        .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
        .unwrap_or(false);

    if std::env::var("OXBOOK_DEBUG").is_ok() {
        eprintln!(
            "DEBUG: step RUN {} inherit_override={}",
            rendered, inherit_override
        );
    }

    let stdout_mode = if inherit_override {
        CommandStdout::Inherit
    } else {
        cx.out
            .clone()
            .map(|handle| handle.to_stdout())
            .unwrap_or(CommandStdout::Inherit)
    };
    let stderr_mode = if inherit_override {
        CommandStderr::Inherit
    } else {
        cx.err
            .clone()
            .map(|handle| handle.to_stderr())
            .unwrap_or(CommandStderr::Inherit)
    };

    let mut options = CommandOptions::foreground();
    options.stdin = step_stdin;
    options.stdout = stdout_mode;
    options.stderr = stderr_mode;
    match cx
        .process
        .run_command(&ctx, &rendered, options)
        .with_context(|| format!("step {}: RUN {}", idx + 1, rendered))?
    {
        CommandResult::Completed => Ok(()),
        CommandResult::Captured(_) => {
            bail!(
                "step {}: RUN {} unexpectedly captured output",
                idx + 1,
                rendered
            )
        }
        CommandResult::Background(_) => {
            bail!(
                "step {}: RUN {} returned background handle",
                idx + 1,
                rendered
            )
        }
    }
}

pub(super) fn echo<P: ProcessManager>(cx: &mut StepCtx<'_, P>, msg: &TemplateString) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(msg, &ctx);
    write_stdout(cx.out.clone(), |writer| {
        writeln!(writer, "{}", rendered)?;
        Ok(())
    })?;
    Ok(())
}

pub(super) fn run_bg<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    cmd: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(cmd, &ctx);
    let step_stdin = if cx.expose_stdin {
        cx.stdin.clone()
    } else {
        None
    };
    let stdout_mode = cx
        .out
        .clone()
        .map(|handle| handle.to_stdout())
        .unwrap_or(CommandStdout::Inherit);
    let stderr_mode = cx
        .err
        .clone()
        .map(|handle| handle.to_stderr())
        .unwrap_or(CommandStderr::Inherit);
    let mut options = CommandOptions::background();
    options.stdin = step_stdin;
    options.stdout = stdout_mode;
    options.stderr = stderr_mode;
    match cx
        .process
        .run_command(&ctx, &rendered, options)
        .with_context(|| format!("step {}: RUN_BG {}", idx + 1, rendered))?
    {
        CommandResult::Background(handle) => {
            cx.state.bg_children.push(handle);
            Ok(())
        }
        CommandResult::Completed => {
            bail!(
                "step {}: RUN_BG {} finished synchronously",
                idx + 1,
                rendered
            )
        }
        CommandResult::Captured(_) => {
            bail!(
                "step {}: RUN_BG {} attempted to capture output",
                idx + 1,
                rendered
            )
        }
    }
}

pub(super) fn copy<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    from_current_workspace: bool,
    from: &TemplateString,
    to: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let from_rendered = super::expand_template(from, &ctx);
    let to_rendered = super::expand_template(to, &ctx);
    let from_abs = if from_current_workspace {
        cx.state
            .fs
            .resolve_copy_source_from_workspace(&from_rendered)
            .with_context(|| format!("step {}: COPY {} {}", idx + 1, from_rendered, to_rendered))?
    } else {
        cx.state
            .fs
            .resolve_copy_source(&from_rendered)
            .with_context(|| format!("step {}: COPY {} {}", idx + 1, from_rendered, to_rendered))?
    };
    let to_abs = cx
        .state
        .fs
        .resolve_write(&cx.state.cwd, &to_rendered)
        .with_context(|| format!("step {}: COPY {} {}", idx + 1, from_rendered, to_rendered))?;
    copy_entry(cx.state.fs.as_ref(), &from_abs, &to_abs)
        .with_context(|| format!("step {}: COPY {} {}", idx + 1, from_rendered, to_rendered))?;
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub(super) fn copy_git<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    rev: &TemplateString,
    from: &TemplateString,
    to: &TemplateString,
    include_dirty: bool,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rev_rendered = super::expand_template(rev, &ctx);
    let from_rendered = super::expand_template(from, &ctx);
    let to_rendered = super::expand_template(to, &ctx);
    let to_abs = cx
        .state
        .fs
        .resolve_write(&cx.state.cwd, &to_rendered)
        .with_context(|| {
            format!(
                "step {}: COPY_GIT {} {} {}",
                idx + 1,
                rev_rendered,
                from_rendered,
                to_rendered
            )
        })?;
    cx.state
        .fs
        .copy_from_git(&rev_rendered, &from_rendered, &to_abs, include_dirty)
        .with_context(|| {
            format!(
                "step {}: COPY_GIT {} {} {}",
                idx + 1,
                rev_rendered,
                from_rendered,
                to_rendered
            )
        })?;
    Ok(())
}

pub(super) fn hash_sha256<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    path: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(path, &ctx);
    let target = cx
        .state
        .fs
        .resolve_read(&cx.state.cwd, &rendered)
        .with_context(|| format!("step {}: HASH_SHA256 {}", idx + 1, rendered))?;
    let mut hasher = Sha256::new();
    hash_path(cx.state.fs.as_ref(), &target, "", &mut hasher)?;
    let digest = hasher.finalize();
    let bytes: &[u8] = digest.as_ref();
    write_stdout(cx.out.clone(), |writer| {
        for b in bytes {
            write!(writer, "{b:02x}")?;
        }
        writeln!(writer)?;
        Ok(())
    })?;
    Ok(())
}

pub(super) fn symlink<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    from: &TemplateString,
    to: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let from_rendered = super::expand_template(from, &ctx);
    let to_rendered = super::expand_template(to, &ctx);
    let to_abs = cx
        .state
        .fs
        .resolve_write(&cx.state.cwd, &to_rendered)
        .with_context(|| {
            format!(
                "step {}: SYMLINK {} {}",
                idx + 1,
                from_rendered,
                to_rendered
            )
        })?;
    let from_abs = cx
        .state
        .fs
        .resolve_copy_source(&from_rendered)
        .with_context(|| {
            format!(
                "step {}: SYMLINK {} {}",
                idx + 1,
                from_rendered,
                to_rendered
            )
        })?;
    cx.state.fs.symlink(&from_abs, &to_abs).with_context(|| {
        format!(
            "step {}: SYMLINK {} {}",
            idx + 1,
            from_rendered,
            to_rendered
        )
    })?;
    Ok(())
}

pub(super) fn mkdir<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    path: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(path, &ctx);
    let target = cx
        .state
        .fs
        .resolve_write(&cx.state.cwd, &rendered)
        .with_context(|| format!("step {}: MKDIR {}", idx + 1, rendered))?;
    cx.state
        .fs
        .create_dir_all(&target)
        .with_context(|| format!("failed to create dir {}", target.display()))?;
    Ok(())
}

pub(super) fn ls<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    arg: &Option<TemplateString>,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let target_dir = if let Some(p) = arg {
        let rendered = super::expand_template(p, &ctx);
        cx.state
            .fs
            .resolve_read(&cx.state.cwd, &rendered)
            .with_context(|| format!("step {}: LS {}", idx + 1, rendered))?
    } else {
        cx.state.cwd.clone()
    };
    let mut entries = cx
        .state
        .fs
        .read_dir_entries(&target_dir)
        .with_context(|| format!("step {}: LS {}", idx + 1, target_dir.display()))?;
    entries.sort_by_key(|e| e.file_name());
    write_stdout(cx.out.clone(), |writer| {
        writeln!(writer, "{}:", target_dir.display())?;
        for entry in &entries {
            writeln!(writer, "{}", entry.file_name().to_string_lossy())?;
        }
        Ok(())
    })?;
    Ok(())
}

pub(super) fn cwd<P: ProcessManager>(cx: &mut StepCtx<'_, P>, idx: usize) -> Result<()> {
    // Print the canonical (physical) current working directory to stdout.
    let real = canonical_cwd(cx.state.fs.as_ref(), &cx.state.cwd).with_context(|| {
        format!(
            "step {}: CWD failed to canonicalize {}",
            idx + 1,
            cx.state.cwd.display()
        )
    })?;
    write_stdout(cx.out.clone(), |writer| {
        writeln!(writer, "{}", real)?;
        Ok(())
    })?;
    Ok(())
}

pub(super) fn read<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    path_opt: &Option<TemplateString>,
) -> Result<()> {
    let data = if let Some(path) = path_opt {
        let ctx = cx.state.command_ctx()?;
        let rendered = super::expand_template(path, &ctx);
        let target = cx
            .state
            .fs
            .resolve_read(&cx.state.cwd, &rendered)
            .with_context(|| format!("step {}: READ {}", idx + 1, rendered))?;
        cx.state
            .fs
            .read_file(&target)
            .with_context(|| format!("failed to read {}", target.display()))?
    } else {
        let mut buf = Vec::new();
        if let Some(input_stream) = cx.stdin.clone()
            && let Ok(mut guard) = input_stream.lock()
        {
            guard
                .read_to_end(&mut buf)
                .context("failed to read from stdin")?;
        }

        buf
    };
    write_stdout(cx.out.clone(), |writer| {
        writer
            .write_all(&data)
            .context("failed to write to output")?;
        Ok(())
    })?;
    Ok(())
}

pub(super) fn write<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    path: &TemplateString,
    contents: &Option<TemplateString>,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let path_rendered = super::expand_template(path, &ctx);
    let target = cx
        .state
        .fs
        .resolve_write(&cx.state.cwd, &path_rendered)
        .with_context(|| format!("step {}: WRITE {}", idx + 1, path_rendered))?;
    cx.state
        .fs
        .ensure_parent_dir(&target)
        .with_context(|| format!("failed to create parent for {}", target.display()))?;
    if let Some(body) = contents {
        let rendered = super::expand_template(body, &ctx);
        cx.state
            .fs
            .write_file(&target, rendered.as_bytes())
            .with_context(|| format!("failed to write {}", target.display()))?;
    } else {
        let Some(input_stream) = cx.stdin.clone() else {
            bail!(
                "step {}: WRITE {} requires stdin (use WITH_IO [stdin=...] WRITE)",
                idx + 1,
                path_rendered
            );
        };
        let mut guard = input_stream
            .lock()
            .map_err(|_| anyhow!("failed to lock stdin for WRITE"))?;
        let mut data = Vec::new();
        guard
            .read_to_end(&mut data)
            .context("failed to read from stdin for WRITE")?;
        drop(guard);
        cx.state
            .fs
            .write_file(&target, &data)
            .with_context(|| format!("failed to write {}", target.display()))?;
    }
    Ok(())
}

pub(super) fn append<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    path: &TemplateString,
    contents: &Option<TemplateString>,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let path_rendered = super::expand_template(path, &ctx);
    let target = cx
        .state
        .fs
        .resolve_write(&cx.state.cwd, &path_rendered)
        .with_context(|| format!("step {}: APPEND {}", idx + 1, path_rendered))?;
    cx.state
        .fs
        .ensure_parent_dir(&target)
        .with_context(|| format!("failed to create parent for {}", target.display()))?;
    if let Some(body) = contents {
        let rendered = super::expand_template(body, &ctx);
        cx.state
            .fs
            .append_file(&target, rendered.as_bytes())
            .with_context(|| format!("failed to append to {}", target.display()))?;
    } else {
        let Some(input_stream) = cx.stdin.clone() else {
            bail!(
                "step {}: APPEND {} requires stdin (use WITH_IO [stdin=...] APPEND)",
                idx + 1,
                path_rendered
            );
        };
        let mut guard = input_stream
            .lock()
            .map_err(|_| anyhow!("failed to lock stdin for APPEND"))?;
        let mut data = Vec::new();
        guard
            .read_to_end(&mut data)
            .context("failed to read from stdin for APPEND")?;
        drop(guard);
        cx.state
            .fs
            .append_file(&target, &data)
            .with_context(|| format!("failed to append to {}", target.display()))?;
    }
    Ok(())
}

pub(super) fn assert_file<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    hash: &Option<String>,
    path: &TemplateString,
    contents: &Option<TemplateString>,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(path, &ctx);
    let target = cx
        .state
        .fs
        .resolve_read(&cx.state.cwd, &rendered)
        .with_context(|| format!("step {}: ASSERT_FILE {}", idx + 1, rendered))?;
    if !matches!(cx.state.fs.entry_kind(&target)?, EntryKind::File) {
        bail!("step {}: ASSERT_FILE {} is not a file", idx + 1, rendered);
    }
    if let Some(expected) = hash {
        let mut hasher = Sha256::new();
        hash_path(cx.state.fs.as_ref(), &target, "", &mut hasher)?;
        let digest = hasher.finalize();
        let bytes: &[u8] = digest.as_ref();
        let actual: String = bytes.iter().map(|b| format!("{b:02x}")).collect();
        if !actual.eq_ignore_ascii_case(expected) {
            bail!(
                "step {}: ASSERT_FILE --hash mismatch for {}: expected {}, computed {}",
                idx + 1,
                rendered,
                expected,
                actual
            );
        }
        return Ok(());
    }
    if let Some(body) = contents {
        let expected = super::expand_template(body, &ctx);
        let actual = cx.state.fs.read_file(&target).with_context(|| {
            format!(
                "step {}: ASSERT_FILE {} could not be read",
                idx + 1,
                rendered
            )
        })?;
        if actual != expected.as_bytes() {
            bail!(
                "step {}: ASSERT_FILE content mismatch for {}\nexpected: {:?}\nactual:   {:?}",
                idx + 1,
                rendered,
                expected,
                String::from_utf8_lossy(&actual)
            );
        }
    }
    Ok(())
}

pub(super) fn assert_dir<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    path: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(path, &ctx);
    let target = cx
        .state
        .fs
        .resolve_read(&cx.state.cwd, &rendered)
        .with_context(|| format!("step {}: ASSERT_DIR {}", idx + 1, rendered))?;
    if !matches!(cx.state.fs.entry_kind(&target)?, EntryKind::Dir) {
        bail!(
            "step {}: ASSERT_DIR {} is not a directory",
            idx + 1,
            rendered
        );
    }
    Ok(())
}

pub(super) fn assert_absent<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    path: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(path, &ctx);
    let target = cx
        .state
        .fs
        .resolve_write(&cx.state.cwd, &rendered)
        .with_context(|| format!("step {}: ASSERT_ABSENT {}", idx + 1, rendered))?;
    // Containment was already enforced by `resolve_write`; a lookup failure
    // therefore means the path is absent, which is this command's success
    // condition.
    if cx.state.fs.entry_kind(&target).is_ok() {
        bail!("step {}: ASSERT_ABSENT {} exists", idx + 1, rendered);
    }
    Ok(())
}

pub(super) fn assert_stdout<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    needle: &TemplateString,
) -> Result<()> {
    let ctx = cx.state.command_ctx()?;
    let rendered = super::expand_template(needle, &ctx);
    let log = String::from_utf8_lossy(
        &cx.state
            .stdout_log
            .lock()
            .map_err(|_| anyhow!("stdout log poisoned"))?,
    )
    .into_owned();
    if !log.contains(&rendered) {
        bail!(
            "step {}: ASSERT_STDOUT did not contain '{}'; emitted:\n{}",
            idx + 1,
            rendered,
            log.trim_end()
        );
    }
    Ok(())
}

pub(super) fn with_io<P: ProcessManager>(
    cx: &mut StepCtx<'_, P>,
    idx: usize,
    bindings: &[IoBinding],
    cmd: &StepKind,
) -> Result<()> {
    let inner_step = Step {
        guard: None,
        kind: cmd.clone(),
        scope_enter: 0,
        scope_exit: 0,
    };
    let steps = vec![inner_step];

    let mut step_stdin = None;
    let mut step_stdout = cx.out.clone();
    let mut step_stderr = cx.err.clone();
    let mut next_expose_stdin = false;
    let mut seen_stdin = false;
    let mut seen_stdout = false;
    let mut seen_stderr = false;

    for binding in bindings {
        if let Some(pipe) = &binding.pipe {
            cx.state.io.ensure_script_pipe(pipe);
        }
        match binding.stream {
            IoStream::Stdin => {
                if seen_stdin {
                    bail!("step {}: WITH_IO declared stdin more than once", idx + 1);
                }
                seen_stdin = true;
                next_expose_stdin = true;
                step_stdin = if let Some(pipe) = &binding.pipe {
                    Some(cx.state.io.input_pipe(pipe).ok_or_else(|| {
                        anyhow!(
                            "step {}: WITH_IO stdin pipe '{}' is undefined",
                            idx + 1,
                            pipe
                        )
                    })?)
                } else {
                    cx.stdin.clone()
                };
            }
            IoStream::Stdout => {
                if seen_stdout {
                    bail!("step {}: WITH_IO declared stdout more than once", idx + 1);
                }
                seen_stdout = true;
                step_stdout = if let Some(pipe) = &binding.pipe {
                    Some(
                        cx.state
                            .io
                            .output_pipe_stdout(pipe)
                            .ok_or_else(|| {
                                anyhow!(
                                    "step {}: WITH_IO stdout pipe '{}' is undefined",
                                    idx + 1,
                                    pipe
                                )
                            })?
                            .to_stream_handle(),
                    )
                } else {
                    cx.out.clone()
                };
            }
            IoStream::Stderr => {
                if seen_stderr {
                    bail!("step {}: WITH_IO declared stderr more than once", idx + 1);
                }
                seen_stderr = true;
                step_stderr = if let Some(pipe) = &binding.pipe {
                    Some(
                        cx.state
                            .io
                            .output_pipe_stderr(pipe)
                            .ok_or_else(|| {
                                anyhow!(
                                    "step {}: WITH_IO stderr pipe '{}' is undefined",
                                    idx + 1,
                                    pipe
                                )
                            })?
                            .to_stream_handle(),
                    )
                } else {
                    cx.err.clone()
                };
            }
        }
    }

    super::steps::execute_steps(
        cx.state,
        cx.process,
        &steps,
        step_stdin,
        next_expose_stdin,
        step_stdout,
        step_stderr,
        false,
    )?;
    Ok(())
}

pub(super) fn exit<P: ProcessManager>(cx: &mut StepCtx<'_, P>, code: i32) -> Result<()> {
    for child in cx.state.bg_children.iter_mut() {
        if child.try_wait()?.is_none() {
            let _ = child.kill();
            // Reap without joining IO pump threads (see `check_bg`).
            let _ = child.try_wait();
        }
    }
    cx.state.bg_children.clear();
    bail!("EXIT requested with code {}", code);
}