hashicorp/terraform · error

failed to create stacks plugin stored path file

Error message

failed to create stacks plugin stored path file: %w

What it means

Thrown by StacksCommand.storeStacksPluginPath when os.Create of `.stackspluginpath` in c.WorkingDir.DataDir() fails. This is the write side of the cache path store that [743] reads. os.Create failure means the data dir is missing/unwritable, or a file of the same name exists as a directory or is locked.

Solutions

  1. Run `terraform init` first so `.terraform/` exists and is owned by the current user.
  2. Verify ownership and writability: `ls -la .terraform/ && touch .terraform/.test`.
  3. Run terraform with the same user account that owns the working directory.
  4. If on a read-only filesystem, point TF_DATA_DIR at a writable location.

Example fix

# before: read-only workspace mount
 terraform stacks up

# after
 export TF_DATA_DIR=/tmp/tfdata
 terraform init
 terraform stacks up
Defensive patterns

Strategy: validation

Validate before calling

// Ensure DataDir exists and is writable before storeStacksPluginPath.
dataDir := c.WorkingDir.DataDir()
if err := os.MkdirAll(dataDir, 0755); err != nil {
    return fmt.Errorf("data dir %s not writable: %w", dataDir, err)
}
if info, err := os.Stat(dataDir); err == nil && info.Mode().Perm()&0200 == 0 {
    return fmt.Errorf("data dir %s is not user-writable", dataDir)
}

Try / catch

f, err := os.Create(path.Join(c.WorkingDir.DataDir(), ".stackspluginpath"))
if err != nil {
    if os.IsPermission(err) {
        // helpful hint rather than raw PathError
        return fmt.Errorf("cannot write %s/.stackspluginpath (permission denied); check TF_DATA_DIR ownership: %w", c.WorkingDir.DataDir(), err)
    }
    return fmt.Errorf("failed to create stacks plugin stored path file: %w", err)
}

Prevention

When it happens

Trigger: DataDir() (typically `.terraform/`) does not exist or is read-only; `.terraform/.stackspluginpath` already exists as a directory; disk full; Windows file lock by another process holding it open.

Common situations: `.terraform/` was deleted between init and stacks run; running terraform as a different user than the one that owns `.terraform/`; container with read-only mount for the workspace; concurrent terraform processes writing the marker.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/15d69ec277582ec6. Report an issue: GitHub.

Appendix: source

Thrown at internal/command/stacks.go:415

	}

	if info, err := os.Stat(pluginPath); err != nil || !info.IsDir() {
		log.Printf("[TRACE] initialized stacksplugin cache directory at %q", pluginPath)
		err = os.MkdirAll(pluginPath, 0755)
		if err != nil {
			return "", fmt.Errorf("failed to initialize stacksplugin cache directory: %w", err)
		}
	} else {
		log.Printf("[TRACE] stacksplugin cache directory found at %q", pluginPath)
	}

	return pluginPath, nil
}

func (c *StacksCommand) storeStacksPluginPath(pluginCachePath string) error {
	f, err := os.Create(path.Join(c.WorkingDir.DataDir(), ".stackspluginpath"))
	if err != nil {
		return fmt.Errorf("failed to create stacks plugin stored path file: %w", err)
	}
	defer f.Close()
	f.WriteString(pluginCachePath)

	return nil
}

// Run runs the stacks command with the given arguments.
func (c *StacksCommand) Run(args []string) int {
	args = c.Meta.process(args)
	return c.realRun(args, c.Meta.Streams.Stdout.File, c.Meta.Streams.Stderr.File)
}

// Help returns help text for the stacks command.
func (c *StacksCommand) Help() string {
	helpText := new(bytes.Buffer)
	errorText := new(bytes.Buffer)

View on GitHub (pinned to d32a084675)