hashicorp/terraform · error

Error getting pwd: %s

Error message

Error getting pwd: %s

What it means

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.

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

Solutions

  1. Restart Terraform from a directory that currently exists: `cd ~/stable-dir && terraform <cmd>`.
  2. If the original cwd was deleted and recreated, simply cd into it again so the shell re-resolves it, then re-run.
  3. Ensure the process user has execute (search) permission on every parent directory of the cwd.
  4. In CI, run Terraform from a stable workspace path that is not cleaned underneath the run.

Example fix

# before
$ terraform plan
Error getting pwd: getwd: no such file or directory
# (cwd was deleted mid-session)

# after
cd ~/projects/myinfra  # an existing directory
terraform plan
Defensive patterns

Strategy: try-catch

Validate before calling

// Resolve cwd explicitly and fail fast with an actionable message
wd, err := os.Getwd()
if err != nil {
    if fallback, ferr := os.UserHomeDir(); ferr == nil {
        if cherr := os.Chdir(fallback); cherr == nil {
            wd, err = os.Getwd()
        }
    }
}
if err != nil {
    return "", fmt.Errorf("working directory unavailable; restart from an existing directory: %w", err)
}

Try / catch

// If ModulePath fails on getwd, advise the operator to restart from a stable cwd rather than retrying in place.
if _, err := command.ModulePath(args); err != nil {
    if strings.Contains(err.Error(), "pwd") || strings.Contains(err.Error(), "getwd") {
        fmt.Fprintln(os.Stderr, "Working directory is gone. cd into an existing directory and rerun terraform.")
        os.Exit(1)
    }
}

Prevention

When it happens

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

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

Related errors


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