{"record":{"id":"b2995d16c294f8da","repo":"micro-editor/micro","slug":"parent-dirs-don-t-exist-enable-mkparents-for-au","errorCode":null,"errorMessage":"Parent dirs don't exist, enable 'mkparents' for auto creation","messagePattern":"Parent dirs don't exist, enable 'mkparents' for auto creation","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/buffer/save.go","lineNumber":300,"sourceCode":"\tif err == nil && !fileInfo.Mode().IsRegular() {\n\t\treturn errors.New(\"Error: \" + filename + \" is not a regular file and cannot be saved\")\n\t}\n\n\tabsFilename := util.ResolvePath(filename)\n\n\t// Get the leading path to the file | \".\" is returned if there's no leading path provided\n\tif dirname := filepath.Dir(absFilename); dirname != \".\" {\n\t\t// Check if the parent dirs don't exist\n\t\tif _, statErr := os.Stat(dirname); errors.Is(statErr, fs.ErrNotExist) {\n\t\t\t// Prompt to make sure they want to create the dirs that are missing\n\t\t\tif b.Settings[\"mkparents\"].(bool) {\n\t\t\t\t// Create all leading dir(s) since they don't exist\n\t\t\t\tif mkdirallErr := os.MkdirAll(dirname, os.ModePerm); mkdirallErr != nil {\n\t\t\t\t\t// If there was an error creating the dirs\n\t\t\t\t\treturn mkdirallErr\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\treturn errors.New(\"Parent dirs don't exist, enable 'mkparents' for auto creation\")\n\t\t\t}\n\t\t}\n\t}\n\n\tsaveResponseChan := make(chan saveResponse)\n\tsaveRequestChan <- saveRequest{b, absFilename, withSudo, newFile, saveResponseChan}\n\tresult := <-saveResponseChan\n\terr = result.err\n\tif err != nil {\n\t\tif errors.Is(err, util.ErrOverwrite) {\n\t\t\tscreen.TermMessage(err)\n\t\t\terr = errors.Unwrap(err)\n\n\t\t\tb.UpdateModTime()\n\t\t}\n\t\treturn err\n\t}\n","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/micro-editor/micro/blob/1c8b82b32e408a5faf340039ed92d5266bea3293/internal/buffer/save.go#L282-L318","documentation":"Thrown by buffer save when the leading directory of the absolute target path does not exist (os.Stat(dirname) returns fs.ErrNotExist) and the 'mkparents' setting is false. Micro deliberately refuses to silently create missing parent directories unless the user opted in via mkparents.","triggerScenarios":"SaveAs to 'newproj/src/main.go' when 'newproj/src' does not exist, with the mkparents option left at its default false. The check runs in internal/buffer/save.go:300 right after util.ResolvePath/filepath.Dir computes the parent; any missing intermediate directory triggers it.","commonSituations":"Starting a new project layout from inside micro, saving into a not-yet-cloned repo path, or a typo in a directory component ('~/Documnets/file.txt'). Users coming from editors that auto-create directories hit this most.","solutions":["Enable the option: run `set mkparents true` (persist by adding \"mkparents\": true to settings.json), then retry the save.","Or create the directory yourself first: mkdir -p <parent-dir>, then save again.","Or fix the typo in the path — check each directory component with ls.","If embedding micro, set the mkparents buffer/global setting programmatically before SaveAs."],"exampleFix":"// before (~/.config/micro/settings.json):\n{ \"mkparents\": false }\n// save as newdir/file.go -> error\n\n// after:\n{ \"mkparents\": true }\n// save as newdir/file.go -> parents created, save succeeds","handlingStrategy":"validation","validationCode":"func ensureParentDir(path string, mkparents bool) error {\n    dir := filepath.Dir(util.ResolvePath(path))\n    if dir == \".\" {\n        return nil\n    }\n    if _, err := os.Stat(dir); errors.Is(err, fs.ErrNotExist) {\n        if !mkparents {\n            return os.MkdirAll(dir, 0o755) // or surface a prompt to the user\n        }\n    }\n    return nil\n}\n// run before buf.SaveAs(path)","typeGuard":null,"tryCatchPattern":"if err := buf.Save(); err != nil {\n    if strings.Contains(err.Error(), \"enable 'mkparents'\") {\n        _ = config.SetGlobalOption(\"mkparents\", true)\n        // user re-triggers save, or call buf.Save() again\n    }\n}","preventionTips":["Set \"mkparents\": true once in settings.json if you regularly create new directory trees from the editor.","Alternatively mkdir -p the target tree from your shell before saving into it.","Double-check directory components for typos before hitting enter on SaveAs."],"tags":["filesystem","save","settings","directories"],"backgroundTag":null,"analyzedSha":"1c8b82b32e408a5faf340039ed92d5266bea3293","analyzedAt":"2026-08-15T20:01:45.134Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}