dodot-lib 1.1.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
//! `up` command — deploy packs (create symlinks, run provisioning).
//!
//! Uses a two-phase execution model:
//! 1. **Collect** intents from all packs (no mutations).
//! 2. **Detect** cross-pack conflicts across all collected intents.
//! 3. **Execute** only if no conflicts are found.
//!
//! This prevents partial deployments where one pack silently overwrites
//! another pack's symlinks.
//!
//! ## Output rendering
//!
//! For non-dry-run executions, `up` renders by calling `status::status()`
//! on the affected packs after execution and overlaying any operation
//! errors. This guarantees that the per-file labels you see after `up`
//! match exactly what you'd see if you ran `status` immediately
//! afterward — there's a single rendering path, not two.
//!
//! Dry-run keeps the per-intent rendering since there's no
//! post-execution state to verify.

use std::collections::HashMap;

use tracing::{debug, info};

use crate::commands::{
    handler_description, handler_symbol, status, status_style, DisplayConflict, DisplayFile,
    DisplayNote, DisplayPack, PackStatusResult,
};
use crate::conflicts;
use crate::datastore::format_command_for_display;
use crate::handlers;
use crate::operations::HandlerIntent;
use crate::packs::orchestration::{self, ExecutionContext, PackResult};
use crate::packs::Pack;
use crate::probe;
use crate::shell;
use crate::Result;

