kopia/kopia · error

error writing template to file

Error message

error writing template to file

What it means

When --html-output is used, notification-template show writes the rendered template text to a temp file (kopia-template-preview.html) so it can be opened in a browser, and wraps any os.WriteFile failure as "error writing template to file". It is a filesystem write failure, not a template problem.

Solutions

  1. Fix TMPDIR / ensure the temp directory exists and is writable by the current user.
  2. Free disk space if the filesystem is full.
  3. Run without --html-output to print the template to stdout instead.
  4. Check permissions/quota on the temp filesystem.

Example fix

// before
TMPDIR=/nonexistent kopia notification template show email-html --html-output
// after
export TMPDIR=/tmp
kopia notification template show email-html --html-output
Defensive patterns

Strategy: fallback

Validate before calling

if fi, err := os.Stat(os.TempDir()); err != nil || !fi.IsDir() { return errors.New("temp dir unavailable; skip --html-output") }

Try / catch

if err := os.WriteFile(tf, []byte(text), 0o644); err != nil {
    log.Warnf("html preview unavailable (%v); printing to stdout", err)
    fallbackToStdout(text)
    return nil
}

Prevention

When it happens

Trigger: os.WriteFile(tf, []byte(text), 0o644) fails while writing to os.TempDir(): read-only temp directory, full disk, TMPDIR pointing to a non-writable or nonexistent path, or permission restrictions.

Common situations: TMPDIR set to a path the user cannot write; disk full; running in a hardened sandbox/container with /tmp mounted noexec or read-only; quota exceeded.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of kopia/kopia@82495e54b5 (2026-09-07). Data as JSON: /api/errors/b9d7b20aa9488952. Report an issue: GitHub.

Appendix: source

Thrown at cli/command_notification_template_show.go:64

	} else {
		var found bool

		text, found, err = notifytemplate.GetTemplate(ctx, rep, c.templateName)
		if !found {
			text, err = notifytemplate.GetEmbeddedTemplate(c.templateName)
		}
	}

	if err != nil {
		return errors.Wrap(err, "error listing templates")
	}

	if c.htmlOutput {
		tf := filepath.Join(os.TempDir(), "kopia-template-preview.html")

		//nolint:gosec,mnd
		if err := os.WriteFile(tf, []byte(text), 0o644); err != nil {
			return errors.Wrap(err, "error writing template to file")
		}

		open.Run(tf) //nolint:errcheck
	}

	c.out.printStdout("%v\n", strings.TrimRight(text, "\n"))

	return nil
}

View on GitHub (pinned to 82495e54b5)