{"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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/command.go#L53-L76","documentation":"Thrown by command.ModulePath when os.Getwd fails after the argument-count check passed. Getwd fails when the current working directory has been deleted out from under the process, or when directory permissions prevent resolving the path via getcwd(3).","triggerScenarios":"The shell/process is sitting in a directory that was removed in another session; a parent directory of cwd lost execute permission; on some systems, cwd is on an unmounted filesystem.","commonSituations":"A build/test runner deletes and recreates the working directory while Terraform still runs in the old inode; `cd` into a tmpdir that another process cleaned up; permission changes on a parent dir stripping +x.","solutions":["Change to a directory that exists: in the shell, `cd` to a known-good path and rerun.","Ensure parent directories of the intended cwd have execute (+x) permission for the current user.","In CI, run Terraform from a stable checkout path, not from a dir that gets wiped mid-run.","If a runner recreates the dir, restart the Terraform process afterward."],"exampleFix":"// before: shell cwd was deleted by another process\n//   $ terraform validate   -> Error getting pwd: getwd: no such file or directory\n// after\n//   $ cd ~ && cd - && terraform validate   # or rerun from a valid directory","handlingStrategy":"validation","validationCode":"func cwdValid() error {\n    wd, err := os.Getwd()\n    if err != nil {\n        // try to recover by changing to $HOME\n        if h := os.Getenv(\"HOME\"); h != \"\" {\n            if e2 := os.Chdir(h); e2 == nil { return nil }\n        }\n        return fmt.Errorf(\"working directory unavailable: %w\", err)\n    }\n    _ = wd\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run Terraform from a stable directory that won't be deleted mid-run.","In CI, cd into the checkout before invoking rather than referencing a soon-cleaned tmpdir.","Keep +x on parent directories of the working directory."],"tags":["cli","filesystem","working-directory","environment"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}