{"record":{"id":"83f146b3a6f5692d","repo":"hashicorp/terraform","slug":"can-t-locate-credentials-file-s","errorCode":null,"errorMessage":"can't locate credentials file: %s","messagePattern":"can't locate credentials file: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/cliconfig/credentials.go","lineNumber":45,"sourceCode":"// that the credentials source will use when asked to save or forget credentials\n// and when a \"credentials helper\" program is not active.\nfunc CredentialsConfigFile() (string, error) {\n\tconfigDir, err := ConfigDir()\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn filepath.Join(configDir, \"credentials.tfrc.json\"), nil\n}\n\n// CredentialsSource creates and returns a service credentials source whose\n// behavior depends on which \"credentials\" and \"credentials_helper\" blocks,\n// if any, are present in the receiving config.\nfunc (c *Config) CredentialsSource(helperPlugins pluginDiscovery.PluginMetaSet) (*CredentialsSource, error) {\n\tcredentialsFilePath, err := CredentialsConfigFile()\n\tif err != nil {\n\t\t// If we managed to load a Config object at all then we would already\n\t\t// have located this file, so this error is very unlikely.\n\t\treturn nil, fmt.Errorf(\"can't locate credentials file: %s\", err)\n\t}\n\n\tvar helper svcauth.CredentialsSource\n\tvar helperType string\n\tfor givenType, givenConfig := range c.CredentialsHelpers {\n\t\tavailable := helperPlugins.WithName(givenType)\n\t\tif available.Count() == 0 {\n\t\t\tlog.Printf(\"[ERROR] Unable to find credentials helper %q; ignoring\", givenType)\n\t\t\tbreak\n\t\t}\n\n\t\tselected := available.Newest()\n\n\t\thelperSource := svcauth.HelperProgramCredentialsSource(selected.Path, givenConfig.Args...)\n\t\thelper = svcauth.CachingCredentialsSource(helperSource) // cached because external operation may be slow/expensive\n\t\thelperType = givenType\n\n\t\t// There should only be zero or one \"credentials_helper\" blocks. We","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/cliconfig/credentials.go#L27-L63","documentation":"Emitted by `Config.CredentialsSource` (credentials.go:45) when `CredentialsConfigFile()` (which calls `ConfigDir()` then `filepath.Join`) returns an error — i.e. the user's config/home directory cannot be determined. The comment at credentials.go:43-44 notes this is very unlikely because a successfully loaded `Config` implies the dir was already found.","triggerScenarios":"Calling `CredentialsSource(...)` when `ConfigDir()` fails: `$HOME` is unset on Unix, `%APPDATA%` unavailable on Windows, or the home-dir resolver returns an error.","commonSituations":"Running Terraform under a service account/container without `HOME` set; hardened environments that strip home-dir env vars; running embedded Terraform internals from a context with no user profile.","solutions":["Ensure `HOME` (Unix) or `APPDATA`/`USERPROFILE` (Windows) is set to a writable directory.","Set `TF_CLI_CONFIG_FILE` to an explicit config path so `ConfigDir()` is bypassed where possible.","Run Terraform as a user with a resolvable home directory."],"exampleFix":"# before (container without HOME)\nterraform login\n# can't locate credentials file: $HOME is not defined\n\n# after\nexport HOME=/tmp/tfhome\nmkdir -p \"$HOME/.terraform.d\"\nterraform login","handlingStrategy":"try-catch","validationCode":"// Verify a config/home dir is resolvable before login/apply.\nfunc homeResolvable() error {\n    if dir, err := cliconfig.ConfigDir(); err != nil {\n        return fmt.Errorf(\"no config/home dir: %w; set HOME or TF_CLI_CONFIG_FILE\", err)\n    } else {\n        return os.MkdirAll(dir, 0o755)\n    }\n}","typeGuard":null,"tryCatchPattern":"// src, err := cfg.CredentialsSource(plugins)\nif err != nil {\n    if strings.Contains(err.Error(), \"can't locate credentials file\") {\n        // fix HOME / TF_CLI_CONFIG_FILE and retry\n    }\n    return err\n}","preventionTips":["Always set `HOME` (or `APPDATA` on Windows) for Terraform processes.","Use `TF_CLI_CONFIG_FILE` to pin an explicit config path in containers.","Run Terraform under a user with a real home directory."],"tags":["terraform","cli-config","credentials","home-dir","environment"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}