{"record":{"id":"9ee7ab1508d747e3","repo":"hashicorp/packer","slug":"failed-to-retrieve-user-s-home-directory-path-v","errorCode":null,"errorMessage":"failed to retrieve user's home directory path: %v","messagePattern":"failed to retrieve user's home directory path: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/hcp/env/env.go","lineNumber":86,"sourceCode":"\t}\n\n\treturn true\n}\n\nfunc HasHCPCertificateFile() (bool, error) {\n\tenvVarCredFile, _ := os.LookupEnv(HCPCredFile)\n\tvar envVarCertExists bool\n\tvar err error\n\tif envVarCredFile != \"\" {\n\t\tenvVarCertExists, err = fileExists(envVarCredFile)\n\t\tif err != nil {\n\t\t\treturn false, err\n\t\t}\n\t}\n\t// Get the user's home directory.\n\tuserHome, err := os.UserHomeDir()\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"failed to retrieve user's home directory path: %v\", err)\n\t}\n\n\t// builds file path ~/.config/hcp/cred_file.json, if we don't parse the home directory os.Stat can't find the default credential path\n\tdefaultCredFilePath := filepath.Join(userHome, HCPDefaultCredFilePath, HCPDefaultCredFile)\n\tlog.Printf(\"Checking for default HCP credential file at path %s\", defaultCredFilePath)\n\tdefaultPathCertExists, err := fileExists(defaultCredFilePath)\n\tif err != nil {\n\t\treturn false, err\n\t}\n\tlog.Printf(\"Default file found status - %t\", defaultPathCertExists)\n\tif envVarCertExists && defaultPathCertExists {\n\t\tfmt.Println(\"A HCP credential file was found at the default path, and an HCP_CRED_FILE was specified, the HCP SDK will use the HCP_CRED_FILE\")\n\t}\n\tif envVarCertExists || defaultPathCertExists {\n\t\treturn true, nil\n\t}\n\treturn false, nil\n}","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/internal/hcp/env/env.go#L68-L104","documentation":"HasHCPCertificateFile checks for the default HCP credential file (~/.config/hcp/cred_file.json) as part of HasHCPAuth; when os.UserHomeDir fails (home directory cannot be determined), this error wraps the OS failure. Without the home directory the code cannot build the default credential path to stat. This makes the HCP auth detection fail early.","triggerScenarios":"os.UserHomeDir() returns an error inside HasHCPCertificateFile — e.g. $HOME unset (non-login shells, CI containers, cron/systemd services), or the platform home lookup fails.","commonSituations":"Running Packer in CI containers without HOME set; systemd/cron environments stripping environment variables; Docker images running as non-root users without a passwd entry; restricted environments where os/user lookup fails.","solutions":["Set the HOME environment variable to a writable user directory.","Run the process as a user that has a valid home directory entry.","Prefer explicit HCP_CLIENT_ID/HCP_CLIENT_SECRET env vars so home-directory credential detection is not needed.","Pre-create ~/.config/hcp/ with valid credentials after fixing HOME."],"exampleFix":"// before\n// HOME unset in CI container -> os.UserHomeDir fails\n// after\nENV HOME=/home/packer  # or: export HOME=$(getent passwd $(whoami) | cut -d: -f6)","handlingStrategy":"validation","validationCode":"if os.Getenv(\"HOME\") == \"\" {\n    return fmt.Errorf(\"HOME is not set; cannot locate default HCP credentials\")\n}","typeGuard":null,"tryCatchPattern":"ok, err := env.HasHCPCertificateFile()\nif err != nil {\n    if strings.Contains(err.Error(), \"home directory path\") {\n        return fmt.Errorf(\"set HOME or use HCP_CLIENT_ID/HCP_CLIENT_SECRET instead\")\n    }\n    return err\n}","preventionTips":["Always set HOME in CI containers, cron jobs, and systemd units.","Prefer explicit HCP_CLIENT_ID/HCP_CLIENT_SECRET env vars over the credential file.","Run as a user with a valid passwd entry so home lookup succeeds."],"tags":["hcp","environment","home-directory","auth"],"backgroundTag":"missing-home-directory","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}