hashicorp/terraform · error

Error getting pwd

Error message

Error getting pwd: %s

What it means

Returned from `Meta.checkRequiredVersion` when `os.Getwd()` fails. Before loading any Terraform config to evaluate `required_version` constraints, the command needs the current working directory; if the OS cannot return it, version checking aborts with this diagnostic.

Solutions

  1. Re-run the command from a directory that still exists and is accessible: `cd` to a valid path first.
  2. If a script deleted its own cwd, fix the script to `cd` to a stable directory before invoking Terraform.
  3. Check parent directory permissions: `ls -la` on each ancestor to ensure execute (`x`) bits are present.
  4. Avoid running Terraform inside a directory managed by a cleanup tool or ephemeral mount.

Example fix

# before: invoked after cwd was deleted
terraform plan
# after: move to an existing dir first
cd "$HOME/project" && terraform plan
Defensive patterns

Strategy: validation

Validate before calling

// Ensure cwd exists and is accessible before invoking terraform.
wd, err := os.Getwd()
if err != nil {
    if err := os.Chdir(fallbackDir); err != nil { return err }
}

Type guard

null

Try / catch

// os.Getwd errors are not retryable; the caller must chdir to a valid directory.

Prevention

When it happens

Trigger: `os.Getwd()` returns an error when the current working directory has been deleted out from under the process, when a parent directory has its read/execute permission revoked, or on platforms where the cwd inode can no longer be resolved (e.g. a removed tmpfs mount).

Common situations: Running Terraform from a directory that was deleted or moved by another process mid-session; a wrapper script that `cd`s into a temp dir it then `rm -rf`s; filesystem permission changes; container/namespace filesystem teardown.

Related errors


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

Appendix: source

Thrown at internal/command/meta.go:858

// code paths which use these arguments to be passed them directly for clarity.
func (m *Meta) applyStateArguments(args *arguments.State) {
	m.stateLock = args.Lock
	m.stateLockTimeout = args.LockTimeout
	m.statePath = args.StatePath
	m.stateOutPath = args.StateOutPath
	m.backupPath = args.BackupPath
}

// checkRequiredVersion loads the config and check if the
// core version requirements are satisfied.
func (m *Meta) checkRequiredVersion() tfdiags.Diagnostics {
	var diags tfdiags.Diagnostics

	// Cannot use m.WorkingDir.RootModuleDir() here because
	// of path normalization that happens in loadConfig.
	pwd, err := os.Getwd()
	if err != nil {
		diags = diags.Append(fmt.Errorf("Error getting pwd: %s", err))
		return diags
	}

	diags = diags.Append(m.resolveConstVariables(pwd, arguments.ViewHuman))
	if diags.HasErrors() {
		return diags
	}

	config, configDiags := m.loadConfig(pwd)
	if configDiags.HasErrors() {
		diags = diags.Append(configDiags)
		return diags
	}

	versionDiags := terraform.CheckCoreVersionRequirements(config)
	if versionDiags.HasErrors() {
		diags = diags.Append(versionDiags)
		return diags

View on GitHub (pinned to d32a084675)