/// Run the `up` command: deploy packs and regenerate shell init.
///
/// Collects all intents across all packs first, checks for cross-pack
/// conflicts, then executes. If conflicts are found, **no** pack is
/// deployed and a `CrossPackConflict` error is returned — even if
/// `--force` is set, because cross-pack conflicts are a configuration
/// problem, not a deployment problem.
pub fn up(pack_filter: Option<&[String]>, ctx: &ExecutionContext) -> Result<PackStatusResult> {
    info!(
        dry_run = ctx.dry_run,
        force = ctx.force,
        no_provision = ctx.no_provision,
        "starting up command"
    );

    // Phase 1: Discover packs and collect intents
    let packs = orchestration::prepare_packs(pack_filter, ctx)?;

    let mut pack_intents: Vec<(String, Vec<HandlerIntent>)> = Vec::with_capacity(packs.len());
    let mut intent_errors: Vec<PackResult> = Vec::new();

    for pack in &packs {
        match orchestration::collect_pack_intents(pack, ctx) {
            Ok(intents) => {
                pack_intents.push((pack.display_name.clone(), intents));
            }
            Err(e) => {
                info!(pack = %pack.display_name, error = %e, "intent collection failed");
                intent_errors.push(PackResult {
                    pack_name: pack.display_name.clone(),
                    success: false,
                    operations: Vec::new(),
                    error: Some(format!("intent collection error: {e}")),
                });
            }
        }
    }

    // Phase 2: Detect cross-pack conflicts
    info!("checking for cross-pack conflicts");
    let conflicts = conflicts::detect_cross_pack_conflicts(&pack_intents, ctx.fs.as_ref());
    if !conflicts.is_empty() {
        info!(count = conflicts.len(), "cross-pack conflicts detected");
        return Err(crate::DodotError::CrossPackConflict { conflicts });
    }
    debug!("no cross-pack conflicts");

    // Phase 3: Reconcile non-provisioning state, then execute intents.
    //
    // For configuration handlers (path, shell, symlink), every `up` is
    // equivalent to "down (those handlers) + up": we wipe the stored
    // state for each pack before re-applying current source. That way
    // a file deleted from the pack stops appearing in the regenerated
    // init script and the deployed user-side links — instead of
    // lingering as a stale entry that points at a now-missing source.
    //
    // Provisioning handlers (install, homebrew) are deliberately left
    // alone. Their sentinels record "did this run with this content?"
    // independently of whether the source still exists right now;
    // wiping them would force install scripts and `brew bundle` to
    // re-execute on every up, defeating the sentinel mechanism.
    let mut pack_results: Vec<PackResult> = intent_errors;
    let config_handlers = if ctx.dry_run {
        Vec::new()
    } else {
        handlers::configuration_handler_names(ctx.fs.as_ref())
    };
    // pack_intents was built from `packs`, so every display_name maps
    // to exactly one Pack. Keying by &str avoids cloning and makes the
    // missing-pack case a bug rather than silent skip.
    let pack_by_display: HashMap<&str, &Pack> =
        packs.iter().map(|p| (p.display_name.as_str(), p)).collect();

    for (pack_name, intents) in pack_intents {
        info!(pack = %pack_name, intents = intents.len(), "executing pack");

        if !ctx.dry_run {
            let pack = pack_by_display
                .get(pack_name.as_str())
                .copied()
                .expect("pack_intents was built from packs; lookup must succeed");
            if let Err(e) = wipe_configuration_state(pack, &config_handlers, ctx) {
                info!(pack = %pack_name, error = %e, "reconcile failed");
                pack_results.push(PackResult {
                    pack_name,
                    success: false,
                    operations: Vec::new(),
                    error: Some(format!("reconcile error: {e}")),
                });
                continue;
            }
        }

        match orchestration::execute_intents(intents, ctx) {
            Ok(operations) => {
                let success = operations.iter().all(|r| r.success);
                let succeeded = operations.iter().filter(|o| o.success).count();
                let failed = operations.iter().filter(|o| !o.success).count();
                debug!(pack = %pack_name, succeeded, failed, "pack execution complete");
                pack_results.push(PackResult {
                    pack_name,
                    success,
                    operations,
                    error: None,
                });
            }
            Err(e) => {
                info!(pack = %pack_name, error = %e, "pack execution failed");
                pack_results.push(PackResult {
                    pack_name,
                    success: false,
                    operations: Vec::new(),
                    error: Some(format!("execution error: {e}")),
                });
            }
        }
    }

    // Regenerate shell init script and deployment map
    if !ctx.dry_run {
        info!("regenerating shell init script");
        let root_config = ctx.config_manager.root_config()?;
        shell::write_init_script(
            ctx.fs.as_ref(),
            ctx.paths.as_ref(),
            root_config.profiling.enabled,
        )?;
        info!("writing deployment map");
        probe::write_deployment_map(ctx.fs.as_ref(), ctx.paths.as_ref())?;
        // Record the unix timestamp of this up so `dodot probe shell-init`
        // can flag profiles captured before it as stale. Best effort —
        // a clock skip would only affect the staleness banner, never the
        // deployment itself, so we don't fail the run on a write error.
        if let Err(e) = probe::write_last_up_marker(ctx.fs.as_ref(), ctx.paths.as_ref()) {
            debug!(error = %e, "failed to write last-up marker");
        }
        // Prune old shell-init profile reports. Cheap (one read_dir +
        // a few unlinks at most) and runs in dodot's process, not the
        // user's shell.
        let removed = probe::rotate_profiles(
            ctx.fs.as_ref(),
            ctx.paths.as_ref(),
            root_config.profiling.keep_last_runs,
        )?;
        if removed > 0 {
            debug!(removed, "pruned old shell-init profiles");
        }

        // Pre-flight syntax check: parse-only run of bash/zsh against
        // each deployed shell source so a typo in `aliases.sh` shows up
        // here instead of silently breaking next shell startup. The
        // sidecar files this writes are read back by `dodot status`.
        // The checker is injected via context so tests can stub it out.
        let report = shell::validate_shell_sources(
            ctx.fs.as_ref(),
            ctx.paths.as_ref(),
            ctx.syntax_checker.as_ref(),
        )?;
        if !report.failures.is_empty() {
            info!(
                count = report.failures.len(),
                "shell syntax check found failures"
            );
            eprintln!(
                "dodot: {} shell file{} failed pre-flight syntax check (see `dodot status`)",
                report.failures.len(),
                if report.failures.len() == 1 { "" } else { "s" }
            );
        }
        for interp in &report.missing_interpreters {
            // One-line skip notice per missing interpreter, not per
            // file. Doesn't fail the run — the file is still deployed.
            eprintln!(
                "dodot: `{interp}` not on PATH, skipped syntax check for matching shell files"
            );
        }
    }

    let has_failures = pack_results
        .iter()
        .any(|pr| !pr.success || pr.operations.iter().any(|op| !op.success));

    // Build display packs.
    //
    // For real executions, render through status::status() so the user sees
    // the same labels they'd see by running `dodot status` immediately
    // afterward. Operation failures flip their matching row's status to
    // "error" and attach a command-wide note; pack-level errors synthesize
    // an error row at the end of the pack.
    //
    // For dry-run, render the simulated operations directly — there's no
    // post-execution state to verify, and the user wants to see the planned
    // changes, not the unchanged current state.
    let (display_packs, notes) = if ctx.dry_run {
        render_intents(&pack_results, ctx.paths.home_dir())
    } else {
        let pack_names: Vec<String> = packs.iter().map(|p| p.display_name.clone()).collect();
        let status_result = status::status(Some(&pack_names), ctx)?;
        // status::status() may have populated notes (PendingConflict etc.);
        // preserve them and continue numbering from there.
        let mut notes = status_result.notes;
        let display_packs = overlay_errors(
            status_result.packs,
            &pack_results,
            ctx.paths.home_dir(),
            &mut notes,
        );
        (display_packs, notes)
    };

    let message = if has_failures {
        "Packs deployed with errors.".into()
    } else {
        "Packs deployed.".into()
    };

    Ok(PackStatusResult {
        message: Some(message),
        dry_run: ctx.dry_run,
        packs: display_packs,
        warnings: Vec::new(),
        notes,
        conflicts: Vec::new(),
        ignored_packs: Vec::new(),
        view_mode: ctx.view_mode.as_str().into(),
        group_mode: ctx.group_mode.as_str().into(),
    })
}

