hashicorp/packer · error
failed to retrieve user's home directory path: %v
Error message
failed to retrieve user's home directory path: %v
What it means
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.
Source
Thrown at internal/hcp/env/env.go:86
}
return true
}
func HasHCPCertificateFile() (bool, error) {
envVarCredFile, _ := os.LookupEnv(HCPCredFile)
var envVarCertExists bool
var err error
if envVarCredFile != "" {
envVarCertExists, err = fileExists(envVarCredFile)
if err != nil {
return false, err
}
}
// Get the user's home directory.
userHome, err := os.UserHomeDir()
if err != nil {
return false, fmt.Errorf("failed to retrieve user's home directory path: %v", err)
}
// 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
defaultCredFilePath := filepath.Join(userHome, HCPDefaultCredFilePath, HCPDefaultCredFile)
log.Printf("Checking for default HCP credential file at path %s", defaultCredFilePath)
defaultPathCertExists, err := fileExists(defaultCredFilePath)
if err != nil {
return false, err
}
log.Printf("Default file found status - %t", defaultPathCertExists)
if envVarCertExists && defaultPathCertExists {
fmt.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")
}
if envVarCertExists || defaultPathCertExists {
return true, nil
}
return false, nil
}View on GitHub (pinned to eb36e3c3e4)
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.
Example fix
// before // HOME unset in CI container -> os.UserHomeDir fails // after ENV HOME=/home/packer # or: export HOME=$(getent passwd $(whoami) | cut -d: -f6)
Defensive patterns
Strategy: validation
Validate before calling
if os.Getenv("HOME") == "" {
return fmt.Errorf("HOME is not set; cannot locate default HCP credentials")
} Try / catch
ok, err := env.HasHCPCertificateFile()
if err != nil {
if strings.Contains(err.Error(), "home directory path") {
return fmt.Errorf("set HOME or use HCP_CLIENT_ID/HCP_CLIENT_SECRET instead")
}
return err
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- error retrieving HCP Packer Version from HCP Packer Registry
- there is no HCP Packer Version associated with the channel %
- the HCP Packer Version associated with the channel %s is rev
- signing_mode %q requires an ambient OIDC token; set SIGSTORE
- InvalidClientConfig
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/9ee7ab1508d747e3.
Report an issue: GitHub.