hashicorp/terraform · error

Error saving -plugin-dir to workspace directory: %s

Error message

Error saving -plugin-dir to workspace directory: %s

What it means

Emitted by InitCommand.run when c.storePluginPath(c.pluginPath) returns an error while persisting the -plugin-dir flag value into the workspace's data directory. The %s carries the underlying write error. This happens very early in init, before any backend or provider work, so the command exits immediately with code 1.

Source

Thrown at internal/command/init_run.go:55

	c.Meta.input = initArgs.InputEnabled
	c.Meta.targetFlags = initArgs.TargetFlags
	c.Meta.compactWarnings = initArgs.CompactWarnings

	// Copying the state only happens during backend migration, so setting
	// -force-copy implies -migrate-state
	if c.forceInitCopy {
		c.migrateState = true
	}

	if len(initArgs.PluginPath) > 0 {
		c.pluginPath = initArgs.PluginPath
	}

	// Get the working directory
	path := c.Meta.WorkingDir.RootModuleDir()

	if err := c.storePluginPath(c.pluginPath); err != nil {
		diags = diags.Append(fmt.Errorf("Error saving -plugin-dir to workspace directory: %s", err))
		view.Diagnostics(diags)
		return 1
	}

	// Initialization can be aborted by interruption signals
	ctx, done := c.InterruptibleContext(c.CommandContext())
	defer done()

	if initArgs.FromModule != "" {
		src := initArgs.FromModule

		empty, err := configs.IsEmptyDir(path, initArgs.TestsDirectory)
		if err != nil {
			diags = diags.Append(fmt.Errorf("Error validating destination directory: %s", err))
			view.Diagnostics(diags)
			return 1
		}
		if !empty {

View on GitHub (pinned to c9def3e214)

Solutions

  1. Ensure the working directory (and .terraform if present) is writable by the terraform process: `chmod -R u+w .` or `chown` appropriately.
  2. Remove a stale/foreign .terraform directory: `rm -rf .terraform` and retry init.
  3. Free disk space if the volume is full.
  4. If the tree must stay read-only, point terraform at a writable data dir or run init from a writable checkout.

Example fix

# before
$ terraform init -plugin-dir=/opt/plugins   # ./.terraform read-only
# after
$ sudo chown -R $USER .terraform
$ terraform init -plugin-dir=/opt/plugins
Defensive patterns

Strategy: validation

Validate before calling

// Verify the working dir and .terraform are writable before init -plugin-dir.
if fi, err := os.Stat(wd); err != nil || fi.Mode().Perm()&0200 == 0 {
    return fmt.Errorf("working dir %q not writable; cannot persist plugin path", wd)
}

Prevention

When it happens

Trigger: Running `terraform init -plugin-dir=<dir>` when Terraform cannot write the plugin-path marker file (typically under .terraform/) into the working directory. Causes: working directory read-only or on a read-only mount, .terraform owned by another user, disk full, or the path contains characters the OS rejects.

Common situations: Read-only source trees (e.g. terraform run from a mounted/checked-out directory without write permission); containers where the working dir is bind-mounted read-only; .terraform created by root and init re-run as non-root; full disk; NFS mounts with permission issues.

Related errors


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