/// Run `up`, falling back to a status render when a cross-pack conflict
/// blocks deployment.
///
/// On a plain success, this returns `up()`'s result unchanged. On a
/// cross-pack conflict it re-runs the status scan and folds the
/// conflicts in, so the caller gets the full per-pack file listing plus
/// the conflicts section — the same view `dodot status` produces — with
/// a top-level message explaining that nothing was deployed. Other
/// errors propagate unchanged.
///
/// The CLI uses this so `dodot up` and `dodot status` look identical
/// when a cross-pack conflict is present, rather than stripping the
/// per-pack rows down to a bare conflict dump.
pub fn up_or_status_for_conflict(
    pack_filter: Option<&[String]>,
    ctx: &ExecutionContext,
) -> Result<PackStatusResult> {
    match up(pack_filter, ctx) {
        Ok(r) => Ok(r),
        Err(crate::DodotError::CrossPackConflict { conflicts: raw }) => {
            let home = ctx.paths.home_dir();
            let display_conflicts: Vec<DisplayConflict> = raw
                .iter()
                .map(|c| DisplayConflict::from_conflict(c, home))
                .collect();
            let mut base = status::status(pack_filter, ctx)?;
            base.message = Some("Cross-pack conflicts prevent deployment.".into());
            base.dry_run = ctx.dry_run;
            base.conflicts = display_conflicts;
            Ok(base)
        }
        Err(e) => Err(e),
    }
}

/// Remove datastore state for a pack across the given configuration
/// handlers. Datastore is keyed by on-disk directory name (e.g.
/// `010-nvim`), not the display name (`nvim`).
fn wipe_configuration_state(
    pack: &Pack,
    config_handlers: &[String],
    ctx: &ExecutionContext,
) -> Result<()> {
    for handler in config_handlers {
        ctx.datastore.remove_state(&pack.name, handler)?;
    }
    Ok(())
}

