{"record":{"id":"4c49c121e0a349fd","repo":"hashicorp/nomad","slug":"cannot-detect-windows-systemdrive-path","errorCode":null,"errorMessage":"cannot detect Windows SystemDrive path","messagePattern":"cannot detect Windows SystemDrive path","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"helper/winsvc/path_windows.go","lineNumber":167,"sourceCode":"\n\t// Apply the ACL to the directory\n\tif err := windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT,\n\t\twindows.OWNER_SECURITY_INFORMATION|\n\t\t\twindows.GROUP_SECURITY_INFORMATION|\n\t\t\twindows.DACL_SECURITY_INFORMATION|\n\t\t\twindows.PROTECTED_DACL_SECURITY_INFORMATION,\n\t\tuserSid, groupSid, dacl, nil); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\nfunc (w *windowsPaths) load() error {\n\tw.o.Do(func() {\n\t\tw.SystemDrive = os.Getenv(\"SystemDrive\")\n\t\tif w.SystemDrive == \"\" {\n\t\t\tw.loadErr = fmt.Errorf(\"cannot detect Windows SystemDrive path\")\n\t\t\treturn\n\t\t}\n\t\tw.SystemRoot = strings.ReplaceAll(os.Getenv(\"SystemDrive\"), \"SystemDrive\", w.SystemDrive)\n\n\t\tw.ProgramData = os.Getenv(\"ProgramData\")\n\t\tif w.ProgramData == \"\" {\n\t\t\tpdKey, err := registry.OpenKey(registry.LOCAL_MACHINE,\n\t\t\t\t`SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList`, registry.QUERY_VALUE)\n\t\t\tif err == nil {\n\t\t\t\tif pdVal, _, err := pdKey.GetStringValue(\"ProgramData\"); err == nil {\n\t\t\t\t\tw.ProgramData = pdVal\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif w.ProgramData == \"\" {\n\t\t\tw.loadErr = fmt.Errorf(\"cannot detect Windows ProgramData path\")\n\t\t\treturn\n\t\t}","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/helper/winsvc/path_windows.go#L149-L185","documentation":"windowsPaths.load lazily reads the SystemDrive environment variable inside a sync.Once; if the variable is unset it records loadErr 'cannot detect Windows SystemDrive path' and stops, since all derived Windows system paths depend on it.","triggerScenarios":"Any consumer of the windowsPaths type resolving system paths on Windows when the SystemDrive env var is missing from the process environment.","commonSituations":"Starting the service with a scrubbed/minimal environment (env stripped by a supervisor, container, or scheduler); running the binary on non-Windows hosts where SystemDrive is never set; explicitly clearing env vars when launching the process.","solutions":["Ensure SystemDrive (e.g. 'C:') is present in the service process environment","Fix the service/supervisor definition to not strip Windows-standard env vars","For non-Windows testing, set SystemDrive explicitly or use the non-windows path implementation"],"exampleFix":"// before\nexec.Command(bin) // env cleared: cmd.Env = []string{}\n// after\n// preserve standard Windows environment\ncmd.Env = os.Environ() // includes SystemDrive=C:","handlingStrategy":"validation","validationCode":"if runtime.GOOS == \"windows\" && os.Getenv(\"SystemDrive\") == \"\" {\n\treturn errors.New(\"SystemDrive env var required for windows path resolution\")\n}\n// then use windowsPaths normally","typeGuard":"func hasSystemDrive() bool {\n\treturn runtime.GOOS != \"windows\" || os.Getenv(\"SystemDrive\") != \"\"\n}","tryCatchPattern":"if err := w.load(); err != nil {\n\tif strings.Contains(err.Error(), \"cannot detect Windows SystemDrive\") {\n\t\tos.Setenv(\"SystemDrive\", \"C:\") // or surface a config error\n\t}\n\treturn err\n}","preventionTips":["Launch services with the parent environment preserved (don't clear cmd.Env)","Set SystemDrive explicitly in service definitions and container specs","Never assume a scrubbed environment on Windows; standard vars are required","Detect missing standard env vars at startup and fail with a clear message"],"tags":["windows","environment","env-var","paths"],"backgroundTag":"missing-env-var","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}