hashicorp/terraform · error

Error getting pwd

Error message

Error getting pwd: %s

What it means

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).

Solutions

  1. Change to a directory that exists: in the shell, `cd` to a known-good path and rerun.
  2. Ensure parent directories of the intended cwd have execute (+x) permission for the current user.
  3. In CI, run Terraform from a stable checkout path, not from a dir that gets wiped mid-run.
  4. If a runner recreates the dir, restart the Terraform process afterward.

Example fix

// before: shell cwd was deleted by another process
//   $ terraform validate   -> Error getting pwd: getwd: no such file or directory
// after
//   $ cd ~ && cd - && terraform validate   # or rerun from a valid directory
Defensive patterns

Strategy: validation

Validate before calling

func cwdValid() error {
    wd, err := os.Getwd()
    if err != nil {
        // try to recover by changing to $HOME
        if h := os.Getenv("HOME"); h != "" {
            if e2 := os.Chdir(h); e2 == nil { return nil }
        }
        return fmt.Errorf("working directory unavailable: %w", err)
    }
    _ = wd
    return nil
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/66ac2b632025c30b. Report an issue: GitHub.

Appendix: source

Thrown at internal/command/command.go:71

// ModulePath returns the path to the root module and validates CLI arguments.
//
// This centralizes the logic for any commands that previously accepted
// a module path via CLI arguments. This will error if any extraneous arguments
// are given and suggest using the -chdir flag instead.
//
// If your command accepts more than one arg, then change the slice bounds
// to pass validation.
func ModulePath(args []string) (string, error) {
	// TODO: test

	if len(args) > 0 {
		return "", fmt.Errorf("Too many command line arguments. Did you mean to use -chdir?")
	}

	path, err := os.Getwd()
	if err != nil {
		return "", fmt.Errorf("Error getting pwd: %s", err)
	}

	return path, nil
}

View on GitHub (pinned to d32a084675)