{"record":{"id":"abd33e566f17b793","repo":"caddyserver/caddy","slug":"reading-environment-file-v","errorCode":null,"errorMessage":"reading environment file: %v","messagePattern":"reading environment file: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/main.go","lineNumber":355,"sourceCode":"// not in the flag set.\nfunc (f Flags) Float64(name string) float64 {\n\tval, _ := strconv.ParseFloat(f.String(name), 64)\n\treturn val\n}\n\n// Duration returns the duration representation of the\n// flag given by name. It returns false if the flag\n// is not a duration type. It panics if the flag is\n// not in the flag set.\nfunc (f Flags) Duration(name string) time.Duration {\n\tval, _ := caddy.ParseDuration(f.String(name))\n\treturn val\n}\n\nfunc loadEnvFromFile(envFile string) error {\n\tfile, err := os.Open(envFile)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"reading environment file: %v\", err)\n\t}\n\tdefer file.Close()\n\n\tenvMap, err := parseEnvFile(file)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"parsing environment file: %v\", err)\n\t}\n\n\tfor k, v := range envMap {\n\t\t// do not overwrite existing environment variables\n\t\t_, exists := os.LookupEnv(k)\n\t\tif !exists {\n\t\t\tif err := os.Setenv(k, v); err != nil {\n\t\t\t\treturn fmt.Errorf(\"setting environment variables: %v\", err)\n\t\t\t}\n\t\t}\n\t}\n","sourceCodeStart":337,"sourceCodeEnd":373,"githubUrl":"https://github.com/caddyserver/caddy/blob/50e54ee279aa1e504fe218ca49ab6ae16c100410/cmd/main.go#L337-L373","documentation":"loadEnvFromFile wraps a failed os.Open of the --env-file target. The file could not be opened at all — usually missing or unreadable. (Its sibling 'parsing environment file' fires later, for content problems.) Loaded vars are only set if not already present, so the file is purely supplemental.","triggerScenarios":"Running 'caddy run --env-file .env' where .env does not exist in the working directory, or the CLI user lacks read permission on it.","commonSituations":"Deployments copying configs but not the env file; relative path assumptions in service units with a different WorkingDirectory; permissions tightened on secrets files (which is fine — but then grant the caddy user read access).","solutions":["Verify the path exists and is readable by the caddy process user","Use an absolute path for --env-file in service units","Remember existing environment variables win; you can also set vars directly in the unit instead of the file"],"exampleFix":"# before\nExecStart=/usr/bin/caddy run --config /etc/caddy/Caddyfile --env-file .env\n\n# after\nExecStart=/usr/bin/caddy run --config /etc/caddy/Caddyfile --env-file /etc/caddy/.env","handlingStrategy":"validation","validationCode":"# Ensure the env file exists and is readable before launch:\n[ -r /etc/caddy/.env ] || { echo \"env file missing/unreadable\" >&2; exit 1; }\ncaddy run --config /etc/caddy/Caddyfile --env-file /etc/caddy/.env","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use absolute --env-file paths in service units","Ship the env file with the config in deployment bundles","Remember existing environment variables are never overwritten by --env-file"],"tags":["cli","environment","file-not-found","permissions"],"backgroundTag":null,"analyzedSha":"50e54ee279aa1e504fe218ca49ab6ae16c100410","analyzedAt":"2026-08-15T09:20:21.641Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}