wavetermdev/waveterm · warning

failed to marshal backup metadata: %w

Error message

failed to marshal backup metadata: %w

What it means

MakeFileBackup serializes BackupMetadata (FullPath, Timestamp, Perm) to indented JSON before writing the .json sidecar. This error wraps json.MarshalIndent failing. It is practically unreachable: the struct contains only string fields which cannot fail to marshal, so seeing it indicates an extraordinary encoding/json failure.

Source

Thrown at pkg/filebackup/filebackup.go:76

	}

	backupName := fmt.Sprintf("%s.%s.%s.bak", basename, dirHash8, uuidStr)
	backupPath := filepath.Join(backupDir, backupName)

	err = os.WriteFile(backupPath, fileData, 0600)
	if err != nil {
		return "", fmt.Errorf("failed to write backup file: %w", err)
	}

	metadata := BackupMetadata{
		FullPath:  absFilePath,
		Timestamp: now.Format(time.RFC3339),
		Perm:      fmt.Sprintf("%04o", fileInfo.Mode().Perm()),
	}

	metadataJSON, err := json.MarshalIndent(metadata, "", "  ")
	if err != nil {
		return "", fmt.Errorf("failed to marshal backup metadata: %w", err)
	}

	metadataName := fmt.Sprintf("%s.%s.%s.json", basename, dirHash8, uuidStr)
	metadataPath := filepath.Join(backupDir, metadataName)

	err = os.WriteFile(metadataPath, metadataJSON, 0600)
	if err != nil {
		return "", fmt.Errorf("failed to write backup metadata: %w", err)
	}

	return backupPath, nil
}

func RestoreBackup(backupFilePath string, restoreToFileName string) error {
	backupData, err := os.ReadFile(backupFilePath)
	if err != nil {
		return fmt.Errorf("failed to read backup file: %w", err)
	}

View on GitHub (pinned to a4447c1563)

Solutions

  1. Inspect BackupMetadata for any newly added non-serializable fields if the struct was modified.
  2. Check memory availability if running under extreme constraints.
  3. Retry the operation; treat as transient if it cannot be reproduced.
Defensive patterns

Strategy: try-catch

Try / catch

backupPath, err := filebackup.MakeFileBackup(absPath)
if err != nil {
    if strings.Contains(err.Error(), "marshal backup metadata") {
        log.Errorf("unexpected metadata marshal failure: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: json.MarshalIndent(metadata, "", " ") returns an error — with the fixed BackupMetadata string-only struct this essentially cannot happen; only a broken/patched json package or extreme memory conditions would trigger it.

Common situations: Custom builds with modified BackupMetadata containing unsupported types (e.g. channels, funcs, cycles); out-of-memory conditions.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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