{"record":{"id":"afa8a1ec492b3fc1","repo":"hashicorp/nomad","slug":"path-exists-and-is-not-directory-s","errorCode":null,"errorMessage":"path exists and is not directory - %s","messagePattern":"path exists and is not directory - (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"helper/winsvc/path_windows.go","lineNumber":63,"sourceCode":"\t}\n\n\treturn result.String(), nil\n}\n\nfunc (w *windowsPaths) CreateDirectory(path string, restrict_on_create bool) error {\n\ts, err := os.Stat(path)\n\n\tif err != nil && !errors.Is(err, os.ErrNotExist) {\n\t\treturn err\n\t}\n\n\tif err == nil {\n\t\t// Directory exists so nothing to do\n\t\tif s.IsDir() {\n\t\t\treturn nil\n\t\t}\n\n\t\treturn fmt.Errorf(\"path exists and is not directory - %s\", path)\n\t}\n\n\t// NOTE: mode ignored on Windows. If directory should\n\t// be restricted, an ACL will be applied below.\n\tif err := os.MkdirAll(path, 0o000); err != nil {\n\t\treturn err\n\t}\n\n\t// Since the directory was just created, apply access\n\t// restrictions if requested\n\tif restrict_on_create {\n\t\tif err := setDirectoryPermissions(path); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\treturn nil\n}","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/helper/winsvc/path_windows.go#L45-L81","documentation":"CreateDirectory first stats the target path; if it exists and is a directory it succeeds, but if it exists as a regular file, symlink target, or otherwise non-directory entry, it returns this error rather than overwriting the path. It refuses to clobber existing non-directory files.","triggerScenarios":"Calling winsvc.CreateDirectory(path, mode) when a regular file (e.g. a stale data file or config) already exists at exactly `path`, so os.Stat succeeds with !s.IsDir().","commonSituations":"A previous run left a file where a directory is now required; a user manually placed a file at the configured data/alloc dir path; mount or volume shadowing the path with a file.","solutions":["Remove or rename the conflicting file at the path, then retry CreateDirectory","Point the service configuration at a different directory path","If the file is expected data, migrate it into a proper directory layout first"],"exampleFix":"// before\nos.WriteFile(\"C:\\\\ProgramData\\\\nomad\\\\data\", blob, 0o644)\nwinsvc.CreateDirectory(\"C:\\\\ProgramData\\\\nomad\\\\data\", 0o755) // conflict\n// after\nwinsvc.CreateDirectory(\"C:\\\\ProgramData\\\\nomad\\\\data\", 0o755) // create dir before any file use","handlingStrategy":"validation","validationCode":"if fi, err := os.Stat(path); err == nil && !fi.IsDir() {\n\treturn fmt.Errorf(\"%s exists as a non-directory; remove or rename it first\", path)\n}\nif err := winsvc.CreateDirectory(path, 0o755); err != nil {\n\treturn err\n}","typeGuard":"func isExistingDirectory(path string) bool {\n\tfi, err := os.Stat(path)\n\treturn err == nil && fi.IsDir()\n}","tryCatchPattern":"if err := winsvc.CreateDirectory(path, mode); err != nil {\n\tif strings.Contains(err.Error(), \"path exists and is not directory\") {\n\t\tif err := os.Rename(path, path+\".bak\"); err != nil {\n\t\t\treturn err\n\t\t}\n\t\treturn winsvc.CreateDirectory(path, mode)\n\t}\n\treturn err\n}","preventionTips":["Create service directories before writing any file that could occupy the path","Use path-specific filenames (data.db) distinct from directory names","Check for leftover files after crashes before restarting the service","Avoid mounting volumes/files directly onto configured directory paths"],"tags":["windows","filesystem","directory","path-conflict"],"backgroundTag":"path-exists-not-a-directory","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"}