hashicorp/nomad · error

cannot detect Windows SystemDrive path

Error message

cannot detect Windows SystemDrive path

What it means

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.

Source

Thrown at helper/winsvc/path_windows.go:167

	// Apply the ACL to the directory
	if err := windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT,
		windows.OWNER_SECURITY_INFORMATION|
			windows.GROUP_SECURITY_INFORMATION|
			windows.DACL_SECURITY_INFORMATION|
			windows.PROTECTED_DACL_SECURITY_INFORMATION,
		userSid, groupSid, dacl, nil); err != nil {
		return err
	}

	return nil
}

func (w *windowsPaths) load() error {
	w.o.Do(func() {
		w.SystemDrive = os.Getenv("SystemDrive")
		if w.SystemDrive == "" {
			w.loadErr = fmt.Errorf("cannot detect Windows SystemDrive path")
			return
		}
		w.SystemRoot = strings.ReplaceAll(os.Getenv("SystemDrive"), "SystemDrive", w.SystemDrive)

		w.ProgramData = os.Getenv("ProgramData")
		if w.ProgramData == "" {
			pdKey, err := registry.OpenKey(registry.LOCAL_MACHINE,
				`SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList`, registry.QUERY_VALUE)
			if err == nil {
				if pdVal, _, err := pdKey.GetStringValue("ProgramData"); err == nil {
					w.ProgramData = pdVal
				}
			}
		}
		if w.ProgramData == "" {
			w.loadErr = fmt.Errorf("cannot detect Windows ProgramData path")
			return
		}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Ensure SystemDrive (e.g. 'C:') is present in the service process environment
  2. Fix the service/supervisor definition to not strip Windows-standard env vars
  3. For non-Windows testing, set SystemDrive explicitly or use the non-windows path implementation

Example fix

// before
exec.Command(bin) // env cleared: cmd.Env = []string{}
// after
// preserve standard Windows environment
cmd.Env = os.Environ() // includes SystemDrive=C:
Defensive patterns

Strategy: validation

Validate before calling

if runtime.GOOS == "windows" && os.Getenv("SystemDrive") == "" {
	return errors.New("SystemDrive env var required for windows path resolution")
}
// then use windowsPaths normally

Type guard

func hasSystemDrive() bool {
	return runtime.GOOS != "windows" || os.Getenv("SystemDrive") != ""
}

Try / catch

if err := w.load(); err != nil {
	if strings.Contains(err.Error(), "cannot detect Windows SystemDrive") {
		os.Setenv("SystemDrive", "C:") // or surface a config error
	}
	return err
}

Prevention

When it happens

Trigger: Any consumer of the windowsPaths type resolving system paths on Windows when the SystemDrive env var is missing from the process environment.

Common situations: 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.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/4c49c121e0a349fd. Report an issue: GitHub.