/// Render operations directly from pack_results — used for dry-run, where
/// there's no executed state to verify and the user wants to see the
/// planned changes rather than the unchanged status quo.
///
/// Returns (packs, notes). Failed operations keep their row but receive a
/// `note_ref` into the command-wide notes list, keeping the column layout
/// intact.
fn render_intents(
    pack_results: &[PackResult],
    home: &std::path::Path,
) -> (Vec<DisplayPack>, Vec<DisplayNote>) {
    let mut notes: Vec<DisplayNote> = Vec::new();
    let packs = pack_results
        .iter()
        .map(|pr| {
            let mut files: Vec<DisplayFile> = pr
                .operations
                .iter()
                .map(|op| {
                    let (handler, name, user_target) = extract_op_info(&op.operation, home);
                    let (status, status_label, note_ref) = if op.success {
                        (status_style(true).to_string(), op.message.clone(), None)
                    } else {
                        notes.push(DisplayNote {
                            body: op.message.clone(),
                            hint: None,
                        });
                        (
                            "error".to_string(),
                            "error".to_string(),
                            Some(notes.len() as u32),
                        )
                    };
                    DisplayFile {
                        name: name.clone(),
                        symbol: handler_symbol(&handler).into(),
                        description: handler_description(&handler, &name, user_target.as_deref()),
                        status,
                        status_label,
                        handler,
                        note_ref,
                    }
                })
                .collect();

            if let Some(err) = &pr.error {
                notes.push(DisplayNote {
                    body: err.clone(),
                    hint: None,
                });
                files.push(DisplayFile {
                    name: String::new(),
                    symbol: "×".into(),
                    description: String::new(),
                    status: "error".into(),
                    status_label: "error".into(),
                    handler: String::new(),
                    note_ref: Some(notes.len() as u32),
                });
            }

            DisplayPack::new(pr.pack_name.clone(), files)
        })
        .collect();
    (packs, notes)
}

