kubernetes/kubernetes · error

creating temp directory: %w

Error message

creating temp directory: %w

What it means

Returned from runDiff (main.go:123) when os.MkdirTemp("", "apidiff-") fails. The tool creates a scratch temp directory to hold apidiff state files and git worktrees; this is its very first allocation. Failure is an OS-level inability to create a directory in the system temp dir.

Source

Thrown at hack/apidiff-changelog/main.go:123

			os.Exit(errExit.exitCode)
		}
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}
}

// runDiff runs the full API diff workflow: dumps API state for the base and target
// revisions, compares each module, reports incompatibilities, and optionally updates
// or patches CHANGELOG.md files.
// Returns a non-nil error if any module has unresolved incompatible changes.
func runDiff(opts runDiffOptions, dirs []string) error {
	if _, err := exec.LookPath("apidiff"); err != nil {
		return fmt.Errorf("apidiff binary not found in PATH; please install it with 'go install golang.org/x/exp/cmd/apidiff@latest'")
	}

	tempDir, err := os.MkdirTemp("", "apidiff-")
	if err != nil {
		return fmt.Errorf("creating temp directory: %w", err)
	}
	defer func() { _ = os.RemoveAll(tempDir) }()

	repoRoot, err := filepath.Abs(opts.repoRoot)
	if err != nil {
		return fmt.Errorf("resolving repo root %q: %w", opts.repoRoot, err)
	}

	cwd, err := os.Getwd()
	if err != nil {
		return fmt.Errorf("getting working directory: %w", err)
	}
	dirs, err = normalizeDirs(cwd, repoRoot, dirs)
	if err != nil {
		return fmt.Errorf("normalizing directories: %w", err)
	}

	afterDir := filepath.Join(tempDir, "after")

View on GitHub (pinned to b882c60b40)

Solutions

  1. Check the temp dir is writable: `mktemp -d` should succeed.
  2. Free disk/inodes on the filesystem backing TMPDIR.
  3. Point TMPDIR at a writable location: `export TMPDIR=/var/tmp/apidiff-work && mkdir -p $TMPDIR`.
  4. If running in a container with a read-only root FS, mount a writable emptyDir at /tmp.
Defensive patterns

Strategy: try-catch

Validate before calling

func ensureTempWritable() error {
    dir := os.TempDir()
    f, err := os.CreateTemp(dir, ".apidiff-writetest")
    if err != nil {
        return fmt.Errorf("temp dir %q not writable: %w", dir, err)
    }
    _ = f.Close()
    _ = os.Remove(f.Name())
    return nil
}

Try / catch

tempDir, err := os.MkdirTemp("", "apidiff-")
if err != nil {
    return fmt.Errorf("create temp dir under %q: %w", os.TempDir(), err)
}

Prevention

When it happens

Trigger: TMPDIR (/tmp) does not exist, is read-only, full, or the process lacks permission; the process has exhausted its file/inode quota; disk full.

Common situations: CI runner out of disk; container with read-only /tmp and no writable emptyDir; TMPDIR env var pointed at a deleted/unwritable path; node under disk pressure.

Related errors


AI-assisted analysis of kubernetes/kubernetes@b882c60b40 (2026-08-07). Data as JSON: /api/errors/3a2a7f355a313d9b. Report an issue: GitHub.