{"record":{"id":"f2006fa0754335fa","repo":"chenhg5/cc-connect","slug":"remove-unit-w","errorCode":null,"errorMessage":"remove unit: %w","messagePattern":"remove unit: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"daemon/systemd.go","lineNumber":96,"sourceCode":"\t\tm.sysArgs(\"enable\", systemdServiceName),\n\t\tm.sysArgs(\"restart\", systemdServiceName),\n\t} {\n\t\tif out, err := runSystemctl(cmdArgs...); err != nil {\n\t\t\treturn fmt.Errorf(\"systemctl %s: %s (%w)\", strings.Join(cmdArgs, \" \"), out, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc (m *systemdManager) Uninstall() error {\n\tif _, err := runSystemctl(m.sysArgs(\"disable\", \"--now\", systemdServiceName)...); err != nil {\n\t\tslog.Warn(\"systemd: disable failed\", \"error\", err)\n\t}\n\n\tunitPath := m.unitPath()\n\tif err := os.Remove(unitPath); err != nil && !os.IsNotExist(err) {\n\t\treturn fmt.Errorf(\"remove unit: %w\", err)\n\t}\n\n\tif _, err := runSystemctl(m.sysArgs(\"daemon-reload\")...); err != nil {\n\t\tslog.Warn(\"systemd: daemon-reload failed\", \"error\", err)\n\t}\n\treturn nil\n}\n\nfunc (m *systemdManager) Start() error {\n\tout, err := runSystemctl(m.sysArgs(\"start\", systemdServiceName)...)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"start: %s (%w)\", out, err)\n\t}\n\treturn nil\n}\n\nfunc (m *systemdManager) Stop() error {\n\tout, err := runSystemctl(m.sysArgs(\"stop\", systemdServiceName)...)","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/daemon/systemd.go#L78-L114","documentation":"Uninstall() wraps os.Remove failure for the unit file. Only unexpected errors (anything other than os.IsNotExist) are wrapped; disable and daemon-reload failures are merely logged. It means the unit file exists but could not be deleted.","triggerScenarios":"Calling Uninstall when unitPath exists but the process lacks write permission on the unit directory, the unit file is owned by another user, or the filesystem is read-only.","commonSituations":"Uninstalling a root-installed unit as a normal user; unit directory permissions tightened by an admin; container/WSL with read-only /etc.","solutions":["Run the uninstall with sudo for system-level units.","Check ownership of the unit file: `ls -l <unitPath>` and chown if needed.","Delete manually and reload: `sudo rm <unitPath> && sudo systemctl daemon-reload`.","Ensure the filesystem is writable."],"exampleFix":"// before\ncmd.Exec(\"cc-connect uninstall\") // remove unit: permission denied\n// after\ncmd.Exec(\"sudo cc-connect uninstall\")","handlingStrategy":"try-catch","validationCode":"unitPath := mgr.unitPath()\nif _, err := os.Stat(unitPath); os.IsNotExist(err) {\n    return nil // nothing to uninstall\n}\nif err := syscall.Access(filepath.Dir(unitPath), 0o200); err != nil {\n    return fmt.Errorf(\"no write permission on %s; rerun with sudo\", filepath.Dir(unitPath))\n}","typeGuard":"func unitRemovable(unitPath string) bool {\n    return err := os.Remove(unitPath); err == nil || os.IsNotExist(err)\n}","tryCatchPattern":"if err := mgr.Uninstall(); err != nil {\n    var pe *fs.PathError\n    if errors.As(err, &pe) && errors.Is(pe.Err, fs.ErrPermission) {\n        return fmt.Errorf(\"use sudo: %w\", err)\n    }\n    return err\n}","preventionTips":["Check unit ownership before uninstalling at a different privilege level.","Treat IsNotExist as success (idempotent uninstall).","Verify the unit directory is writable before removal.","After removal, confirm with systemctl cat cc-connect."],"tags":["go","systemd","filesystem","permissions"],"backgroundTag":"file-not-found","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}