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
Thrown by command.ModulePath when args is non-empty. ModulePath is the shared validator for commands that historically accepted a module-path positional argument but now expect module selection via the global -chdir flag. Any extra positional argument trips this guard before os.Getwd is called.
Solutions
- Remove the positional path and use the global -chdir flag placed before the subcommand.
- If your command legitimately takes extra args, change the slice bounds passed to ModulePath (per the doc comment) or use a different code path.
- Check the command's help (`terraform <cmd> -help`) for accepted arguments.
Example fix
// before // terraform validate ./infra // after // terraform -chdir=./infra validate
Defensive patterns
Strategy: validation
Validate before calling
// For commands that take no positional args, validate before invoking Terraform
func validateNoPositional(args []string) error {
if len(args) > 0 {
return fmt.Errorf("use -chdir=%s instead of a positional path", args[0])
}
return nil
} Prevention
- Use `-chdir=<path>` (before the subcommand) for module selection.
- Read each command's -help to know which positional args are accepted.
- Wrap Terraform in scripts that translate legacy positional paths to -chdir.
When it happens
Trigger: Running a command (e.g. validate, fmt in some flows, init with a stray path) and passing a positional path argument instead of using -chdir. Example: `terraform validate ./my-module` instead of `terraform -chdir=./my-module validate`.
Common situations: Users upgrading from older Terraform where positional module paths were accepted; copy-pasting examples that pre-date -chdir; muscle memory of passing `.` as a final arg.
Related errors
- No '=' value in arg
- No file or directory at
- Option -write cannot be used when reading from stdin
- Too many command line arguments. Did you mean to use -chdir?
- A managed resource address is required. Importing into a…
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/0e5992e3223438c1.
Report an issue: GitHub.
Appendix: 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 d32a084675)