{"record":{"id":"b262abe98bf06b88","repo":"nikivdev/code","slug":"failed-to-bootstrap-service","errorCode":null,"errorMessage":"Failed to bootstrap service: {}","messagePattern":"Failed to bootstrap service: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/macos.rs","lineNumber":771,"sourceCode":"    // Then bootstrap (load)\n    let mut cmd = if svc.service_type.requires_sudo() {\n        let mut c = Command::new(\"sudo\");\n        c.args([\"launchctl\", \"bootstrap\", &domain]);\n        c.arg(&svc.plist_path);\n        c\n    } else {\n        let mut c = Command::new(\"launchctl\");\n        c.args([\"bootstrap\", &domain]);\n        c.arg(&svc.plist_path);\n        c\n    };\n\n    let output = cmd.output()?;\n    if !output.status.success() {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        // Bootstrap may fail if already loaded\n        if !stderr.contains(\"already loaded\") && !stderr.contains(\"service already loaded\") {\n            bail!(\"Failed to bootstrap service: {}\", stderr);\n        }\n    }\n\n    Ok(())\n}\n\n/// Get the current user's UID.\nfn get_uid() -> u32 {\n    unsafe { libc::getuid() }\n}\n\n/// Load macOS config from global flow.toml.\nfn load_macos_config() -> Option<MacosConfig> {\n    let config_path = config::default_config_path();\n    if !config_path.exists() {\n        return None;\n    }\n","sourceCodeStart":753,"sourceCodeEnd":789,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/macos.rs#L753-L789","documentation":"This is the second bootstrap attempt in enable_service (loading the service), which unlike error 684 tolerates 'already loaded' stderr but still fails when launchctl exits non-zero for any other reason. The distinguishing stderr text is checked before bailing, so genuine bootstrap errors surface here.","triggerScenarios":"launchctl bootstrap fails with an error whose stderr does NOT contain \"already loaded\" or \"service already loaded\" — e.g. permission denied on the plist, invalid domain, or bootstrap for a GUI service without a logged-in session.","commonSituations":"Enabling a service that was loaded under a different domain, so bootstrap fails with an unexpected message; running from an SSH session where the GUI domain is unavailable.","solutions":["Read the embedded stderr for launchctl's specific failure reason.","If the service actually is already loaded, verify with `launchctl print <domain>/<label>` and treat the enable as a no-op success.","Run with sudo / in a GUI session as appropriate for the service's domain.","Check the plist is readable by the invoking user (permissions/ownership)."],"exampleFix":"// before\n// error: \"Failed to bootstrap service: Bootstrap failed: 125: Domain operation not supported\"\nssh host flow macos enable my-daemon\n// after\n// run in a GUI session, or target the system domain explicitly\nsudo flow macos enable my-daemon","handlingStrategy":"try-catch","validationCode":"let already = std::process::Command::new(\"launchctl\")\n    .args([\"print\", &format!(\"gui/{}/{}\", uid, label)])\n    .status()\n    .map(|s| s.success())\n    .unwrap_or(false);\nif already { eprintln!(\"service already loaded; skipping enable\"); return; }","typeGuard":"fn is_non_idempotent_bootstrap_failure(err: &anyhow::Error) -> bool {\n    let m = err.to_string();\n    m.contains(\"Failed to bootstrap service:\") && !m.contains(\"already loaded\")\n}","tryCatchPattern":"match macos::run_enable(opts) {\n    Err(e) if e.to_string().contains(\"Failed to bootstrap service:\") => {\n        // \"already loaded\" cases are tolerated internally; anything reaching here is real\n        eprintln!(\"bootstrap failed: {}\", e);\n    }\n    other => other?,\n}","preventionTips":["Check `launchctl print <domain>/<label>` first to detect already-loaded services.","Run enable from a GUI session if the service targets the gui/ domain.","Verify plist file permissions for the invoking user.","Use sudo for domain targets that require elevated privileges."],"tags":["macos","launchctl","subprocess","service-management"],"backgroundTag":"external-command-exited-nonzero","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}