hashicorp/terraform · error

Too many command line arguments. Did you mean to use -chdir?

Error message

Too many command line arguments. Did you mean to use -chdir?

What it means

Returned by ModulePath (command.go:62) when args is non-empty. Historically some Terraform commands accepted a module path as a positional argument; that was replaced by the global `-chdir` flag. ModulePath now rejects any positional args to surface the migration clearly — it returns this error rather than silently misinterpreting the argument. The error message itself suggests the fix.

Source

Thrown at internal/command/command.go:66

The "backend" in Terraform defines how Terraform operates. The default
backend performs all operations locally on your machine. Your configuration
is configured to use a non-local backend. This backend doesn't support this
operation.
`

// 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. Use the global -chdir flag before the subcommand: `terraform -chdir=modules/foo init`.
  2. Move into the target directory and run the command with no positional arg: `cd modules/foo && terraform init`.
  3. Drop the positional path entirely if you intend the current directory.
  4. Update scripts and docs to the -chdir form.

Example fix

# before
terraform init ./modules/vpc
# Too many command line arguments. Did you mean to use -chdir?

# after
terraform -chdir=./modules/vpc init
Defensive patterns

Strategy: validation

Validate before calling

// Reject positional args before calling ModulePath and surface -chdir guidance
if len(args) > 0 {
    return "", fmt.Errorf("Too many command line arguments. Did you mean to use -chdir?")
}
return ModulePath(nil)

Prevention

When it happens

Trigger: Passing a directory path as a positional argument to a command that uses ModulePath: `terraform init ./modules/foo`, `terraform validate path/to/module`, or `terraform fmt ./dir` (for commands routed through ModulePath). Any len(args) > 0 triggers it.

Common situations: Users upgrading from old Terraform versions where positional module paths were accepted; copy-pasted commands from outdated tutorials; muscle memory of `terraform validate .`; scripts that pass $PWD positionally.

Related errors


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