dodot-lib 5.10.0

Core library for dodot dotfiles manager
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
//! `Run` intent: execute a run-once handler command (install scripts,
//! Brewfile bundle, `nix profile install`), gated by [`DataStore::did_run`]'s
//! three-way classification.
//!
//! Policy: run on `NeverRan`, skip silently on `RanCurrent`, skip with
//! a "ran older version" notice on `RanDifferent`. `provision_rerun =
//! true` (the `--provision-rerun` flag) bypasses both skip cases.
//!
//! A command that fails is a **failed operation, not an aborted pack**.
//! The failure comes back as an `OperationResult::fail` against the
//! run-once file, carrying the command line, its exit code, and the
//! manager's own stderr. The executor keeps going, so the rest of the
//! pack — symlinks, `$PATH` entries, shell init, all of which run in
//! later phases than provisioning — still deploys and still reports.
//! No sentinel is written, so the next `dodot up` retries the file.
//!
//! Only the command failing is contained this way. A datastore I/O
//! error — notably the sentinel write that happens *after* a
//! successful command — still aborts the run, as it does everywhere
//! else in the executor.
//!
//! Two names for the run-once file appear here and mean different
//! things: `relative_path` identifies the file for reporting (status
//! rows are keyed by pack-relative path), and its basename is the
//! sentinel key `did_run` looks up, since sentinels are named
//! `<basename>-<hash>`.

use tracing::info;

use crate::datastore::{CommandSpec, DidRunStatus};
use crate::operations::{HandlerIntent, Operation, OperationResult};
use crate::{DodotError, Result};

use super::Executor;

/// The sentinel key for a run-once file: its basename.
///
/// Sentinels are written as `<basename>-<hash>` in the handler's data
/// directory, so `did_run` matches on the basename even though the
/// intent identifies the file by its pack-relative path.
fn sentinel_key(relative_path: &str) -> &str {
    std::path::Path::new(relative_path)
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or(relative_path)
}

/// Whether this intent's command is actually going to be spawned —
/// the receipt gate [`Executor::execute_run`] applies, asked ahead of
/// it.
///
/// `dodot up`'s fitness probe needs the answer *before* execution
/// starts, because a probe that spawns the user's package manager to
/// read its version has no business running for a file whose receipt
/// is already current: on the second `up` of the day that would be a
/// subprocess bought for a warning about a run that is not happening.
///
/// The gate below is the authoritative one — this returns the same
/// verdict from the same call, and `commands/tests/fitness.rs` pins
/// the two together by asserting that a current receipt costs no
/// version probe. A datastore that cannot answer reports `true`: the
/// executor is about to raise that failure properly, and guessing
/// "will not run" here would suppress a warning on the strength of an
/// error nobody has seen yet.
pub(crate) fn will_spawn(
    datastore: &dyn crate::datastore::DataStore,
    provision_rerun: bool,
    intent: &HandlerIntent,
) -> bool {
    let HandlerIntent::Run {
        pack,
        handler,
        relative_path,
        content_hash,
        ..
    } = intent
    else {
        return false;
    };
    if provision_rerun {
        return true;
    }
    match datastore.did_run(pack, handler, sentinel_key(relative_path), content_hash) {
        Ok(status) => matches!(status, DidRunStatus::NeverRan),
        Err(_) => true,
    }
}

