hashicorp/terraform · error

Error getting pwd: %s

Error message

Error getting pwd: %s

What it means

Appended as a diagnostic in checkRequiredVersion() when os.Getwd() fails. Terraform needs the current working directory to locate and load the root module configuration for version constraint checking. The %s wraps the OS-level error (errno) from getwd.

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

Solutions

  1. Re-run from a directory that currently exists and is readable: cd to a valid path first.
  2. In CI, ensure the working directory is not deleted between steps; avoid cleaning the workspace while a shell still has it as cwd.
  3. Check parent directory permissions (chmod/chown) so the process can resolve its cwd.

Example fix

// before: CI deletes workspace mid-pipeline
rm -rf $WORKSPACE  # while shell cwd is there
terraform plan
// after: run terraform from a stable directory
cd /opt/infra
terraform plan
Defensive patterns

Strategy: validation

Validate before calling

// Verify cwd is resolvable before launching terraform
if _, err := os.Getwd(); err != nil {
    log.Fatalf("current working directory unavailable: %v", err)
}

Prevention

When it happens

Trigger: The Go runtime's os.Getwd() returns an error, typically when the current working directory has been deleted out from under the process, when a parent directory has insufficient permissions, or on platforms where the cwd is represented by an unresolvable file descriptor.

Common situations: Running terraform from a directory that was since removed (common in CI when a prior step cleans the workspace); a mounted filesystem being unmounted mid-run; permission/ACL changes on a parent directory; a container where the cwd path no longer exists after a volume remount.

Related errors


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