hashicorp/terraform · error

unable to locate module

Error message

unable to locate module: %s

What it means

Thrown by validate.go when filepath.Abs(args.Path) returns an error during `terraform validate`. filepath.Abs almost never errors — it only fails when os.Getwd fails (no current directory, e.g. the cwd was deleted out from under the process). Despite the message text 'unable to locate module', this is really a cwd-resolution failure, not a missing module.

Solutions

  1. Re-run from a directory that exists: `cd <existing-dir> && terraform validate`.
  2. Pass an absolute path: `terraform validate /abs/path/to/module`.
  3. In CI, ensure the workspace dir exists for the whole terraform invocation.
  4. Check that the script did not delete its own cwd before invoking terraform.

Example fix

# before
 cd /tmp/sandbox && rm -rf /tmp/sandbox && terraform validate

# after
 cd /tmp/existing-dir
 terraform validate /abs/path/to/module
Defensive patterns

Strategy: validation

Validate before calling

// Confirm cwd exists before calling validate.
if _, err := os.Getwd(); err != nil {
    return fmt.Errorf("current working directory is not accessible: %w", err)
}

Try / catch

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

Prevention

When it happens

Trigger: The shell's current working directory was removed or renamed while the terraform process is starting; filepath.Abs cannot resolve the relative args.Path because os.Getwd returns ENOENT.

Common situations: Running terraform from inside a directory that another process `rm -rf`'d; CI workspace wiped between checkout and terraform step; a wrapper that chdirs into a temp dir then deletes it; running in a container whose cwd mount was removed.

Related errors


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

Appendix: 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 d32a084675)