{"record":{"id":"8171bca822a30eee","repo":"hashicorp/terraform","slug":"failed-to-prepare-working-directory-w","errorCode":null,"errorMessage":"failed to prepare working directory: %w","messagePattern":"failed to prepare working directory: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/workdir/dir.go","lineNumber":155,"sourceCode":"\n// TestDataDir returns the path where the receiver keeps settings\n// and artifacts related to terraform tests.\nfunc (d *Dir) TestDataDir() string {\n\treturn filepath.Join(d.dataDir, \"test\")\n}\n\n// ensureDataDir creates the data directory and all of the necessary parent\n// directories that lead to it, if they don't already exist.\n//\n// For directories that already exist ensureDataDir will preserve their\n// permissions, while it'll create any new directories to be owned by the user\n// running Terraform, readable and writable by that user, and readable by\n// all other users, or some approximation of that on non-Unix platforms which\n// have a different permissions model.\nfunc (d *Dir) ensureDataDir() error {\n\terr := os.MkdirAll(d.dataDir, 0755)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to prepare working directory: %w\", err)\n\t}\n\treturn nil\n}\n","sourceCodeStart":137,"sourceCodeEnd":159,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/workdir/dir.go#L137-L159","documentation":"Dir.ensureDataDir wraps a failed os.MkdirAll on the working directory's data directory (.terraform). The %w carries the OS-level error. ensureDataDir is called during normal working-directory setup, so this surfaces as a failure to prep the workspace.","triggerScenarios":"Terraform cannot create .terraform (or its parents): permission denied on a parent, read-only filesystem, path-too-long, disk full, or an existing non-directory file at the dataDir path.","commonSituations":"Running in a CI step with a read-only workspace, restricted container mount, NFS permission mismatch, a file named `.terraform` left in the way, or filesystem out of space.","solutions":["Check the wrapped error for EACCES / ENOSPC / EROFS and address that specific cause.","Ensure the user running Terraform owns (or can write) the workspace and all parents.","Remove any regular file named `.terraform` blocking the directory creation.","Free inodes/space on the target filesystem."],"exampleFix":"# before — running in read-only mount\nterraform init  # -> failed to prepare working directory: ...\n\n# after — run from a writable workspace\ncd ~/work/myenv && terraform init","handlingStrategy":"try-catch","validationCode":"// pre-flight: confirm we can write to the workspace\ntestPath := filepath.Join(d.dataDir, \".write-test\")\nif err := os.WriteFile(filepath.Dir(d.dataDir)+\"/.write-probe\", []byte(\"x\"), 0644); err != nil {\n    return fmt.Errorf(\"workspace not writable: %w\", err)\n}\n_ = os.Remove(testPath)","typeGuard":null,"tryCatchPattern":"if err := d.ensureDataDir(); err != nil {\n    if errors.Is(err, os.ErrPermission) { return fmt.Errorf(\"permission denied creating .terraform: %w\", err) }\n    if errors.Is(err, os.ErrNotExist) { return fmt.Errorf(\"parent directory missing: %w\", err) }\n    return err\n}","preventionTips":["Run Terraform from a workspace the invoking user owns.","In CI, mount the workspace as read-write.","Confirm disk space and inodes before large plans."],"tags":["workdir","filesystem","permissions","init"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}