{"record":{"id":"a7104539e325ca65","repo":"micro-editor/micro","slug":"error-s-is-not-a-regular-file-and-cannot-be-save","errorCode":null,"errorMessage":"Error: %s is not a regular file and cannot be saved","messagePattern":"Error: (.+?) is not a regular file and cannot be saved","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/buffer/save.go","lineNumber":283,"sourceCode":"\n\tfilename, err = util.ReplaceHome(filename)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tnewFile := false\n\tfileInfo, err := os.Stat(filename)\n\tif err != nil {\n\t\tif !errors.Is(err, fs.ErrNotExist) {\n\t\t\treturn err\n\t\t}\n\t\tnewFile = true\n\t}\n\tif err == nil && fileInfo.IsDir() {\n\t\treturn errors.New(\"Error: \" + filename + \" is a directory and cannot be saved\")\n\t}\n\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}","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/micro-editor/micro/blob/1c8b82b32e408a5faf340039ed92d5266bea3293/internal/buffer/save.go#L265-L301","documentation":"Thrown by buffer.Save/SaveAs when the target path exists, is not a directory, but is also not a regular file (os.Stat succeeded and fileInfo.Mode().IsRegular() is false). This covers character/block devices, named pipes (FIFOs), unix sockets, and other special files. Micro refuses to write buffer contents to such nodes because the save loop expects a seekable, plain file.","triggerScenarios":"Calling SaveAs on /dev/null or /dev/zero (character devices), on a named pipe created with mkfifo, on a unix socket file, or on a device node under /dev. Any os.Stat(filename) that succeeds with a non-regular, non-directory mode hits the `!fileInfo.Mode().IsRegular()` branch in internal/buffer/save.go:283.","commonSituations":"Users piping micro output toward /dev/null-style targets, typoing a path that resolves to /dev/..., or saving onto a fifo used by another process. Also happens on systems where a config-managed path (e.g. a ssh control socket) collides with the file being edited.","solutions":["Save to a real file path: pick a name that does not exist yet, or an existing regular file.","If you intended /dev/null semantics, save to a scratch regular file instead and let your shell/tooling discard it.","Remove the special file (rm /path/to/fifo) if it was created accidentally and you own it.","If you truly must write to a device node, do it outside micro (e.g. write file then `cat file > /dev/...`)."],"exampleFix":"// before (micro command bar):\n> save /dev/null\n// error: /dev/null is not a regular file and cannot be saved\n\n// after:\n> save /tmp/scratch.txt","handlingStrategy":"validation","validationCode":"func canSaveTo(path string) bool {\n    fi, err := os.Stat(path)\n    if errors.Is(err, fs.ErrNotExist) {\n        return true // new file is fine\n    }\n    if err != nil {\n        return false\n    }\n    return fi.Mode().IsRegular()\n}\n\n// before b.SaveAs(path):\nif !canSaveTo(\"/dev/null\") { /* pick another path */ }","typeGuard":"func isRegularFile(path string) (bool, error) {\n    fi, err := os.Stat(path)\n    if err != nil {\n        return false, err\n    }\n    return fi.Mode().IsRegular(), nil\n}","tryCatchPattern":"if err := buf.SaveAs(p); err != nil {\n    if strings.Contains(err.Error(), \"is not a regular file\") {\n        // prompt user for a different path or shell out to write elsewhere\n    }\n}","preventionTips":["Never pre-fill SaveAs prompts with device or fifo paths; default to a filename in the current directory.","In wrappers, stat the target and require Mode().IsRegular() or non-existence before invoking save.","Keep mkfifo/socket nodes out of directories you browse for save targets."],"tags":["filesystem","save","io","special-files"],"backgroundTag":null,"analyzedSha":"1c8b82b32e408a5faf340039ed92d5266bea3293","analyzedAt":"2026-08-15T20:01:45.134Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}