{"record":{"id":"aefee90f3ff80f26","repo":"opentofu/opentofu","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":207,"sourceCode":"\n// ModulesDir returns the directory in the [Dir.DataDir] that is used to store\n// the cached modules.\nfunc (d *Dir) ModulesDir() string {\n\treturn filepath.Join(d.DataDir(), modulesDir)\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 OpenTofu, 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":189,"sourceCodeEnd":211,"githubUrl":"https://github.com/opentofu/opentofu/blob/3561785c48c1ce615e7c50261bd351f26053efa2/internal/command/workdir/dir.go#L189-L211","documentation":"Returned by Dir.ensureDataDir when os.MkdirAll on the data directory (.terraform by default, or $TF_DATA_DIR) fails. Every command that persists per-run state needs this directory; creation fails when a path component exists as a regular file, the location is read-only, or permissions deny writing.","triggerScenarios":"A file named .terraform exists where a directory is needed; TF_DATA_DIR points into a read-only filesystem or a path owned by another user; disk quota exhausted; running tofu in a directory where the user has no write permission.","commonSituations":"CI containers running as a non-root user against a root-owned checkout; artifacts or dotfile managers creating a file named .terraform; NFS/ro mounts; TF_DATA_DIR set to a shared path with restrictive permissions; disk full.","solutions":["If .terraform is a file, remove it (rm .terraform) so a directory can be created","Check/fix write permissions: chown -R $USER . or run from a writable checkout","Verify TF_DATA_DIR points to a creatable/writable path; unset it to use the default","Free disk space or quota if MkdirAll reports no space"],"exampleFix":"# before: a file named .terraform blocks the directory\n$ ls -la | grep terraform\n-rw-r--r--  1 user user   0 .terraform\n$ tofu init   # failed to prepare working directory: ...\n# after\n$ rm .terraform && tofu init","handlingStrategy":"validation","validationCode":"# Preflight: data dir path must not be blocked by a file, and must be writable\nDATA_DIR=\"${TF_DATA_DIR:-.terraform}\"\nif [ -f \"$DATA_DIR\" ]; then\n  echo \"$DATA_DIR is a file; remove it so a directory can be created\" >&2; exit 2\nfi\nmkdir -p \"$DATA_DIR\" 2>/dev/null || { echo \"cannot create $DATA_DIR (permissions/space?)\" >&2; exit 2; }\ntofu init","typeGuard":"func isDataDirPrepErr(err error) bool {\n  return err != nil && strings.HasPrefix(err.Error(), \"failed to prepare working directory:\")\n}","tryCatchPattern":"if err := cmd.Run(); err != nil {\n  if isDataDirPrepErr(unwrapCause(err)) {\n    // filesystem issue: surface actionable context rather than retrying blindly\n    return fmt.Errorf(\"%w: check TF_DATA_DIR writability and disk space\", err)\n  }\n  return err\n}","preventionTips":["Run tofu from checkouts the current user owns; avoid root-owned .terraform in containers","Never create files named .terraform; add it to .gitignore as a directory","Set TF_DATA_DIR to a known-writable, spacious location in CI"],"tags":["go","filesystem","permissions","initialization"],"backgroundTag":null,"analyzedSha":"3561785c48c1ce615e7c50261bd351f26053efa2","analyzedAt":"2026-08-15T23:27:16.226Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}