charmbracelet/gum · error

error opening file: %w

Error message

error opening file: %w

What it means

Returned by the log command's Run when os.OpenFile cannot open the requested log file with create/write/append flags. The underlying *PathError is wrapped with %w and names the file and reason. This is a filesystem/permission problem, not a logging problem.

Source

Thrown at log/command.go:22

import (
	"fmt"
	"math"
	"os"
	"strings"
	"time"

	"charm.land/lipgloss/v2"
	log "charm.land/log/v2"
)

// Run is the command-line interface for logging text.
func (o Options) Run() error {
	l := log.New(os.Stderr)

	if o.File != "" {
		f, err := os.OpenFile(o.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm) //nolint:gosec
		if err != nil {
			return fmt.Errorf("error opening file: %w", err)
		}

		defer f.Close() //nolint:errcheck
		l.SetOutput(f)
	}

	l.SetPrefix(o.Prefix)
	l.SetLevel(-math.MaxInt32) // log all levels
	l.SetReportTimestamp(o.Time != "")
	if o.MinLevel != "" {
		lvl, err := log.ParseLevel(o.MinLevel)
		if err != nil {
			return err //nolint:wrapcheck
		}
		l.SetLevel(lvl)
	}

	timeFormats := map[string]string{

View on GitHub (pinned to 4d089f9550)

Solutions

  1. Verify the target directory exists and is writable by the current user (touch <file> to test)
  2. Create the directory first or choose a writable path (e.g. $HOME or $TMPDIR)
  3. Check mount status — read-only filesystems need remounting or a different target
  4. Inspect errors.Unwrap(err) (os.PathError) for the exact syscall reason

Example fix

// before
gum log --file /var/log/app.log "message"  # permission denied
// after
gum log --file "$HOME/.local/state/app.log" "message"  # writable location
Defensive patterns

Strategy: validation

Validate before calling

dir := filepath.Dir(logFile)
if err := os.MkdirAll(dir, 0o755); err != nil { return err }
if f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644); err != nil { return err } else { f.Close() }

Type guard

func canWriteFile(path string) bool {
    f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o644)
    if err != nil { return false }
    f.Close(); return true
}

Try / catch

err := logCmd.Run()
var perr *fs.PathError
if err != nil && errors.As(err, &perr) {
    return fmt.Errorf("cannot open log file %s: %v", perr.Path, perr.Err)
}

Prevention

When it happens

Trigger: Running the log command with --file pointing to a path that is unwritable, in a nonexistent directory, or otherwise unopenable (e.g. permission denied, read-only filesystem, path is a directory).

Common situations: Writing to /var/log or another root-owned dir without sudo; typo'd directory path; running in a read-only container filesystem; SELinux/AppArmor restrictions; disk full (less common).

Related errors


AI-assisted analysis of charmbracelet/gum@4d089f9550 (2026-08-31). Data as JSON: /api/errors/8f0c1bd3343eff7c. Report an issue: GitHub.