impl<'a> Executor<'a> {
    pub(super) fn execute_run(&self, intent: &HandlerIntent) -> Result<Vec<OperationResult>> {
        let HandlerIntent::Run {
            pack,
            handler,
            executable,
            arguments,
            environment,
            sentinel,
            relative_path,
            content_hash,
        } = intent
        else {
            unreachable!("execute_run called with non-Run intent");
        };

        if !self.provision_rerun {
            match self.datastore.did_run(
                pack,
                handler,
                sentinel_key(relative_path),
                content_hash,
            )? {
                DidRunStatus::RanCurrent => {
                    info!(
                        pack,
                        handler = handler.as_str(),
                        sentinel,
                        "current-version sentinel found, skipping"
                    );
                    let op = Operation::CheckSentinel {
                        pack: pack.clone(),
                        handler: handler.clone(),
                        sentinel: sentinel.clone(),
                    };
                    return Ok(vec![OperationResult::ok(op, "already completed")]);
                }
                DidRunStatus::RanDifferent { previous_hash, .. } => {
                    info!(
                        pack,
                        handler = handler.as_str(),
                        relative_path,
                        previous_hash,
                        current_hash = content_hash,
                        "older-version sentinel found, skipping (run with --provision-rerun to apply)"
                    );
                    let op = Operation::CheckSentinel {
                        pack: pack.clone(),
                        handler: handler.clone(),
                        sentinel: sentinel.clone(),
                    };
                    return Ok(vec![OperationResult::ok(
                        op,
                        format!(
                            "ran older version of {relative_path} — run `dodot up --provision-rerun` to apply the current one"
                        ),
                    )]);
                }
                DidRunStatus::NeverRan => {
                    // fall through and run
                }
            }
        }

        let cmd_str = format!("{} {}", executable, arguments.join(" "));
        info!(pack, handler = handler.as_str(), command = %cmd_str.trim(), "running command");

        let op = Operation::RunCommand {
            pack: pack.clone(),
            handler: handler.clone(),
            executable: executable.clone(),
            arguments: arguments.clone(),
            environment: environment.clone(),
            sentinel: sentinel.clone(),
            relative_path: relative_path.clone(),
        };

        // `force=true` here tells run_and_record to skip its own
        // internal has_sentinel pre-check — we've already made the
        // policy decision above via did_run.
        //
        // A failing command is reported, not propagated: `?` here
        // would abort the executor's intent loop, and `up` would
        // record the whole pack as failed and drop every operation
        // that already succeeded in it. The error text carries the
        // command, its exit code, and the manager's stderr, which is
        // what the failure row's note shows.
        //
        // Only `CommandFailed` is contained. `run_and_record` also
        // creates the sentinel directory and writes the sentinel
        // *after* the command has already succeeded, and those
        // failures come back as `Fs` errors. Demoting one of those to
        // a failure row would misreport a command that ran fine as a
        // command that failed, and would keep mutating the pack
        // through a datastore that has just proved it cannot be
        // written to. Everything that is not the user's script
        // failing still aborts the run, which is `Executor::execute`'s
        // standing contract for I/O errors.
        match self.datastore.run_and_record(
            pack,
            handler,
            CommandSpec::with_environment(executable, arguments, environment),
            sentinel,
            true,
        ) {
            Ok(()) => {}
            Err(e @ DodotError::CommandFailed { .. }) => {
                info!(
                    pack,
                    handler = handler.as_str(),
                    relative_path,
                    error = %e,
                    "command failed; no sentinel written, remaining intents continue"
                );
                return Ok(vec![OperationResult::fail(op, e.to_string())]);
            }
            Err(e) => return Err(e),
        }

        info!(pack, sentinel, "command completed, sentinel recorded");

        Ok(vec![OperationResult::ok(
            op,
            format!("executed: {}", cmd_str.trim()),
        )])
    }

    pub(super) fn simulate_run(&self, intent: &HandlerIntent) -> Vec<OperationResult> {
        let HandlerIntent::Run {
            pack,
            handler,
            executable,
            arguments,
            environment,
            sentinel,
            relative_path,
            content_hash,
        } = intent
        else {
            unreachable!("simulate_run called with non-Run intent");
        };

        // Mirror execute_run's three-way policy for dry-run output so
        // the user sees the same skip/notify decisions they'd get on a
        // real run. We don't error on lookup failures — fall through
        // to "would execute" if did_run fails.
        if !self.provision_rerun {
            if let Ok(status) =
                self.datastore
                    .did_run(pack, handler, sentinel_key(relative_path), content_hash)
            {
                match status {
                    DidRunStatus::RanCurrent => {
                        let op = Operation::CheckSentinel {
                            pack: pack.clone(),
                            handler: handler.clone(),
                            sentinel: sentinel.clone(),
                        };
                        return vec![OperationResult::ok(
                            op,
                            "[dry-run] would skip (already completed)",
                        )];
                    }
                    DidRunStatus::RanDifferent { .. } => {
                        let op = Operation::CheckSentinel {
                            pack: pack.clone(),
                            handler: handler.clone(),
                            sentinel: sentinel.clone(),
                        };
                        return vec![OperationResult::ok(
                            op,
                            format!(
                                "[dry-run] would skip (ran older version of {relative_path}; run `dodot up --provision-rerun` to apply the current one)"
                            ),
                        )];
                    }
                    DidRunStatus::NeverRan => {}
                }
            }
        }

        let cmd_str = format!("{} {}", executable, arguments.join(" "));
        vec![OperationResult::ok(
            Operation::RunCommand {
                pack: pack.clone(),
                handler: handler.clone(),
                executable: executable.clone(),
                arguments: arguments.clone(),
                environment: environment.clone(),
                sentinel: sentinel.clone(),
                relative_path: relative_path.clone(),
            },
            format!("[dry-run] would execute: {}", cmd_str.trim()),
        )]
    }
}

