{"record":{"id":"5c34851966dd9968","repo":"cloudflare/cloudflared","slug":"error-resolving-path-for-service-q-w","errorCode":null,"errorMessage":"error resolving path for service %q: %w","messagePattern":"error resolving path for service %q: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cloudflared/linux_service.go","lineNumber":473,"sourceCode":"\t\terr = uninstallSysv(log)\n\t}\n\n\tremoveTokenFile(serviceConfigDir, log)\n\n\tif err == nil {\n\t\tlog.Info().Msg(\"Linux service for cloudflared uninstalled successfully\")\n\t}\n\n\treturn err\n}\n\nfunc uninstallSystemd(log *zerolog.Logger) error {\n\t// Get only the installed services\n\tinstalledServices := make(map[string]ServiceTemplate)\n\tfor serviceName, serviceTemplate := range systemdAllTemplates {\n\t\tpath, err := serviceTemplate.ResolvePath()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error resolving path for service %q: %w\", serviceName, err)\n\t\t}\n\t\tif _, err := os.Stat(path); err == nil {\n\t\t\tinstalledServices[serviceName] = serviceTemplate\n\t\t} else {\n\t\t\tlog.Info().Msgf(\"Service '%s' not installed, skipping its uninstall\", serviceName)\n\t\t}\n\t}\n\n\tif _, exists := installedServices[cloudflaredService]; exists {\n\t\tif err := runCommand(\"systemctl\", \"disable\", cloudflaredService); err != nil {\n\t\t\tlog.Err(err).Msgf(\"systemctl disable %s error\", cloudflaredService)\n\t\t\treturn err\n\t\t}\n\t\tif err := runCommand(\"systemctl\", \"stop\", cloudflaredService); err != nil {\n\t\t\tlog.Err(err).Msgf(\"systemctl stop %s error\", cloudflaredService)\n\t\t\treturn err\n\t\t}\n\t}","sourceCodeStart":455,"sourceCodeEnd":491,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/cmd/cloudflared/linux_service.go#L455-L491","documentation":"uninstallSystemd iterates over all known systemd service templates and calls ServiceTemplate.ResolvePath() (homedir.Expand on the template path) to determine whether each service file is installed. If expanding the path fails, cloudflared aborts the whole systemd uninstall with this wrapped error. It exists because the uninstall cannot safely decide which service files to remove without knowing their concrete paths.","triggerScenarios":"During `cloudflared service uninstall` on a Linux host with systemd, when homedir.Expand() fails on one of the systemdAllTemplates paths — practically only when the path contains a '~' that cannot be resolved because HOME/username lookup fails (e.g. empty or invalid HOME env, unknown user).","commonSituations":"Running the uninstall from an environment with no valid HOME set (cron job, systemd unit, container with stripped env); running as a UID that has no passwd entry so user lookup inside homedir.Expand fails; sandboxed environments where os/user lookups are restricted (CGO disabled + unusual NSS setup).","solutions":["Run the uninstall with a valid HOME: `sudo HOME=/root cloudflared service uninstall`","Check `echo $HOME` is set and points to an existing user's directory; fix the environment if empty or bogus","Ensure the invoking user exists in /etc/passwd (getent passwd $(id -u)) so homedir.Expand can resolve '~'","As a workaround, remove the unit files manually: `sudo rm /etc/systemd/system/cloudflared.service` (and any *.service cloudflared variants) then `sudo systemctl daemon-reload`","Read the wrapped (%w) error — it names the exact template path and expansion failure"],"exampleFix":"// before: cron-like env without HOME\n$ cloudflared service uninstall\n// error: error resolving path for service \"cloudflared\": error resolving path /etc/systemd/system/cloudflared.service: ...\n// after\n$ sudo HOME=/root cloudflared service uninstall","handlingStrategy":"validation","validationCode":"// pre-flight env check before running service uninstall\nfunc ensureUninstallEnv() error {\n    if os.Getenv(\"HOME\") == \"\" {\n        return errors.New(\"HOME is unset; set HOME (e.g. HOME=/root) before running service uninstall\")\n    }\n    if _, err := exec.LookPath(\"systemctl\"); err != nil {\n        return errors.New(\"systemctl not found: not a systemd system\")\n    }\n    return nil\n}","typeGuard":"func homeResolvable() bool {\n    home := os.Getenv(\"HOME\")\n    if home == \"\" {\n        if u, err := user.Current(); err == nil && u.HomeDir != \"\" {\n            return true\n        }\n        return false\n    }\n    return true\n}","tryCatchPattern":null,"preventionTips":["Ensure HOME is exported when running cloudflared from cron, CI, or containers (sudo -H or explicit HOME=/root)","Run service management commands as a real user with a passwd entry","Prefer `sudo -H cloudflared service uninstall` in scripts","If uninstall keeps failing, remove unit files manually and run systemctl daemon-reload"],"tags":["linux","systemd","uninstall","path-resolution"],"backgroundTag":"file-not-found","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}