{"record":{"id":"40d0d11495f6edf3","repo":"nikivdev/code","slug":"failed-to-enable-service","errorCode":null,"errorMessage":"Failed to enable service: {}","messagePattern":"Failed to enable service: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/macos.rs","lineNumber":750,"sourceCode":"fn enable_service(svc: &LaunchdService) -> Result<()> {\n    let domain = svc.service_type.domain();\n\n    // First enable\n    let target = format!(\"{}/{}\", domain, svc.id);\n    let mut cmd = if svc.service_type.requires_sudo() {\n        let mut c = Command::new(\"sudo\");\n        c.args([\"launchctl\", \"enable\", &target]);\n        c\n    } else {\n        let mut c = Command::new(\"launchctl\");\n        c.args([\"enable\", &target]);\n        c\n    };\n\n    let output = cmd.output()?;\n    if !output.status.success() {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        bail!(\"Failed to enable service: {}\", stderr);\n    }\n\n    // 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);","sourceCodeStart":732,"sourceCodeEnd":768,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/macos.rs#L732-L768","documentation":"enable_service first bootstraps (or, for sudo services, shells out to sudo launchctl bootstrap) to load the launchd service; this error is raised when that first bootstrap command exits non-zero, with launchctl's stderr embedded in the message. It is the load step failing, distinct from the later bootstrap retry at error 685.","triggerScenarios":"Running enable_service for a service whose type requires sudo but is run without it, or launchctl bootstrap fails for reasons like a malformed plist, nonexistent plist path, or wrong domain target.","commonSituations":"Enabling a system service without sudo; typo in the plist path; plist with XML errors that launchctl rejects.","solutions":["Inspect the stderr embedded in the error for launchctl's exact reason.","Run the enable command with sudo when the service type requires it.","Validate the plist: `plutil -lint <path/to/plist>` and confirm the file exists.","Bootstrap manually to debug: `sudo launchctl bootstrap gui/$(id -u) <plist>`."],"exampleFix":"// before\nflow macos enable my-daemon   // fails: Bootstrap failed: 5: Input/output error\n// after\nplutil -lint ~/Library/LaunchAgents/com.example.my-daemon.plist\nsudo flow macos enable my-daemon","handlingStrategy":"validation","validationCode":"let plist = format!(\"~/Library/LaunchAgents/com.example.{}.plist\", label);\nlet ok = std::process::Command::new(\"plutil\").args([\"-lint\", &plist]).status().map(|s| s.success()).unwrap_or(false);\nif !ok { eprintln!(\"plist invalid or missing\"); return; }","typeGuard":"fn is_bootstrap_failure(err: &anyhow::Error) -> bool {\n    err.to_string().starts_with(\"Failed to enable service: \") || err.to_string().starts_with(\"Failed to bootstrap service: \")\n}","tryCatchPattern":"match macos::run_enable(opts) {\n    Err(e) if e.to_string().contains(\"Failed to enable service:\") => {\n        if e.to_string().contains(\"Operation not permitted\") {\n            // escalate: rerun with sudo\n        } else {\n            eprintln!(\"launchctl: {}\", e);\n        }\n    }\n    other => other?,\n}","preventionTips":["Run enable with sudo when the service type requires it.","Validate plists with `plutil -lint` before enabling.","Ensure the plist path exists and the label matches the file's Label key."],"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"}