micro-editor/micro · error

could not open file

Error message

could not open file

What it means

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'.

Source

Thrown at internal/buffer/buffer.go:321

	f, err := os.OpenFile(filename, os.O_WRONLY, 0)
	readonly := errors.Is(err, fs.ErrPermission)
	f.Close()

	file, err := os.Open(filename)
	if err == nil {
		defer file.Close()
	}

	var buf *Buffer
	if errors.Is(err, fs.ErrNotExist) {
		// File does not exist -- create an empty buffer with that name
		buf = NewBufferFromString("", filename, btype)
	} else if err != nil {
		return nil, err
	} else {
		buf = NewBuffer(file, util.FSize(file), filename, btype, cmd)
		if buf == nil {
			return nil, errors.New("could not open file")
		}
	}

	if readonly && prompt != nil {
		prompt.Message(fmt.Sprintf("Warning: file is readonly - %s will be attempted when saving", config.GlobalSettings["sucmd"].(string)))
		// buf.SetOptionNative("readonly", true)
	}

	return buf, nil
}

// NewBufferFromFile opens a new buffer using the given path
// It will also automatically handle `~`, and line/column with filename:l:c
// It will return an empty buffer if the path does not exist
// and an error if the file is a directory
func NewBufferFromFile(path string, btype BufType) (*Buffer, error) {
	return NewBufferFromFileWithCommand(path, btype, emptyCommand)
}

View on GitHub (pinned to 1c8b82b32e)

Solutions

  1. Sanity-check readability outside micro: head -c 1M thefile — if that fails, fix perms/AV/MAC first
  2. Check the encoding option: micro file with -default-settings or temporarily remove the "encoding" entry from settings.json to fall back to utf-8
  3. Avoid /proc,/sys and device pseudo-files; copy content to a real file first: cat /proc/x > /tmp/x && micro /tmp/x
  4. If it reproduces, run micro -debug and capture the log; consider reporting upstream since the underlying cause is swallowed
Defensive patterns

Strategy: validation

Validate before calling

// Pre-read the file yourself; if you can't, micro can't either
func preflight(path string) error {
    f, err := os.Open(path)
    if err != nil { return err }
    defer f.Close()
    br := bufio.NewReader(io.LimitReader(f, 1<<20))
    if _, err := br.Peek(1); err != nil && err != io.EOF {
        return fmt.Errorf("unreadable content: %w", err)
    }
    return nil
}

Try / catch

buf, err := buffer.NewBufferFromFile(path, buffer.BTDefault)
if err != nil {
    if err.Error() == "could not open file" {
        // generic wrapper: retry once via explicit read, else report precisely
        data, rerr := os.ReadFile(path)
        if rerr != nil { return nil, fmt.Errorf("underlying read error: %w", rerr) }
        buf = buffer.NewBufferFromString(string(data), path, buffer.BTDefault)
        return buf, nil
    }
    return nil, err
}

Prevention

When it happens

Trigger: 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).

Common situations: "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.

Related errors


AI-assisted analysis of micro-editor/micro@1c8b82b32e (2026-08-15). Data as JSON: /api/errors/b711f98b1295cea3. Report an issue: GitHub.