{"record":{"id":"5d8c72416c59ef6e","repo":"cloudflare/cloudflared","slug":"cannot-determine-home-directory-for-the-user","errorCode":null,"errorMessage":"Cannot determine home directory for the user","messagePattern":"Cannot determine home directory for the user","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cloudflared/macos_service.go","lineNumber":108,"sourceCode":"func isRootUser() bool {\n\treturn os.Geteuid() == 0\n}\n\nfunc resolveLibraryPath(subPath, fileName string) (string, error) {\n\tconst libraryDirName = \"Library\"\n\n\t// We use the system-wide /Library/... instead of ~/Library/... if the user is root\n\tif isRootUser() {\n\t\treturn filepath.Join(\"/\", libraryDirName, subPath, fileName), nil\n\t}\n\n\t// This returns the home dir of the executing user using OS-specific method\n\t// for discovering the home dir. It's not recommended to call this when the\n\t// user has root permission as $HOME depends on what options the user uses\n\t// with sudo.\n\tuserHomeDir, err := homedir.Dir()\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"Cannot determine home directory for the user\")\n\t}\n\treturn filepath.Join(userHomeDir, libraryDirName, subPath, fileName), nil\n}\n\n// For docs on these subdirectories, see:\n// https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html\nfunc installPath() (string, error) {\n\tsubpath := \"LaunchAgents\"\n\tif isRootUser() {\n\t\tsubpath = \"LaunchDaemons\"\n\t}\n\treturn resolveLibraryPath(subpath, launchdIdentifier+\".plist\")\n}\n\nfunc stdoutPath() (string, error) {\n\treturn resolveLibraryPath(\"Logs\", launchdIdentifier+\".out.log\")\n}\n","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/cmd/cloudflared/macos_service.go#L90-L126","documentation":"On macOS, resolveLibraryPath computes paths under ~/Library using go-homedir. \"Cannot determine home directory for the user\" wraps homedir.Dir() failures, so cloudflared cannot resolve install/log/config paths for the user-level service. The comment notes this is unreliable when running under sudo, since $HOME may be altered.","triggerScenarios":"Running `cloudflared service install` (or any path via installPath/stdoutPath/stderrPath/configPath) on macOS when homedir.Dir() fails: $HOME unset and no passwd entry, or homedir lookup failing due to sudo/sandboxed environments.","commonSituations":"Installing the service via sudo where HOME points nowhere; running inside CI containers or LaunchDaemons without a login user; deleted/mismatched passwd entries for the executing user.","solutions":["Ensure $HOME is set and correct: run `echo $HOME`; when using sudo, add `sudo -H` so HOME resolves to the invoking user's directory.","Run the service install as a normal user with a valid login shell, or install system-wide where homedir isn't needed.","If HOME is genuinely unset, export it (e.g., export HOME=$(cd ~; pwd)) before invoking cloudflared.","Verify the executing user exists in /etc/passwd/dscl with a valid home directory."],"exampleFix":"// before\nsudo cloudflared service install\n// after\nsudo -H cloudflared service install","handlingStrategy":"fallback","validationCode":"if os.Getenv(\"HOME\") == \"\" {\n\treturn errors.New(\"$HOME is not set; run as a normal user or use sudo -H\")\n}","typeGuard":null,"tryCatchPattern":"p, err := resolveLibraryPath(\"config.json\")\nif err != nil {\n\tlog.Err(err).Msgf(\"cannot resolve macOS library path: %+v\", err)\n\treturn err\n}","preventionTips":["Use `sudo -H` when installing services so HOME is correct","Ensure $HOME is exported in CI/container environments","Run service installs as a user with a valid passwd home entry","Prefer system-wide install paths when no user home exists"],"tags":["macos","home-directory","service-install"],"backgroundTag":"missing-env-var","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"}