/// Take the steady-state DisplayPacks produced by `status::status()` and
/// flip the matching row's status to "error" for any failed operation,
/// attaching a note with the full error body. Pack-level errors (intent
/// collection, execution) synthesize a dedicated error row at the end of
/// the pack. All notes share a single 1-based command-wide index.
pub(crate) fn overlay_errors(
    mut packs: Vec<DisplayPack>,
    pack_results: &[PackResult],
    home: &std::path::Path,
    notes: &mut Vec<DisplayNote>,
) -> Vec<DisplayPack> {
    for pr in pack_results {
        let display_pack = match packs.iter_mut().find(|p| p.name == pr.pack_name) {
            Some(p) => p,
            None => continue,
        };

        for op_result in &pr.operations {
            if op_result.success {
                continue;
            }
            let (handler, name, user_target) = extract_op_info(&op_result.operation, home);
            let body = op_result.message.clone();

            // Prefer to flip the existing status row so the file listing
            // stays one line per item. Match order:
            //   1. (handler, name) — exact match by pack-relative basename.
            //      Correct when the operation's source basename equals the
            //      file row's pack-relative path (flat layout, common case).
            //   2. (handler, user_target) — covers the pre-link CreateUserLink
            //      conflict case where datastore_path is defaulted (empty)
            //      and the op's "name" comes from user_path.file_name(),
            //      which won't match a `home.X` or subdir pack row. The
            //      row's description is the shortened user_target, so that
            //      matches what the op tried to write.
            //   3. Fallback: match by name only (any handler).
            //   4. Synthesize a new error row if nothing matched.
            let pos = display_pack
                .files
                .iter()
                .position(|f| f.handler == handler && f.name == name)
                .or_else(|| {
                    user_target.as_ref().and_then(|ut| {
                        display_pack
                            .files
                            .iter()
                            .position(|f| f.handler == handler && &f.description == ut)
                    })
                })
                .or_else(|| display_pack.files.iter().position(|f| f.name == name));

            match pos {
                Some(idx) => {
                    // If the row already carries a note (e.g. status flagged
                    // it as PendingConflict), replace that note in place so
                    // numbering stays contiguous and we don't leave a stale
                    // "would conflict" note alongside the actual failure.
                    let file = &mut display_pack.files[idx];
                    if let Some(existing) = file.note_ref {
                        notes[(existing - 1) as usize] = DisplayNote { body, hint: None };
                    } else {
                        notes.push(DisplayNote { body, hint: None });
                        file.note_ref = Some(notes.len() as u32);
                    }
                    file.status = "error".into();
                    file.status_label = "error".into();
                }
                None => {
                    notes.push(DisplayNote { body, hint: None });
                    display_pack.files.push(DisplayFile {
                        name: name.clone(),
                        symbol: handler_symbol(&handler).into(),
                        description: handler_description(&handler, &name, user_target.as_deref()),
                        status: "error".into(),
                        status_label: "error".into(),
                        handler,
                        note_ref: Some(notes.len() as u32),
                    });
                }
            }
        }

        if let Some(err) = &pr.error {
            // Pack-level errors (intent collection failure, orchestration
            // failure bubbled up from execute_intents) don't name a
            // specific file. Attach the note to an existing row so the
            // user can tell which item the failure relates to. Prefer a
            // row that isn't already flipped to error (otherwise we'd
            // clobber a more specific per-op note); if no row qualifies,
            // synthesize a pack-level error row as a last resort.
            let fallback_idx = if display_pack.files.is_empty() {
                None
            } else {
                Some(0)
            };
            let target_idx = display_pack
                .files
                .iter()
                .position(|f| f.status != "error")
                .or(fallback_idx);
            let body = err.clone();
            match target_idx {
                Some(idx) => {
                    let file = &mut display_pack.files[idx];
                    if let Some(existing) = file.note_ref {
                        notes[(existing - 1) as usize] = DisplayNote { body, hint: None };
                    } else {
                        notes.push(DisplayNote { body, hint: None });
                        file.note_ref = Some(notes.len() as u32);
                    }
                    file.status = "error".into();
                    file.status_label = "error".into();
                }
                None => {
                    notes.push(DisplayNote { body, hint: None });
                    display_pack.files.push(DisplayFile {
                        name: String::new(),
                        symbol: "×".into(),
                        description: String::new(),
                        status: "error".into(),
                        status_label: "error".into(),
                        handler: String::new(),
                        note_ref: Some(notes.len() as u32),
                    });
                }
            }
        }
    }
    for pack in &mut packs {
        pack.recompute_summary();
    }
    packs
}

/// Extract handler name, display name, and optional user target from an operation.
fn extract_op_info(
    op: &crate::operations::Operation,
    home: &std::path::Path,
) -> (String, String, Option<String>) {
    match op {
        crate::operations::Operation::CreateDataLink {
            handler, source, ..
        } => (
            handler.clone(),
            source
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
                .into_owned(),
            None,
        ),
        crate::operations::Operation::CreateUserLink {
            handler,
            datastore_path,
            user_path,
            ..
        } => {
            // Name: filename from the datastore path (pack-relative name)
            let name = datastore_path
                .file_name()
                .unwrap_or_else(|| user_path.file_name().unwrap_or_default())
                .to_string_lossy()
                .into_owned();
            // Target: user_path displayed relative to ~ for readability
            let target = if let Ok(rel) = user_path.strip_prefix(home) {
                format!("~/{}", rel.display())
            } else {
                user_path.display().to_string()
            };
            (handler.clone(), name, Some(target))
        }
        crate::operations::Operation::RunCommand {
            handler,
            executable,
            arguments,
            ..
        } => (
            handler.clone(),
            format_command_for_display(executable, arguments),
            None,
        ),
        crate::operations::Operation::CheckSentinel {
            handler, sentinel, ..
        } => (handler.clone(), sentinel.clone(), None),
    }
}