hashicorp/terraform · error

cannot write to temporary file %s: %s

Error message

cannot write to temporary file %s: %s

What it means

Emitted after the temp file was successfully created (credentials.go:398) but writing the marshaled JSON credential payload to it failed (f.Write at line 415). Because the write is paired with an immediate f.Close and a deferred os.Remove of the temp file, this error leaves the original credentials file untouched — only the throwaway temp file is affected.

Source

Thrown at internal/command/cliconfig/credentials.go:418

			return fmt.Errorf("cannot create temporary file to update credentials: %s", err)
		}
		tmpName := f.Name()
		moved := false
		defer func(f *os.File, name string) {
			// Remove the temporary file if it hasn't been moved yet. We're
			// ignoring errors here because there's nothing we can do about
			// them anyway.
			if !moved {
				os.Remove(name)
			}
		}(f, tmpName)

		// Write the credentials to the temporary file, then immediately close
		// it, whether or not the write succeeds.
		_, err = f.Write(newSrc)
		f.Close()
		if err != nil {
			return fmt.Errorf("cannot write to temporary file %s: %s", tmpName, err)
		}

		// Temporary file now replaces the original file, as atomically as
		// possible. (At the very least, we should not end up with a file
		// containing only a partial JSON object.)
		err = replacefile.AtomicRename(tmpName, filename)
		if err != nil {
			return fmt.Errorf("failed to replace %s with temporary file %s: %s", filename, tmpName, err)
		}

		// Credentials file should be readable only by its owner. (This may
		// not be effective on all platforms, but should at least work on
		// Unix-like targets and should be harmless elsewhere.)
		if err := os.Chmod(filename, 0600); err != nil {
			return fmt.Errorf("cannot set mode for credentials file %s: %s", filename, err)
		}

		moved = true

View on GitHub (pinned to c9def3e214)

Solutions

  1. Free space on the volume that holds the credentials directory (`df -h ~/.terraform.d`) and retry the login/operation.
  2. If the directory is on a flaky network filesystem, move TF_DATA_DIR to local storage and retry.
  3. Check disk health and inode quota (`df -i`); a full inode table also yields write errors even with free bytes.
  4. Re-run `terraform login` (or whichever command triggered the credential write) once space/connectivity is restored — the original file is untouched.

Example fix

# before
$ terraform login
cannot write to temporary file /home/me/.terraform.d/credentials429918238: disk quota exceeded

# after
$ rm -rf ~/.terraform.d/plugin-cache  # free space
$ quota -s                             # confirm quota
$ terraform login
Defensive patterns

Strategy: try-catch

Validate before calling

// Check free space / writability of the credentials directory before persisting
var stat unix.Statfs_t
if err := unix.Statfs(filepath.Dir(credsPath), &stat); err == nil && stat.Bavail == 0 {
    return errors.New("no free space on credentials volume")
}

Try / catch

// Retry once after a transient write (network FS / full disk), then surface a clear error
if err := credsStore.StoreHostCredentialsEntry(host, token); err != nil {
    time.Sleep(200 * time.Millisecond)
    if err2 := credsStore.StoreHostCredentialsEntry(host, token); err2 != nil {
        return fmt.Errorf("persisting credential failed twice (last: %w); original credentials file is intact", err2)
    }
}

Prevention

When it happens

Trigger: f.Write(newSrc) returns a non-nil error. Concrete causes: the filesystem holding the temp file fills up mid-write (ENOSPC), a network filesystem drops the connection (NFS/SMB), the device is quota-limited, an I/O error occurs on the underlying disk, or the temp file was created on a tmpfs that hit its size limit.

Common situations: CI runner with a small /tmp or home volume running out of space while logging in; home directory on a network share that flaps; container with an undersized tmpfs mounted at HOME; disk hardware degradation on a long-lived workstation.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/c8e08a6ab59802d6. Report an issue: GitHub.