{"record":{"id":"b711f98b1295cea3","repo":"micro-editor/micro","slug":"could-not-open-file","errorCode":null,"errorMessage":"could not open file","messagePattern":"could not open file","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/buffer/buffer.go","lineNumber":321,"sourceCode":"\tf, err := os.OpenFile(filename, os.O_WRONLY, 0)\n\treadonly := errors.Is(err, fs.ErrPermission)\n\tf.Close()\n\n\tfile, err := os.Open(filename)\n\tif err == nil {\n\t\tdefer file.Close()\n\t}\n\n\tvar buf *Buffer\n\tif errors.Is(err, fs.ErrNotExist) {\n\t\t// File does not exist -- create an empty buffer with that name\n\t\tbuf = NewBufferFromString(\"\", filename, btype)\n\t} else if err != nil {\n\t\treturn nil, err\n\t} else {\n\t\tbuf = NewBuffer(file, util.FSize(file), filename, btype, cmd)\n\t\tif buf == nil {\n\t\t\treturn nil, errors.New(\"could not open file\")\n\t\t}\n\t}\n\n\tif readonly && prompt != nil {\n\t\tprompt.Message(fmt.Sprintf(\"Warning: file is readonly - %s will be attempted when saving\", config.GlobalSettings[\"sucmd\"].(string)))\n\t\t// buf.SetOptionNative(\"readonly\", true)\n\t}\n\n\treturn buf, nil\n}\n\n// NewBufferFromFile opens a new buffer using the given path\n// It will also automatically handle `~`, and line/column with filename:l:c\n// It will return an empty buffer if the path does not exist\n// and an error if the file is a directory\nfunc NewBufferFromFile(path string, btype BufType) (*Buffer, error) {\n\treturn NewBufferFromFileWithCommand(path, btype, emptyCommand)\n}","sourceCodeStart":303,"sourceCodeEnd":339,"githubUrl":"https://github.com/micro-editor/micro/blob/1c8b82b32e408a5faf340039ed92d5266bea3293/internal/buffer/buffer.go#L303-L339","documentation":"Catch-all from the buffer constructor at internal/buffer/buffer.go:321: os.Open on the path succeeded (so stat/read-permission checks passed) but NewBuffer(file, util.FSize(file), ...) returned nil. NewBuffer internally decodes the stream (encoding transformer, fileformat detection, ApplyBackup); if that reader path fails it yields a nil buffer, and this generic error replaces the lost cause. Treat it as 'file opened but could not be decoded/loaded'.","triggerScenarios":"An unreadable-content file: encoding setting in micro set to an invalid/unsupportedIANA name producing a broken decoder, a file that changes size/disappears between open and FSize (race), or exotic sparse/proc files where Open works but reads fail (e.g. /proc entries on some kernels).","commonSituations":"\"encoding\": \"cp10000\"-style bad setting in settings.json; opening files under /proc or /sys that stat oddly; antivirus/mandatory-access-control systems that permit open() but block read(); very large files on flaky NFS.","solutions":["Sanity-check readability outside micro: head -c 1M thefile — if that fails, fix perms/AV/MAC first","Check the encoding option: micro file with -default-settings or temporarily remove the \"encoding\" entry from settings.json to fall back to utf-8","Avoid /proc,/sys and device pseudo-files; copy content to a real file first: cat /proc/x > /tmp/x && micro /tmp/x","If it reproduces, run micro -debug and capture the log; consider reporting upstream since the underlying cause is swallowed"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Pre-read the file yourself; if you can't, micro can't either\nfunc preflight(path string) error {\n    f, err := os.Open(path)\n    if err != nil { return err }\n    defer f.Close()\n    br := bufio.NewReader(io.LimitReader(f, 1<<20))\n    if _, err := br.Peek(1); err != nil && err != io.EOF {\n        return fmt.Errorf(\"unreadable content: %w\", err)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"buf, err := buffer.NewBufferFromFile(path, buffer.BTDefault)\nif err != nil {\n    if err.Error() == \"could not open file\" {\n        // generic wrapper: retry once via explicit read, else report precisely\n        data, rerr := os.ReadFile(path)\n        if rerr != nil { return nil, fmt.Errorf(\"underlying read error: %w\", rerr) }\n        buf = buffer.NewBufferFromString(string(data), path, buffer.BTDefault)\n        return buf, nil\n    }\n    return nil, err\n}","preventionTips":["Keep the 'encoding' setting valid (IANA names like utf-8, latin1); bad values degrade loading","Test exotic paths (procfs, device nodes) with head/cat before handing them to micro","When this fires, capture micro -debug output — the original cause is swallowed"],"tags":["io","open","encoding","buffer","generic"],"backgroundTag":null,"analyzedSha":"1c8b82b32e408a5faf340039ed92d5266bea3293","analyzedAt":"2026-08-15T20:01:45.134Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}