{"record":{"id":"e0689d02844dda59","repo":"cloudflare/cloudflared","slug":"could-not-find-program-data-directory-s-env-var","errorCode":null,"errorMessage":"could not find program data directory, %s env var must be set","messagePattern":"could not find program data directory, (.+?) env var must be set","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cloudflared/windows_service.go","lineNumber":295,"sourceCode":"\t\t\t}\n\t\tcase err := <-errC:\n\t\t\tif err != nil {\n\t\t\t\telog.Error(1, fmt.Sprintf(\"cloudflared terminated with error %v\", err))\n\t\t\t\tssec = true\n\t\t\t\terrno = 1\n\t\t\t} else {\n\t\t\t\telog.Info(1, \"cloudflared terminated without error\")\n\t\t\t\terrno = 0\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t}\n}\n\nfunc getConfigDir() (string, error) {\n\tprogDat, progDatSet := os.LookupEnv(programDataEnvVar)\n\tif !progDatSet {\n\t\treturn \"\", fmt.Errorf(\"could not find program data directory, %s env var must be set\", programDataEnvVar)\n\t}\n\n\treturn filepath.Join(progDat, configDirName), nil\n}\n\nfunc installWindowsService(c *cli.Context) error {\n\tzeroLogger := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)\n\n\tzeroLogger.Info().Msg(\"Installing cloudflared Windows service\")\n\texepath, err := os.Executable()\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Cannot find path name that start the process\")\n\t}\n\tm, err := mgr.Connect()\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Cannot establish a connection to the service control manager\")\n\t}\n\tdefer m.Disconnect()","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/cmd/cloudflared/windows_service.go#L277-L313","documentation":"cloudflared stores its Windows service configuration (including the tunnel token file) under %PROGRAMDATA%\\<configDirName>. getConfigDir reads the PROGRAMDATA environment variable and throws this error when it is unset, because it cannot locate the program-data directory. It is called by installWindowsService and uninstallWindowsService during service installation/removal.","triggerScenarios":"os.LookupEnv(\"PROGRAMDATA\") returns ok=false — the variable is not defined in the process environment when service install/uninstall runs.","commonSituations":"Launching cloudflared from a stripped environment (custom service runner, scheduled task, or SSH session where the user profile/PROGRAMDATA is not populated); running inside a container or under a tool that clears the environment; a corrupted system environment variable block.","solutions":["Ensure PROGRAMDATA is set system-wide (it is by default: C:\\ProgramData) — check `echo %PROGRAMDATA%`","Run the install/uninstall from a normal elevated cmd/PowerShell session, not a stripped environment","Set it explicitly for the invocation: set PROGRAMDATA=C:\\ProgramData && cloudflared service install <args>","If launched by another service/scheduler, add PROGRAMDATA to that task's environment"],"exampleFix":"# before (stripped shell)\ncloudflared service install <token>\n// after (cmd)\nset PROGRAMDATA=C:\\ProgramData\ncloudflared service install <token>","handlingStrategy":"validation","validationCode":"# cmd pre-check\nif \"%PROGRAMDATA%\"==\"\" (echo PROGRAMDATA is not set & exit /b 1)\n# PowerShell pre-check\nif (-Not $env:PROGRAMDATA) { throw 'PROGRAMDATA env var must be set before service install' }","typeGuard":null,"tryCatchPattern":"if err := installWindowsService(ctx); err != nil {\n\tif strings.Contains(err.Error(), \"program data directory\") {\n\t\treturn fmt.Errorf(\"PROGRAMDATA env var missing; run from a normal elevated shell: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Run service install from a standard elevated cmd/PowerShell, not stripped environments","Avoid launching cloudflared install from containers or custom runners that clear the env","Keep the system PROGRAMDATA variable intact (default C:\\ProgramData)","Verify with `echo %PROGRAMDATA%` before installing"],"tags":["windows","environment","windows-service"],"backgroundTag":"missing-env-var","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}