#[cfg(test)]
mod tests {
    use super::super::test_support::make_datastore;
    use super::super::Executor;
    use super::sentinel_key;
    use crate::datastore::CommandSpec;
    use crate::fs::Fs;
    use crate::operations::HandlerIntent;
    use crate::paths::Pather;
    use crate::testing::TempEnvironment;

    fn run_intent(
        pack: &str,
        handler: &str,
        executable: &str,
        args: &[&str],
        relative_path: &str,
        hash: &str,
    ) -> HandlerIntent {
        HandlerIntent::Run {
            pack: pack.into(),
            handler: handler.into(),
            executable: executable.into(),
            arguments: args.iter().map(|s| (*s).into()).collect(),
            environment: Vec::new(),
            sentinel: format!("{}-{hash}", sentinel_key(relative_path)),
            relative_path: relative_path.into(),
            content_hash: hash.into(),
        }
    }

    /// The seam, end to end through the executor: what a handler
    /// declared on the intent is what the spawn layer is handed.
    #[test]
    fn execute_run_hands_the_intents_environment_to_the_runner() {
        let env = TempEnvironment::builder().build();
        let (ds, runner) = make_datastore(&env);
        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            false,
            true,
        );

        let mut intent = run_intent(
            "dev",
            "homebrew",
            "echo",
            &["bundle"],
            "Brewfile",
            "abc1234567890def",
        );
        let HandlerIntent::Run { environment, .. } = &mut intent else {
            unreachable!("run_intent builds a Run intent");
        };
        environment.push(("HOMEBREW_NO_AUTO_UPDATE".into(), "1".into()));

        let results = executor.execute(vec![intent]).unwrap();

