Help us shape the future of Go! Take the 2026 Go Developer Survey

atomicfile

package
v1.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package atomicfile is the one way forge writes a file it generates or scaffolds.

os.WriteFile truncates the target and then writes it. A process that dies between the two — a sandbox restart in the middle of `forge generate` — leaves the file empty or partial, and a generated file that is empty is not "stale": it is a compile error in every package that imports it. WriteFile instead writes a sibling temp file, fsyncs it, and renames it over the target, so the target is always either the complete old bytes or the complete new bytes.

internal/writeguard fails the build on a direct os.WriteFile under internal/, so a new generator cannot reintroduce the truncating write.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarkScratch added in v1.0.0

func MarkScratch(dir string) (unmark func())

MarkScratch declares dir a throwaway tree: WriteFile still writes atomically under it (readers never see a partial file) but skips the fsync, which on a busy disk dominates a render of hundreds of files. The returned function ends the declaration. Never mark a directory whose contents outlive the process.

func Replace added in v1.0.0

func Replace(oldPath, newPath string) error

Replace renames oldPath over newPath, for a file the caller finished writing some other way (a `go install` into a staging dir). It is the rename WriteFile commits with: on Windows, where a replace is refused while any handle — a reader, a virus scanner — has newPath open, it retries for a moment, and a refusal that outlasts that leaves newPath's old content.

func WriteFile

func WriteFile(path string, data []byte, perm fs.FileMode) error

WriteFile writes data to path atomically. It is a drop-in for os.WriteFile: a new file gets perm (subject to the umask); an existing regular file keeps its current permission bits and perm is ignored; a symlink is written through to its target. A path that exists but is not a regular file (a device, a FIFO) cannot be replaced by rename and is written in place.

On any error the target is untouched and the temp file is removed.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL