wavetermdev/waveterm · error

file too large for editing: %d bytes (max: %d)

Error message

file too large for editing: %d bytes (max: %d)

What it means

Returned when the file exceeds the maximum size allowed for in-memory editing, protecting against loading huge files. Both the actual size in bytes and the configured maximum are included in the message.

Source

Thrown at pkg/util/fileutil/fileutil.go:349

			failed = true
		}
	}

	return modifiedContents, results
}

func ReplaceInFile(filePath string, edits []EditSpec) error {
	fileInfo, err := os.Stat(filePath)
	if err != nil {
		return fmt.Errorf("failed to stat file: %w", err)
	}

	if !fileInfo.Mode().IsRegular() {
		return fmt.Errorf("not a regular file: %s", filePath)
	}

	if fileInfo.Size() > MaxEditFileSize {
		return fmt.Errorf("file too large for editing: %d bytes (max: %d)", fileInfo.Size(), MaxEditFileSize)
	}

	contents, err := os.ReadFile(filePath)
	if err != nil {
		return fmt.Errorf("failed to read file: %w", err)
	}

	modifiedContents, err := ApplyEdits(contents, edits)
	if err != nil {
		return err
	}

	if err := os.WriteFile(filePath, modifiedContents, fileInfo.Mode()); err != nil {
		return fmt.Errorf("failed to write file: %w", err)
	}

	return nil
}

View on GitHub (pinned to a4447c1563)

Solutions

  1. Edit the file with a streaming tool (sed/awk) or process it line-by-line yourself.
  2. Split the file and edit only the relevant portion.
  3. Trim/generated content programmatically instead of text-editing it.
  4. If legitimately needed, reduce the file below 5 MB or extend MaxEditFileSize in the library consciously (memory cost).

Example fix

// before
fileutil.ReplaceInFile("huge.log", edits) // 40 MB

// after
if fi, _ := os.Stat(path); fi.Size() > 5*1024*1024 {
    return fmt.Errorf("use streaming edit for %s", path)
}
fileutil.ReplaceInFile(path, edits)
Defensive patterns

Strategy: validation

Validate before calling

const maxEdit = 5 * 1024 * 1024
if fi, err := os.Stat(path); err == nil && fi.Size() > maxEdit {
    return fmt.Errorf("%s is %d bytes; exceeds 5MB edit limit", path, fi.Size())
}

Try / catch

// Go: fall back to streaming edit on size rejection
if err := fileutil.ReplaceInFile(path, edits); err != nil {
    if strings.Contains(err.Error(), "file too large") {
        return streamingReplace(path, edits) // line-by-line implementation
    }
    return err
}

Prevention

When it happens

Trigger: Calling ReplaceInFile on a file whose os.Stat size exceeds 5*1024*1024 bytes.

Common situations: Editing large logs, generated code, lockfiles (package-lock.json), datasets, or binary-ish files that grew past 5 MB.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/5a2dc996d708ca28. Report an issue: GitHub.