{"record":{"id":"6614419395e36634","repo":"direnv/direnv","slug":"couldn-t-find-a-configuration-directory-for-direnv","errorCode":null,"errorMessage":"couldn't find a configuration directory for direnv","messagePattern":"couldn't find a configuration directory for direnv","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/cmd/config.go","lineNumber":99,"sourceCode":"\t\tif homedir, homedirErr := os.UserHomeDir(); homedirErr == nil {\n\t\t\tpathExpanded = filepath.Join(homedir, path[2:])\n\t\t}\n\t}\n\treturn pathExpanded\n}\n\n// LoadConfig opens up the direnv configuration from the Env.\nfunc LoadConfig(env Env) (config *Config, err error) {\n\tconfig = &Config{\n\t\tEnv: env,\n\t}\n\n\tconfig.ConfDir = env[DIRENV_CONFIG]\n\tif config.ConfDir == \"\" {\n\t\tconfig.ConfDir = xdg.ConfigDir(env, \"direnv\")\n\t}\n\tif config.ConfDir == \"\" {\n\t\terr = fmt.Errorf(\"couldn't find a configuration directory for direnv\")\n\t\treturn\n\t}\n\n\tvar exePath string\n\tif exePath, err = os.Executable(); err != nil {\n\t\terr = fmt.Errorf(\"LoadConfig() os.Executable() failed: %w\", err)\n\t\treturn\n\t}\n\t// Fix for mingsys\n\texePath = strings.ReplaceAll(exePath, \"\\\\\", \"/\")\n\tconfig.SelfPath = exePath\n\n\tvar wdErr error\n\tif config.WorkDir, wdErr = os.Getwd(); wdErr != nil {\n\t\t// handled by `findEnvUp`\n\t\treturn //nolint:nilnesserr\n\t}\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/direnv/direnv/blob/b00e451f547f39be7ab836d969054114a465a0f9/internal/cmd/config.go#L81-L117","documentation":"LoadConfig determines direnv's configuration directory: DIRENV_CONFIG first, then xdg.ConfigDir(env, \"direnv\"). If both are empty it cannot locate a config directory and returns this error, aborting configuration load.","triggerScenarios":"Calling LoadConfig (directly or via command Call) with DIRENV_CONFIG unset and an environment where XDG_CONFIG_HOME is unset/empty AND $HOME is unset/empty, so xdg.ConfigDir returns \"\".","commonSituations":"Running direnv from cron/systemd/init with a stripped environment lacking HOME; containers running as a user with no home directory; tests that clear the environment; setting DIRENV_CONFIG to an empty string.","solutions":["Set DIRENV_CONFIG to an explicit config directory: export DIRENV_CONFIG=~/.config/direnv.","Ensure HOME is set in the execution environment (export HOME=/home/user).","Set XDG_CONFIG_HOME (e.g. export XDG_CONFIG_HOME=$HOME/.config) so the XDG fallback resolves.","Fix the service/container definition so the process runs as a user with a home directory."],"exampleFix":"// before (systemd unit)\nExecStart=/usr/bin/direnv export bash\n// after\n[Service]\nEnvironment=HOME=%h\nEnvironment=DIRENV_CONFIG=%h/.config/direnv\nExecStart=/usr/bin/direnv export bash","handlingStrategy":"validation","validationCode":"func hasConfigDir() bool {\n    if os.Getenv(\"DIRENV_CONFIG\") != \"\" { return true }\n    if os.Getenv(\"XDG_CONFIG_HOME\") != \"\" { return true }\n    home, err := os.UserHomeDir()\n    return err == nil && home != \"\"\n}\nif !hasConfigDir() { os.Setenv(\"DIRENV_CONFIG\", filepath.Join(homeOrDefault(), \".config\", \"direnv\")) }","typeGuard":null,"tryCatchPattern":"err := cmd.Run() // direnv invocation\nif err != nil && strings.Contains(err.Error(), \"couldn't find a configuration directory\") {\n    // fix environment then retry\n    os.Setenv(\"DIRENV_CONFIG\", filepath.Join(home, \".config\", \"direnv\"))\n    err = cmd.Run()\n}","preventionTips":["Always set HOME (or DIRENV_CONFIG) for services, cron jobs, and containers that run direnv.","Never export DIRENV_CONFIG as an empty string.","Smoke-test direnv in a stripped env (`env -i PATH=... direnv version`) before deploying.","Run as a user with a home directory."],"tags":["direnv","configuration","xdg","environment"],"backgroundTag":"missing-env-var","analyzedSha":"b00e451f547f39be7ab836d969054114a465a0f9","analyzedAt":"2026-09-05T21:39:49.983Z","contentChangedAt":"2026-09-05T21:39:49.983Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}