hashicorp/terraform · error

unable to locate module: %s

Error message

unable to locate module: %s

What it means

Thrown by `terraform validate` when filepath.Abs(args.Path) fails. filepath.Abs only fails when the current working directory cannot be determined (os.Getwd fails), not for nonexistent input paths. The %s is the os error. Reaching this means Terraform itself lost track of its cwd.

Source

Thrown at internal/command/validate.go:73

	loader, err := c.initConfigLoader()
	if err != nil {
		diags = diags.Append(err)
		return view.Results(diags)
	}

	var varDiags tfdiags.Diagnostics
	c.VariableValues, varDiags = args.Vars.CollectValues(func(filename string, src []byte) {
		loader.Parser().ForceFileSource(filename, src)
	})
	if varDiags.HasErrors() {
		diags = diags.Append(varDiags)
		return view.Results(diags)
	}

	dir, err := filepath.Abs(args.Path)
	if err != nil {
		diags = diags.Append(fmt.Errorf("unable to locate module: %s", err))
		return view.Results(diags)
	}

	// Check for user-supplied plugin path
	if c.pluginPath, err = c.loadPluginPath(); err != nil {
		diags = diags.Append(fmt.Errorf("error loading plugin path: %s", err))
		return view.Results(diags)
	}

	validateDiags := c.validate(dir)
	diags = diags.Append(validateDiags)

	// Validating with dev overrides in effect means that the result might
	// not be valid for a stable release, so we'll warn about that in case
	// the user is trying to use "terraform validate" as a sort of pre-flight
	// check before submitting a change.
	diags = diags.Append(c.providerDevOverrideRuntimeWarnings())

View on GitHub (pinned to c9def3e214)

Solutions

  1. Ensure you run `terraform validate` from an existing, accessible directory: cd into a valid path first.
  2. Pass an explicit absolute path argument so filepath.Abs does not depend on cwd: `terraform validate /abs/path`.
  3. If in CI, verify the workspace checkout directory still exists at validate time (no cleanup racing the build).

Example fix

# before: cwd removed mid-run
(cd /tmp/willbedeleted && rm -rf /tmp/willbedeleted && terraform validate)
# unable to locate module: getwd: no such file or directory

# after: pass an explicit existing path
terraform validate /home/user/config
Defensive patterns

Strategy: validation

Validate before calling

// ensure cwd exists before calling validate
if _, err := os.Getwd(); err != nil {
    // switch to a known-good directory or pass an absolute path
}

Prevention

When it happens

Trigger: Running `terraform validate <path>` from a working directory that was deleted or made inaccessible between process start and the filepath.Abs call, or in a sandbox that reports no cwd.

Common situations: Running validate from a directory that a parallel process removed (e.g. rm -rf in CI); running inside a container where cwd was unmounted; shells whose cwd no longer exists; obscure OS/cwd permission errors.

Related errors


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