{"record":{"id":"e2d324124adbc22e","repo":"hashicorp/terraform","slug":"failed-to-initialize-stacksplugin-cache-directory","errorCode":null,"errorMessage":"failed to initialize stacksplugin cache directory: %w","messagePattern":"failed to initialize stacksplugin cache directory: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/stacks.go","lineNumber":403,"sourceCode":"\t\t\tpluginPath = defaultPluginPath\n\t\t} else {\n\t\t\tlog.Printf(\"[TRACE] Failed to check the stacksplugin cache path store at '%s', using default '%s'\", cacheStorePath, defaultPluginPath)\n\t\t\t// Use the default plugin cache path if there was an error checking the file\n\t\t\tpluginPath = defaultPluginPath\n\t\t}\n\t} else {\n\t\tdata, err := os.ReadFile(cacheStorePath)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to read stacks plugin stored path file: %w\", err)\n\t\t}\n\t\tpluginPath = string(data)\n\t}\n\n\tif info, err := os.Stat(pluginPath); err != nil || !info.IsDir() {\n\t\tlog.Printf(\"[TRACE] initialized stacksplugin cache directory at %q\", pluginPath)\n\t\terr = os.MkdirAll(pluginPath, 0755)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to initialize stacksplugin cache directory: %w\", err)\n\t\t}\n\t} else {\n\t\tlog.Printf(\"[TRACE] stacksplugin cache directory found at %q\", pluginPath)\n\t}\n\n\treturn pluginPath, nil\n}\n\nfunc (c *StacksCommand) storeStacksPluginPath(pluginCachePath string) error {\n\tf, err := os.Create(path.Join(c.WorkingDir.DataDir(), \".stackspluginpath\"))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create stacks plugin stored path file: %w\", err)\n\t}\n\tdefer f.Close()\n\tf.WriteString(pluginCachePath)\n\n\treturn nil\n}","sourceCodeStart":385,"sourceCodeEnd":421,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/stacks.go#L385-L421","documentation":"Wrapped error from StacksCommand.initPackagesCache when os.MkdirAll(pluginPath, 0755) fails. After resolving the stacks plugin cache path (from .stackspluginpath or the default), if the directory does not exist or is not a directory, Terraform tries to create it. Failure means the cache directory cannot be created — almost always a permission or invalid-path problem.","triggerScenarios":"Running a stacks command where the resolved pluginPath is non-creatable: a parent path is a file (not a dir), permission denied on a parent, read-only filesystem, or pluginPath contains an invalid/relative segment. MkdirAll returns an OS error wrapped here.","commonSituations":"CLIConfigDir is unset making the default path empty/invalid; .stackspluginpath contains a bad path (typo, absolute path under a read-only mount); container/CI with read-only home; disk full; SELinux/AppArmor denying mkdir.","solutions":["Inspect the wrapped OS error for the exact path and reason (permission, not a directory, read-only).","Fix the path recorded in .stackspluginpath or delete the file to fall back to the default.","Ensure CLIConfigDir (~/.terraform.d) and its parent are writable.","Run with appropriate filesystem permissions / writable mount in CI."],"exampleFix":"// before: .stackspluginpath points at a read-only path\n$ cat .terraform/.stackspluginpath\n/readonly/plugins\n// after\n$ rm -f .terraform/.stackspluginpath\n$ terraform stacks init   # recreates default writable cache dir","handlingStrategy":"validation","validationCode":"// Pre-flight: ensure the resolved stacks plugin cache path is creatable before stacks commands\npackage main\n\nfunc preflightStacksCacheDir(path string) error {\n\tif path == \"\" { return fmt.Errorf(\"stacks plugin cache path is empty; set CLIConfigDir\") }\n\tparent := filepath.Dir(path)\n\tif info, err := os.Stat(parent); err != nil {\n\t\treturn fmt.Errorf(\"parent %s of cache dir missing: %w\", parent, err)\n\t} else if !info.IsDir() {\n\t\treturn fmt.Errorf(\"parent %s of cache dir is not a directory\", parent)\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure CLIConfigDir (~/.terraform.d) and its parents are writable.","Delete a bad .stackspluginpath to fall back to the default cache location.","Mount a writable home/data volume in CI containers."],"tags":["stacks","plugin-cache","filesystem","permissions"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}