hashicorp/nomad · error

cannot detect Windows ProgramFiles path

Error message

cannot detect Windows ProgramFiles path

What it means

The winsvc helper cannot determine the Windows ProgramFiles directory. It checks the ProgramFiles environment variable, then falls back to the registry value ProgramFilesDir under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion. If both are empty it records this error in w.loadErr and stops, so path resolution for the library fails.

Source

Thrown at helper/winsvc/path_windows.go:199

		}
		if w.ProgramData == "" {
			w.loadErr = fmt.Errorf("cannot detect Windows ProgramData path")
			return
		}
		w.ProgramData = strings.ReplaceAll(w.ProgramData, "SystemDrive", w.SystemDrive)

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

	return w.loadErr
}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Set the ProgramFiles environment variable (e.g. set ProgramFiles=C:\Program Files) before the process starts.
  2. Check and restore the registry value ProgramFilesDir under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion.
  3. Run under a standard Windows service environment so default system env vars are populated.
  4. For custom images/containers, explicitly inject standard Windows environment variables at launch.

Example fix

// before
ProgramFiles env empty, registry ProgramFilesDir missing -> loadErr

// after
# launch wrapper
set ProgramFiles=C:\Program Files
Defensive patterns

Strategy: validation

Validate before calling

if runtime.GOOS == "windows" && os.Getenv("ProgramFiles") == "" {
    return errors.New("ProgramFiles env var missing; set it before use")
}

Try / catch

if err != nil && strings.Contains(err.Error(), "cannot detect Windows ProgramFiles path") {
    // use fallback default C:\\Program Files or fail fast
}

Prevention

When it happens

Trigger: Calling a path lookup that triggers the sync.Once loader when os.Getenv("ProgramFiles") is empty and the registry GetStringvalue on 'ProgramFilesDir' fails or is empty.

Common situations: Service running under an account with a sanitized environment; minimal/modified Windows images missing the registry key; running in containers or emulated Windows where standard env vars are absent.

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/87c2f798501515d2. Report an issue: GitHub.