        assert!(results[0].success);
        assert_eq!(
            runner.environments.lock().unwrap().as_slice(),
            &[vec![(
                "HOMEBREW_NO_AUTO_UPDATE".to_string(),
                "1".to_string()
            )]]
        );
    }

    #[test]
    fn execute_run_passes_no_environment_when_the_intent_declares_none() {
        let env = TempEnvironment::builder().build();
        let (ds, runner) = make_datastore(&env);
        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            false,
            true,
        );

        executor
            .execute(vec![run_intent(
                "vim",
                "install",
                "echo",
                &["hello"],
                "install.sh",
                "abc1234567890def",
            )])
            .unwrap();

        assert_eq!(
            runner.environments.lock().unwrap().as_slice(),
            &[Vec::new()]
        );
    }

    #[test]
    fn execute_run_runs_when_never_ran() {
        let env = TempEnvironment::builder().build();
        let (ds, runner) = make_datastore(&env);
        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            false,
            true,
        );

        let results = executor
            .execute(vec![run_intent(
                "vim",
                "install",
                "echo",
                &["hello"],
                "install.sh",
                "abc1234567890def",
            )])
            .unwrap();

        assert_eq!(results.len(), 1);
        assert!(results[0].success);
        assert_eq!(runner.calls.lock().unwrap().as_slice(), &["echo hello"]);
        env.assert_sentinel("vim", "install", "install.sh-abc1234567890def");
    }

    #[test]
    fn execute_run_skips_silently_when_current_hash_matches() {
        let env = TempEnvironment::builder().build();
        let (ds, runner) = make_datastore(&env);

        let sentinel_dir = env.paths.handler_data_dir("vim", "install");
        env.fs.mkdir_all(&sentinel_dir).unwrap();
        env.fs
            .write_file(
                &sentinel_dir.join("install.sh-abc1234567890def"),
                b"completed|12345",
            )
            .unwrap();

        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            false,
            true,
        );
        let results = executor
            .execute(vec![run_intent(
                "vim",
                "install",
                "echo",
                &["should-not-run"],
                "install.sh",
                "abc1234567890def",
            )])
            .unwrap();

        assert_eq!(results.len(), 1);
        assert!(results[0].success);
        assert!(results[0].message.contains("already completed"));
        assert!(runner.calls.lock().unwrap().is_empty());
    }

    #[test]
    fn execute_run_skips_with_notice_when_older_version_ran() {
        let env = TempEnvironment::builder().build();
        let (ds, runner) = make_datastore(&env);

        let sentinel_dir = env.paths.handler_data_dir("vim", "install");
        env.fs.mkdir_all(&sentinel_dir).unwrap();
        env.fs
            .write_file(
                &sentinel_dir.join("install.sh-aaaaaaaaaaaaaaaa"),
                b"completed|12345",
            )
            .unwrap();

        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            false,
            true,
        );
        let results = executor
            .execute(vec![run_intent(
                "vim",
                "install",
                "echo",
                &["new-content"],
                "install.sh",
                "bbbbbbbbbbbbbbbb",
            )])
            .unwrap();

        assert_eq!(results.len(), 1);
        assert!(results[0].success);
        assert!(
            results[0].message.contains("ran older version"),
            "msg: {}",
            results[0].message
        );
        assert!(
            results[0].message.contains("--provision-rerun"),
            "the notice must name the flag that actually re-runs a run-once \
             handler; `--force` only overwrites pre-existing files at symlink \
             targets. msg: {}",
            results[0].message
        );
        assert!(
            runner.calls.lock().unwrap().is_empty(),
            "command must not run on older-version detection"
        );
    }

    /// `--dry-run` must name the same remedy the real run does. A
    /// preview that points at a different flag than the command it
    /// previews sends the user somewhere the real run won't honour.
    #[test]
    fn dry_run_older_version_notice_names_provision_rerun() {
        let env = TempEnvironment::builder().build();
        let (ds, runner) = make_datastore(&env);

        let sentinel_dir = env.paths.handler_data_dir("vim", "install");
        env.fs.mkdir_all(&sentinel_dir).unwrap();
        env.fs
            .write_file(
                &sentinel_dir.join("install.sh-aaaaaaaaaaaaaaaa"),
                b"completed|12345",
            )
            .unwrap();

        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            true, // dry_run
            false,
            false,
            true,
        );
        let results = executor
            .execute(vec![run_intent(
                "vim",
                "install",
                "echo",
                &["new-content"],
                "install.sh",
                "bbbbbbbbbbbbbbbb",
            )])
            .unwrap();

        assert_eq!(results.len(), 1);
        assert!(
            results[0].message.contains("--provision-rerun"),
            "msg: {}",
            results[0].message
        );
        assert!(
            !results[0].message.contains("--force"),
            "msg: {}",
            results[0].message
        );
        assert!(
            runner.calls.lock().unwrap().is_empty(),
            "dry-run must not execute anything"
        );
    }

    #[test]
    fn provision_rerun_bypasses_skip_when_current() {
        let env = TempEnvironment::builder().build();
        let (ds, runner) = make_datastore(&env);

        let sentinel_dir = env.paths.handler_data_dir("vim", "install");
        env.fs.mkdir_all(&sentinel_dir).unwrap();
        env.fs
            .write_file(
                &sentinel_dir.join("install.sh-abc1234567890def"),
                b"completed|12345",
            )
            .unwrap();

        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            true, // provision_rerun
            true,
        );
        let results = executor
            .execute(vec![run_intent(
                "vim",
                "install",
                "echo",
                &["rerun"],
                "install.sh",
                "abc1234567890def",
            )])
            .unwrap();

        assert_eq!(results.len(), 1);
        assert!(results[0].success);
        assert!(
            results[0].message.contains("executed"),
            "msg: {}",
            results[0].message
        );
        assert_eq!(runner.calls.lock().unwrap().as_slice(), &["echo rerun"]);
    }

    #[test]
    fn provision_rerun_bypasses_skip_when_older_version() {
        let env = TempEnvironment::builder().build();
        let (ds, runner) = make_datastore(&env);

        let sentinel_dir = env.paths.handler_data_dir("vim", "install");
        env.fs.mkdir_all(&sentinel_dir).unwrap();
        env.fs
            .write_file(
                &sentinel_dir.join("install.sh-aaaaaaaaaaaaaaaa"),
                b"completed|12345",
            )
            .unwrap();

        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            true, // provision_rerun
            true,
        );
        let results = executor
            .execute(vec![run_intent(
                "vim",
                "install",
                "echo",
                &["forced"],
                "install.sh",
                "bbbbbbbbbbbbbbbb",
            )])
            .unwrap();

        assert_eq!(results.len(), 1);
        assert!(results[0].success);
        assert!(results[0].message.contains("executed"));
        assert_eq!(runner.calls.lock().unwrap().as_slice(), &["echo forced"]);
    }

    // ── A failing command is contained ──────────────────────

    /// A runner whose commands all fail the way a real manager does:
    /// non-zero exit, with its own diagnostics on stderr.
    struct FailingRunner {
        stderr: String,
    }

    impl crate::datastore::CommandRunner for FailingRunner {
        fn run(&self, command: CommandSpec<'_>) -> crate::Result<crate::datastore::CommandOutput> {
            let CommandSpec {
                executable,
                arguments,
                ..
            } = command;
            Err(crate::DodotError::CommandFailed {
                command: crate::datastore::format_command_for_display(executable, arguments),
                exit_code: 3,
                stderr: self.stderr.clone(),
            })
        }
    }

    fn failing_datastore(
        env: &TempEnvironment,
        stderr: &str,
    ) -> crate::datastore::FilesystemDataStore {
        crate::datastore::FilesystemDataStore::new(
            env.fs.clone(),
            env.paths.clone(),
            std::sync::Arc::new(FailingRunner {
                stderr: stderr.into(),
            }),
        )
    }

    #[test]
    fn a_failed_command_is_a_failed_operation_carrying_the_managers_output() {
        let env = TempEnvironment::builder().build();
        let ds = failing_datastore(&env, "Error: Cask 'ghostty' is unavailable.");
        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            false,
            true,
        );

        let results = executor
            .execute(vec![run_intent(
                "tools",
                "homebrew",
                "brew",
                &["bundle", "--file", "/packs/tools/Brewfile"],
                "Brewfile",
                "abc1234567890def",
            )])
            .expect("a failing command must not error out of the executor");

        assert_eq!(results.len(), 1);
        assert!(!results[0].success, "msg: {}", results[0].message);
        assert!(
            results[0].message.contains("Cask 'ghostty' is unavailable"),
            "the manager's own output has to reach the row: {}",
            results[0].message
        );
        assert!(
            results[0].message.contains("exit code 3"),
            "msg: {}",
            results[0].message
        );
        env.assert_no_handler_state("tools", "homebrew");
    }

    /// Containment stops at the command. `run_and_record` writes the
    /// sentinel *after* the command has succeeded, so a datastore
    /// that cannot be written to surfaces here as an `Fs` error —
    /// dodot malfunctioning, not the user's script failing. It has to
    /// abort the run rather than become a failure row that claims the
    /// command failed when it did not.
    #[test]
    fn a_sentinel_write_failure_aborts_instead_of_becoming_a_failure_row() {
        let env = TempEnvironment::builder().build();
        let (ds, runner) = make_datastore(&env);

        // Occupy the sentinel's own path with a directory, so the
        // sentinel write fails after the command has already run.
        // `did_run` still reports NeverRan (it only considers files),
        // so the command is reached exactly as in a healthy run.
        let handler_dir = env.paths.handler_data_dir("vim", "install");
        env.fs
            .mkdir_all(&handler_dir.join("install.sh-abc1234567890def"))
            .unwrap();

        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            false,
            true,
        );

        let err = executor
            .execute(vec![run_intent(
                "vim",
                "install",
                "echo",
                &["hello"],
                "install.sh",
                "abc1234567890def",
            )])
            .expect_err("a datastore that cannot record the run must abort execution");

        assert!(
            !matches!(err, crate::DodotError::CommandFailed { .. }),
            "a lost sentinel is not a failed command: {err}"
        );
        // The command itself ran fine — the failure is dodot's, after it.
        assert_eq!(runner.calls.lock().unwrap().as_slice(), &["echo hello"]);
    }

    /// The reason failure containment matters: provisioning runs
    /// before symlinks, `$PATH`, and shell init, so a `Brewfile` that
    /// fails used to cost its pack all three.
    #[test]
    fn work_after_a_failed_command_still_executes_and_still_reports() {
        let env = TempEnvironment::builder()
            .pack("tools")
            .file("vimrc", "set nocompatible")
            .done()
            .build();
        let ds = failing_datastore(&env, "boom");
        let executor = Executor::new(
            &ds,
            env.fs.as_ref(),
            env.paths.as_ref(),
            false,
            false,
            false,
            true,
        );

        let results = executor
            .execute(vec![
                run_intent(
                    "tools",
                    "install",
                    "bash",
                    &["--", "/packs/tools/install.sh"],
                    "install.sh",
                    "abc1234567890def",
                ),
                HandlerIntent::Link {
                    pack: "tools".into(),
                    handler: "symlink".into(),
                    source: env.dotfiles_root.join("tools/vimrc"),
                    user_path: env.home.join(".vimrc"),
                },
            ])
            .unwrap();

        assert_eq!(results.len(), 2);
        assert!(!results[0].success);
        assert!(
            results[1..].iter().all(|r| r.success),
            "the symlink must deploy despite the earlier failure: {results:?}"
        );
        env.assert_double_link(
            "tools",
            "symlink",
            "vimrc",
            &env.dotfiles_root.join("tools/vimrc"),
            &env.home.join(".vimrc"),
        );
    }
}