{"record":{"id":"66ac2b632025c30b","repo":"hashicorp/terraform","slug":"error-getting-pwd-s","errorCode":null,"errorMessage":"Error getting pwd: %s","messagePattern":"Error getting pwd: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/command.go","lineNumber":71,"sourceCode":"\n// ModulePath returns the path to the root module and validates CLI arguments.\n//\n// This centralizes the logic for any commands that previously accepted\n// a module path via CLI arguments. This will error if any extraneous arguments\n// are given and suggest using the -chdir flag instead.\n//\n// If your command accepts more than one arg, then change the slice bounds\n// to pass validation.\nfunc ModulePath(args []string) (string, error) {\n\t// TODO: test\n\n\tif len(args) > 0 {\n\t\treturn \"\", fmt.Errorf(\"Too many command line arguments. Did you mean to use -chdir?\")\n\t}\n\n\tpath, err := os.Getwd()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"Error getting pwd: %s\", err)\n\t}\n\n\treturn path, nil\n}\n","sourceCodeStart":53,"sourceCodeEnd":76,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/command.go#L53-L76","documentation":"Returned by ModulePath (command.go:69) when os.Getwd fails. ModulePath calls os.Getwd to obtain the current directory as the module path (after confirming no positional args). A failure means the OS cannot determine the process's working directory, which can happen when the cwd has been deleted out from under the process or the process lacks permission to traverse the path.","triggerScenarios":"os.Getwd returns an error: the current working directory was deleted while the process was running; a parent directory component no longer exists; permission was revoked on a path component (chmod/chown removed execute bit); on Linux, the cwd was removed and recreated so the inode no longer matches.","commonSituations":"Terraform launched in a directory that another process (or `rm -rf`) deleted mid-run; CI job where the workspace dir was cleaned and recreated underneath the process; a container where the cwd mount was removed; permission change on a parent dir while Terraform is running.","solutions":["Restart Terraform from a directory that currently exists: `cd ~/stable-dir && terraform <cmd>`.","If the original cwd was deleted and recreated, simply cd into it again so the shell re-resolves it, then re-run.","Ensure the process user has execute (search) permission on every parent directory of the cwd.","In CI, run Terraform from a stable workspace path that is not cleaned underneath the run."],"exampleFix":"# before\n$ terraform plan\nError getting pwd: getwd: no such file or directory\n# (cwd was deleted mid-session)\n\n# after\ncd ~/projects/myinfra  # an existing directory\nterraform plan","handlingStrategy":"try-catch","validationCode":"// Resolve cwd explicitly and fail fast with an actionable message\nwd, err := os.Getwd()\nif err != nil {\n    if fallback, ferr := os.UserHomeDir(); ferr == nil {\n        if cherr := os.Chdir(fallback); cherr == nil {\n            wd, err = os.Getwd()\n        }\n    }\n}\nif err != nil {\n    return \"\", fmt.Errorf(\"working directory unavailable; restart from an existing directory: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"// If ModulePath fails on getwd, advise the operator to restart from a stable cwd rather than retrying in place.\nif _, err := command.ModulePath(args); err != nil {\n    if strings.Contains(err.Error(), \"pwd\") || strings.Contains(err.Error(), \"getwd\") {\n        fmt.Fprintln(os.Stderr, \"Working directory is gone. cd into an existing directory and rerun terraform.\")\n        os.Exit(1)\n    }\n}","preventionTips":["Launch Terraform from a stable, long-lived directory.","In CI, cd into the workspace at job start and avoid cleaning it underneath the run.","Do not delete and recreate the cwd around a long-running Terraform process.","Ensure the process user has execute permission on every parent of the cwd."],"tags":["cli","filesystem","environment","working-directory"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}